Skip to content

Commit 98220af

Browse files
committed
fix: sửa thời gian và lịch kiểm tra auto-bid
- Chuẩn hóa timestamp của bid sang epoch milliseconds - Sửa hiển thị thời gian bid trên bảng và biểu đồ - Thêm `nextCheckAt` để mỗi auto-bid tự kiểm tra theo chu kỳ 5 giây từ lúc bắt đầu - Ưu tiên auto-bid cùng thời điểm theo max bid cao hơn, sau đó user ID nhỏ hơn - Bổ sung test cho cadence riêng và tie-break auto-bid
1 parent 52e9aeb commit 98220af

10 files changed

Lines changed: 250 additions & 49 deletions

File tree

client/src/main/java/com/auction/controller/BiddingController.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public void init(AppState appState, Stage stage, Item item) {
6262

6363

6464
public void setItem(String itemId) {
65-
long startSeconds = item.getBidStartTime() != null
66-
? item.getBidStartTime().atZone(ZoneId.systemDefault()).toEpochSecond()
67-
: System.currentTimeMillis() / 1000L;
68-
Bid bid = new Bid(item.getSellerId(), item.getId(), item.getStartingPrice(), startSeconds);
65+
long startMillis = item.getBidStartTime() != null
66+
? item.getBidStartTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
67+
: System.currentTimeMillis();
68+
Bid bid = new Bid(item.getSellerId(), item.getId(), item.getStartingPrice(), startMillis);
6969
List<Bid> bids = new ArrayList<>(appState.bidService.getBidsForItem(itemId));
7070
bids.add(0, bid);
7171
loadChart(bids);
@@ -123,10 +123,8 @@ private void loadChart(List<Bid> bids) {
123123

124124
for (Bid bid : bids) {
125125

126-
long seconds = bid.getTimestamp();
127-
long milliSeconds = seconds * 1000L;
128126
LocalDateTime dateTime = LocalDateTime.ofInstant(
129-
Instant.ofEpochMilli(milliSeconds),
127+
Instant.ofEpochMilli(bid.getTimestamp()),
130128
ZoneId.systemDefault()
131129
);
132130
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");

client/src/main/java/com/auction/service/http/JsonMappers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public static AutoBid toAutoBid(Map<String, Object> map) {
103103
num(map, "maxBid"),
104104
num(map, "increment"),
105105
Math.round(num(map, "createdAt")),
106-
Math.round(num(map, "lastBidAt"))
106+
Math.round(num(map, "lastBidAt")),
107+
Math.round(num(map, "nextCheckAt"))
107108
);
108109
}
109110

server/src/main/java/com/auction/repository/DatabaseManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ CREATE TABLE IF NOT EXISTS auto_bids (
129129
increment REAL NOT NULL,
130130
created_at INTEGER NOT NULL,
131131
last_bid_at INTEGER NOT NULL DEFAULT 0,
132+
next_check_at INTEGER NOT NULL,
132133
PRIMARY KEY (user_id, item_id)
133134
)
134135
""");

server/src/main/java/com/auction/repository/SqliteAutoBidRepository.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ public SqliteAutoBidRepository(DatabaseManager dbManager) {
2121
@Override
2222
public void save(AutoBid autoBid) {
2323
String sql = """
24-
INSERT INTO auto_bids (user_id, item_id, max_bid, increment, created_at, last_bid_at)
25-
VALUES (?, ?, ?, ?, ?, ?)
24+
INSERT INTO auto_bids (user_id, item_id, max_bid, increment, created_at, last_bid_at, next_check_at)
25+
VALUES (?, ?, ?, ?, ?, ?, ?)
2626
ON CONFLICT(user_id, item_id) DO UPDATE SET
2727
max_bid = excluded.max_bid,
2828
increment = excluded.increment,
2929
created_at = auto_bids.created_at,
30-
last_bid_at = auto_bids.last_bid_at
30+
last_bid_at = auto_bids.last_bid_at,
31+
next_check_at = auto_bids.next_check_at
3132
""";
3233
try (PreparedStatement ps = conn.prepareStatement(sql)) {
3334
ps.setString(1, autoBid.getUserId());
@@ -36,6 +37,7 @@ ON CONFLICT(user_id, item_id) DO UPDATE SET
3637
ps.setDouble(4, autoBid.getIncrement());
3738
ps.setLong(5, autoBid.getCreatedAt());
3839
ps.setLong(6, autoBid.getLastBidAt());
40+
ps.setLong(7, autoBid.getNextCheckAt());
3941
ps.executeUpdate();
4042
} catch (SQLException e) {
4143
throw new RuntimeException("Failed to save auto-bid: " + e.getMessage(), e);
@@ -58,7 +60,7 @@ public Optional<AutoBid> findByUserAndItem(String userId, String itemId) {
5860

5961
@Override
6062
public List<AutoBid> findByItemId(String itemId) {
61-
String sql = "SELECT * FROM auto_bids WHERE item_id = ? ORDER BY max_bid DESC, created_at ASC";
63+
String sql = "SELECT * FROM auto_bids WHERE item_id = ? ORDER BY next_check_at ASC, max_bid DESC, user_id ASC";
6264
return query(sql, itemId);
6365
}
6466

@@ -81,15 +83,29 @@ public void delete(String userId, String itemId) {
8183
}
8284

8385
@Override
84-
public void recordBid(String userId, String itemId, long bidAt) {
85-
String sql = "UPDATE auto_bids SET last_bid_at = ? WHERE user_id = ? AND item_id = ?";
86+
public void recordBid(String userId, String itemId, long bidAt, long nextCheckAt) {
87+
String sql = "UPDATE auto_bids SET last_bid_at = ?, next_check_at = ? WHERE user_id = ? AND item_id = ?";
8688
try (PreparedStatement ps = conn.prepareStatement(sql)) {
8789
ps.setLong(1, bidAt);
90+
ps.setLong(2, nextCheckAt);
91+
ps.setString(3, userId);
92+
ps.setString(4, itemId);
93+
ps.executeUpdate();
94+
} catch (SQLException e) {
95+
throw new RuntimeException("Failed to update auto-bid cooldown: " + e.getMessage(), e);
96+
}
97+
}
98+
99+
@Override
100+
public void recordCheck(String userId, String itemId, long nextCheckAt) {
101+
String sql = "UPDATE auto_bids SET next_check_at = ? WHERE user_id = ? AND item_id = ?";
102+
try (PreparedStatement ps = conn.prepareStatement(sql)) {
103+
ps.setLong(1, nextCheckAt);
88104
ps.setString(2, userId);
89105
ps.setString(3, itemId);
90106
ps.executeUpdate();
91107
} catch (SQLException e) {
92-
throw new RuntimeException("Failed to update auto-bid cooldown: " + e.getMessage(), e);
108+
throw new RuntimeException("Failed to update auto-bid check time: " + e.getMessage(), e);
93109
}
94110
}
95111

@@ -112,7 +128,8 @@ private AutoBid mapRow(ResultSet rs) throws SQLException {
112128
rs.getDouble("max_bid"),
113129
rs.getDouble("increment"),
114130
rs.getLong("created_at"),
115-
rs.getLong("last_bid_at")
131+
rs.getLong("last_bid_at"),
132+
rs.getLong("next_check_at")
116133
);
117134
}
118135
}

server/src/main/java/com/auction/server/controller/BidController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ private Map<String, Object> autoBidToMap(AutoBid autoBid) {
134134
map.put("increment", autoBid.getIncrement());
135135
map.put("createdAt", autoBid.getCreatedAt());
136136
map.put("lastBidAt", autoBid.getLastBidAt());
137+
map.put("nextCheckAt", autoBid.getNextCheckAt());
137138
return map;
138139
}
139140

shared/src/main/java/com/auction/model/AutoBid.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@ public class AutoBid {
77
private final double increment;
88
private final long createdAt;
99
private final long lastBidAt;
10+
private final long nextCheckAt;
1011

1112
public AutoBid(String userId, String itemId, double maxBid, double increment, long createdAt) {
1213
this(userId, itemId, maxBid, increment, createdAt, 0L);
1314
}
1415

1516
public AutoBid(String userId, String itemId, double maxBid, double increment, long createdAt, long lastBidAt) {
17+
this(userId, itemId, maxBid, increment, createdAt, lastBidAt,
18+
lastBidAt > 0L ? lastBidAt + 5_000L : createdAt);
19+
}
20+
21+
public AutoBid(String userId, String itemId, double maxBid, double increment,
22+
long createdAt, long lastBidAt, long nextCheckAt) {
1623
this.userId = userId;
1724
this.itemId = itemId;
1825
this.maxBid = maxBid;
1926
this.increment = increment;
2027
this.createdAt = createdAt;
2128
this.lastBidAt = lastBidAt;
29+
this.nextCheckAt = nextCheckAt > 0L
30+
? nextCheckAt
31+
: (lastBidAt > 0L ? lastBidAt + 5_000L : createdAt);
2232
}
2333

2434
public String getUserId() { return userId; }
@@ -32,4 +42,6 @@ public AutoBid(String userId, String itemId, double maxBid, double increment, lo
3242
public long getCreatedAt() { return createdAt; }
3343

3444
public long getLastBidAt() { return lastBidAt; }
45+
46+
public long getNextCheckAt() { return nextCheckAt; }
3547
}

shared/src/main/java/com/auction/model/Bid.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Bid {
77
private final long timestamp;
88

99
public Bid(String bidderId, String itemId, double amount) {
10-
this(bidderId, itemId, amount, System.currentTimeMillis() / 1000L);
10+
this(bidderId, itemId, amount, System.currentTimeMillis());
1111
}
1212

1313
public Bid(String bidderId, String itemId, double amount, long timestamp) {

shared/src/main/java/com/auction/repository/AutoBidRepository.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@ public interface AutoBidRepository {
1212
List<AutoBid> findByUserId(String userId);
1313
void delete(String userId, String itemId);
1414

15-
default void recordBid(String userId, String itemId, long bidAt) {
15+
default void recordBid(String userId, String itemId, long bidAt, long nextCheckAt) {
1616
findByUserAndItem(userId, itemId).ifPresent(autoBid -> save(new AutoBid(
1717
autoBid.getUserId(),
1818
autoBid.getItemId(),
1919
autoBid.getMaxBid(),
2020
autoBid.getIncrement(),
2121
autoBid.getCreatedAt(),
22-
bidAt
22+
bidAt,
23+
nextCheckAt
24+
)));
25+
}
26+
27+
default void recordCheck(String userId, String itemId, long nextCheckAt) {
28+
findByUserAndItem(userId, itemId).ifPresent(autoBid -> save(new AutoBid(
29+
autoBid.getUserId(),
30+
autoBid.getItemId(),
31+
autoBid.getMaxBid(),
32+
autoBid.getIncrement(),
33+
autoBid.getCreatedAt(),
34+
autoBid.getLastBidAt(),
35+
nextCheckAt
2336
)));
2437
}
2538
}

0 commit comments

Comments
 (0)