-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTableRegionFingerprintService.java
More file actions
349 lines (316 loc) · 13.8 KB
/
Copy pathTableRegionFingerprintService.java
File metadata and controls
349 lines (316 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package top.ellan.mahjong.table.render;
import top.ellan.mahjong.model.SeatWind;
import top.ellan.mahjong.render.TableRenderSubject;
import top.ellan.mahjong.render.layout.TableRenderLayout;
import top.ellan.mahjong.render.snapshot.TableRenderSnapshot;
import top.ellan.mahjong.render.snapshot.TableSeatRenderSnapshot;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public final class TableRegionFingerprintService {
private static final String REGION_TABLE = "table";
private static final String REGION_WALL = "wall";
private static final String REGION_DORA = "dora";
private static final String REGION_CENTER = "center";
public Map<String, Long> precomputeRegionFingerprints(TableRenderSubject session, TableRenderSnapshot snapshot) {
Map<String, Long> fingerprints = new HashMap<>();
fingerprints.put(REGION_TABLE, this.tableFingerprint(session, snapshot));
fingerprints.put(REGION_WALL, this.wallFingerprint(snapshot));
fingerprints.put(REGION_DORA, this.doraFingerprint(snapshot));
fingerprints.put(REGION_CENTER, this.centerFingerprint(snapshot));
for (SeatWind wind : SeatWind.values()) {
TableSeatRenderSnapshot seat = snapshot.seat(wind);
fingerprints.put(this.seatRegionKey("visual", wind), this.seatVisualFingerprint(session, wind));
fingerprints.put(this.seatRegionKey("labels", wind), this.seatLabelFingerprint(session, snapshot, seat));
fingerprints.put(this.seatRegionKey("sticks", wind), this.stickFingerprint(snapshot, seat));
fingerprints.put(this.seatRegionKey("hand-public", wind), this.handPublicFingerprint(snapshot, seat));
}
return Map.copyOf(fingerprints);
}
public long handPrivateTileFingerprint(TableSeatRenderSnapshot seat, TableRenderLayout.SeatLayoutPlan plan, int tileIndex) {
TableRenderLayout.Point point = plan.privateHandPoints().get(tileIndex);
return fingerprintBuilder(160)
.field("hand-private-tile")
.field(seat.wind().name())
.field(Objects.toString(seat.playerId(), "empty"))
.field(tileIndex)
.field(seat.online())
.field(seat.hand().size())
.field(seat.selectedHandTileIndices().contains(tileIndex))
.field(Double.doubleToLongBits(point.x()))
.field(Double.doubleToLongBits(point.y()))
.field(Double.doubleToLongBits(point.z()))
.field(seat.hand().get(tileIndex).name())
.value();
}
public long handPublicTileFingerprint(
TableRenderSnapshot snapshot,
TableSeatRenderSnapshot seat,
TableRenderLayout.SeatLayoutPlan plan,
int tileIndex
) {
TableRenderLayout.Point point = plan.publicHandPoints().get(tileIndex);
boolean concealHand = snapshot.started();
return fingerprintBuilder(160)
.field("hand-public-tile")
.field(seat.wind().name())
.field(Objects.toString(seat.playerId(), "empty"))
.field(tileIndex)
.field(snapshot.started())
.field(seat.online())
.field(seat.viewerMembershipSignature())
.field(seat.stickLayoutCount())
.field(Double.doubleToLongBits(point.x()))
.field(Double.doubleToLongBits(point.y()))
.field(Double.doubleToLongBits(point.z()))
.field(concealHand ? "unknown" : seat.hand().get(tileIndex).name())
.value();
}
public long discardTileFingerprint(TableSeatRenderSnapshot seat, TableRenderLayout.SeatLayoutPlan plan, int discardIndex) {
TableRenderLayout.TilePlacement placement = plan.discardPlacements().get(discardIndex);
return fingerprintBuilder(160)
.field("discard-tile")
.field(seat.wind().name())
.field(Objects.toString(seat.playerId(), "empty"))
.field(discardIndex)
.field(seat.riichiDiscardIndex())
.field(Float.floatToIntBits(placement.yaw()))
.field(Double.doubleToLongBits(placement.point().x()))
.field(Double.doubleToLongBits(placement.point().y()))
.field(Double.doubleToLongBits(placement.point().z()))
.field(placement.tile().name())
.field(placement.pose().name())
.value();
}
long wallTileFingerprint(TableRenderLayout.LayoutPlan plan, int wallIndex) {
TableRenderLayout.TilePlacement placement = plan.wallTiles().get(wallIndex);
if (placement == null) {
return fingerprintBuilder(32)
.field("wall-empty")
.field(wallIndex)
.value();
}
return fingerprintBuilder(160)
.field("wall-tile")
.field(wallIndex)
.field(Float.floatToIntBits(placement.yaw()))
.field(Double.doubleToLongBits(placement.point().x()))
.field(Double.doubleToLongBits(placement.point().y()))
.field(Double.doubleToLongBits(placement.point().z()))
.field(placement.tile().name())
.field(placement.pose().name())
.value();
}
public long opaqueFingerprint(String value) {
return fingerprintBuilder(64)
.field("opaque")
.field(value)
.value();
}
private long wallFingerprint(TableRenderSnapshot snapshot) {
if (!snapshot.started()) {
return fingerprintBuilder(24)
.field("wall")
.field("waiting")
.value();
}
return fingerprintBuilder(32)
.field("wall")
.field(snapshot.started())
.field(snapshot.gameFinished())
.field(snapshot.remainingWallCount())
.value();
}
private long tableFingerprint(TableRenderSubject session, TableRenderSnapshot snapshot) {
return fingerprintBuilder(48)
.field("table")
.field(snapshot.worldName())
.field((int) Math.floor(snapshot.centerX()))
.field((int) Math.floor(snapshot.centerY()))
.field((int) Math.floor(snapshot.centerZ()))
.field(Objects.toString(session.settings().craftEngineTableFurnitureId(), ""))
.value();
}
private long doraFingerprint(TableRenderSnapshot snapshot) {
if (!snapshot.started()) {
return fingerprintBuilder(24)
.field("dora")
.field("waiting")
.value();
}
FingerprintBuilder builder = fingerprintBuilder(64)
.field("dora")
.field(snapshot.started())
.field(snapshot.doraIndicators().size());
snapshot.doraIndicators().forEach(tile -> builder.field(tile.name()));
return builder.value();
}
private long centerFingerprint(TableRenderSnapshot snapshot) {
return fingerprintBuilder(192)
.field("center")
.field(snapshot.started() ? "started" : "waiting")
.field(snapshot.publicCenterText())
.field(snapshot.lastPublicDiscardPlayerId())
.field(snapshot.lastPublicDiscardTile())
.value();
}
private long seatLabelFingerprint(TableRenderSubject session, TableRenderSnapshot snapshot, TableSeatRenderSnapshot seat) {
return fingerprintBuilder(128)
.field("labels")
.field("depth-v2")
.field(seat.wind().name())
.field(snapshot.currentSeat().name())
.field(snapshot.started())
// Read roundStartInProgress from the snapshot rather than the live session.
// precomputeRegionFingerprints runs on the async render-precompute thread;
// although the session field is now volatile (so the live read would be safe),
// using the snapshot guarantees the fingerprint matches the rest of the
// snapshot state captured on the region thread, avoiding a torn read where
// the seat label fingerprint reflects a newer roundStartInProgress value than
// the seat occupancy snapshots around it.
.field(snapshot.roundStartInProgress())
.field(Objects.toString(seat.playerId(), "empty"))
.field(seat.displayName())
.field(seat.publicSeatStatus())
.field(seat.riichi())
.field(seat.ready())
.field(seat.queuedToLeave())
.value();
}
private long seatVisualFingerprint(TableRenderSubject session, SeatWind wind) {
return fingerprintBuilder(96)
.field("visual")
.field("chair-stable-v2")
.field(wind.name())
.field(Objects.toString(session.settings().craftEngineSeatFurnitureId(), ""))
.value();
}
private long handPublicFingerprint(TableRenderSnapshot snapshot, TableSeatRenderSnapshot seat) {
FingerprintBuilder builder = fingerprintBuilder(256)
.field("hand-public")
.field(seat.wind().name())
.field(Objects.toString(seat.playerId(), "empty"));
if (seat.playerId() == null) {
return builder.value();
}
builder.field(snapshot.started())
.field(seat.online())
.field(seat.viewerMembershipSignature())
.field(seat.stickLayoutCount());
seat.hand().forEach(tile -> builder.field(tile.name()));
return builder.value();
}
public long meldTileFingerprint(TableSeatRenderSnapshot seat, TableRenderLayout.SeatLayoutPlan plan, int meldIndex) {
TableRenderLayout.TilePlacement placement = plan.meldPlacements().get(meldIndex);
return fingerprintBuilder(160)
.field("meld-tile")
.field(seat.wind().name())
.field(Objects.toString(seat.playerId(), "empty"))
.field(meldIndex)
.field(Float.floatToIntBits(placement.yaw()))
.field(Double.doubleToLongBits(placement.point().x()))
.field(Double.doubleToLongBits(placement.point().y()))
.field(Double.doubleToLongBits(placement.point().z()))
.field(placement.tile().name())
.field(placement.pose().name())
.value();
}
private long stickFingerprint(TableRenderSnapshot snapshot, TableSeatRenderSnapshot seat) {
FingerprintBuilder builder = fingerprintBuilder(128)
.field("sticks")
.field(seat.wind().name())
.field(Objects.toString(seat.playerId(), "empty"))
.field(snapshot.honbaCount())
.field(snapshot.dealerSeat().name());
if (seat.playerId() == null) {
return builder.value();
}
builder.field(seat.riichi());
seat.scoringSticks().forEach(stick -> builder.field(stick.name()));
seat.cornerSticks().forEach(stick -> builder.field(stick.name()));
return builder.value();
}
private String seatRegionKey(String region, SeatWind wind) {
return region + ":" + wind.name();
}
private static FingerprintBuilder fingerprintBuilder(int capacity) {
return new FingerprintBuilder();
}
private static final class FingerprintBuilder {
private static final long FNV_OFFSET_BASIS = 0xcbf29ce484222325L;
private static final long FNV_PRIME = 0x100000001b3L;
private long hash = FNV_OFFSET_BASIS;
private boolean needsSeparator;
private FingerprintBuilder field(Object value) {
this.startField();
this.mixText(Objects.toString(value, ""));
return this.finishField();
}
private FingerprintBuilder field(boolean value) {
this.startField();
this.mixText(value ? "true" : "false");
return this.finishField();
}
private FingerprintBuilder field(char value) {
this.startField();
this.mix(value);
return this.finishField();
}
private FingerprintBuilder field(int value) {
this.startField();
this.mixDecimal(value);
return this.finishField();
}
private FingerprintBuilder field(long value) {
this.startField();
this.mixDecimal(value);
return this.finishField();
}
private long value() {
return this.hash;
}
private void startField() {
if (this.needsSeparator) {
this.mix(':');
}
}
private FingerprintBuilder finishField() {
this.needsSeparator = true;
return this;
}
private void mixText(String value) {
for (int index = 0; index < value.length(); index++) {
this.mix(value.charAt(index));
}
}
private void mixDecimal(long value) {
long remaining = value > 0L ? -value : value;
long lowDigits = 0L;
long highDigits = 0L;
int digitCount = 0;
do {
int digit = (int) -(remaining % 10L);
if (digitCount < 16) {
lowDigits |= (long) digit << (digitCount * 4);
} else {
highDigits |= (long) digit << ((digitCount - 16) * 4);
}
digitCount++;
remaining /= 10L;
} while (remaining != 0L);
if (value < 0L) {
this.mix('-');
}
for (int index = digitCount - 1; index >= 0; index--) {
int digit = index < 16
? (int) (lowDigits >>> (index * 4)) & 0x0f
: (int) (highDigits >>> ((index - 16) * 4)) & 0x0f;
this.mix((char) ('0' + digit));
}
}
private void mix(char value) {
this.hash ^= value;
this.hash *= FNV_PRIME;
}
}
}