-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFile.php
More file actions
537 lines (492 loc) · 16.6 KB
/
LogFile.php
File metadata and controls
537 lines (492 loc) · 16.6 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
###
# Provides the methods to access an SFB-online log file
###
# create( $log )
# - Populates itself from the provided log. This is a large string, as if from file_get_contents()
# read( $impulse )
# - Returns an array of each item that occurs during the given turn.impulse
# readAll( $unitName )
# - Shows all impulses from a certain unit
# get_sequence()
# - Returns a list of each segment, in order, than can occur in an impulse
# get_unit_facing( $unitName, $time )
# - Retrieve a unit's facing
# get_unit_location( $unitName, $time )
# - Retrieve a unit's location
# get_unit_location_trail( $unitName, $time, $amt )
# - Retrieve a unit's location for several impulses
# get_units ()
# - Shows all of the units used in the game
# get_weapons( $name )
# - Shows all of the weapons that have been fired, by the named unit
###
require_once( __DIR__."/LogUnit.php" );
class LogFile
{
public $error = ""; # error string, should something fail
private $UNITS = array();
private $UNIT_LOOKUP = array();
private $ADDREGEX = "/(.*) \(Type:(.*?)\) has been added at (\d{4,4})/";
private $FRAMEREGEX = "/^Impulse (\d*\.\d*):$/";
private $PLAYERREGEX = "/^(.*?) has selected (.*?)$/";
public const SEQUENCE_MOVEMENT_SHIPS = 0;
public const SEQUENCE_MOVEMENT_SHUTTLES = 1;
public const SEQUENCE_MOVEMENT_SEEKERS = 2;
public const SEQUENCE_MOVEMENT_TAC = 3;
public const SEQUENCE_ESG_DAMAGE = 4;
public const SEQUENCE_ENVELOPER_DAMAGE = 5;
public const SEQUENCE_SEEKER_DAMAGE = 6;
public const SEQUENCE_WEB_DAMAGE = 7;
public const SEQUENCE_BREAKDOWNS = 8;
public const SEQUENCE_SPEED_CHANGES = 9;
public const SEQUENCE_THOLIAN_WEB_PASS = 10;
public const SEQUENCE_EMER_DECEL_EFFECT = 11;
public const SEQUENCE_VOLUNTARY_FIRE_CONTROL = 12;
public const SEQUENCE_CLOAKING_DEVICE = 13;
public const SEQUENCE_TRACTORS = 14;
public const SEQUENCE_LABS = 15;
public const SEQUENCE_LAUNCH_PLASMA = 16;
public const SEQUENCE_LAUNCH_DRONES = 17;
public const SEQUENCE_ESGS = 18;
public const SEQUENCE_DROP_SHIELDS = 19;
public const SEQUENCE_TRANSPORTERS = 20;
public const SEQUENCE_MINES_ACTIVE = 21;
public const SEQUENCE_LAND_SHUTTLES = 22;
public const SEQUENCE_LAUNCH_SHUTTLES = 23;
public const SEQUENCE_ANNOUNCE_EMER_DECEL = 24;
public const SEQUENCE_DIS_DEV_DECLARATION = 25;
public const SEQUENCE_FIRE_DECLARATION = 26;
public const SEQUENCE_PPDS = 27;
public const SEQUENCE_FIRST_HELLBORES = 28;
public const SEQUENCE_DIRECT_FIRE = 29;
public const SEQUENCE_SECOND_HELLBORES = 30;
public const SEQUENCE_CAST_WEB = 31;
public const SEQUENCE_DAMAGE_ALLOCATION = 32;
public const SEQUENCE_DIS_DEV_OPERATE = 33;
public const SEQUENCE_IMPULSE_END = 34;
###
# Class constructor
###
# Args are:
# - (string) The entire log file to be parsed
# Returns:
# - None
###
function __construct( $log )
{
if( is_string($log) == true ) # check if the log data is a string. if so, convert to array
$log = explode( "\n", $log );
else if( is_array( $log ) != true ) # if the log data is not an array or string, then exit
$this->error .= "Input of ".self::class." constructor is not a string or array.\n";
# List of player for units.
# $player_list[ unit ] = player
$player_list = array();
# go through each line of the input file
foreach( $log as $lineNum => $line )
{
# ADDREGEX
$status = preg_match( $this->ADDREGEX, $line, $matches );
if( $status == 1 )
{
# skip those markers that we don't want to be units
if( str_ends_with( $matches[2], "Point of Slip") )
continue;
if( str_ends_with( $matches[2], "Point of Turn") )
continue;
# go on with building the unit
$this->UNITS[] = new LogUnit( $log, $lineNum );
$unit_key = array_key_last( $this->UNITS );
$unit_name = $this->UNITS[ $unit_key ]->name;
$this->UNIT_LOOKUP[ $unit_name ] = $unit_key; # populate the reverse lookup
# error reporting for the construction of the unit
if( $this->UNITS[ $unit_key ]->error != "" )
$this->error .= "Unit '$unit_name' errors:\n".$this->UNITS[ $unit_key ]->error;
continue; # Go to next line if the ADDREGEX matched
}
# PLAYERREGEX
$status = preg_match( $this->PLAYERREGEX, $line, $matches );
if( $status == 1 )
$player_list[ $matches[2] ] = $matches[1];
}
# Assign players to units:
# This is being done post-unit-creation
# Cloaking depends on this, due to how it's reported in the logs
foreach( $player_list as $unit_type => $player_name )
{
foreach( $this->UNITS as &$unit_obj )
if( $unit_obj->type == $unit_type )
$unit_obj->owner = $player_name;
}
foreach( $this->UNITS as &$unit_obj )
$unit_obj->postProcess( $log );
# error reporting for the entire read
if( $this->error != "" )
echo $this->error;
}
###
# Retrieve a unit's facing
###
# Args are:
# - (string) The name of the unit to examine
# - (string) The time to examine, in 'turn.impulse' notation
# Returns:
# - (string) The last location change
###
function get_unit_facing( $unitName, $time )
{
$lookup = $this->UNIT_LOOKUP[ $unitName ];
$unit = $this->UNITS[ $lookup ];
return $unit->getCurrentFacing( $time );
}
###
# Retrieve a unit's location
###
# Args are:
# - (string) The name of the unit to examine
# - (string) The time to examine, in 'turn.impulse' notation
# Returns:
# - (string) The last location change
###
function get_unit_location( $unitName, $time )
{
if( ! isset($this->UNIT_LOOKUP[ $unitName ]) ){
$this->error .= "Unable to find '$unitName' in get_unit_loction()";
return "0000";
}
$lookup = $this->UNIT_LOOKUP[ $unitName ];
$unit = $this->UNITS[ $lookup ];
$location = $unit->getCurrentLocation( $time );
if( ! $location ){
$this->error .= $unit->error;
return "0000";
}
return $location;
}
###
# Retrieve a unit's location for several impulses
###
# Args are:
# - (string) The name of the unit to examine
# - (string) The last impulse to examine, in 'turn.impulse' notation
# - (int) The number of impulses to capture, leading up to the last impulse
# Returns:
# - (array) An array of the last X impulses of location changes
# Format is Array[ 0 => "Impulse -3 Location",
# 1 => "Impulse -2 Location",
# 2 => "Impulse -1 Location",
# 3 => "Latest Impulse Location"
# ]
# Where "X impulses" is 4
###
function get_unit_location_trail( $unitName, $time, $amt )
{
$lookup = $this->UNIT_LOOKUP[ $unitName ];
$unit = $this->UNITS[ $lookup ];
$location = $unit->getLocationTrail( $time, $amt );
return $location;
}
###
# Determines the range between two items for a given impulse
###
# Args are:
# - (string) The name of the unit to examine
# - (string) The name of the unit to examine
# - (string) The impulse to examine, in 'turn.impulse' notation
# Returns:
# - (int) The number of hexes between the two units, plus the hex of the final unit
# e.g. units in neighboring hexes will give a range of 1
function get_unit_range($unitA, $unitB, $time)
{
$locA = $this->get_unit_location( $unitA, $time );
$locB = $this->get_unit_location( $unitB, $time );
# Parse the CCRR format
[$colA, $rowA] = sscanf($locA, "%2d%2d");
[$colB, $rowB] = sscanf($locB, "%2d%2d");
# Offset for shifted odd columns
$rowA = $rowA - intdiv(($colA - 1), 2);
$rowB = $rowB - intdiv(($colB - 1), 2);
# get Axial Distance
$dq = $colB - $colA;
$dr = $rowB - $rowA;
return intdiv(abs($dq) + abs($dr) + abs($dq + $dr), 2);
}
###
# Shows all sequence constants
###
# Args are:
# - None
# Returns:
# - (array) A list of the constants
###
function get_sequence ()
{
$output = array();
$fooClass = new ReflectionClass ( $this::class );
$constants = $fooClass->getConstants();
foreach ( $constants as $name => $value )
if( str_starts_with( $name, "SEQUENCE_" ) )
$output[ $value ] = $name;
return $output;
}
###
# Shows all of the units used in the game
###
# Args are:
# - None
# Returns:
# - (array) Collection of units and their information
###
function get_units ()
{
$output = array();
foreach( $this->UNIT_LOOKUP as $unit=>$value )
$output[] = array(
"added"=> $this->UNITS[ $value ]->added,
"basic"=> $this->UNITS[ $value ]->basicType,
"name"=> $this->UNITS[ $value ]->name,
"removed"=> $this->UNITS[ $value ]->removed,
"type"=> $this->UNITS[ $value ]->type
);
return $output;
}
###
# Shows all of the weapons that have been fired, by the named unit
###
# Args are:
# - (string) The name of the unit, as found in the reverse lookup
# Returns:
# - (array) collection of weapons in the format [] = array( [weapon],[number],[arc] )
###
function get_weapons( $name )
{
if( ! isset( $this->UNIT_LOOKUP[ $name ] ) )
{
$this->error .= " ".self::class."->get_weapons(): Cannot find unit '$name' in list of units.\n";
return false;
}
$unitIndex = $this->UNIT_LOOKUP[ $name ];
return $this->UNITS[ $unitIndex ]->weapons;
}
###
# Shows all items from a certain impulse, in order of segments
###
# Args are:
# - (string) The time to examine, in 'turn.impulse' notation
# Returns:
# - (array) all of the actions for this impulse
# format is array( self::SEQUENCE_xxx => array( 0=>array( 0=>action, 1=>reason, "owner"=>originating_ship ) ) )
###
function read( $time )
{
$time = LogUnit::convertToImp( $time );
$output = array();
foreach( $this->UNITS as $unit )
{
# get the orders for this unit for this impulse
$impulse = $unit->read( $time );
# skip if nothing happened on this impulse from this unit
if( ! $impulse )
continue;
# go through each order for this unit
foreach( $impulse as $key=>$value )
{
switch( $key )
{
case "launchDrone":
$phase = self::SEQUENCE_LAUNCH_DRONES;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "launchPlasma":
$phase = self::SEQUENCE_LAUNCH_PLASMA;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "add":
switch( $unit->basicType )
{
case "esg":
$phase = self::SEQUENCE_ESGS;
break;
case "dis dev":
$phase = self::SEQUENCE_DIS_DEV_DECLARATION;
break;
case "drone":
$phase = self::SEQUENCE_LAUNCH_DRONES;
break;
case "plasma":
$phase = self::SEQUENCE_LAUNCH_PLASMA;
break;
case "ppd":
$phase = self::SEQUENCE_FIRE_DECLARATION;
break;
case "ship":
$phase = self::SEQUENCE_MOVEMENT_SHIPS;
break;
case "shuttle":
$phase = self::SEQUENCE_LAUNCH_SHUTTLES;
break;
case "web":
$phase = self::SEQUENCE_CAST_WEB;
break;
}
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "cloak":
$phase = self::SEQUENCE_CLOAKING_DEVICE;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "damage":
$phase = self::SEQUENCE_DAMAGE_ALLOCATION;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
# capture the second dimension arrays as the output
foreach( $value as $out )
$output[ $phase ][] = $out;
break;
case "facing":
case "location":
# place units that are added
if( isset($value[1]) && ( $value[1] == "Launch" || $value[1] == "add" ) )
{
switch( $unit->basicType )
{
case "esg":
$phase = self::SEQUENCE_ESGS;
break;
case "drone":
$phase = self::SEQUENCE_LAUNCH_DRONES;
break;
case "plasma":
$phase = self::SEQUENCE_LAUNCH_PLASMA;
break;
case "ship":
$phase = self::SEQUENCE_MOVEMENT_SHIPS;
break;
case "shuttle":
$phase = self::SEQUENCE_LAUNCH_SHUTTLES;
break;
case "web":
$phase = self::SEQUENCE_CAST_WEB;
break;
}
}
# move TACcing units
else if( $unit->getCurrentSpeed( $time ) === 0 )
$phase = self::SEQUENCE_MOVEMENT_TAC;
# move everything else
else
switch( $unit->basicType )
{
case "ship":
$phase = self::SEQUENCE_MOVEMENT_SHIPS;
break;
case "shuttle":
$phase = self::SEQUENCE_MOVEMENT_SHUTTLES;
break;
case "drone":
case "plasma":
$phase = self::SEQUENCE_MOVEMENT_SEEKERS;
break;
}
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "fire":
$phase = self::SEQUENCE_FIRE_DECLARATION;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
# capture the second dimension arrays as the output
foreach( $value as $out )
$output[ $phase ][] = $out;
break;
case "launchDrone":
$phase = self::SEQUENCE_LAUNCH_DRONE;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "launchPlasma":
$phase = self::SEQUENCE_LAUNCH_PLASMA;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "speed":
$phase = self::SEQUENCE_SPEED_CHANGES;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "remove":
switch( $unit->basicType )
{
case "dis dev":
$phase = self::SEQUENCE_DIS_DEV_DECLARATION;
break;
case "esg":
$phase = self::SEQUENCE_ESGS;
break;
case "ppd":
$phase = self::SEQUENCE_PPDS;
break;
case "drone":
case "plasma":
case "ship":
case "shuttle":
case "web":
default:
$phase = self::SEQUENCE_IMPULSE_END;
break;
}
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
case "tractordown":
case "tractorup":
$phase = self::SEQUENCE_TRACTORS;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
default:
$phase = 99;
if( ! isset($output[ $phase ]) || ! is_array($output[ $phase ]) )
$output[ $phase ] = array();
$output[ $phase ][] = $value;
break;
}
}
}
ksort($output, SORT_NUMERIC);
return $output;
}
###
# Shows all impulses from a certain unit
###
# Args are:
# - (string) The unit name to examine
# Returns:
# - (array) all of the actions for this unit
###
function readAll( $unitName )
{
if( ! isset( $this->UNIT_LOOKUP[ $unitName ] ) )
{
$this->error .= self::class."->readAll(): Cannot find unit '$unitName' in list of units.\n";
return false;
}
$unitIndex = $this->UNIT_LOOKUP[ $unitName ];
return $this->UNITS[ $unitIndex ]->readAll();
}
}
?>