-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzzTurnGenerator.php
More file actions
249 lines (225 loc) · 9.48 KB
/
zzTurnGenerator.php
File metadata and controls
249 lines (225 loc) · 9.48 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
<?php
include './Core/HTTPLibraries.php';
$response = new stdClass();
// If CheckLoggedInUserMod exists (web environment), use it; allow CLI runs otherwise
if(function_exists('CheckLoggedInUserMod')) {
$error = CheckLoggedInUserMod();
if($error !== "") {
$response->error = $error;
echo json_encode($response);
exit();
}
}
$rootName = TryGET("rootName", "");
// If running from CLI, accept args like rootName=RBSim or the first positional arg
if($rootName == "" && php_sapi_name() == 'cli') {
global $argv;
for($i=1; $i<count($argv); ++$i) {
if(strpos($argv[$i], 'rootName=') === 0) {
$rootName = substr($argv[$i], strlen('rootName='));
break;
}
}
if($rootName == '' && isset($argv[1])) $rootName = $argv[1];
}
$schemaFile = "./Schemas/" . $rootName . "/TurnSchema.txt";
if(!file_exists($schemaFile)) {
$response->error = "Turn schema not found: " . $schemaFile;
echo json_encode($response);
exit();
}
$handler = fopen($schemaFile, 'r');
$states = []; // array of state objects: {id, abbr, code, transitions: [{input, target}]}
$decisionQueueEnabled = false;
$currentState = null;
// First pass: look for DecisionQueue: Enabled and collect lines for state parsing
$schemaLines = [];
while(!feof($handler)) {
$line = fgets($handler);
if($line === false) break;
$line = trim($line);
if($line == "") continue;
if(stripos($line, 'DecisionQueue:') === 0) {
if(stripos($line, 'enabled') !== false) $decisionQueueEnabled = true;
continue;
}
$schemaLines[] = $line;
}
fclose($handler);
// Second pass: parse states and transitions from $schemaLines
$currentState = null;
foreach($schemaLines as $line) {
if(strpos($line, ';') !== false) {
$parts = array_map('trim', explode(';', $line));
$title = $parts[0];
$abbr = isset($parts[1]) ? $parts[1] : $title;
$code = isset($parts[2]) ? $parts[2] : $abbr;
$currentState = new stdClass();
$currentState->Title = $title;
$currentState->Abbr = $abbr;
$currentState->Code = $code;
$currentState->Transitions = [];
$states[$abbr] = $currentState;
continue;
}
$arr = preg_split('/->|→/', $line);
if(count($arr) >= 2 && $currentState != null) {
$input = trim($arr[0]);
$target = trim($arr[1]);
$t = $target;
$currentState->Transitions[] = (object)[ 'Input' => $input, 'Target' => $t ];
}
}
// Ensure output folder exists
$rootPath = './' . $rootName;
if(!is_dir($rootPath)) mkdir($rootPath, 0755, true);
// Write TurnStates.php
$statesFile = $rootPath . '/TurnStates.php';
$h = fopen($statesFile, 'w');
fwrite($h, "<?php\n");
fwrite($h, "// Auto-generated by zzTurnGenerator.php\n");
fwrite($h, "// Generated turn states and transitions\n\n");
fwrite($h, "\$turnStates = array();\n");
// Build a states array in PHP code
fwrite($h, "\$turnStates = array(\n");
foreach($states as $abbr => $st) {
$titleSafe = addslashes($st->Title);
$codeSafe = addslashes($st->Code);
fwrite($h, " '" . $abbr . "' => array(\n");
fwrite($h, " 'Title' => '" . $titleSafe . "',\n");
fwrite($h, " 'Code' => '" . $codeSafe . "',\n");
fwrite($h, " 'Transitions' => array(\n");
foreach($st->Transitions as $tr) {
$in = addslashes($tr->Input);
$tgt = addslashes($tr->Target);
fwrite($h, " array('Input' => '" . $in . "', 'Target' => '" . $tgt . "'),\n");
}
fwrite($h, " ),\n");
fwrite($h, " ),\n");
}
fwrite($h, ");\n\n");
fclose($h);
// Write TurnController.php
$ctrlFile = $rootPath . '/TurnController.php';
$h = fopen($ctrlFile, 'w');
fwrite($h, "<?php\n");
fwrite($h, "// Auto-generated turn controller by zzTurnGenerator.php\n\n");
fwrite($h, "include_once __DIR__ . '/TurnStates.php';\n");
if ($decisionQueueEnabled) {
fwrite($h, "include_once __DIR__ . '/../Core/DecisionQueueController.php';\n");
}
fwrite($h, "\n");
// Emit DecisionQueue flag for use in controller logic
fwrite($h, "function IsDecisionQueueEnabled() { return $decisionQueueEnabled; }\n\n");
// Emit a phase-based EvaluateTransition that uses the CurrentPhase zone
fwrite($h, "function EvaluateTransition(\$input) {\n");
fwrite($h, " \$currentPhase = GetCurrentPhase();\n");
if ($decisionQueueEnabled) {
fwrite($h, " // DecisionQueue check: block phase progression if any player has pending decisions\n");
fwrite($h, " \$dqController = new DecisionQueueController();\n");
fwrite($h, " if (!\$dqController->AllQueuesEmpty()) return 'PENDING_DECISION';\n");
}
fwrite($h, " if(!isset(\$currentPhase)) return \$currentPhase;\n");
fwrite($h, " switch(\$currentPhase) {\n");
// For each state, emit case block checking that state's transitions
foreach($states as $abbr => $st) {
fwrite($h, " case '" . addslashes($abbr) . "':\n");
// Exact-match transitions first
foreach($st->Transitions as $tr) {
$in = strtoupper(trim($tr->Input));
$tgt = addslashes($tr->Target);
fwrite($h, " if(strtoupper(trim(\$input)) == '" . addslashes($in) . "') return '" . $tgt . "';\n");
}
// AUTO fallback for this state (first AUTO transition)
$autoTarget = null;
foreach($st->Transitions as $tr) { if(strtoupper(trim($tr->Input)) == 'AUTO') { $autoTarget = $tr->Target; break; } }
if($autoTarget !== null) fwrite($h, " // AUTO fallback\n return '" . addslashes($autoTarget) . "';\n");
// PASS fallback for this state
$passTarget = null;
foreach($st->Transitions as $tr) { if(strtoupper(trim($tr->Input)) == 'PASS') { $passTarget = $tr->Target; break; } }
if($passTarget !== null) fwrite($h, " // PASS fallback\n return '" . addslashes($passTarget) . "';\n");
fwrite($h, " break;\n");
}
fwrite($h, " default: break;\n");
fwrite($h, " }\n");
fwrite($h, " return \$currentPhase;\n");
fwrite($h, "}\n\n");
// AdvanceTurnState now operates on CurrentPhase zone
fwrite($h, "function AdvanceTurnState(\$input, \$params = null) {\n");
fwrite($h, " \$next = EvaluateTransition(\$input);\n");
fwrite($h, " if(\$next !== GetCurrentPhase() && \$next !== \"PENDING_DECISION\") { \n");
fwrite($h, " SetCurrentPhase(\$next);\n");
fwrite($h, " if(\$params !== null) SetPhaseParameters(json_encode(\$params));\n");
fwrite($h, " return true;\n");
fwrite($h, " }\n");
fwrite($h, " return false;\n");
fwrite($h, "}\n\n");
// Auto-advance along AUTO transitions until none remain. Returns true if state changed.
fwrite($h, "function AutoAdvance() {\n");
fwrite($h, " \$changed = false;\n");
fwrite($h, " while(true) {\n");
fwrite($h, " // Use EvaluateTransition('AUTO') to determine the next auto target for the current phase.\n");
fwrite($h, " \$next = EvaluateTransition('AUTO');\n");
fwrite($h, " if(\$next === GetCurrentPhase() || \$next == \"PENDING_DECISION\") break;\n");
fwrite($h, " SetCurrentPhase(\$next);\n");
fwrite($h, " ExecutePhase();\n");
fwrite($h, " \$changed = true;\n");
fwrite($h, " }\n");
fwrite($h, " return \$changed;\n");
fwrite($h, "}\n\n");
// Emit ExecutePhase and convenience helpers that advance+execute or auto-advance+execute
fwrite($h, "// Execute the current phase's code using a switch-based registry for performance.\n");
fwrite($h, "function ExecutePhase() {\n");
fwrite($h, " \$currentPhase = GetCurrentPhase();\n");
fwrite($h, " if(!isset(\$currentPhase)) return false;\n");
fwrite($h, " \$phaseParamsStr = GetPhaseParameters();\n");
fwrite($h, " \$storedParams = (\$phaseParamsStr !== '') ? json_decode(\$phaseParamsStr, true) : null;\n");
fwrite($h, " SetPhaseParameters('');\n");
fwrite($h, " switch(\$currentPhase) {\n");
foreach($states as $abbr => $st) {
$codeName = addslashes($st->Code);
fwrite($h, " case '" . addslashes($abbr) . "':\n");
if($codeName !== '') {
// Parse function name and default parameters
if(strpos($codeName, '(') !== false) {
$funcParts = explode('(', $codeName, 2);
$funcName = trim($funcParts[0]);
$defaultArgs = rtrim($funcParts[1], ')');
$defaultArgs = $defaultArgs === '' ? [] : explode(',', $defaultArgs);
$defaultArgs = array_map('trim', $defaultArgs);
fwrite($h, " if(function_exists('" . $funcName . "')) { \n");
fwrite($h, " if(\$storedParams !== null) " . $funcName . "(...\$storedParams);\n");
fwrite($h, " else " . $funcName . "(" . implode(',', $defaultArgs) . ");\n");
fwrite($h, " }\n");
} else {
fwrite($h, " if(function_exists('" . $codeName . "')) { \n");
fwrite($h, " if(\$storedParams !== null) " . $codeName . "(...\$storedParams);\n");
fwrite($h, " else " . $codeName . "();\n");
fwrite($h, " }\n");
}
}
fwrite($h, " break;\n");
}
fwrite($h, " default: break;\n");
fwrite($h, " }\n");
fwrite($h, " // Optional persistence hook that user code can implement to persist the current phase.\n");
fwrite($h, " if(function_exists('PersistCurrentPhase')) { PersistCurrentPhase(\$currentPhase); }\n");
fwrite($h, " return true;\n");
fwrite($h, "}\n\n");
fwrite($h, "// Advance by input and execute the new phase if it changed. Returns true if changed.\n");
fwrite($h, "function AdvanceAndExecute(\$input) {\n");
fwrite($h, " \$changed = AdvanceTurnState(\$input);\n");
fwrite($h, " if(\$changed) ExecutePhase();\n");
fwrite($h, " return \$changed;\n");
fwrite($h, "}\n\n");
fwrite($h, "// Auto-advance along AUTO transitions and execute final phase if changed.\n");
fwrite($h, "function AutoAdvanceAndExecute() {\n");
fwrite($h, " \$changed = AutoAdvance();\n");
fwrite($h, " return \$changed;\n");
fwrite($h, "}\n\n");
fclose($h);
$response->ok = true;
$response->statesFile = $statesFile;
$response->controllerFile = $ctrlFile;
echo json_encode($response);
?>