Skip to content

Commit a8fdb66

Browse files
feat: humongous amount of work on Bazaar
Took 2 hours 15 minutes
1 parent 6f7ed9a commit a8fdb66

39 files changed

Lines changed: 2422 additions & 711 deletions

File tree

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
package net.swofty.commons.bazaar;
22

3-
public interface BazaarTransaction {}
3+
import org.json.JSONObject;
4+
5+
public interface BazaarTransaction {
6+
JSONObject toJSON();
7+
BazaarTransaction fromJSON(JSONObject json);
8+
}
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
package net.swofty.commons.bazaar;
22

3+
import org.json.JSONObject;
4+
35
import java.time.Instant;
46
import java.util.UUID;
57

68
public record OrderExpiredBazaarTransaction(
79
UUID orderId,
810
String itemName,
911
UUID owner,
10-
String side, // "BUY" or "SELL"
12+
UUID ownerProfile,
13+
String side, // "BUY" or "SELL"
14+
double originalPricePaid,
1115
double remainingQty,
1216
Instant expiredAt
13-
) implements BazaarTransaction {}
17+
) implements BazaarTransaction {
18+
@Override
19+
public JSONObject toJSON() {
20+
JSONObject json = new JSONObject();
21+
json.put("orderId", orderId.toString());
22+
json.put("itemName", itemName);
23+
json.put("owner", owner.toString());
24+
json.put("ownerProfile", ownerProfile.toString());
25+
json.put("side", side);
26+
json.put("remainingQty", remainingQty);
27+
json.put("expiredAt", expiredAt.toString());
28+
json.put("originalPricePaid", originalPricePaid);
29+
return json;
30+
}
31+
32+
@Override
33+
public BazaarTransaction fromJSON(JSONObject json) {
34+
return new OrderExpiredBazaarTransaction(
35+
UUID.fromString(json.getString("orderId")),
36+
json.getString("itemName"),
37+
UUID.fromString(json.getString("owner")),
38+
UUID.fromString(json.getString("ownerProfile")),
39+
json.getString("side"),
40+
json.getDouble("originalPricePaid"),
41+
json.getDouble("remainingQty"),
42+
Instant.parse(json.getString("expiredAt"))
43+
);
44+
}
45+
}
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,49 @@
11
package net.swofty.commons.bazaar;
22

3+
import org.json.JSONObject;
4+
35
import java.time.Instant;
46
import java.util.UUID;
57

68
// Fired when a buy+sell actually crosses
79
public record SuccessfulBazaarTransaction(
810
String itemName,
911
UUID buyer,
12+
UUID buyerProfile,
1013
UUID seller,
14+
UUID sellerProfile,
1115
double pricePerUnit,
1216
double quantity,
13-
double taxTaken, // coins withheld by the system
17+
double taxTaken, // coins withheld by the system
1418
Instant timestamp
15-
) implements BazaarTransaction {}
19+
) implements BazaarTransaction {
20+
@Override
21+
public JSONObject toJSON() {
22+
JSONObject json = new JSONObject();
23+
json.put("itemName", itemName);
24+
json.put("buyer", buyer.toString());
25+
json.put("buyerProfile", buyerProfile.toString());
26+
json.put("seller", seller.toString());
27+
json.put("sellerProfile", sellerProfile.toString());
28+
json.put("pricePerUnit", pricePerUnit);
29+
json.put("quantity", quantity);
30+
json.put("taxTaken", taxTaken);
31+
json.put("timestamp", timestamp.toString());
32+
return json;
33+
}
34+
35+
@Override
36+
public BazaarTransaction fromJSON(JSONObject json) {
37+
return new SuccessfulBazaarTransaction(
38+
json.getString("itemName"),
39+
UUID.fromString(json.getString("buyer")),
40+
UUID.fromString(json.getString("buyerProfile")),
41+
UUID.fromString(json.getString("seller")),
42+
UUID.fromString(json.getString("sellerProfile")),
43+
json.getDouble("pricePerUnit"),
44+
json.getDouble("quantity"),
45+
json.getDouble("taxTaken"),
46+
Instant.parse(json.getString("timestamp"))
47+
);
48+
}
49+
}

commons/src/main/java/net/swofty/commons/protocol/objects/bazaar/BazaarBuyProtocolObject.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public String serialize(BazaarBuyMessage value) {
1919
JSONObject json = new JSONObject();
2020
json.put("item-name", value.itemName);
2121
json.put("player-uuid", value.playerUUID);
22+
json.put("profile-uuid", value.profileUUID);
2223
json.put("price", value.price);
2324
json.put("amount", value.amount);
2425
return json.toString();
@@ -30,14 +31,15 @@ public BazaarBuyMessage deserialize(String json) {
3031
BazaarBuyMessage message = new BazaarBuyMessage();
3132
message.itemName = jsonObject.getString("item-name");
3233
message.playerUUID = UUID.fromString(jsonObject.getString("player-uuid"));
34+
message.profileUUID = UUID.fromString(jsonObject.getString("profile-uuid"));
3335
message.price = jsonObject.getInt("price");
3436
message.amount = jsonObject.getInt("amount");
3537
return message;
3638
}
3739

3840
@Override
3941
public BazaarBuyMessage clone(BazaarBuyMessage value) {
40-
return new BazaarBuyMessage(value.itemName, value.amount, value.price, value.playerUUID);
42+
return new BazaarBuyMessage(value.itemName, value.amount, value.price, value.playerUUID, value.profileUUID);
4143
}
4244
};
4345
}
@@ -74,6 +76,7 @@ public static class BazaarBuyMessage {
7476
public int amount;
7577
public Integer price;
7678
public UUID playerUUID;
79+
public UUID profileUUID;
7780
}
7881

7982
@AllArgsConstructor

commons/src/main/java/net/swofty/commons/protocol/objects/bazaar/BazaarCancelProtocolObject.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ public String serialize(CancelMessage v) {
2020
JSONObject o = new JSONObject();
2121
o.put("order-id", v.orderId.toString());
2222
o.put("player-uuid", v.playerUuid.toString());
23+
o.put("profile-uuid", v.profileUuid.toString());
2324
return o.toString();
2425
}
2526
@Override
2627
public CancelMessage deserialize(String json) {
2728
JSONObject o = new JSONObject(json);
2829
return new CancelMessage(
2930
UUID.fromString(o.getString("order-id")),
30-
UUID.fromString(o.getString("player-uuid"))
31+
UUID.fromString(o.getString("player-uuid")),
32+
UUID.fromString(o.getString("profile-uuid"))
3133
);
3234
}
3335
@Override
3436
public CancelMessage clone(CancelMessage v) {
35-
return new CancelMessage(v.orderId, v.playerUuid);
37+
return new CancelMessage(v.orderId, v.playerUuid, v.profileUuid);
3638
}
3739
};
3840
}
@@ -62,6 +64,7 @@ public CancelResponse clone(CancelResponse v) {
6264
public static class CancelMessage {
6365
public UUID orderId;
6466
public UUID playerUuid;
67+
public UUID profileUuid;
6568
}
6669

6770
@AllArgsConstructor @NoArgsConstructor

commons/src/main/java/net/swofty/commons/protocol/objects/bazaar/BazaarGetItemProtocolObject.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public record BazaarGetItemResponse(
6868

6969
public record OrderRecord(
7070
UUID playerUUID,
71+
UUID profileUUID,
7172
double price,
7273
double amount
7374
) {}

commons/src/main/java/net/swofty/commons/protocol/objects/bazaar/BazaarGetPendingOrdersProtocolObject.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@ public Serializer<BazaarGetPendingOrdersMessage> getSerializer() {
2121
public String serialize(BazaarGetPendingOrdersMessage v) {
2222
JSONObject o = new JSONObject();
2323
o.put("player-uuid", v.playerUUID.toString());
24+
o.put("profile-uuid", v.profileUUID.toString());
2425
return o.toString();
2526
}
2627

2728
@Override
2829
public BazaarGetPendingOrdersMessage deserialize(String json) {
2930
JSONObject o = new JSONObject(json);
3031
return new BazaarGetPendingOrdersMessage(
31-
UUID.fromString(o.getString("player-uuid"))
32+
UUID.fromString(o.getString("player-uuid")),
33+
UUID.fromString(o.getString("profile-uuid"))
3234
);
3335
}
3436

3537
@Override
3638
public BazaarGetPendingOrdersMessage clone(BazaarGetPendingOrdersMessage v) {
37-
return new BazaarGetPendingOrdersMessage(v.playerUUID);
39+
return new BazaarGetPendingOrdersMessage(v.playerUUID, v.profileUUID);
3840
}
3941
};
4042
}
@@ -51,6 +53,7 @@ public String serialize(BazaarGetPendingOrdersResponse v) {
5153
o.put("side", order.side);
5254
o.put("price", order.price);
5355
o.put("amount", order.amount);
56+
o.put("profile-uuid", order.profileUUID.toString());
5457
return o;
5558
}).collect(Collectors.toList()));
5659
return arr.toString();
@@ -66,7 +69,8 @@ public BazaarGetPendingOrdersResponse deserialize(String json) {
6669
o.getString("item-name"),
6770
o.getString("side"),
6871
o.getDouble("price"),
69-
o.getDouble("amount")
72+
o.getDouble("amount"),
73+
UUID.fromString(o.getString("profile-uuid"))
7074
);
7175
}).collect(Collectors.toList());
7276
return new BazaarGetPendingOrdersResponse(list);
@@ -82,14 +86,16 @@ public BazaarGetPendingOrdersResponse clone(BazaarGetPendingOrdersResponse v) {
8286
@lombok.AllArgsConstructor
8387
public static class BazaarGetPendingOrdersMessage {
8488
public UUID playerUUID;
89+
public UUID profileUUID;
8590
}
8691

8792
public record PendingOrder(
8893
UUID orderId,
8994
String itemName,
9095
String side,
9196
double price,
92-
double amount
97+
double amount,
98+
UUID profileUUID
9399
) {}
94100

95101
/** Response is just a list of those. */
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package net.swofty.commons.protocol.objects.bazaar;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.NoArgsConstructor;
5+
import net.swofty.commons.protocol.ProtocolObject;
6+
import net.swofty.commons.protocol.Serializer;
7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
10+
import java.time.Instant;
11+
import java.util.List;
12+
import java.util.UUID;
13+
import java.util.stream.Collectors;
14+
15+
public class BazaarGetPendingTransactionsProtocolObject extends ProtocolObject<
16+
BazaarGetPendingTransactionsProtocolObject.BazaarGetPendingTransactionsMessage,
17+
BazaarGetPendingTransactionsProtocolObject.BazaarGetPendingTransactionsResponse> {
18+
19+
@Override
20+
public Serializer<BazaarGetPendingTransactionsMessage> getSerializer() {
21+
return new Serializer<>() {
22+
@Override
23+
public String serialize(BazaarGetPendingTransactionsMessage v) {
24+
JSONObject o = new JSONObject();
25+
o.put("player-uuid", v.playerUUID.toString());
26+
o.put("profile-uuid", v.profileUUID.toString());
27+
return o.toString();
28+
}
29+
30+
@Override
31+
public BazaarGetPendingTransactionsMessage deserialize(String json) {
32+
JSONObject o = new JSONObject(json);
33+
return new BazaarGetPendingTransactionsMessage(
34+
UUID.fromString(o.getString("player-uuid")),
35+
UUID.fromString(o.getString("profile-uuid"))
36+
);
37+
}
38+
39+
@Override
40+
public BazaarGetPendingTransactionsMessage clone(BazaarGetPendingTransactionsMessage v) {
41+
return new BazaarGetPendingTransactionsMessage(v.playerUUID, v.profileUUID);
42+
}
43+
};
44+
}
45+
46+
@Override
47+
public Serializer<BazaarGetPendingTransactionsResponse> getReturnSerializer() {
48+
return new Serializer<>() {
49+
@Override
50+
public String serialize(BazaarGetPendingTransactionsResponse v) {
51+
JSONArray arr = new JSONArray();
52+
for (PendingTransactionInfo transaction : v.transactions) {
53+
JSONObject o = new JSONObject();
54+
o.put("id", transaction.id);
55+
o.put("transaction-type", transaction.transactionType);
56+
o.put("transaction-data", transaction.transactionData);
57+
o.put("created-at", transaction.createdAt.toString());
58+
arr.put(o);
59+
}
60+
return arr.toString();
61+
}
62+
63+
@Override
64+
public BazaarGetPendingTransactionsResponse deserialize(String json) {
65+
JSONArray arr = new JSONArray(json);
66+
var list = arr.toList().stream().map(obj -> {
67+
JSONObject o = new JSONObject((java.util.Map<?,?>) obj);
68+
return new PendingTransactionInfo(
69+
o.getString("id"),
70+
o.getString("transaction-type"),
71+
o.getJSONObject("transaction-data"),
72+
Instant.parse(o.getString("created-at"))
73+
);
74+
}).collect(Collectors.toList());
75+
return new BazaarGetPendingTransactionsResponse(list);
76+
}
77+
78+
@Override
79+
public BazaarGetPendingTransactionsResponse clone(BazaarGetPendingTransactionsResponse v) {
80+
return new BazaarGetPendingTransactionsResponse(v.transactions);
81+
}
82+
};
83+
}
84+
85+
@AllArgsConstructor
86+
public static class BazaarGetPendingTransactionsMessage {
87+
public UUID playerUUID;
88+
public UUID profileUUID;
89+
}
90+
91+
@AllArgsConstructor
92+
public static class BazaarGetPendingTransactionsResponse {
93+
public List<PendingTransactionInfo> transactions;
94+
}
95+
96+
/**
97+
* Represents a pending transaction for protocol transfer
98+
*/
99+
public static class PendingTransactionInfo {
100+
public String id;
101+
public String transactionType;
102+
public JSONObject transactionData;
103+
public Instant createdAt;
104+
105+
public PendingTransactionInfo(String id, String transactionType, JSONObject transactionData, Instant createdAt) {
106+
this.id = id;
107+
this.transactionType = transactionType;
108+
this.transactionData = transactionData;
109+
this.createdAt = createdAt;
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)