Skip to content

Commit 9d39c0f

Browse files
fix: starting to fix orchestrator system
1 parent ca88237 commit 9d39c0f

30 files changed

Lines changed: 121 additions & 195 deletions

File tree

type.bedwarsgeneric/src/main/java/net/swofty/type/bedwarsgeneric/game/GameType.java renamed to commons/src/main/java/net/swofty/commons/BedwarsGameType.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package net.swofty.type.bedwarsgeneric.game;
1+
package net.swofty.commons;
22

33
import lombok.Getter;
44
import org.jetbrains.annotations.Nullable;
55

66
@Getter
7-
public enum GameType {
7+
public enum BedwarsGameType {
88
SOLO(0, "Solo", 1, 8),
99
DOUBLES(1, "Doubles", 2, 8),
1010
THREE_THREE_THREE_THREE(2, "3v3v3v3", 3, 4),
@@ -18,16 +18,16 @@ public enum GameType {
1818
private final int teamSize;
1919
private final int teams;
2020

21-
GameType(int id, String displayName, int teamSize, int teams) {
21+
BedwarsGameType(int id, String displayName, int teamSize, int teams) {
2222
this.id = id;
2323
this.displayName = displayName;
2424
this.teamSize = teamSize;
2525
this.teams = teams;
2626
}
2727

2828
@Nullable
29-
public static GameType from(String field) {
30-
for (GameType type : values()) {
29+
public static BedwarsGameType from(String field) {
30+
for (BedwarsGameType type : values()) {
3131
if (type.name().equalsIgnoreCase(field)) {
3232
return type;
3333
}
@@ -36,8 +36,8 @@ public static GameType from(String field) {
3636
}
3737

3838
@Nullable
39-
public static GameType fromDisplayName(String displayName) {
40-
for (GameType type : values()) {
39+
public static BedwarsGameType fromDisplayName(String displayName) {
40+
for (BedwarsGameType type : values()) {
4141
if (type.displayName.equalsIgnoreCase(displayName)) {
4242
return type;
4343
}
@@ -46,8 +46,8 @@ public static GameType fromDisplayName(String displayName) {
4646
}
4747

4848
@Nullable
49-
public static GameType fromId(int id) {
50-
for (GameType type : values()) {
49+
public static BedwarsGameType fromId(int id) {
50+
for (BedwarsGameType type : values()) {
5151
if (type.id == id) {
5252
return type;
5353
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.swofty.commons.game;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
import net.swofty.commons.ServerType;
8+
import org.json.JSONObject;
9+
10+
import java.util.List;
11+
import java.util.UUID;
12+
13+
@Setter
14+
@Getter
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class Game {
18+
private List<UUID> involvedPlayers;
19+
private UUID gameId;
20+
private ServerType type;
21+
private String map;
22+
23+
public JSONObject toJSON() {
24+
JSONObject json = new JSONObject();
25+
json.put("gameId", gameId.toString());
26+
json.put("type", type.name());
27+
json.put("map", map);
28+
List<String> players = involvedPlayers.stream().map(UUID::toString).toList();
29+
json.put("players", players);
30+
return json;
31+
}
32+
33+
public static Game fromJSON(JSONObject json) {
34+
Game game = new Game();
35+
game.gameId = UUID.fromString(json.getString("gameId"));
36+
game.type = ServerType.valueOf(json.getString("type"));
37+
game.map = json.getString("map");
38+
game.involvedPlayers = json.getJSONArray("players").toList().stream().map(obj -> UUID.fromString(obj.toString())).toList();
39+
return game;
40+
}
41+
}

commons/src/main/java/net/swofty/commons/protocol/objects/orchestrator/GameHeartbeatProtocolObject.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.swofty.commons.protocol.objects.orchestrator;
22

3+
import net.swofty.commons.BedwarsGameType;
34
import net.swofty.commons.ServerType;
5+
import net.swofty.commons.game.Game;
46
import net.swofty.commons.protocol.ProtocolObject;
57
import net.swofty.commons.protocol.Serializer;
68
import org.json.JSONArray;
@@ -21,10 +23,13 @@ public String serialize(HeartbeatMessage value) {
2123
json.put("uuid", value.uuid.toString());
2224
json.put("shortName", value.shortName);
2325
json.put("type", value.type.name());
24-
json.put("maps", new JSONArray(value.maps));
25-
if (value.mode != null) json.put("mode", value.mode);
2626
json.put("maxPlayers", value.maxPlayers);
2727
json.put("onlinePlayers", value.onlinePlayers);
28+
JSONArray games = new JSONArray();
29+
for (Game game : value.games) {
30+
games.put(game.toJSON());
31+
}
32+
json.put("games", games);
2833
return json.toString();
2934
}
3035

@@ -34,18 +39,20 @@ public HeartbeatMessage deserialize(String json) {
3439
UUID uuid = UUID.fromString(obj.getString("uuid"));
3540
String shortName = obj.getString("shortName");
3641
ServerType type = ServerType.valueOf(obj.getString("type"));
37-
List<String> maps = new ArrayList<>();
38-
JSONArray arr = obj.getJSONArray("maps");
39-
for (int i = 0; i < arr.length(); i++) maps.add(arr.getString(i));
40-
String mode = obj.has("mode") ? obj.getString("mode") : null;
42+
List<Game> games = new ArrayList<>();
43+
JSONArray gamesArray = obj.getJSONArray("games");
44+
for (int i = 0; i < gamesArray.length(); i++) {
45+
JSONObject game = gamesArray.getJSONObject(i);
46+
games.add(Game.fromJSON(game));
47+
}
4148
int max = obj.getInt("maxPlayers");
4249
int online = obj.getInt("onlinePlayers");
43-
return new HeartbeatMessage(uuid, shortName, type, maps, mode, max, online);
50+
return new HeartbeatMessage(uuid, shortName, type, max, online, games);
4451
}
4552

4653
@Override
4754
public HeartbeatMessage clone(HeartbeatMessage value) {
48-
return new HeartbeatMessage(value.uuid, value.shortName, value.type, new ArrayList<>(value.maps), value.mode, value.maxPlayers, value.onlinePlayers);
55+
return new HeartbeatMessage(value.uuid, value.shortName, value.type, value.maxPlayers, value.onlinePlayers, value.games);
4956
}
5057
};
5158
}
@@ -73,7 +80,7 @@ public HeartbeatResponse clone(HeartbeatResponse value) {
7380
};
7481
}
7582

76-
public record HeartbeatMessage(UUID uuid, String shortName, ServerType type, List<String> maps, String mode, int maxPlayers, int onlinePlayers) { }
83+
public record HeartbeatMessage(UUID uuid, String shortName, ServerType type, int maxPlayers, int onlinePlayers, List<Game> games) { }
7784

7885
public record HeartbeatResponse(boolean ok) { }
7986
}

commons/src/main/java/net/swofty/commons/proxy/FromProxyChannels.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public enum FromProxyChannels {
1414
REFRESH_COOP_DATA_ON_SERVER("refresh-coop-data", new RefreshCoopDataOnServerRequirements()),
1515
RUN_EVENT_ON_SERVER("run-event", new RunEventRequirements()),
1616
PING_SERVER("ping-server", new PingServerRequirements()),
17-
GIVE_PLAYERS_ORIGIN_TYPE("give-players-origin-type", new GivePlayersOriginTypeRequirements()),
18-
BEDWARS_JOIN_PREFERENCE("bedwars-join-preference", new BedWarsJoinPreferenceRequirements()),
17+
GIVE_PLAYERS_ORIGIN_TYPE("give-players-origin-type", new GivePlayersOriginTypeRequirements())
1918
;
2019

2120
private final String channelName;

commons/src/main/java/net/swofty/commons/proxy/requirements/from/BedWarsJoinPreferenceRequirements.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

commons/src/main/java/net/swofty/commons/proxy/requirements/to/PlayerHandlerRequirements.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public enum PlayerHandlerActions {
2828
REFRESH_COOP_DATA,
2929
MESSAGE,
3030
TRANSFER_WITH_UUID,
31-
GET_SERVER,
32-
BEDWARS_SET_PREFERENCE;
31+
GET_SERVER
3332
}
3433
}

proxy.api/src/main/java/net/swofty/proxyapi/ProxyPlayer.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,6 @@ public CompletableFuture<UUID> getBankHash() {
186186
return future;
187187
}
188188

189-
public void setBedWarsJoinPreference(String mode, String map) {
190-
JSONObject json = new JSONObject();
191-
json.put("uuid", uuid.toString());
192-
json.put("mode", mode == null ? "" : mode);
193-
json.put("map", map == null ? "" : map);
194-
195-
PlayerHandlerRequirements.PlayerHandlerActions action =
196-
PlayerHandlerRequirements.PlayerHandlerActions.BEDWARS_SET_PREFERENCE;
197-
json.put("action", action.name());
198-
199-
ServerOutboundMessage.sendMessageToProxy(ToProxyChannels.PLAYER_HANDLER,
200-
json, (s) -> {});
201-
}
202-
203189
public void refreshCoopData(String datapoint) {
204190
JSONObject json = new JSONObject();
205191
json.put("uuid", uuid.toString());

service.orchestrator/src/main/java/net/swofty/service/orchestrator/OrchestratorCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static GameServerState pickServerForMap(ServerType type, String map, int
5151
return pickServerForMap(type, map, null, neededSlots);
5252
}
5353

54-
public static GameServerState pickServerForMap(ServerType type, String map, String mode, int neededSlots) {
54+
public static GameServerState pickServerForMap(ServerType type, String map, Bed, int neededSlots) {
5555
cleanup();
5656
List<GameServerState> candidates = new ArrayList<>();
5757
for (GameServerState s : serversByShortName.values()) {

service.orchestrator/src/main/java/net/swofty/service/orchestrator/endpoints/GameHeartbeatEndpoint.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public GameHeartbeatProtocolObject.HeartbeatResponse onMessage(ServiceProxyReque
2121
body.uuid(),
2222
body.shortName(),
2323
body.type(),
24-
body.maps(),
25-
body.mode(),
2624
body.maxPlayers(),
2725
body.onlinePlayers()
2826
);

service.orchestrator/src/main/java/net/swofty/service/orchestrator/endpoints/GetServerForMapEndpoint.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import net.swofty.service.generic.redis.ServiceEndpoint;
88
import net.swofty.service.orchestrator.OrchestratorCache;
99

10+
import java.util.ArrayList;
11+
1012
public class GetServerForMapEndpoint implements ServiceEndpoint
1113
<GetServerForMapProtocolObject.GetServerForMapMessage,
1214
GetServerForMapProtocolObject.GetServerForMapResponse> {
@@ -18,14 +20,14 @@ public ProtocolObject<GetServerForMapProtocolObject.GetServerForMapMessage, GetS
1820

1921
@Override
2022
public GetServerForMapProtocolObject.GetServerForMapResponse onMessage(ServiceProxyRequest message, GetServerForMapProtocolObject.GetServerForMapMessage body) {
21-
OrchestratorCache.GameServerState chosen = OrchestratorCache.pickServerForMap(body.type(), body.map(), body.mode(), body.neededSlots());
23+
OrchestratorCache.GameServerState chosen = OrchestratorCache.pickServerForMap(body.type(), body.map(), body.mode(), body.neededSlots());
2224
if (chosen == null) return new GetServerForMapProtocolObject.GetServerForMapResponse(null);
2325
UnderstandableProxyServer proxy = new UnderstandableProxyServer(
2426
chosen.shortName(),
2527
chosen.uuid(),
2628
chosen.type(),
2729
-1,
28-
java.util.List.of(),
30+
new ArrayList<>(),
2931
chosen.maxPlayers(),
3032
chosen.shortName()
3133
);

0 commit comments

Comments
 (0)