Skip to content

Commit 5353092

Browse files
committed
fix: make code make more sense
1 parent 1cf4f15 commit 5353092

5 files changed

Lines changed: 23 additions & 18 deletions

File tree

commons/src/main/java/net/swofty/commons/protocol/objects/election/CastVoteProtocolObject.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import net.swofty.commons.protocol.Serializer;
55
import org.json.JSONObject;
66

7+
import java.util.HashMap;
8+
import java.util.Map;
79
import java.util.UUID;
810

911
public class CastVoteProtocolObject
@@ -44,16 +46,24 @@ public Serializer<CastVoteResponse> getReturnSerializer() {
4446
public String serialize(CastVoteResponse value) {
4547
JSONObject json = new JSONObject();
4648
json.put("success", value.success());
47-
json.put("talliesJson", value.talliesJson());
49+
json.put("tallies", value.tallies() == null ? null : new JSONObject(value.tallies()));
4850
return json.toString();
4951
}
5052

5153
@Override
5254
public CastVoteResponse deserialize(String json) {
5355
JSONObject obj = new JSONObject(json);
56+
Map<String, Long> tallies = null;
57+
if (!obj.isNull("tallies")) {
58+
tallies = new HashMap<>();
59+
JSONObject talliesObject = obj.getJSONObject("tallies");
60+
for (String key : talliesObject.keySet()) {
61+
tallies.put(key, talliesObject.getLong(key));
62+
}
63+
}
5464
return new CastVoteResponse(
5565
obj.getBoolean("success"),
56-
obj.optString("talliesJson", null)
66+
tallies
5767
);
5868
}
5969

@@ -66,5 +76,6 @@ public CastVoteResponse clone(CastVoteResponse value) {
6676

6777
public record CastVoteMessage(UUID accountId, String candidateName) {}
6878

69-
public record CastVoteResponse(boolean success, String talliesJson) {}
79+
public record CastVoteResponse(boolean success, Map<String, Long> tallies) {
80+
}
7081
}

commons/src/main/java/net/swofty/commons/protocol/objects/election/GetCandidatesProtocolObject.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.gson.reflect.TypeToken;
55
import net.swofty.commons.protocol.ProtocolObject;
66
import net.swofty.commons.protocol.Serializer;
7+
import org.json.JSONArray;
78
import org.json.JSONObject;
89

910
import java.util.List;
@@ -17,7 +18,7 @@ public Serializer<GetCandidatesMessage> getSerializer() {
1718
return new Serializer<>() {
1819
@Override
1920
public String serialize(GetCandidatesMessage value) {
20-
return new JSONObject().put("_", true).toString();
21+
return "";
2122
}
2223

2324
@Override
@@ -41,7 +42,7 @@ public Serializer<GetCandidatesResponse> getReturnSerializer() {
4142
public String serialize(GetCandidatesResponse value) {
4243
JSONObject json = new JSONObject();
4344
json.put("electionOpen", value.electionOpen());
44-
json.put("candidates", gson.toJson(value.candidates()));
45+
json.put("candidates", value.candidates() == null ? null : new JSONArray(gson.toJson(value.candidates())));
4546
return json.toString();
4647
}
4748

@@ -52,7 +53,7 @@ public GetCandidatesResponse deserialize(String json) {
5253
List<CandidateInfo> candidates = List.of();
5354
if (!obj.isNull("candidates")) {
5455
candidates = gson.fromJson(
55-
obj.getString("candidates"),
56+
obj.getJSONArray("candidates").toString(),
5657
new TypeToken<List<CandidateInfo>>() {}.getType()
5758
);
5859
}

commons/src/main/java/net/swofty/commons/protocol/objects/election/GetElectionDataProtocolObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public Serializer<GetElectionDataMessage> getSerializer() {
1313
return new Serializer<>() {
1414
@Override
1515
public String serialize(GetElectionDataMessage value) {
16-
return new JSONObject().put("_", true).toString();
16+
return "";
1717
}
1818

1919
@Override

service.elections/src/main/java/net/swofty/service/election/endpoints/CastVoteEndpoint.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public class CastVoteEndpoint implements ServiceEndpoint
1515
<CastVoteProtocolObject.CastVoteMessage,
1616
CastVoteProtocolObject.CastVoteResponse> {
1717

18-
private static final Gson GSON = new Gson();
19-
2018
@Override
2119
public ProtocolObject<CastVoteProtocolObject.CastVoteMessage,
2220
CastVoteProtocolObject.CastVoteResponse> associatedProtocolObject() {
@@ -34,7 +32,7 @@ public CastVoteProtocolObject.CastVoteResponse onMessage(
3432
return new CastVoteProtocolObject.CastVoteResponse(false, null);
3533
}
3634

37-
Map<String, Object> data = GSON.fromJson(rawData, Map.class);
35+
Map<String, Object> data = new Gson().fromJson(rawData, Map.class);
3836
Boolean electionOpen = (Boolean) data.get("electionOpen");
3937
if (electionOpen == null || !electionOpen) {
4038
return new CastVoteProtocolObject.CastVoteResponse(false, null);
@@ -60,7 +58,7 @@ public CastVoteProtocolObject.CastVoteResponse onMessage(
6058
);
6159

6260
Map<String, Long> tallies = ElectionDatabase.getTallies(electionYear);
63-
return new CastVoteProtocolObject.CastVoteResponse(true, GSON.toJson(tallies));
61+
return new CastVoteProtocolObject.CastVoteResponse(true, tallies);
6462
} catch (Exception e) {
6563
Logger.error(e, "Failed to cast vote");
6664
return new CastVoteProtocolObject.CastVoteResponse(false, null);

type.skyblockgeneric/src/main/java/net/swofty/type/skyblockgeneric/elections/ElectionManager.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.swofty.type.skyblockgeneric.elections;
22

33
import com.google.gson.Gson;
4-
import com.google.gson.reflect.TypeToken;
54
import lombok.Getter;
65
import net.minestom.server.MinecraftServer;
76
import net.minestom.server.timer.TaskSchedule;
@@ -143,12 +142,8 @@ public static CompletableFuture<Void> castVote(UUID accountId, String candidateN
143142
).thenAccept(response -> {
144143
if (response.success()) {
145144
playerVoteCache.put(accountId, candidateName);
146-
if (response.talliesJson() != null) {
147-
Map<String, Long> tallies = GSON.fromJson(
148-
response.talliesJson(),
149-
new TypeToken<Map<String, Long>>(){}.getType()
150-
);
151-
electionData.updateTallies(tallies);
145+
if (response.tallies() != null) {
146+
electionData.updateTallies(response.tallies());
152147
}
153148
}
154149
}).exceptionally(e -> {

0 commit comments

Comments
 (0)