Teams corrections
full implementation of FightDetector
This commit is contained in:
parent
b1f188dece
commit
69e971f618
@ -93,9 +93,8 @@ public abstract class HttpRepository extends Repository {
|
|||||||
private <TResponse> ReqResp<TResponse> execute(HttpRequest request, Class<TResponse> clazz) {
|
private <TResponse> ReqResp<TResponse> execute(HttpRequest request, Class<TResponse> clazz) {
|
||||||
ReqResp<String> rawResponse = this.sendHttp(request);
|
ReqResp<String> rawResponse = this.sendHttp(request);
|
||||||
Main.logger().info(String.format(
|
Main.logger().info(String.format(
|
||||||
"HTTP-Repository fired %s, sending: %s, response: %s",
|
"Request: %s\nResponse: %s",
|
||||||
request,
|
request,
|
||||||
request.bodyPublisher().orElse(HttpRequest.BodyPublishers.ofString("none")),
|
|
||||||
rawResponse
|
rawResponse
|
||||||
));
|
));
|
||||||
return new ReqResp<>(rawResponse.status(), this.gson.fromJson(rawResponse.data(), clazz));
|
return new ReqResp<>(rawResponse.status(), this.gson.fromJson(rawResponse.data(), clazz));
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.fightDetector;
|
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.fightDetector;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.core.Main;
|
||||||
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams.Teams;
|
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams.Teams;
|
||||||
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams.VaroTeam;
|
import eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.teams.VaroTeam;
|
||||||
|
import net.kyori.adventure.util.Ticks;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -10,21 +13,81 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class FightDetector extends Appliance {
|
public class FightDetector extends Appliance {
|
||||||
public static final Long FIGHT_TIMEOUT = 60 * 1000L;
|
public static final long FIGHT_TIMEOUT = 60 * 1000;
|
||||||
|
private static final long BLOCK_RADIUS = 30;
|
||||||
|
|
||||||
public final Map<VaroTeam, Long> fights = new HashMap<>();
|
public final Map<VaroTeam, Long> fights = new HashMap<>();
|
||||||
|
|
||||||
|
public FightDetector() {
|
||||||
|
Bukkit.getScheduler().runTaskTimer(
|
||||||
|
Main.instance(),
|
||||||
|
() -> {
|
||||||
|
var teamFights = this.fights.keySet().stream()
|
||||||
|
.filter(this::isInFight)
|
||||||
|
.toList();
|
||||||
|
if(teamFights.isEmpty()) return;
|
||||||
|
Main.logger().info(String.format(
|
||||||
|
"There are %d Teams in Fight: %s",
|
||||||
|
teamFights.size(),
|
||||||
|
teamFights.stream()
|
||||||
|
.map(varoTeam -> String.format(
|
||||||
|
"%s[%s]",
|
||||||
|
varoTeam.name,
|
||||||
|
varoTeam.members.stream()
|
||||||
|
.map(member -> member.player.getName())
|
||||||
|
.collect(Collectors.joining(","))))
|
||||||
|
.collect(Collectors.joining(", "))
|
||||||
|
));
|
||||||
|
},
|
||||||
|
Ticks.TICKS_PER_SECOND * 15,
|
||||||
|
Ticks.TICKS_PER_SECOND * 15
|
||||||
|
);
|
||||||
|
|
||||||
|
Bukkit.getScheduler().runTaskTimer(
|
||||||
|
Main.instance(),
|
||||||
|
this::detectNearbyFights,
|
||||||
|
Ticks.TICKS_PER_SECOND,
|
||||||
|
Ticks.TICKS_PER_SECOND
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void detectNearbyFights() {
|
||||||
|
var players = Bukkit.getOnlinePlayers();
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(Main.instance(), () -> {
|
||||||
|
for (Player player : players) {
|
||||||
|
VaroTeam ownTeam = this.queryAppliance(Teams.class).getTeamFromPlayer(player.getUniqueId());
|
||||||
|
if (ownTeam == null) continue;
|
||||||
|
|
||||||
|
for (Player otherPlayer : players) {
|
||||||
|
if (player.equals(otherPlayer)) continue;
|
||||||
|
|
||||||
|
VaroTeam otherTeam = this.queryAppliance(Teams.class).getTeamFromPlayer(otherPlayer.getUniqueId());
|
||||||
|
if (otherTeam == null || ownTeam.equals(otherTeam)) continue;
|
||||||
|
|
||||||
|
if (player.getLocation().distance(otherPlayer.getLocation()) <= BLOCK_RADIUS) {
|
||||||
|
this.setInFight(ownTeam);
|
||||||
|
this.setInFight(otherTeam);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isInFight(VaroTeam team) {
|
public boolean isInFight(VaroTeam team) {
|
||||||
Long lastFightTime = this.fights.get(team);
|
Long lastFightTime = this.fights.get(team);
|
||||||
if(lastFightTime == null) return false;
|
if(lastFightTime == null) return false;
|
||||||
return (System.currentTimeMillis() - lastFightTime <= FIGHT_TIMEOUT);
|
return (System.currentTimeMillis() - lastFightTime <= FIGHT_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInFight(Player player) {
|
public void setInFight(VaroTeam team) {
|
||||||
VaroTeam team = this.queryAppliance(Teams.class).getTeamFromPlayer(player.getUniqueId());
|
|
||||||
this.fights.put(team, System.currentTimeMillis());
|
this.fights.put(team, System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
public void setInFight(Player player) {
|
||||||
|
this.setInFight(this.queryAppliance(Teams.class).getTeamFromPlayer(player.getUniqueId()));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected @NotNull List<Listener> listeners() {
|
protected @NotNull List<Listener> listeners() {
|
||||||
|
@ -35,7 +35,6 @@ public class ShrinkingBorder extends Appliance {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Bukkit.getScheduler().runTask(Main.instance(), ShrinkingBorder.this::shrinkBorder);
|
Bukkit.getScheduler().runTask(Main.instance(), ShrinkingBorder.this::shrinkBorder);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Teams extends Appliance implements DisplayName.Prefixed, DisplayName.Colored {
|
public class Teams extends Appliance implements DisplayName.Prefixed, DisplayName.Colored {
|
||||||
private List<VaroTeam> teams = List.of();
|
private final List<VaroTeam> teams = new ArrayList<>();
|
||||||
|
|
||||||
public Teams() {
|
public Teams() {
|
||||||
Bukkit.getScheduler().runTaskTimerAsynchronously(
|
Bukkit.getScheduler().runTaskTimerAsynchronously(
|
||||||
@ -31,12 +31,36 @@ public class Teams extends Appliance implements DisplayName.Prefixed, DisplayNam
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void refreshTeamList() {
|
public void refreshTeamList() {
|
||||||
this.teams = this.queryRepository(TeamRepository.class).getTeams().data().stream()
|
var updatedTeams = this.queryRepository(TeamRepository.class).getTeams().data();
|
||||||
.map(team -> new VaroTeam(
|
|
||||||
team.users().stream().map(user -> new VaroTeam.Member(user.uuid(), user.dead())).toList(),
|
for (var updatedTeam : updatedTeams) {
|
||||||
team.name(),
|
VaroTeam existingTeam = this.findTeamByName(updatedTeam.name());
|
||||||
team.color()
|
|
||||||
)).toList();
|
if (existingTeam != null) {
|
||||||
|
existingTeam.members = updatedTeam.users().stream()
|
||||||
|
.map(user -> new VaroTeam.Member(user.uuid(), user.dead()))
|
||||||
|
.toList();
|
||||||
|
existingTeam.color = updatedTeam.color();
|
||||||
|
existingTeam.name = updatedTeam.name();
|
||||||
|
} else {
|
||||||
|
VaroTeam newTeam = new VaroTeam(
|
||||||
|
updatedTeam.users().stream()
|
||||||
|
.map(user -> new VaroTeam.Member(user.uuid(), user.dead()))
|
||||||
|
.toList(),
|
||||||
|
updatedTeam.name(),
|
||||||
|
updatedTeam.color()
|
||||||
|
);
|
||||||
|
this.teams.add(newTeam);
|
||||||
|
Main.logger().info("Added missing team to Teams registry: " + newTeam);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private @Nullable VaroTeam findTeamByName(String name) {
|
||||||
|
for (VaroTeam team : this.teams) {
|
||||||
|
if (team.name.equals(name)) return team;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canLogin(UUID playerId) {
|
public boolean canLogin(UUID playerId) {
|
||||||
|
@ -5,6 +5,7 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@ -20,14 +21,12 @@ public class VaroTeam {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public final List<Member> members;
|
public String name;
|
||||||
public final UUID teamUuid;
|
public String color;
|
||||||
public final String name;
|
public List<Member> members;
|
||||||
public final String color;
|
|
||||||
|
|
||||||
public VaroTeam(List<Member> members, String name, String color) {
|
public VaroTeam(List<Member> members, String name, String color) {
|
||||||
this.teamUuid = UUID.randomUUID();
|
this.members = new ArrayList<>(members);
|
||||||
this.members = members;
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
@ -53,7 +52,7 @@ public class VaroTeam {
|
|||||||
"Unbekannter Fehler",
|
"Unbekannter Fehler",
|
||||||
"Verbindung wurde aufgrund eines unbekannten Grundes getrennt",
|
"Verbindung wurde aufgrund eines unbekannten Grundes getrennt",
|
||||||
"Falls du denkst, dass dies ein Fehler ist, melde dich bei einem Admin!",
|
"Falls du denkst, dass dies ein Fehler ist, melde dich bei einem Admin!",
|
||||||
this.teamUuid
|
UUID.randomUUID()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user