-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBotDiagnostics.java
More file actions
319 lines (277 loc) · 10.2 KB
/
BotDiagnostics.java
File metadata and controls
319 lines (277 loc) · 10.2 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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Instant;
public final class BotDiagnostics {
private static final String ENABLE_PROP = "poker.diag";
private static final String TRACE_ENABLE_PROP = "poker.trace";
private static final String FILE_PATH = "bot_diagnostics.jsonl";
private static final boolean ENABLED = isEnabledFromConfig();
private static final boolean TRACE_ENABLED = isTraceEnabledFromConfig();
private static final ThreadLocal<Boolean> THREAD_FORCE_ENABLED = ThreadLocal.withInitial(() -> false);
private static final class SimulatorContext {
final String mode;
final String passLabel;
final int workerId;
final int handNumber;
final String street;
SimulatorContext(String mode, String passLabel, int workerId, int handNumber, String street) {
this.mode = mode;
this.passLabel = passLabel;
this.workerId = workerId;
this.handNumber = handNumber;
this.street = street;
}
SimulatorContext withStreet(String nextStreet) {
return new SimulatorContext(mode, passLabel, workerId, handNumber, nextStreet);
}
}
private static final ThreadLocal<SimulatorContext> SIMULATOR_CONTEXT = new ThreadLocal<>();
private BotDiagnostics() {}
private static boolean isEnabledFromConfig() {
String prop = System.getProperty(ENABLE_PROP);
if (prop != null) {
return "1".equals(prop) || "true".equalsIgnoreCase(prop) || "on".equalsIgnoreCase(prop);
}
String env = System.getenv("POKER_DIAG");
if (env != null) {
return "1".equals(env) || "true".equalsIgnoreCase(env) || "on".equalsIgnoreCase(env);
}
return false;
}
private static boolean isTraceEnabledFromConfig() {
String prop = System.getProperty(TRACE_ENABLE_PROP);
if (prop != null) {
return "1".equals(prop) || "true".equalsIgnoreCase(prop) || "on".equalsIgnoreCase(prop);
}
String env = System.getenv("POKER_TRACE");
if (env != null) {
return "1".equals(env) || "true".equalsIgnoreCase(env) || "on".equalsIgnoreCase(env);
}
return false;
}
public static boolean enabled() {
return ENABLED || THREAD_FORCE_ENABLED.get();
}
public static boolean traceConsoleEnabled() {
return TRACE_ENABLED;
}
public static void setThreadForceEnabled(boolean forceEnabled) {
THREAD_FORCE_ENABLED.set(forceEnabled);
}
public static void clearThreadForceEnabled() {
THREAD_FORCE_ENABLED.remove();
}
public static void setSimulatorContext(String mode, String passLabel, int workerId, int handNumber, String street) {
if (!enabled()) {
return;
}
SIMULATOR_CONTEXT.set(new SimulatorContext(
safeLabel(mode, "SIM"),
safeLabel(passLabel, "BASE"),
workerId,
handNumber,
safeLabel(street, "UNKNOWN")));
}
public static void updateSimulatorStreet(String street) {
if (!enabled()) {
return;
}
SimulatorContext context = SIMULATOR_CONTEXT.get();
if (context == null) {
return;
}
SIMULATOR_CONTEXT.set(context.withStreet(safeLabel(street, "UNKNOWN")));
}
public static void clearSimulatorContext() {
SIMULATOR_CONTEXT.remove();
}
private static String safeLabel(String value, String fallback) {
if (value == null || value.trim().isEmpty()) {
return fallback;
}
return value.trim();
}
private static String jsonEscape(String value) {
if (value == null) {
return "";
}
return value.replace("\\", "\\\\").replace("\"", "\\\"");
}
private static int currentHandNumber() {
SimulatorContext context = SIMULATOR_CONTEXT.get();
if (context != null && context.handNumber >= 0) {
return context.handNumber;
}
PokerGame game = PokerGame.getActiveGame();
if (game == null) {
return -1;
}
return game.getCurrentHandNumber();
}
private static String currentStreetLabel() {
SimulatorContext context = SIMULATOR_CONTEXT.get();
if (context != null && context.street != null && !context.street.isEmpty()) {
return context.street;
}
PokerGame game = PokerGame.getActiveGame();
if (game == null) {
return "UNKNOWN";
}
switch (game.currentStreet) {
case 0:
return "PREFLOP";
case 1:
return "FLOP";
case 2:
return "TURN";
case 3:
return "RIVER";
default:
return "UNKNOWN";
}
}
private static String currentModeLabel() {
SimulatorContext context = SIMULATOR_CONTEXT.get();
if (context == null || context.mode == null || context.mode.isEmpty()) {
return "RUNTIME";
}
return context.mode;
}
private static String currentPassLabel() {
SimulatorContext context = SIMULATOR_CONTEXT.get();
if (context == null || context.passLabel == null || context.passLabel.isEmpty()) {
return "DEFAULT";
}
return context.passLabel;
}
private static int currentWorkerId() {
SimulatorContext context = SIMULATOR_CONTEXT.get();
if (context == null) {
return -1;
}
return context.workerId;
}
private static synchronized void writeJsonl(String category, String payload) {
String safePayload = jsonEscape(payload);
int hand = currentHandNumber();
String street = currentStreetLabel();
String mode = jsonEscape(currentModeLabel());
String pass = jsonEscape(currentPassLabel());
int worker = currentWorkerId();
String json = "{\"ts\":\"" + Instant.now().toString() + "\",\"category\":\"" + category
+ "\",\"mode\":\"" + mode
+ "\",\"pass\":\"" + pass
+ "\",\"worker\":" + worker
+ ",\"hand\":" + hand
+ ",\"street\":\"" + jsonEscape(street) + "\""
+ ",\"payload\":\"" + safePayload + "\"}";
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILE_PATH, true))) {
bw.write(json);
bw.newLine();
} catch (IOException e) {
// Intentionally silent: diagnostics should never disturb gameplay output.
}
}
public static void recordGodDecision(String street, String botName, String decisionReason,
String actionLabel, int amount, String details) {
if (!enabled()) {
return;
}
writeJsonl("decision",
"street=" + street + ", bot=" + botName + ", action=" + actionLabel + ", amount=" + amount
+ ", reason=" + decisionReason + ", " + details);
}
public static void recordPlayerDecision(String street, String playerName, String actionLabel, int amount, String details) {
if (!enabled()) {
return;
}
writeJsonl("decision",
"street=" + street + ", bot=" + playerName + " (HUMAN), action=" + actionLabel + ", amount=" + amount
+ ", reason=human input, " + details);
}
public static void recordArchetypeShift(String playerName,
PokerBot.CognitiveArchetype before,
PokerBot.CognitiveArchetype after,
PokerBot.CognitiveProfile profile,
String trigger) {
if (!enabled() || before == after) {
return;
}
String payload = "player=" + playerName
+ ", before=" + before
+ ", after=" + after
+ ", trigger=" + trigger
+ ", vpipEMA=" + String.format("%.3f", profile.vpipEMA)
+ ", pfrEMA=" + String.format("%.3f", profile.pfrEMA)
+ ", afqFlopEMA=" + String.format("%.3f", profile.afqFlopEMA)
+ ", afqTurnEMA=" + String.format("%.3f", profile.afqTurnEMA)
+ ", afqRiverEMA=" + String.format("%.3f", profile.afqRiverEMA)
+ ", foldToCbetEMA=" + String.format("%.3f", profile.foldToCbetEMA)
+ ", wtsdEMA=" + String.format("%.3f", profile.wtsdEMA)
+ ", vIndex=" + String.format("%.3f", profile.vIndex)
+ ", styleShiftEMA=" + String.format("%.3f", profile.styleShiftEMA);
writeJsonl("archetype-shift", payload);
}
public static void recordProfileSnapshot(String playerName, PokerBot.CognitiveProfile profile, String trigger) {
if (!enabled()) {
return;
}
String payload = "player=" + playerName
+ ", trigger=" + trigger
+ ", archetype=" + profile.getArchetype()
+ ", handsPlayed=" + profile.handsPlayed
+ ", vpipEMA=" + String.format("%.3f", profile.vpipEMA)
+ ", pfrEMA=" + String.format("%.3f", profile.pfrEMA)
+ ", afqPreflopEMA=" + String.format("%.3f", profile.afqPreflopEMA)
+ ", afqFlopEMA=" + String.format("%.3f", profile.afqFlopEMA)
+ ", afqTurnEMA=" + String.format("%.3f", profile.afqTurnEMA)
+ ", afqRiverEMA=" + String.format("%.3f", profile.afqRiverEMA)
+ ", foldToCbetEMA=" + String.format("%.3f", profile.foldToCbetEMA)
+ ", wtsdEMA=" + String.format("%.3f", profile.wtsdEMA)
+ ", vIndex=" + String.format("%.3f", profile.vIndex)
+ ", styleShiftEMA=" + String.format("%.3f", profile.styleShiftEMA);
writeJsonl("profile", payload);
}
public static void recordConvergence(String playerName,
int seat,
long samples,
double trueVpip,
double truePfr,
double emaVpip,
double emaPfr,
String trigger) {
if (!enabled()) {
return;
}
double signedVpipErr = emaVpip - trueVpip;
double signedPfrErr = emaPfr - truePfr;
double absVpipErr = Math.abs(signedVpipErr);
double absPfrErr = Math.abs(signedPfrErr);
String payload = "player=" + playerName
+ ", seat=" + seat
+ ", trigger=" + trigger
+ ", samples=" + samples
+ ", trueVPIP=" + String.format("%.4f", trueVpip)
+ ", emaVPIP=" + String.format("%.4f", emaVpip)
+ ", absErrVPIP=" + String.format("%.4f", absVpipErr)
+ ", signedErrVPIP=" + String.format("%.4f", signedVpipErr)
+ ", truePFR=" + String.format("%.4f", truePfr)
+ ", emaPFR=" + String.format("%.4f", emaPfr)
+ ", absErrPFR=" + String.format("%.4f", absPfrErr)
+ ", signedErrPFR=" + String.format("%.4f", signedPfrErr);
writeJsonl("convergence", payload);
}
public static void recordSourceTransferHand(String payload) {
if (!enabled()) {
return;
}
writeJsonl("source-transfer-hand", payload);
}
public static void recordSourceTransferRollup(String payload) {
if (!enabled()) {
return;
}
writeJsonl("source-transfer-rollup", payload);
}
}