-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderForm.php
238 lines (213 loc) · 8.43 KB
/
orderForm.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
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
<?php
###
# Accepts orders from the player sheet in the form of POST data
# and puts them into the data file
###
###
# Order names
###
# ORDER TYPE - ORDER NAME
# Break a treaty - break
# Build unit - build_unit
# Build intel points - build_intel
# Colonize system - colonize
# Convert Unit - convert
# Cripple unit - cripple
# Destroy unit - destroy
# Assign flights - flight
# Perform an intel action - intel
# Load units - load
# Mothball a unit - mothball
# Move fleet - move
# Move unit - move_unit
# (Re) name a place - name
# Offer a treaty - offer
# Increase productivity - productivity
# Repair unit - repair
# Invest into research - research
# Sign a treaty - sign
# Set a trade route - trade_route
# Unload units - unload
# Unmothball a unit - unmothball
###
###
# Configuration
###
$EXIT_PAGE = $_SERVER["HTTP_HOST"]."/sheet/index.html"; // the page to show when the script is finished
$dataFileDir = "files/"; // location of the data files
###
# Initialization
###
include( "./postFunctions.php" );
$errorStrings = ""; // ongoing litany of errors to send to the UI
// Format is $orderTable['internal "type" keyword'] = [ require "reciever", require "target", require "note", 'external "type" phrase' ]
$orderTable = array();
$orderTable['break'] = [ false, false, false, 'Break a treaty' ];
$orderTable['build_unit'] = [ true, true, true, 'Build unit' ];
$orderTable['build_intel'] = [ true, false, true, 'Build intel points' ];
$orderTable['colonize'] = [ true, false, false, 'Colonize system' ];
$orderTable['convert'] = [ true, true, false, 'Convert Unit' ];
$orderTable['cripple'] = [ true, false, false, 'Cripple unit' ];
$orderTable['destroy'] = [ true, false, false, 'Destroy unit' ];
$orderTable['flight'] = [ true, true, false, 'Assign flights' ];
$orderTable['intel'] = [ true, true, true, 'Perform an intel action' ];
$orderTable['load'] = [ true, true, true, 'Load units' ];
$orderTable['mothball'] = [ true, false, false, 'Mothball a unit' ];
$orderTable['move'] = [ true, true, false, 'Move fleet' ];
$orderTable['move_unit'] = [ true, false, true, 'Move unit' ];
$orderTable['name'] = [ true, false, false, '(Re) name a place' ];
$orderTable['name_fleet'] = [ true, false, true, 'Rename a fleet' ];
$orderTable['offer'] = [ true, true, false, 'Offer a treaty' ];
$orderTable['productivity'] = [ true, false, false, 'Increase productivity' ];
$orderTable['repair'] = [ true, false, false, 'Repair unit' ];
$orderTable['research'] = [ false, false, true, 'Invest into research' ];
$orderTable['sign'] = [ true, true, false, 'Sign a treaty' ];
$orderTable['trade_route'] = [ true, true, false, 'Set a trade route' ];
$orderTable['unload'] = [ true, false, true, 'Unload units' ];
$orderTable['unmothball'] = [ true, false, false, 'Unmothball a unit' ];
/*
Array
(
[dataFile] = sample01
// "perm" orders below
[OrderEntry0A] => move
[OrderEntry0B] => Exploration Alpha
[OrderEntry0C] => Fraxee Dir A
[OrderEntry0D] =>
[OrderEntry1A] => move
[OrderEntry1B] => Exploration Beta
[OrderEntry1C] => Fraxee Dir F
[OrderEntry1D] =>
[OrderEntry2A] => invest
[OrderEntry2B] =>
[OrderEntry2C] =>
[OrderEntry2D] => 13
[OrderEntry3A] => build
[OrderEntry3B] => Colony Fleet
[OrderEntry3C] => Fraxee
[OrderEntry3D] => Colony-1
// drop-down entries below
[OrderEntry4A] => productivity
[OrderEntry4B] => Fraxee
[OrderEntry4D] =>
[OrderEntry5A] => colonize
[OrderEntry5B] => Fraxee dir A
[OrderEntry5D] =>
[OrderEntry6A] => move
[OrderEntry6B] => Exploration Beta
[OrderEntry6C] => Fraxee
[OrderEntry6D] =>
)
Always gives OrderEntryxD (the text-entry)
"finished" entries gives all four entries
unassigned orders are given "<-- No Order -->" for the A entry
"Order \""+orders[i].reciever+"\" to do \""+orders[i].type+"\" to \""+orders[i].target+"\" with \""+orders[i].note+"\"";
var orders = [
{"type":"move","reciever":"Exploration Alpha","target":"Fraxee Dir A","note":"","perm":0},
{"type":"move","reciever":"Exploration Beta","target":"Fraxee Dir F","note":"","perm":0},
{"type":"invest","reciever":"","target":"","note":"13","perm":1},
{"type":"build","reciever":"Colony Fleet","target":"Fraxee","note":"Colony-1","perm":0}
];
*/
// find the data-file name
$dataFileRoot = $_REQUEST["dataFile"];
$dataFileName = $dataFileDir.$dataFileRoot.".js";
// error-catching for inability to read or write to $dataFileName
if( ! is_readable($dataFileName) )
{
echo "Cannot write to '$dataFileName'\n\n";
exit(0);
}
if( ! is_writeable($dataFileName) )
{
echo "Cannot write to '$dataFileName'\n\n";
exit(0);
}
// get the data file
$fileContents = extractJSON( $dataFileName );
$flagDelete = -1; // set to an order number to be deleted
// Iterate through the order array
// If an order if "<-- No Order -->" then try to delete the entry from the data file
// otherwise add to the data file
foreach( array_keys($_REQUEST) as $key )
{
// skip if this is not an order entry
if( strpos( $key, "OrderEntry" ) !== 0 ) // catches if the needle is missing or not at start
continue;
// get the order to affect with this $key
$orderNum = intval( substr( $key, 10, 2 ) );
$orderPos = strtolower( substr( $key, 10+strlen($orderNum), 1 ) );
// skip if we failed to get the location inside the order from the $key
if( $orderNum === false || $orderPos === false )
continue;
if( $_REQUEST[ $key ] == "<-- No Order -->" || $flagDelete == $orderNum )
{
$flagDelete = $orderNum;
if( isset($fileContents["orders"][$orderNum]) )
unset( $fileContents["orders"][$orderNum] ); // remove the deleted item
continue; // order is empty
}
// set everything else. They might have changed
// set up the entry to the orders array if not already
if( ! isset( $fileContents["orders"][$orderNum]) )
$fileContents["orders"][$orderNum] = array();
// create a segment of an order with this $key entry
switch($orderPos)
{
case "a":
$fileContents["orders"][$orderNum]["type"] = $_REQUEST[$key];
break;
case "b":
$fileContents["orders"][$orderNum]["reciever"] = $_REQUEST[$key];
break;
case "c":
$fileContents["orders"][$orderNum]["target"] = $_REQUEST[$key];
break;
case "d":
$fileContents["orders"][$orderNum]["note"] = $_REQUEST[$key];
break;
}
}
foreach( array_keys($fileContents["orders"]) as $orderNum )
{
// used to check which parts of the order array are required for each order type
$orderTableEntry = $orderTable[ $fileContents["orders"][$orderNum]["type"] ];
// create any otherwise-empty order fields
if( ! isset($fileContents["orders"][$orderNum]["type"]) )
$fileContents["orders"][$orderNum]["type"] = "";
if( ! isset($fileContents["orders"][$orderNum]["reciever"]) )
$fileContents["orders"][$orderNum]["reciever"] = "";
if( ! isset($fileContents["orders"][$orderNum]["target"]) )
$fileContents["orders"][$orderNum]["target"] = "";
if( ! isset($fileContents["orders"][$orderNum]["note"]) )
$fileContents["orders"][$orderNum]["note"] = "";
// Check that all required fields are not blank
if( ! isset( $orderTable[ $fileContents["orders"][$orderNum]["type"] ] ) )
{
$errorStrings .= "Order processing file does not know of order type '".$fileContents["orders"][$orderNum]["type"]."'.\n";
unset( $fileContents["orders"][$orderNum] ); // delete the offending entry
}
if( empty($fileContents["orders"][$orderNum]["reciever"]) && $orderTableEntry[0] == true )
{
$errorStrings .= "Order type '".$orderTableEntry[3]."' requires a reciever. None given.\n";
unset( $fileContents["orders"][$orderNum] ); // delete the offending entry
}
if( empty($fileContents["orders"][$orderNum]["target"]) && $orderTableEntry[1] == true )
{
$errorStrings .= "Order type '".$orderTableEntry[3]."' requires a target. None given.\n";
unset( $fileContents["orders"][$orderNum] ); // delete the offending entry
}
if( empty($fileContents["orders"][$orderNum]["note"]) && $orderTableEntry[2] == true )
{
$errorStrings .= "Order type '".$orderTableEntry[3]."' requires a note. None given.\n";
unset( $fileContents["orders"][$orderNum] ); // delete the offending entry
}
}
// re-index the orders array
$fileContents["orders"] = array_values( $fileContents["orders"] );
// write the (edited) file
writeJSON( $fileContents, $dataFileName );
// go back to the player-page
header( "location: http://".$EXIT_PAGE."?data=".$_REQUEST["dataFile"]."&e=".urlencode($errorStrings)."&t=".time(), true, 302 );
exit;
?>