Skip to content

Commit 3742634

Browse files
committed
feat: complete orchestrator
1 parent 0317d20 commit 3742634

14 files changed

Lines changed: 290 additions & 130 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package net.swofty.commons.protocol.objects.orchestrator;
2+
3+
import net.swofty.commons.UnderstandableProxyServer;
4+
import net.swofty.commons.protocol.ProtocolObject;
5+
import net.swofty.commons.protocol.Serializer;
6+
import org.json.JSONObject;
7+
8+
import java.util.UUID;
9+
10+
public class ChooseGameProtocolObject extends ProtocolObject
11+
<ChooseGameProtocolObject.ChooseGameMessage,
12+
ChooseGameProtocolObject.ChooseGameResponse> {
13+
14+
@Override
15+
public Serializer<ChooseGameMessage> getSerializer() {
16+
return new Serializer<ChooseGameMessage>() {
17+
@Override
18+
public String serialize(ChooseGameMessage value) {
19+
JSONObject json = new JSONObject();
20+
json.put("uuid", value.player.toString());
21+
json.put("server", value.server.toJSON());
22+
json.put("gameId", value.gameId);
23+
return json.toString();
24+
}
25+
26+
@Override
27+
public ChooseGameMessage deserialize(String json) {
28+
JSONObject obj = new JSONObject(json);
29+
return new ChooseGameMessage(
30+
UUID.fromString(obj.getString("player")),
31+
UnderstandableProxyServer.singleFromJSON(obj.getJSONObject("server")),
32+
obj.getString("gameId")
33+
);
34+
}
35+
36+
@Override
37+
public ChooseGameMessage clone(ChooseGameMessage value) {
38+
return new ChooseGameMessage(value.player, value.server, value.gameId);
39+
}
40+
};
41+
}
42+
43+
@Override
44+
public Serializer<ChooseGameResponse> getReturnSerializer() {
45+
return new Serializer<ChooseGameResponse>() {
46+
@Override
47+
public String serialize(ChooseGameResponse value) {
48+
JSONObject json = new JSONObject();
49+
json.put("error", value.error);
50+
return json.toString();
51+
}
52+
53+
@Override
54+
public ChooseGameResponse deserialize(String json) {
55+
JSONObject obj = new JSONObject(json);
56+
return new ChooseGameResponse(obj.getBoolean("error"));
57+
}
58+
59+
@Override
60+
public ChooseGameResponse clone(ChooseGameResponse value) {
61+
return new ChooseGameResponse(value.error);
62+
}
63+
};
64+
}
65+
66+
public record ChooseGameMessage(UUID player, UnderstandableProxyServer server, String gameId) {
67+
}
68+
69+
public record ChooseGameResponse(boolean error) {
70+
}
71+
}

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

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,77 @@
44
import net.swofty.commons.UnderstandableProxyServer;
55
import net.swofty.commons.protocol.ProtocolObject;
66
import net.swofty.commons.protocol.Serializer;
7+
import org.jetbrains.annotations.Nullable;
78
import org.json.JSONObject;
89

910
public class GetServerForMapProtocolObject extends ProtocolObject
10-
<GetServerForMapProtocolObject.GetServerForMapMessage,
11-
GetServerForMapProtocolObject.GetServerForMapResponse> {
11+
<GetServerForMapProtocolObject.GetServerForMapMessage,
12+
GetServerForMapProtocolObject.GetServerForMapResponse> {
1213

13-
@Override
14-
public Serializer<GetServerForMapMessage> getSerializer() {
15-
return new Serializer<GetServerForMapMessage>() {
16-
@Override
17-
public String serialize(GetServerForMapMessage value) {
18-
JSONObject json = new JSONObject();
19-
json.put("type", value.type.name());
20-
json.put("map", value.map);
21-
if (value.mode != null) json.put("mode", value.mode);
22-
json.put("neededSlots", value.neededSlots);
23-
return json.toString();
24-
}
14+
@Override
15+
public Serializer<GetServerForMapMessage> getSerializer() {
16+
return new Serializer<GetServerForMapMessage>() {
17+
@Override
18+
public String serialize(GetServerForMapMessage value) {
19+
JSONObject json = new JSONObject();
20+
json.put("type", value.type.name());
21+
json.put("map", value.map);
22+
if (value.mode != null) json.put("mode", value.mode);
23+
json.put("neededSlots", value.neededSlots);
24+
return json.toString();
25+
}
2526

26-
@Override
27-
public GetServerForMapMessage deserialize(String json) {
28-
JSONObject obj = new JSONObject(json);
29-
return new GetServerForMapMessage(
30-
ServerType.valueOf(obj.getString("type")),
31-
obj.getString("map"),
32-
obj.has("mode") ? obj.getString("mode") : null,
33-
obj.getInt("neededSlots")
34-
);
35-
}
27+
@Override
28+
public GetServerForMapMessage deserialize(String json) {
29+
JSONObject obj = new JSONObject(json);
30+
return new GetServerForMapMessage(
31+
ServerType.valueOf(obj.getString("type")),
32+
obj.has("map") ? obj.getString("map") : null,
33+
obj.has("mode") ? obj.getString("mode") : null,
34+
obj.getInt("neededSlots")
35+
);
36+
}
3637

37-
@Override
38-
public GetServerForMapMessage clone(GetServerForMapMessage value) {
39-
return new GetServerForMapMessage(value.type, value.map, value.mode, value.neededSlots);
40-
}
41-
};
42-
}
38+
@Override
39+
public GetServerForMapMessage clone(GetServerForMapMessage value) {
40+
return new GetServerForMapMessage(value.type, value.map, value.mode, value.neededSlots);
41+
}
42+
};
43+
}
4344

44-
@Override
45-
public Serializer<GetServerForMapResponse> getReturnSerializer() {
46-
return new Serializer<GetServerForMapResponse>() {
47-
@Override
48-
public String serialize(GetServerForMapResponse value) {
49-
return value.server() == null ? new JSONObject().put("server", (Object) null).toString()
50-
: new JSONObject().put("server", value.server().toJSON()).toString();
51-
}
45+
@Override
46+
public Serializer<GetServerForMapResponse> getReturnSerializer() {
47+
return new Serializer<GetServerForMapResponse>() {
48+
@Override
49+
public String serialize(GetServerForMapResponse value) {
50+
JSONObject json = new JSONObject();
51+
if (value.server == null) {
52+
json.put("server", JSONObject.NULL);
53+
json.put("gameId", JSONObject.NULL);
54+
} else {
55+
json.put("server", value.server.toJSON());
56+
json.put("gameId", value.gameId);
57+
}
58+
return json.toString();
59+
}
5260

53-
@Override
54-
public GetServerForMapResponse deserialize(String json) {
55-
JSONObject obj = new JSONObject(json);
56-
if (obj.isNull("server")) return new GetServerForMapResponse(null);
57-
return new GetServerForMapResponse(UnderstandableProxyServer.singleFromJSON(obj.getJSONObject("server")));
58-
}
61+
@Override
62+
public GetServerForMapResponse deserialize(String json) {
63+
JSONObject obj = new JSONObject(json);
64+
if (obj.isNull("server")) return new GetServerForMapResponse(null, null);
65+
return new GetServerForMapResponse(UnderstandableProxyServer.singleFromJSON(obj.getJSONObject("server")), obj.getString("gameId"));
66+
}
5967

60-
@Override
61-
public GetServerForMapResponse clone(GetServerForMapResponse value) {
62-
return new GetServerForMapResponse(value.server);
63-
}
64-
};
65-
}
68+
@Override
69+
public GetServerForMapResponse clone(GetServerForMapResponse value) {
70+
return new GetServerForMapResponse(value.server, value.gameId);
71+
}
72+
};
73+
}
6674

67-
public record GetServerForMapMessage(ServerType type, String map, String mode, int neededSlots) {
68-
}
75+
public record GetServerForMapMessage(ServerType type, @Nullable String map, String mode, int neededSlots) {
76+
}
6977

70-
public record GetServerForMapResponse(UnderstandableProxyServer server) {
71-
}
78+
public record GetServerForMapResponse(UnderstandableProxyServer server, String gameId) {
79+
}
7280
}

commons/src/main/java/net/swofty/commons/service/FromServiceChannels.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public enum FromServiceChannels {
1212
PROPAGATE_BAZAAR_TRANSACTION("propagate-bazaar-transaction"),
1313
SEND_MESSAGE("send-message"),
1414
PROPAGATE_PARTY_EVENT("propagate_party_event"),
15+
GAME_INFORMATION("game-information"),
1516
INSTANTIATE_GAME("instantiate-game");
1617
;
1718

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public CompletableFuture<Boolean> isOnline() {
2121
hasReceivedResponse.set(true);
2222
});
2323

24-
CompletableFuture.delayedExecutor(50, TimeUnit.MILLISECONDS)
24+
CompletableFuture.delayedExecutor(150, TimeUnit.MILLISECONDS)
2525
.execute(() -> {
2626
if (!hasReceivedResponse.get()) {
2727
future.complete(false);

service.generic/src/main/java/net/swofty/service/generic/redis/ServiceToServerManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,12 @@ public static CompletableFuture<Map<UUID, JSONObject>> kickFromGUI(List<UUID> se
203203

204204
return sendToServers(serverUUIDs, FromServiceChannels.KICK_FROM_GUI, message);
205205
}
206+
207+
public static CompletableFuture<Map<UUID, JSONObject>> gameInformation(UUID serverUUID, UUID playerUUID, String gameId) {
208+
JSONObject message = new JSONObject()
209+
.put("playerUUID", playerUUID)
210+
.put("game-id", gameId);
211+
212+
return sendToServers(List.of(serverUUID), FromServiceChannels.GAME_INFORMATION, message);
213+
}
206214
}

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
@@ -61,7 +61,7 @@ public static GameWithServer findExisting(BedwarsGameType gameType, String map)
6161

6262
// Prefer games with more players (closer to starting)
6363
candidates.sort(Comparator.comparingInt((GameWithServer g) -> g.game().getInvolvedPlayers().size()).reversed());
64-
return candidates.get(0);
64+
return candidates.getFirst();
6565
}
6666

6767
/**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.swofty.service.orchestrator.endpoints;
2+
3+
import net.swofty.commons.impl.ServiceProxyRequest;
4+
import net.swofty.commons.protocol.ProtocolObject;
5+
import net.swofty.commons.protocol.objects.orchestrator.ChooseGameProtocolObject;
6+
import net.swofty.service.generic.redis.ServiceEndpoint;
7+
import net.swofty.service.generic.redis.ServiceToServerManager;
8+
9+
public class GameChooseEndpoint implements ServiceEndpoint
10+
<ChooseGameProtocolObject.ChooseGameMessage,
11+
ChooseGameProtocolObject.ChooseGameResponse> {
12+
13+
@Override
14+
public ProtocolObject<ChooseGameProtocolObject.ChooseGameMessage, ChooseGameProtocolObject.ChooseGameResponse> associatedProtocolObject() {
15+
return new ChooseGameProtocolObject();
16+
}
17+
18+
@Override
19+
public ChooseGameProtocolObject.ChooseGameResponse onMessage(ServiceProxyRequest message,
20+
ChooseGameProtocolObject.ChooseGameMessage body) {
21+
ServiceToServerManager.gameInformation(body.server().uuid(), body.player(), body.gameId());
22+
return new ChooseGameProtocolObject.ChooseGameResponse(false);
23+
}
24+
25+
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public GetServerForMapProtocolObject.GetServerForMapResponse onMessage(ServicePr
3131
try {
3232
BedwarsGameType gameType = parseBedwarsGameType(body.mode());
3333
if (gameType == null) {
34-
return new GetServerForMapProtocolObject.GetServerForMapResponse(null);
34+
return new GetServerForMapProtocolObject.GetServerForMapResponse(null, null);
3535
}
3636

3737
// First, try to find an existing joinable game
@@ -48,7 +48,7 @@ public GetServerForMapProtocolObject.GetServerForMapResponse onMessage(ServicePr
4848
hostingServer.maxPlayers(),
4949
hostingServer.shortName()
5050
);
51-
return new GetServerForMapProtocolObject.GetServerForMapResponse(proxy);
51+
return new GetServerForMapProtocolObject.GetServerForMapResponse(proxy, existingGameWithServer.game().getGameId().toString());
5252
}
5353
}
5454

@@ -80,17 +80,16 @@ public GetServerForMapProtocolObject.GetServerForMapResponse onMessage(ServicePr
8080
availableServer.maxPlayers(),
8181
availableServer.shortName()
8282
);
83-
return new GetServerForMapProtocolObject.GetServerForMapResponse(proxy);
83+
return new GetServerForMapProtocolObject.GetServerForMapResponse(proxy, response.getString("gameId"));
8484
}
8585
} catch (Exception e) {
8686
System.err.println("Failed to instantiate game: " + e.getMessage());
8787
}
8888
}
8989

90-
return new GetServerForMapProtocolObject.GetServerForMapResponse(null);
91-
90+
return new GetServerForMapProtocolObject.GetServerForMapResponse(null, null);
9291
} catch (Exception e) {
93-
return new GetServerForMapProtocolObject.GetServerForMapResponse(null);
92+
return new GetServerForMapProtocolObject.GetServerForMapResponse(null, null);
9493
}
9594
}
9695

type.bedwarsgame/src/main/java/net/swofty/type/bedwarsgame/TypeBedWarsGameLoader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import net.swofty.type.generic.entity.villager.HypixelVillagerNPC;
4545
import net.swofty.type.generic.event.HypixelEventClass;
4646
import net.swofty.type.generic.redis.RedisOriginServer;
47+
import net.swofty.type.generic.redis.service.RedisGameMessage;
4748
import net.swofty.type.generic.tab.EmptyTabModule;
4849
import net.swofty.type.generic.tab.TablistManager;
4950
import net.swofty.type.generic.tab.TablistModule;
@@ -166,6 +167,7 @@ public void onInitialize(MinecraftServer server) {
166167
allowed = types.contains(BedwarsGameType.SOLO);
167168
}
168169
if (allowed) filteredMaps.add(e);
170+
createGame(e);
169171
}
170172
}
171173
} catch (Exception e) {
@@ -236,7 +238,7 @@ public void afterInitialize(MinecraftServer server) {
236238
commonsGames
237239
);
238240
new ProxyService(ServiceType.ORCHESTRATOR).handleRequest(heartbeat);
239-
}).repeat(TaskSchedule.seconds(1)).schedule();
241+
}).delay(TaskSchedule.seconds(5)).repeat(TaskSchedule.seconds(1)).schedule();
240242
}
241243

242244
@Override

0 commit comments

Comments
 (0)