Skip to content

Commit 827a2af

Browse files
feat(punishment): add query protocol objects for punishment service
Add GetActivePunishmentProtocolObject and GetAllBannedIdsProtocolObject so proxy and game servers can query the punishment service instead of accessing PunishmentRedis directly.
1 parent 7594c45 commit 827a2af

4 files changed

Lines changed: 217 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package net.swofty.commons.protocol.objects.punishment;
2+
3+
import com.google.gson.Gson;
4+
import net.swofty.commons.protocol.ProtocolObject;
5+
import net.swofty.commons.protocol.Serializer;
6+
import net.swofty.commons.punishment.PunishmentReason;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
import org.json.JSONObject;
10+
11+
import java.util.UUID;
12+
13+
public class GetActivePunishmentProtocolObject
14+
extends ProtocolObject<GetActivePunishmentProtocolObject.GetActivePunishmentMessage,
15+
GetActivePunishmentProtocolObject.GetActivePunishmentResponse> {
16+
17+
@Override
18+
public Serializer<GetActivePunishmentMessage> getSerializer() {
19+
return new Serializer<>() {
20+
@Override
21+
public String serialize(GetActivePunishmentMessage value) {
22+
JSONObject json = new JSONObject();
23+
json.put("target", value.target().toString());
24+
return json.toString();
25+
}
26+
27+
@Override
28+
public GetActivePunishmentMessage deserialize(String json) {
29+
JSONObject obj = new JSONObject(json);
30+
return new GetActivePunishmentMessage(
31+
UUID.fromString(obj.getString("target"))
32+
);
33+
}
34+
35+
@Override
36+
public GetActivePunishmentMessage clone(GetActivePunishmentMessage value) {
37+
return value;
38+
}
39+
};
40+
}
41+
42+
@Override
43+
public Serializer<GetActivePunishmentResponse> getReturnSerializer() {
44+
return new Serializer<>() {
45+
@Override
46+
public String serialize(GetActivePunishmentResponse value) {
47+
JSONObject json = new JSONObject();
48+
json.put("found", value.found());
49+
json.put("type", value.type());
50+
json.put("banId", value.banId());
51+
json.put("reason", value.reason() != null ? new Gson().toJson(value.reason()) : null);
52+
json.put("expiresAt", value.expiresAt());
53+
return json.toString();
54+
}
55+
56+
@Override
57+
public GetActivePunishmentResponse deserialize(String json) {
58+
JSONObject obj = new JSONObject(json);
59+
boolean found = obj.getBoolean("found");
60+
if (!found) {
61+
return new GetActivePunishmentResponse(false, null, null, null, 0);
62+
}
63+
return new GetActivePunishmentResponse(
64+
true,
65+
obj.optString("type", null),
66+
obj.optString("banId", null),
67+
obj.isNull("reason") ? null : new Gson().fromJson(obj.getString("reason"), PunishmentReason.class),
68+
obj.getLong("expiresAt")
69+
);
70+
}
71+
72+
@Override
73+
public GetActivePunishmentResponse clone(GetActivePunishmentResponse value) {
74+
return value;
75+
}
76+
};
77+
}
78+
79+
public record GetActivePunishmentMessage(
80+
@NotNull UUID target
81+
) {}
82+
83+
public record GetActivePunishmentResponse(
84+
boolean found,
85+
@Nullable String type,
86+
@Nullable String banId,
87+
@Nullable PunishmentReason reason,
88+
long expiresAt
89+
) {}
90+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package net.swofty.commons.protocol.objects.punishment;
2+
3+
import net.swofty.commons.protocol.ProtocolObject;
4+
import net.swofty.commons.protocol.Serializer;
5+
import org.json.JSONArray;
6+
import org.json.JSONObject;
7+
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
public class GetAllBannedIdsProtocolObject
12+
extends ProtocolObject<GetAllBannedIdsProtocolObject.GetAllBannedIdsMessage,
13+
GetAllBannedIdsProtocolObject.GetAllBannedIdsResponse> {
14+
15+
@Override
16+
public Serializer<GetAllBannedIdsMessage> getSerializer() {
17+
return new Serializer<>() {
18+
@Override
19+
public String serialize(GetAllBannedIdsMessage value) {
20+
return new JSONObject().toString();
21+
}
22+
23+
@Override
24+
public GetAllBannedIdsMessage deserialize(String json) {
25+
return new GetAllBannedIdsMessage();
26+
}
27+
28+
@Override
29+
public GetAllBannedIdsMessage clone(GetAllBannedIdsMessage value) {
30+
return value;
31+
}
32+
};
33+
}
34+
35+
@Override
36+
public Serializer<GetAllBannedIdsResponse> getReturnSerializer() {
37+
return new Serializer<>() {
38+
@Override
39+
public String serialize(GetAllBannedIdsResponse value) {
40+
JSONObject json = new JSONObject();
41+
json.put("ids", new JSONArray(value.ids()));
42+
return json.toString();
43+
}
44+
45+
@Override
46+
public GetAllBannedIdsResponse deserialize(String json) {
47+
JSONObject obj = new JSONObject(json);
48+
JSONArray arr = obj.getJSONArray("ids");
49+
Set<String> ids = new HashSet<>();
50+
for (int i = 0; i < arr.length(); i++) {
51+
ids.add(arr.getString(i));
52+
}
53+
return new GetAllBannedIdsResponse(ids);
54+
}
55+
56+
@Override
57+
public GetAllBannedIdsResponse clone(GetAllBannedIdsResponse value) {
58+
return value;
59+
}
60+
};
61+
}
62+
63+
public record GetAllBannedIdsMessage() {}
64+
65+
public record GetAllBannedIdsResponse(Set<String> ids) {}
66+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.swofty.service.punishment.endpoints;
2+
3+
import net.swofty.commons.impl.ServiceProxyRequest;
4+
import net.swofty.commons.protocol.ProtocolObject;
5+
import net.swofty.commons.protocol.objects.punishment.GetActivePunishmentProtocolObject;
6+
import net.swofty.commons.punishment.PunishmentRedis;
7+
import net.swofty.service.generic.redis.ServiceEndpoint;
8+
9+
import java.util.Optional;
10+
11+
public class GetActivePunishmentEndpoint implements ServiceEndpoint
12+
<GetActivePunishmentProtocolObject.GetActivePunishmentMessage,
13+
GetActivePunishmentProtocolObject.GetActivePunishmentResponse> {
14+
15+
@Override
16+
public ProtocolObject<GetActivePunishmentProtocolObject.GetActivePunishmentMessage, GetActivePunishmentProtocolObject.GetActivePunishmentResponse> associatedProtocolObject() {
17+
return new GetActivePunishmentProtocolObject();
18+
}
19+
20+
@Override
21+
public GetActivePunishmentProtocolObject.GetActivePunishmentResponse onMessage(ServiceProxyRequest message, GetActivePunishmentProtocolObject.GetActivePunishmentMessage messageObject) {
22+
Optional<PunishmentRedis.ActivePunishment> existing = PunishmentRedis.getActive(messageObject.target());
23+
if (existing.isEmpty()) {
24+
return new GetActivePunishmentProtocolObject.GetActivePunishmentResponse(false, null, null, null, 0);
25+
}
26+
27+
PunishmentRedis.ActivePunishment punishment = existing.get();
28+
return new GetActivePunishmentProtocolObject.GetActivePunishmentResponse(
29+
true,
30+
punishment.type(),
31+
punishment.banId(),
32+
punishment.reason(),
33+
punishment.expiresAt()
34+
);
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.swofty.service.punishment.endpoints;
2+
3+
import net.swofty.commons.impl.ServiceProxyRequest;
4+
import net.swofty.commons.protocol.ProtocolObject;
5+
import net.swofty.commons.protocol.objects.punishment.GetAllBannedIdsProtocolObject;
6+
import net.swofty.commons.punishment.PunishmentRedis;
7+
import net.swofty.service.generic.redis.ServiceEndpoint;
8+
9+
import java.util.Set;
10+
11+
public class GetAllBannedIdsEndpoint implements ServiceEndpoint
12+
<GetAllBannedIdsProtocolObject.GetAllBannedIdsMessage,
13+
GetAllBannedIdsProtocolObject.GetAllBannedIdsResponse> {
14+
15+
@Override
16+
public ProtocolObject<GetAllBannedIdsProtocolObject.GetAllBannedIdsMessage, GetAllBannedIdsProtocolObject.GetAllBannedIdsResponse> associatedProtocolObject() {
17+
return new GetAllBannedIdsProtocolObject();
18+
}
19+
20+
@Override
21+
public GetAllBannedIdsProtocolObject.GetAllBannedIdsResponse onMessage(ServiceProxyRequest message, GetAllBannedIdsProtocolObject.GetAllBannedIdsMessage messageObject) {
22+
Set<String> ids = PunishmentRedis.getAllBannedPlayerIds();
23+
return new GetAllBannedIdsProtocolObject.GetAllBannedIdsResponse(ids);
24+
}
25+
}

0 commit comments

Comments
 (0)