Skip to content

Commit f31fe39

Browse files
committed
Merge remote-tracking branch 'origin/master' into feat/jerrysworkshop
2 parents 5caed56 + 5f14d1f commit f31fe39

80 files changed

Lines changed: 4261 additions & 29 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ jobs:
5858
service.datamutex/build/libs/*.jar
5959
service.party/build/libs/*.jar
6060
service.orchestrator/build/libs/*.jar
61+
service.friend/build/libs/*.jar
6162
service.darkauction/build/libs/*.jar

commons/src/main/java/net/swofty/commons/ServiceType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public enum ServiceType {
88
DATA_MUTEX,
99
PARTY,
1010
DARK_AUCTION,
11-
ORCHESTRATOR
11+
ORCHESTRATOR,
12+
FRIEND
1213
;
1314
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package net.swofty.commons.friend;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import net.swofty.commons.protocol.Serializer;
6+
import org.json.JSONObject;
7+
8+
import java.util.UUID;
9+
10+
@Getter
11+
public class Friend {
12+
private final UUID uuid;
13+
@Setter
14+
private String nickname;
15+
@Setter
16+
private boolean bestFriend;
17+
private final long addedTimestamp;
18+
19+
public Friend(UUID uuid, String nickname, boolean bestFriend, long addedTimestamp) {
20+
this.uuid = uuid;
21+
this.nickname = nickname;
22+
this.bestFriend = bestFriend;
23+
this.addedTimestamp = addedTimestamp;
24+
}
25+
26+
public static Friend create(UUID uuid) {
27+
return new Friend(uuid, null, false, System.currentTimeMillis());
28+
}
29+
30+
public static Serializer<Friend> getStaticSerializer() {
31+
return new Friend(UUID.randomUUID(), null, false, 0).getSerializer();
32+
}
33+
34+
public Serializer<Friend> getSerializer() {
35+
return new Serializer<>() {
36+
@Override
37+
public String serialize(Friend value) {
38+
JSONObject json = new JSONObject();
39+
json.put("uuid", value.uuid.toString());
40+
json.put("nickname", value.nickname != null ? value.nickname : JSONObject.NULL);
41+
json.put("bestFriend", value.bestFriend);
42+
json.put("addedTimestamp", value.addedTimestamp);
43+
return json.toString();
44+
}
45+
46+
@Override
47+
public Friend deserialize(String json) {
48+
JSONObject jsonObject = new JSONObject(json);
49+
return new Friend(
50+
UUID.fromString(jsonObject.getString("uuid")),
51+
jsonObject.isNull("nickname") ? null : jsonObject.getString("nickname"),
52+
jsonObject.getBoolean("bestFriend"),
53+
jsonObject.getLong("addedTimestamp")
54+
);
55+
}
56+
57+
@Override
58+
public Friend clone(Friend value) {
59+
return new Friend(value.uuid, value.nickname, value.bestFriend, value.addedTimestamp);
60+
}
61+
};
62+
}
63+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package net.swofty.commons.friend;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import net.swofty.commons.protocol.Serializer;
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.UUID;
12+
13+
@Getter
14+
public class FriendData {
15+
private final UUID playerUuid;
16+
private final List<Friend> friends;
17+
@Setter
18+
private FriendSettings settings;
19+
20+
public FriendData(UUID playerUuid, List<Friend> friends, FriendSettings settings) {
21+
this.playerUuid = playerUuid;
22+
this.friends = friends;
23+
this.settings = settings;
24+
}
25+
26+
public static FriendData createEmpty(UUID playerUuid) {
27+
return new FriendData(playerUuid, new ArrayList<>(), FriendSettings.createDefault());
28+
}
29+
30+
public boolean isFriendWith(UUID uuid) {
31+
return friends.stream().anyMatch(friend -> friend.getUuid().equals(uuid));
32+
}
33+
34+
public Friend getFriend(UUID uuid) {
35+
return friends.stream()
36+
.filter(friend -> friend.getUuid().equals(uuid))
37+
.findFirst()
38+
.orElse(null);
39+
}
40+
41+
public List<Friend> getBestFriends() {
42+
return friends.stream()
43+
.filter(Friend::isBestFriend)
44+
.toList();
45+
}
46+
47+
public int getFriendCount() {
48+
return friends.size();
49+
}
50+
51+
public int getBestFriendCount() {
52+
return (int) friends.stream().filter(Friend::isBestFriend).count();
53+
}
54+
55+
public void addFriend(Friend friend) {
56+
friends.add(friend);
57+
}
58+
59+
public void removeFriend(UUID uuid) {
60+
friends.removeIf(friend -> friend.getUuid().equals(uuid));
61+
}
62+
63+
public void removeAllNonBestFriends() {
64+
friends.removeIf(friend -> !friend.isBestFriend());
65+
}
66+
67+
public static Serializer<FriendData> getStaticSerializer() {
68+
return createEmpty(UUID.randomUUID()).getSerializer();
69+
}
70+
71+
public Serializer<FriendData> getSerializer() {
72+
return new Serializer<>() {
73+
@Override
74+
public String serialize(FriendData value) {
75+
JSONObject json = new JSONObject();
76+
json.put("playerUuid", value.playerUuid.toString());
77+
78+
JSONArray friendsArray = new JSONArray();
79+
for (Friend friend : value.friends) {
80+
friendsArray.put(new JSONObject(friend.getSerializer().serialize(friend)));
81+
}
82+
json.put("friends", friendsArray);
83+
84+
json.put("settings", new JSONObject(value.settings.getSerializer().serialize(value.settings)));
85+
return json.toString();
86+
}
87+
88+
@Override
89+
public FriendData deserialize(String json) {
90+
JSONObject jsonObject = new JSONObject(json);
91+
UUID playerUuid = UUID.fromString(jsonObject.getString("playerUuid"));
92+
93+
List<Friend> friends = new ArrayList<>();
94+
JSONArray friendsArray = jsonObject.getJSONArray("friends");
95+
Serializer<Friend> friendSerializer = Friend.getStaticSerializer();
96+
for (int i = 0; i < friendsArray.length(); i++) {
97+
friends.add(friendSerializer.deserialize(friendsArray.getJSONObject(i).toString()));
98+
}
99+
100+
FriendSettings settings = FriendSettings.getStaticSerializer()
101+
.deserialize(jsonObject.getJSONObject("settings").toString());
102+
103+
return new FriendData(playerUuid, friends, settings);
104+
}
105+
106+
@Override
107+
public FriendData clone(FriendData value) {
108+
List<Friend> clonedFriends = new ArrayList<>();
109+
Serializer<Friend> friendSerializer = Friend.getStaticSerializer();
110+
for (Friend friend : value.friends) {
111+
clonedFriends.add(friendSerializer.clone(friend));
112+
}
113+
return new FriendData(
114+
value.playerUuid,
115+
clonedFriends,
116+
value.settings.getSerializer().clone(value.settings)
117+
);
118+
}
119+
};
120+
}
121+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package net.swofty.commons.friend;
2+
3+
import lombok.Getter;
4+
import lombok.NonNull;
5+
import net.swofty.commons.protocol.Serializer;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.UUID;
10+
11+
@Getter
12+
public abstract class FriendEvent {
13+
public FriendEvent() {
14+
}
15+
16+
public abstract Serializer getSerializer();
17+
18+
public abstract List<UUID> getParticipants();
19+
20+
public static @NonNull FriendEvent findFromType(String className) {
21+
String[] packageNames = {
22+
"net.swofty.commons.friend.events",
23+
"net.swofty.commons.friend.events.response"
24+
};
25+
26+
for (String packageName : packageNames) {
27+
try {
28+
Class<?> clazz = Class.forName(packageName + "." + className);
29+
return createDummyInstance(clazz);
30+
} catch (Exception e) {
31+
// Try next package
32+
}
33+
}
34+
35+
throw new RuntimeException("Failed to find friend event class: " + className + " in " + Arrays.toString(packageNames));
36+
}
37+
38+
@SuppressWarnings("unchecked")
39+
private static FriendEvent createDummyInstance(Class<?> clazz) throws Exception {
40+
String className = clazz.getSimpleName();
41+
42+
switch (className) {
43+
// Request events
44+
case "FriendAddRequestEvent" -> {
45+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class);
46+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID());
47+
}
48+
case "FriendAcceptRequestEvent" -> {
49+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class);
50+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID());
51+
}
52+
case "FriendDenyRequestEvent" -> {
53+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class);
54+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID());
55+
}
56+
case "FriendRemoveRequestEvent" -> {
57+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class);
58+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID());
59+
}
60+
case "FriendRemoveAllRequestEvent" -> {
61+
var constructor = clazz.getDeclaredConstructor(UUID.class);
62+
return (FriendEvent) constructor.newInstance(UUID.randomUUID());
63+
}
64+
case "FriendToggleBestRequestEvent" -> {
65+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class);
66+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID());
67+
}
68+
case "FriendSetNicknameRequestEvent" -> {
69+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class);
70+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "");
71+
}
72+
case "FriendToggleSettingRequestEvent" -> {
73+
var constructor = clazz.getDeclaredConstructor(UUID.class, FriendSettingType.class);
74+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), FriendSettingType.ACCEPTING_REQUESTS);
75+
}
76+
case "FriendListRequestEvent" -> {
77+
var constructor = clazz.getDeclaredConstructor(UUID.class, int.class, boolean.class);
78+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), 1, false);
79+
}
80+
case "FriendRequestsListEvent" -> {
81+
var constructor = clazz.getDeclaredConstructor(UUID.class, int.class);
82+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), 1);
83+
}
84+
// Response events
85+
case "FriendRequestSentResponseEvent" -> {
86+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class);
87+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "");
88+
}
89+
case "FriendRequestReceivedResponseEvent" -> {
90+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class);
91+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "");
92+
}
93+
case "FriendAddedResponseEvent" -> {
94+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class, String.class);
95+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "", "");
96+
}
97+
case "FriendDeniedResponseEvent" -> {
98+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class);
99+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "");
100+
}
101+
case "FriendRemovedResponseEvent" -> {
102+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class);
103+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "");
104+
}
105+
case "FriendRemoveAllResponseEvent" -> {
106+
var constructor = clazz.getDeclaredConstructor(UUID.class, int.class);
107+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), 0);
108+
}
109+
case "FriendBestToggledResponseEvent" -> {
110+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class, boolean.class);
111+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "", false);
112+
}
113+
case "FriendNicknameSetResponseEvent" -> {
114+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class, String.class);
115+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "", "");
116+
}
117+
case "FriendSettingToggledResponseEvent" -> {
118+
var constructor = clazz.getDeclaredConstructor(UUID.class, FriendSettingType.class, boolean.class);
119+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), FriendSettingType.ACCEPTING_REQUESTS, false);
120+
}
121+
case "FriendJoinNotificationEvent" -> {
122+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class);
123+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "");
124+
}
125+
case "FriendLeaveNotificationEvent" -> {
126+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class);
127+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "");
128+
}
129+
case "FriendRequestExpiredResponseEvent" -> {
130+
var constructor = clazz.getDeclaredConstructor(UUID.class, UUID.class, String.class, String.class);
131+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), UUID.randomUUID(), "", "");
132+
}
133+
case "FriendListResponseEvent" -> {
134+
var constructor = clazz.getDeclaredConstructor(UUID.class, List.class, int.class, int.class, boolean.class);
135+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), List.of(), 1, 1, false);
136+
}
137+
case "FriendRequestsListResponseEvent" -> {
138+
var constructor = clazz.getDeclaredConstructor(UUID.class, List.class, int.class, int.class);
139+
return (FriendEvent) constructor.newInstance(UUID.randomUUID(), List.of(), 1, 1);
140+
}
141+
default -> throw new IllegalArgumentException("Unknown friend event class: " + className);
142+
}
143+
}
144+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.swofty.commons.friend;
2+
3+
public abstract class FriendResponseEvent extends FriendEvent {
4+
public FriendResponseEvent() {
5+
super();
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.swofty.commons.friend;
2+
3+
public enum FriendSettingType {
4+
ACCEPTING_REQUESTS,
5+
JOIN_LEAVE_NOTIFICATIONS
6+
}

0 commit comments

Comments
 (0)