Skip to content

Commit

Permalink
Merge pull request #21 from bolt-rip/maps-refactor
Browse files Browse the repository at this point in the history
Use PGMMap for match maps instead of a string & update pool route
  • Loading branch information
dentmaged authored Feb 1, 2022
2 parents 790366f + eeabf84 commit cd4f698
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 84 deletions.
12 changes: 6 additions & 6 deletions src/main/java/rip/bolt/nerve/api/APIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import retrofit2.converter.jackson.JacksonConverterFactory;
import rip.bolt.nerve.api.definitions.BoltResponse;
import rip.bolt.nerve.api.definitions.Match;
import rip.bolt.nerve.api.definitions.PoolInformation;
import rip.bolt.nerve.api.definitions.Veto;
import rip.bolt.nerve.api.definitions.Pool;
import rip.bolt.nerve.api.definitions.PGMMap;

public class APIManager {

Expand All @@ -28,12 +28,12 @@ public APIManager(ObjectMapper objectMapper, APIConfig config) {
apiService = retrofit.create(APIService.class);
}

public PoolInformation getPoolInformation(int queueSize) {
return apiService.getPoolInformation(queueSize);
public Pool getPool(int seriesId, int queueSize) {
return apiService.getPool(seriesId, queueSize);
}

public BoltResponse veto(Match match, UUID uuid, Veto veto) {
return apiService.veto(match.getId(), uuid.toString(), veto);
public BoltResponse veto(Match match, UUID uuid, PGMMap map) {
return apiService.veto(match.getId(), uuid.toString(), map);
}

public List<Match> matches() {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/rip/bolt/nerve/api/APIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
import rip.bolt.nerve.api.definitions.BoltResponse;
import rip.bolt.nerve.api.definitions.Match;
import rip.bolt.nerve.api.definitions.PoolInformation;
import rip.bolt.nerve.api.definitions.Veto;
import rip.bolt.nerve.api.definitions.Pool;
import rip.bolt.nerve.api.definitions.PGMMap;

public interface APIService {

@GET("config/pools/{queueSize}")
PoolInformation getPoolInformation(@Path("queueSize") int queueSize);
@GET("series/{series}/pool")
Pool getPool(@Path("series") int seriesId, @Query("players") int queueSize);

@POST("ranked/matches/{match}/player/{uuid}/vote")
BoltResponse veto(@Path("match") String match, @Path("uuid") String uuid, @Body Veto veto);
BoltResponse veto(@Path("match") String match, @Path("uuid") String uuid, @Body PGMMap map);

@GET("ranked/matches")
List<Match> matches();
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/rip/bolt/nerve/api/definitions/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
public class Match {

private String id;
private String map;
private PGMMap map;
private String server;
private Integer seriesId;

@JsonProperty(access = Access.WRITE_ONLY)
private List<Team> teams;
Expand All @@ -38,11 +39,11 @@ public String getId() {
return id;
}

public String getMap() {
public PGMMap getMap() {
return map;
}

public void setMap(String map) {
public void setMap(PGMMap map) {
this.map = map;
}

Expand All @@ -54,6 +55,14 @@ public void setServer(String server) {
this.server = server;
}

public Integer getSeriesId() {
return this.seriesId;
}

public void setSeriesId(Integer seriesId) {
this.seriesId = seriesId;
}

public List<Team> getTeams() {
return teams;
}
Expand Down Expand Up @@ -103,7 +112,7 @@ public void setStatus(MatchStatus status) {
}

public int getQueueSize() {
return teams.get(0).getParticipations().size();
return teams.get(0).getParticipations().size() * 2;
}

public Team getPlayerTeam(Player player) {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/rip/bolt/nerve/api/definitions/PGMMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package rip.bolt.nerve.api.definitions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class PGMMap {

private Integer id;
private String name;

public PGMMap() {
}

public PGMMap(Integer mapId) {
this.id = mapId;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
38 changes: 38 additions & 0 deletions src/main/java/rip/bolt/nerve/api/definitions/Pool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package rip.bolt.nerve.api.definitions;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Pool {

private Integer id;
private Integer players;
private List<PGMMap> maps;

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getPlayers() {
return this.players;
}

public void setPlayers(Integer players) {
this.players = players;
}

public List<PGMMap> getMaps() {
return this.maps;
}

public void setMaps(List<PGMMap> maps) {
this.maps = maps;
}

}
21 changes: 0 additions & 21 deletions src/main/java/rip/bolt/nerve/api/definitions/PoolInformation.java

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/java/rip/bolt/nerve/api/definitions/Veto.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/rip/bolt/nerve/commands/BoltCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public void veto(final CommandContext cmd, CommandSource sender) throws CommandE
throw new CommandException("Only players can run this command!");
Player player = (Player) sender;

String map = Components.toSpace(cmd.getJoinedStrings(0));
String query = Components.toSpace(cmd.getJoinedStrings(0));
Match match = registry.getPlayerMatch(player);
if (match == null)
throw new CommandException("You can not veto at this time!");

executor.async(() -> {
vetoManager.vetoMap(player, match, map);
vetoManager.vetoMap(player, match, query);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void doLogic(Player player, RegisteredServer assignedServer, Match match)
player.createConnectionRequest(assignedServer).fireAndForget();
} else {
sounds.playDing(player);
player.sendMessage(Messages.rankedMatchReady(assignedServer.getServerInfo().getName(), match.getMap()));
player.sendMessage(Messages.rankedMatchReady(assignedServer.getServerInfo().getName(), match.getMap().getName()));
}
}

Expand Down
39 changes: 21 additions & 18 deletions src/main/java/rip/bolt/nerve/match/listeners/VetoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import rip.bolt.nerve.api.definitions.BoltResponse;
import rip.bolt.nerve.api.definitions.Match;
import rip.bolt.nerve.api.definitions.MatchStatus;
import rip.bolt.nerve.api.definitions.PoolInformation;
import rip.bolt.nerve.api.definitions.PGMMap;
import rip.bolt.nerve.api.definitions.Pool;
import rip.bolt.nerve.api.definitions.Team;
import rip.bolt.nerve.api.definitions.User;
import rip.bolt.nerve.api.definitions.Veto;
import rip.bolt.nerve.match.MatchStatusListener;
import rip.bolt.nerve.utils.Executor;
import rip.bolt.nerve.utils.Messages;
Expand All @@ -40,7 +40,7 @@ public class VetoManager implements MatchStatusListener {

private Sounds sounds;

private Map<String, PoolInformation> pools;
private Map<String, Pool> pools;
private Set<UUID> vetoed;

private static final TextComponent vetoMessage = Messages.vetoMessage();
Expand All @@ -55,7 +55,7 @@ public VetoManager(Executor executor, ProxyServer server, NervePlugin plugin, AP

this.sounds = sounds;

this.pools = new HashMap<String, PoolInformation>();
this.pools = new HashMap<String, Pool>();
this.vetoed = new HashSet<UUID>();
}

Expand All @@ -64,13 +64,13 @@ public void playerJoin(Player player, Match match) {
if (match.getStatus() != MatchStatus.CREATED)
return;

PoolInformation information = pools.get(match.getId());
if (information == null)
Pool pool = pools.get(match.getId());
if (pool == null)
return; // they will be sent the vetoes in a moment

server.getScheduler().buildTask(plugin, () -> {
if (match.getStatus() == MatchStatus.CREATED && match.getMap() == null)
sendVetoes(player, match, Messages.vetoOptions(information.getMaps()));
sendVetoes(player, match, Messages.vetoOptions(pool.getMaps()));
}).delay(1, TimeUnit.SECONDS).schedule();
}

Expand Down Expand Up @@ -99,10 +99,11 @@ public void matchStatusUpdate(Match match) {

executor.async(() -> {
int queueSize = match.getQueueSize();
PoolInformation information = api.getPoolInformation(queueSize);
pools.put(match.getId(), information);
int seriesId = match.getSeriesId();
Pool pool = api.getPool(seriesId, queueSize);
pools.put(match.getId(), pool);

TextComponent vetoOptions = Messages.vetoOptions(information.getMaps());
TextComponent vetoOptions = Messages.vetoOptions(pool.getMaps());

for (Team team : match.getTeams()) {
inner: for (User participant : team.getPlayers()) {
Expand All @@ -127,15 +128,16 @@ public void sendVetoes(Player player, Match match, TextComponent vetoOptions) {
}

public void vetoMap(Player player, Match match, String query) {
String found = null;
PoolInformation info = pools.get(match.getId());
if (info == null) {
player.sendMessage(Component.text("Please wait a few seconds before running this command again.").color(NamedTextColor.RED));
PGMMap found = null;
Pool pool = pools.get(match.getId());
if (pool == null) {
player.sendMessage(Component.text("Please wait a few seconds before running this command again.")
.color(NamedTextColor.RED));
return;
}

for (String map : info.getMaps()) {
if (map.toLowerCase().startsWith(query.toLowerCase())) {
for (PGMMap map : pool.getMaps()) {
if (map.getName().toLowerCase().startsWith(query.toLowerCase())) {
found = map;
break;
}
Expand All @@ -146,7 +148,7 @@ public void vetoMap(Player player, Match match, String query) {
return;
}

BoltResponse response = api.veto(match, player.getUniqueId(), new Veto(found));
BoltResponse response = api.veto(match, player.getUniqueId(), found);
if (response.isSuccess()) {
player.sendMessage(Messages.vetoed(found, null, vetoed.contains(player.getUniqueId())));
for (User user : match.getPlayerTeam(player).getPlayers()) {
Expand All @@ -157,7 +159,8 @@ public void vetoMap(Player player, Match match, String query) {
if (!viewer.isPresent())
continue;

viewer.get().sendMessage(Messages.vetoed(found, player.getUsername(), vetoed.contains(player.getUniqueId())));
viewer.get().sendMessage(
Messages.vetoed(found, player.getUsername(), vetoed.contains(player.getUniqueId())));
}

vetoed.add(player.getUniqueId());
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/rip/bolt/nerve/utils/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import rip.bolt.nerve.api.definitions.Match;
import rip.bolt.nerve.api.definitions.PGMMap;
import rip.bolt.nerve.api.definitions.QueueUpdate.Action;
import rip.bolt.nerve.api.definitions.Team;
import rip.bolt.nerve.privateserver.PrivateServerConfig;
Expand Down Expand Up @@ -44,7 +45,7 @@ public static TextComponent vetoMessage() {
return text().append(PREFIX).append(colour(NamedTextColor.GOLD, "Your match is starting. Pick the map you want to ")).append(bold(colour(NamedTextColor.RED, "veto"))).append(colour(NamedTextColor.GOLD, ":")).build();
}

public static TextComponent vetoOptions(List<String> maps) {
public static TextComponent vetoOptions(List<PGMMap> maps) {
TextComponent[] message = new TextComponent[maps.size() * 2];
for (int i = 0; i < maps.size(); i++) {
message[i * 2] = DASH;
Expand All @@ -58,15 +59,15 @@ public static TextComponent vetoOptions(List<String> maps) {
return builder.build();
}

public static TextComponent formatMapName(String map) {
return command(NamedTextColor.YELLOW, text(map), "bolt", "veto", map);
public static TextComponent formatMapName(PGMMap map) {
return command(NamedTextColor.YELLOW, text(map.getName()), "bolt", "veto", map.getName());
}

public static TextComponent mapNotFound(String map) {
return text().append(colour(NamedTextColor.RED, "Map ")).append(colour(NamedTextColor.RED, map)).append(colour(NamedTextColor.RED, " not found.")).build();
public static TextComponent mapNotFound(String query) {
return text().append(colour(NamedTextColor.RED, "Map ")).append(colour(NamedTextColor.RED, query)).append(colour(NamedTextColor.RED, " not found.")).build();
}

public static TextComponent vetoed(String map, @Nullable String teammateName, boolean vetoedBefore) {
public static TextComponent vetoed(PGMMap map, @Nullable String teammateName, boolean vetoedBefore) {
TextComponent.Builder builder = text().append(PREFIX);

String displayName = teammateName;
Expand All @@ -86,11 +87,11 @@ public static TextComponent vetoed(String map, @Nullable String teammateName, bo
sentence = String.format(" %s vetoed ", verb);

builder.append(colour(NamedTextColor.YELLOW, displayName)).append(colour(NamedTextColor.GOLD, sentence));
return builder.append(colour(NamedTextColor.YELLOW, map)).append(colour(NamedTextColor.GOLD, "!")).build();
return builder.append(colour(NamedTextColor.YELLOW, map.getName())).append(colour(NamedTextColor.GOLD, "!")).build();
}

public static TextComponent mapDecided(String map) {
return text().append(PREFIX).append(colour(NamedTextColor.GOLD, "You will be playing on ")).append(colour(NamedTextColor.YELLOW, map)).append(colour(NamedTextColor.GOLD, "!")).build();
public static TextComponent mapDecided(PGMMap map) {
return text().append(PREFIX).append(colour(NamedTextColor.GOLD, "You will be playing on ")).append(colour(NamedTextColor.YELLOW, map.getName())).append(colour(NamedTextColor.GOLD, "!")).build();
}

public static TextComponent formatMatchHeader(Match match) {
Expand Down

0 comments on commit cd4f698

Please sign in to comment.