-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
154 lines (131 loc) · 4.79 KB
/
functions.php
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
<?php
require './dot_file_parser.php';
use DotParser\DotParser;
class StateMachineOutput
{
public $machine;
function set_machine($machine)
{
$this->$machine = $machine;
}
}
function buildFunctionFor($name)
{
$trimName = trim($name);
if (strlen($trimName) === 0) {
return "";
}
return "function $trimName()\n{\nself::checkAction('$trimName');\n}";
}
function buildState($id, $const_id, $type, $description, $descriptionmyturn, $possibleActions, $transitions, $updateGameProgression = false)
{
$result = [];
$ucFirstId = ucfirst($id);
$possibleActionsString = '"' . join('", "', array_map('trim', $possibleActions)) . '"';
$result[STATE_FUNCTION] = ["function st$ucFirstId() { }"]; // called on entering state
$result[ARG_FUNCTION] = ["function arg$ucFirstId() { }"]; // returns state values
$result[POSSIBLE_ACTION_FUNCTIONS] = array_filter(array_map('buildFunctionFor', $possibleActions), function ($builtFunction) {
return strlen($builtFunction) > 0;
});
$result[STATE_STRING] = "$const_id => [
\"name\" => \"$id\",
\"description\" => clienttranslate('$description'),
\"descriptionmyturn\" => clienttranslate('$descriptionmyturn'),
\"type\" => \"$type\",
\"action\" => \"st$ucFirstId\",
\"args\" => \"arg$ucFirstId\",
\"possibleactions\" => [$possibleActionsString],
\"transitions\" => [$transitions]";
if($updateGameProgression) {
$result[STATE_STRING] .= "\"updateGameProgression\" => true";
}
$result[STATE_STRING] .= "]";
return $result;
}
function makeIdentifiers($states)
{
$result = [];
foreach ($states as $state) {
$id = $state->getId();
$snake_case = preg_replace(
'/(?<!^)([A-Z][a-z]|(?<=[a-z])[^a-z]|(?<=[A-Z])[0-9_])/',
'_$1',
$id
);
$snake_case = strtoupper($snake_case);
$result[$id] = $snake_case;
}
return $result;
}
function buildStates($states, $const_ids)
{
$builtStates = [];
foreach ($states as $state) {
$stateId = $state->getId();
$transitionString = "";
$transitions = [];
foreach ($state->getVerticesEdgeTo() as $destination) {
$destinationId = $destination->getId();
foreach ($state->getEdgesTo($destination) as $transition) {
$label = $transition->getAttribute("label", '');
$transition = "\"" . $label . "\" => " . $const_ids[$destinationId];
if (in_array($transition, $transitions) == false) {
array_push($transitions, $transition);
}
}
}
$transitionString .= implode(', ', $transitions);
$type = $state->getAttribute('type') ?? "NOT_SPECIFIED";
$description = $state->getAttribute('description') ?? "NOT_SPECIFIED";
$descriptionmyturn = $state->getAttribute('descriptionmyturn') ?? "NOT_SPECIFIED";
$possibleActions = explode(',', $state->getAttribute('possibleActions') ?? "");
$const_id = $const_ids[$stateId];
$builtState = buildState($stateId, $const_id, $type, $description, $descriptionmyturn, $possibleActions, $transitionString);
$builtStates[$stateId] = $builtState;
}
return $builtStates;
}
function cleanDotFile($inFilePath)
{
$inFile = fopen($inFilePath, "r") or die("Unable to open $inFilePath!");
$inFileContents = '';
while (!feof($inFile)) {
$line = fgets($inFile);
$inFileContents .= "\n$line";
}
// Removes multi-line comments and does not create
// a blank line, also treats white spaces/tabs
$inFileContents = preg_replace('!^[ \t]*/\*.*?\*/[ \t]*[\r\n]!s', '', $inFileContents);
$inFileContents = preg_replace('!/\*.*?\*/!s', '', $inFileContents);
// Removes single line '//' comments, treats blank characters
$inFileContents = preg_replace('![ \t]*//.*[ \t]*[\r\n]!', '', $inFileContents);
// Strip blank lines
$inFileContents = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $inFileContents);
return $inFileContents;
}
function writeResult($result, $outFilePath)
{
$outFile = fopen($outFilePath, "w") or die("Unable to open $outFilePath!");
fwrite($outFile, "<?php\n");
fwrite($outFile, implode("\n", $result));
fclose($outFile);
}
function parseDotFile($inFilePath)
{
$inFileContents = cleanDotFile($inFilePath);
$parser = new DotParser();
return $parser->parseDotFile($inFileContents);
}
function buildConstString($const, $value)
{
switch ($const) {
case "GAME_SETUP":
return "const GAME_SETUP = 1;";
case "GAME_END":
return "const GAME_END = 99;";
case "":
return "";
default:
return "const $const = $value;";
}
}