Skip to content

Commit 205756a

Browse files
feat: bazaar...
Took 3 hours 24 minutes
1 parent 89b5ce2 commit 205756a

22 files changed

Lines changed: 1732 additions & 659 deletions
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package net.swofty.commons.bazaar;
2+
3+
import org.json.JSONObject;
4+
import java.time.Instant;
5+
import java.util.UUID;
6+
7+
public record BuyOrderRefundTransaction(
8+
UUID orderId,
9+
String itemName,
10+
UUID owner,
11+
UUID ownerProfile,
12+
double refundAmount,
13+
String reason, // "COMPLETED", "EXPIRED", "CANCELLED"
14+
Instant timestamp
15+
) implements BazaarTransaction {
16+
17+
@Override
18+
public JSONObject toJSON() {
19+
JSONObject json = new JSONObject();
20+
json.put("orderId", orderId.toString());
21+
json.put("itemName", itemName);
22+
json.put("owner", owner.toString());
23+
json.put("ownerProfile", ownerProfile.toString());
24+
json.put("refundAmount", refundAmount);
25+
json.put("reason", reason);
26+
json.put("timestamp", timestamp.toString());
27+
return json;
28+
}
29+
30+
@Override
31+
public BazaarTransaction fromJSON(JSONObject json) {
32+
return new BuyOrderRefundTransaction(
33+
UUID.fromString(json.getString("orderId")),
34+
json.getString("itemName"),
35+
UUID.fromString(json.getString("owner")),
36+
UUID.fromString(json.getString("ownerProfile")),
37+
json.getDouble("refundAmount"),
38+
json.getString("reason"),
39+
Instant.parse(json.getString("timestamp"))
40+
);
41+
}
42+
}

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77

88
// Fired when a buy+sell actually crosses
99
public record SuccessfulBazaarTransaction(
10-
String itemName,
11-
UUID buyer,
12-
UUID buyerProfile,
13-
UUID seller,
14-
UUID sellerProfile,
15-
double pricePerUnit,
16-
double quantity,
17-
double taxTaken, // coins withheld by the system
10+
String itemName,
11+
UUID buyer,
12+
UUID buyerProfile,
13+
UUID seller,
14+
UUID sellerProfile,
15+
double actualPricePerUnit, // What was actually paid
16+
double originalBuyerBid, // What buyer originally offered
17+
double quantity,
18+
double taxTaken,
19+
double priceImprovement, // originalBuyerBid - actualPricePerUnit
20+
UUID buyerOrderId, // Track which order this partial fill belongs to
21+
UUID sellerOrderId, // Track seller's order ID too
1822
Instant timestamp
1923
) implements BazaarTransaction {
2024
@Override
@@ -25,9 +29,13 @@ public JSONObject toJSON() {
2529
json.put("buyerProfile", buyerProfile.toString());
2630
json.put("seller", seller.toString());
2731
json.put("sellerProfile", sellerProfile.toString());
28-
json.put("pricePerUnit", pricePerUnit);
32+
json.put("actualPricePerUnit", actualPricePerUnit);
33+
json.put("originalBuyerBid", originalBuyerBid);
2934
json.put("quantity", quantity);
3035
json.put("taxTaken", taxTaken);
36+
json.put("priceImprovement", priceImprovement);
37+
json.put("buyerOrderId", buyerOrderId.toString());
38+
json.put("sellerOrderId", sellerOrderId.toString());
3139
json.put("timestamp", timestamp.toString());
3240
return json;
3341
}
@@ -40,9 +48,13 @@ public BazaarTransaction fromJSON(JSONObject json) {
4048
UUID.fromString(json.getString("buyerProfile")),
4149
UUID.fromString(json.getString("seller")),
4250
UUID.fromString(json.getString("sellerProfile")),
43-
json.getDouble("pricePerUnit"),
51+
json.getDouble("actualPricePerUnit"),
52+
json.getDouble("originalBuyerBid"),
4453
json.getDouble("quantity"),
4554
json.getDouble("taxTaken"),
55+
json.getDouble("priceImprovement"),
56+
UUID.fromString(json.getString("buyerOrderId")),
57+
UUID.fromString(json.getString("sellerOrderId")),
4658
Instant.parse(json.getString("timestamp"))
4759
);
4860
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public BazaarBuyMessage deserialize(String json) {
3232
message.itemName = jsonObject.getString("item-name");
3333
message.playerUUID = UUID.fromString(jsonObject.getString("player-uuid"));
3434
message.profileUUID = UUID.fromString(jsonObject.getString("profile-uuid"));
35-
message.price = jsonObject.getInt("price");
35+
message.price = jsonObject.getDouble("price");
3636
message.amount = jsonObject.getInt("amount");
3737
return message;
3838
}
@@ -74,7 +74,7 @@ public BazaarBuyResponse clone(BazaarBuyResponse value) {
7474
public static class BazaarBuyMessage {
7575
public String itemName;
7676
public int amount;
77-
public Integer price;
77+
public double price;
7878
public UUID playerUUID;
7979
public UUID profileUUID;
8080
}

proxy.api/src/main/java/net/swofty/proxyapi/redis/ServerOutboundMessage.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.swofty.redisapi.api.ChannelRegistry;
88
import net.swofty.redisapi.api.RedisAPI;
99
import org.json.JSONObject;
10+
import org.tinylog.Logger;
1011

1112
import java.lang.reflect.ParameterizedType;
1213
import java.lang.reflect.Type;
@@ -59,8 +60,12 @@ public static void registerFromProtocolObject(ProtocolObject object) {
5960
message = split[1];
6061
} else message = "";
6162

62-
redisMessageListeners.get(uuid).accept(message);
63-
redisMessageListeners.remove(uuid);
63+
try {
64+
redisMessageListeners.get(uuid).accept(message);
65+
redisMessageListeners.remove(uuid);
66+
} catch (Exception e) {
67+
Logger.error("Failed to handle message from " + uuid + ": " + e.getMessage());
68+
}
6469
});
6570
}
6671

0 commit comments

Comments
 (0)