Skip to content

Commit 6f7ed9a

Browse files
feat: tons of work on Bazaar
Took 3 hours 9 minutes
1 parent 7ded66b commit 6f7ed9a

36 files changed

Lines changed: 1797 additions & 795 deletions

commons/src/main/java/net/swofty/commons/bazaar/BazaarItem.java

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package net.swofty.commons.bazaar;
2+
3+
public interface BazaarTransaction {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.swofty.commons.bazaar;
2+
3+
import java.time.Instant;
4+
import java.util.UUID;
5+
6+
public record OrderExpiredBazaarTransaction(
7+
UUID orderId,
8+
String itemName,
9+
UUID owner,
10+
String side, // "BUY" or "SELL"
11+
double remainingQty,
12+
Instant expiredAt
13+
) implements BazaarTransaction {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.swofty.commons.bazaar;
2+
3+
import java.time.Instant;
4+
import java.util.UUID;
5+
6+
// Fired when a buy+sell actually crosses
7+
public record SuccessfulBazaarTransaction(
8+
String itemName,
9+
UUID buyer,
10+
UUID seller,
11+
double pricePerUnit,
12+
double quantity,
13+
double taxTaken, // coins withheld by the system
14+
Instant timestamp
15+
) implements BazaarTransaction {}
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.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.JSONObject;
8+
9+
import java.util.UUID;
10+
11+
public class BazaarCancelProtocolObject extends ProtocolObject<
12+
BazaarCancelProtocolObject.CancelMessage,
13+
BazaarCancelProtocolObject.CancelResponse> {
14+
15+
@Override
16+
public Serializer<CancelMessage> getSerializer() {
17+
return new Serializer<>() {
18+
@Override
19+
public String serialize(CancelMessage v) {
20+
JSONObject o = new JSONObject();
21+
o.put("order-id", v.orderId.toString());
22+
o.put("player-uuid", v.playerUuid.toString());
23+
return o.toString();
24+
}
25+
@Override
26+
public CancelMessage deserialize(String json) {
27+
JSONObject o = new JSONObject(json);
28+
return new CancelMessage(
29+
UUID.fromString(o.getString("order-id")),
30+
UUID.fromString(o.getString("player-uuid"))
31+
);
32+
}
33+
@Override
34+
public CancelMessage clone(CancelMessage v) {
35+
return new CancelMessage(v.orderId, v.playerUuid);
36+
}
37+
};
38+
}
39+
40+
@Override
41+
public Serializer<CancelResponse> getReturnSerializer() {
42+
return new Serializer<>() {
43+
@Override
44+
public String serialize(CancelResponse v) {
45+
JSONObject o = new JSONObject();
46+
o.put("successful", v.successful);
47+
return o.toString();
48+
}
49+
@Override
50+
public CancelResponse deserialize(String json) {
51+
boolean ok = new JSONObject(json).getBoolean("successful");
52+
return new CancelResponse(ok);
53+
}
54+
@Override
55+
public CancelResponse clone(CancelResponse v) {
56+
return new CancelResponse(v.successful);
57+
}
58+
};
59+
}
60+
61+
@AllArgsConstructor @NoArgsConstructor
62+
public static class CancelMessage {
63+
public UUID orderId;
64+
public UUID playerUuid;
65+
}
66+
67+
@AllArgsConstructor @NoArgsConstructor
68+
public static class CancelResponse {
69+
public boolean successful;
70+
}
71+
}

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package net.swofty.commons.protocol.objects.bazaar;
22

3-
import net.swofty.commons.bazaar.BazaarItem;
43
import net.swofty.commons.protocol.ProtocolObject;
54
import net.swofty.commons.protocol.Serializer;
5+
import com.google.gson.Gson;
6+
import com.google.gson.reflect.TypeToken;
7+
8+
import java.util.List;
9+
import java.util.UUID;
610

711
public class BazaarGetItemProtocolObject extends ProtocolObject<
812
BazaarGetItemProtocolObject.BazaarGetItemMessage,
913
BazaarGetItemProtocolObject.BazaarGetItemResponse> {
1014

15+
private static final Gson gson = new Gson();
16+
1117
@Override
1218
public Serializer<BazaarGetItemMessage> getSerializer() {
1319
return new Serializer<BazaarGetItemMessage>() {
@@ -33,22 +39,36 @@ public Serializer<BazaarGetItemResponse> getReturnSerializer() {
3339
return new Serializer<BazaarGetItemResponse>() {
3440
@Override
3541
public String serialize(BazaarGetItemResponse value) {
36-
return value.item.serialize();
42+
return gson.toJson(value);
3743
}
3844

3945
@Override
4046
public BazaarGetItemResponse deserialize(String json) {
41-
return new BazaarGetItemResponse(BazaarItem.deserialize(json));
47+
return gson.fromJson(json, BazaarGetItemResponse.class);
4248
}
4349

4450
@Override
4551
public BazaarGetItemResponse clone(BazaarGetItemResponse value) {
46-
return new BazaarGetItemResponse(BazaarItem.fromDocument(value.item.toDocument()));
52+
return new BazaarGetItemResponse(
53+
value.itemName,
54+
List.copyOf(value.buyOrders),
55+
List.copyOf(value.sellOrders)
56+
);
4757
}
4858
};
4959
}
5060

5161
public record BazaarGetItemMessage(String itemName) {}
5262

53-
public record BazaarGetItemResponse(BazaarItem item) {}
54-
}
63+
public record BazaarGetItemResponse(
64+
String itemName,
65+
List<OrderRecord> buyOrders,
66+
List<OrderRecord> sellOrders
67+
) {}
68+
69+
public record OrderRecord(
70+
UUID playerUUID,
71+
double price,
72+
double amount
73+
) {}
74+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package net.swofty.commons.protocol.objects.bazaar;
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.List;
9+
import java.util.UUID;
10+
import java.util.stream.Collectors;
11+
12+
public class BazaarGetPendingOrdersProtocolObject
13+
extends ProtocolObject<
14+
BazaarGetPendingOrdersProtocolObject.BazaarGetPendingOrdersMessage,
15+
BazaarGetPendingOrdersProtocolObject.BazaarGetPendingOrdersResponse> {
16+
17+
@Override
18+
public Serializer<BazaarGetPendingOrdersMessage> getSerializer() {
19+
return new Serializer<>() {
20+
@Override
21+
public String serialize(BazaarGetPendingOrdersMessage v) {
22+
JSONObject o = new JSONObject();
23+
o.put("player-uuid", v.playerUUID.toString());
24+
return o.toString();
25+
}
26+
27+
@Override
28+
public BazaarGetPendingOrdersMessage deserialize(String json) {
29+
JSONObject o = new JSONObject(json);
30+
return new BazaarGetPendingOrdersMessage(
31+
UUID.fromString(o.getString("player-uuid"))
32+
);
33+
}
34+
35+
@Override
36+
public BazaarGetPendingOrdersMessage clone(BazaarGetPendingOrdersMessage v) {
37+
return new BazaarGetPendingOrdersMessage(v.playerUUID);
38+
}
39+
};
40+
}
41+
42+
@Override
43+
public Serializer<BazaarGetPendingOrdersResponse> getReturnSerializer() {
44+
return new Serializer<>() {
45+
@Override
46+
public String serialize(BazaarGetPendingOrdersResponse v) {
47+
JSONArray arr = new JSONArray(v.orders.stream().map(order -> {
48+
JSONObject o = new JSONObject();
49+
o.put("order-id", order.orderId.toString());
50+
o.put("item-name", order.itemName);
51+
o.put("side", order.side);
52+
o.put("price", order.price);
53+
o.put("amount", order.amount);
54+
return o;
55+
}).collect(Collectors.toList()));
56+
return arr.toString();
57+
}
58+
59+
@Override
60+
public BazaarGetPendingOrdersResponse deserialize(String json) {
61+
JSONArray arr = new JSONArray(json);
62+
var list = arr.toList().stream().map(obj -> {
63+
JSONObject o = new JSONObject((java.util.Map<?,?>)obj);
64+
return new PendingOrder(
65+
UUID.fromString(o.getString("order-id")),
66+
o.getString("item-name"),
67+
o.getString("side"),
68+
o.getDouble("price"),
69+
o.getDouble("amount")
70+
);
71+
}).collect(Collectors.toList());
72+
return new BazaarGetPendingOrdersResponse(list);
73+
}
74+
75+
@Override
76+
public BazaarGetPendingOrdersResponse clone(BazaarGetPendingOrdersResponse v) {
77+
return new BazaarGetPendingOrdersResponse(v.orders);
78+
}
79+
};
80+
}
81+
82+
@lombok.AllArgsConstructor
83+
public static class BazaarGetPendingOrdersMessage {
84+
public UUID playerUUID;
85+
}
86+
87+
public record PendingOrder(
88+
UUID orderId,
89+
String itemName,
90+
String side,
91+
double price,
92+
double amount
93+
) {}
94+
95+
/** Response is just a list of those. */
96+
@lombok.AllArgsConstructor
97+
public static class BazaarGetPendingOrdersResponse {
98+
public List<PendingOrder> orders;
99+
}
100+
}

0 commit comments

Comments
 (0)