-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstation.php
412 lines (363 loc) · 17.4 KB
/
station.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
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
<?php
include_once('db_railroad.php');
class station {
private $stationInfo;
private $next_stations;
private $trains;
private $fake_date;
private $trainLength;
private $trainMass;
/* Class constructor.
* ARGUMENTS:
* - station params;
* RETURNS: None.
*/
public function __construct( $lon, $lat, $code ) {
$r = &$GLOBALS['r'];
if ($lon != null && $lat != null)
$this->stationInfo = $r->findStationByCoordinate($lon, $lat);
else if ($code != null)
$this->stationInfo = $r->findStationByCode($code);
else
echo 'Bad station requested!';
echo
'<script>loadMap(\'stmap\', ' . $this->stationInfo['longitude'] . ', ' . $this->stationInfo['altitude'] . ', false, 13);' . "\n" .
'h2 = document.getElementById(\'stname\');' . "\n" .
'h2.innerHTML = \'Станция: ' . $this->stationInfo['name'] . '\'; ' . "\n" .
'h4 = document.getElementById(\'stcode\')' . "\n" .
'h4.innerHTML = \'Код в АСУ "Экспресс-3 / ЕСР": ' . $this->stationInfo['code'] . '\'; ' . "\n";
//$d = $r->selectWhatFromTableWhere('paths', 'LonFrom, LatFrom, LonTo, LatTo', 'StationFrom = ' . $this->stationInfo['code']);
$d = $r->selectAllFromTable('paths');
//$nghb = $r->selectWhatFromTableWhere('paths', 'StationTo', 'StationFrom = ' . $this->stationInfo['code']);
/*$q = 'code = ' . $this->stationInfo['code'];
foreach ($nghb as $pth)
$q = $q . ' or code = ' . $pth['StationTo'];
$s = $r->selectWhatFromTableWhere('stations', '*', 'code = ' . $q);*/
$s = $r->selectAllFromTable('stations', '*');
$r->showStations($s, $d);
$GLOBALS['r']->showStations([$this->stationInfo], $d);
// delete yesterday and earlier arrived trains
$GLOBALS['r']->sqlQuery('DELETE FROM trains WHERE arrival_date < \'' . date('Y-m-d') . '\' and way > 0');
echo '</script>';
$this->trains = array();
$this->fake_date = '\'2000-00-00\'';
$this->trainLength = 20;
$this->trainMass = 1800;
$this->next_stations = array_column($GLOBALS['r']->selectWhatFromTableWhere('next_stations', 'destination', 'current_station = ' . $this->stationInfo['code']), 'destination');
} /* End of constructor */
/* Parse train from file function.
* ARGUMENTS:
* - file variable:
* $F;
* RETURNS:
* parsed data.
*/
private function parseTrain( $F ) {
$data = array();
while (!feof($F)) {
$wagon //= list($wagon_id, $cargo_cat, $cargo, $net, $gross, $dep, $dest, $urg)
= fscanf($F, "%d %s %s %d %d %d %d %s");
array_push($data, $wagon);
}
array_pop($data);
return $data;
} /* End of 'parseTrain' function */
/* Get free function.
* ARGUMENTS: None.
* RETURNS: last busy way.
*/
private function getFreeWay() {
$way = $GLOBALS['r']->selectWhatFromTableWhere('trains', 'ABS(way)', '(destination = ' . $this->stationInfo['code'] .
' and way > 0) or (departure = ' . $this->stationInfo['code'] .' and way < 0) ORDER BY `ABS(way)` ASC');
if ($way == null || $way[0]['ABS(way)'] > 1)
return 1;
for ($i = 1; $i < count($way); $i++)
if ($way[$i]['ABS(way)'] - $way[$i - 1]['ABS(way)'] > 1)
return $way[$i - 1]['ABS(way)'] + 1;
return $way[count($way) - 1]['ABS(way)'] + 1;
} /* End of 'getLastBusyWay' function */
/* Get last busy train function.
* ARGUMENTS: None.
* RETURNS: last busy way.
*/
private function getFreeTrain() {
$tr = $GLOBALS['r']->selectWhatFromTableWhere('trains', 'number', '1 ORDER BY number ASC');
if ($tr == null || $tr[0]['number'] > 1)
return 1;
for ($i = 1; $i < count($tr); $i++)
if ($tr[$i]['number'] - $tr[$i - 1]['number'] > 1)
return $tr[$i - 1]['number'] + 1;
return $tr[count($tr) - 1]['number'] + 1;
} /* End of 'getLastBusyWay' function */
public function getCode() {
return $this->stationInfo['code'];
}
/* Get next station by destination function.
* ARGUMENTS:
* - destination code:
* $destination;
* RETURNS:
* next station code.
*/
private function getNextByDestination( $destination ) {
$next = $GLOBALS['r']->selectWhatFromTableWhere('next_stations', 'next_station', 'current_station = ' . $this->stationInfo['code'] . ' and destination = ' . (int)$destination);
return $next[0]['next_station'];
} /* End of 'getNextByDestination' function */
/* Draw table function.
* ARGUMENTS:
* - trains array:
* $today_trains;
* RETURNS: None.
*/
private function drawTrainsTable( array $today_trains, $showVia, $showWay ) {
$r = &$GLOBALS['r'];
for ($i = 0; $i < count($today_trains);) {
$cur_tr = $today_trains[$i]['train_id'];
$way = $r->selectWhatFromTableWhere('trains', 'way', 'number = ' . $today_trains[$i]['train_id']);
echo
'<table border="1">' .
'<tr>' .
'<td><b>Номер поезда:</b></td>' .
'<td>' . $cur_tr . '</td>';
if ($showWay)
echo
'<td><b>Номер пути:</b></td>' .
'<td>' . ABS($way[0]['way']) . '</td>';
echo
'</tr>' .
'<tr>
<td><b>Тип ваг.</b></td>
<td><b>Номер</b></td>
<td><b>П. отправления</b></td>';
if ($showVia)
echo
'<td><b>След. ст.</b></td>';
echo
'<td><b>П. назначения</b></td>
<td><b>Полный</b></td>
<td><b>Срочный</b></td>
</tr>';
while ($i < count($today_trains) && $cur_tr == $today_trains[$i]['train_id']) {
$img = $r->selectWhatFromTableWhere('cargo_images', 'image', 'category = ' .
'(SELECT category FROM cargo WHERE name = \'' . $today_trains[$i]['cargo_name'] . '\')');
$dep = $r->selectWhatFromTableWhere('stations', 'name', 'code = ' . $today_trains[$i]['departure']);
$dest = $r->selectWhatFromTableWhere('stations', 'name', 'code = ' . $today_trains[$i]['destination']);
$via = $r->selectWhatFromTableWhere('stations', 'name', 'code = ' . $today_trains[$i]['direction']);
$is_full = $today_trains[$i]['net'] != 0;
$urgent = $today_trains[$i]['urgency'];
echo
'<tr class="trainTable">' .
'<td width="60px" class="centredtxt"> ' .
'<img src="images/' . $img[0]['image'] . '" width="50" height="30" />' .
'</td>' .
'<td class="centredtxt"> ' . $today_trains[$i]['wagon_id'] . '</td>' .
'<td class="centredtxt">' . $dep[0]['name'] . '</td>';
if ($showVia)
echo
'<td class="centredtxt">' . $via[0]['name'] . '</td>';
echo
'<td class="centredtxt">' . $dest[0]['name'] . '</td>' .
'<td class="centredtxt">' .
'<img src="images/' . ($is_full ? 'is_full.png' : 'is_empty.png') . '" width="20" height="20" /> ' .
'</td>' .
'<td class="centredtxt">' .
'<img src="images/' . ($urgent ? 'is_full.png' : 'is_empty.png') . '" width="20" height="20" /> ' .
'</td>' .
'</tr>';
$i++;
}
echo '</table><br>';
}
} /* End of 'drawTrainsTable' function */
/* Report about arrived trains function.
* ARGUMENTS: None.
* RETURNS: prints a report into table on the called from page.
*/
public function reportArrivedTrains() {
if (isset($_FILES['train_descr_file']['tmp_name'])) {
$F = fopen($_FILES['train_descr_file']['tmp_name'], 'rt');
$trainno = fscanf($F, "%d");
$train = $this->parseTrain($F);
$t = $GLOBALS['r']->selectWhatFromTableWhere('trains', '*', 'number = ' . $trainno[0] .
' and destination = ' . $this->stationInfo['code']);
$way = $this->getFreeWay();
// new trains, from fabric
if ($t == null) {
$res = $GLOBALS['r']->insertIntoTable('trains', array($trainno[0], 0, $this->stationInfo['code'],
$way, '\'' . date('Y-m-d' . '\'')));
if ($res == 0) {
echo '<b>Поезд ' . $trainno[0] . ' едет куда-то не сюда!</b><br>';
return;
}
}
//list($wagon_id, $cargo_cat, $cargo, $net, $gross, $dep, $dest, $urg)
$q = 'UPDATE trains SET arrival_date = \'' . date('Y-m-d') . '\', way = ' . $way .
' WHERE number = ' . $trainno[0] . ' and destination = ' . $this->stationInfo['code'];
//echo 'QUERY: ' . $q;
$GLOBALS['r']->sqlQuery($q);
foreach ($train as $tr) {
//station_id train_id wagon_id urgency date departure destination cargo_name net gross direction way
$arr = array(
'station_id' => $this->stationInfo['code'],
'train_id' => $trainno[0],
'wagon_id' => $tr[0],
'urgency' => strcasecmp($tr[7], 'СРОЧНО') == 0 ? 1 : 0,
'date' => '\'' . date('Y-m-d') . '\'',
'departure' => $tr[5],
'destination' => $tr[6],
'cargo_name' => '\'' . $tr[2] . '\'',
'net' => $tr[3],
'gross' => $tr[4],
'direction' => $this->stationInfo['code']);
$GLOBALS['r']->addWagonEvent($arr);
//echo 'TO DISTRIBUTE:<br>';
//print_r($arr);
//echo '<br>';
$this->distributeWagon($arr);
}
$this->formTrains();
}
$today_trains = $GLOBALS['r']->selectWhatFromTableWhere('wagon_turnover', '*', 'date = \'' . date('Y-m-d') . '\' and station_id = ' . $this->stationInfo['code'] .
' and direction = ' . $this->stationInfo['code'] . ' ORDER BY train_id ASC');
if ($today_trains == null)
echo '<h4>На сегодня принимаемых поездов нет</h4>';
else
echo '<h4>Принятые сегодня поезда:</h4>';
$this->drawTrainsTable($today_trains, 0, 1);
} /* End of 'reportArrivedTrains' function */
/* Distribute wagon into trains function.
* ARGUMENTS:
* - file string:
* $arr;
* RETURNS: None.
*/
private function distributeWagon( &$arr ) {
// wagon arrived - turn back
if ($arr['destination'] == $this->stationInfo['code'] && $arr['net'] == 0) {
$tmp = $arr['destination'];
$arr['destination'] = $arr['departure'];
$arr['departure'] = $tmp;
}
$arr['direction'] = $this->getNextByDestination($arr['destination']); // direction
$arr['train_id'] = 0;
$arr['date'] = $this->fake_date; // fake date
$this->trains[$arr['direction']][] = $arr;
} /* End of 'distributeWagon' function */
/* Write train information into file function.
* ARGUMENTS: train number.
* RETURNS: None.
*/
private function writeTrainInfoToFile( $trainNo ) {
$r = &$GLOBALS['r'];
$F = fopen('trains/train_' . $trainNo .'.txt', 'wt');
fprintf($F, "%d\n", $trainNo);
if ($F == null)
echo "Couldn't create file!";
$train = $r->selectWhatFromTableWhere('wagon_turnover', '*', 'train_id = ' . $trainNo);
foreach ($train as $wagon) {
$cargo_cat = $r->selectWhatFromTableWhere('cargo', 'category', 'name = \'' . $wagon['cargo_name'] . '\'');
//list($wagon_id, $cargo_cat, $cargo, $net, $gross, $dep, $dest, $urg)
fprintf($F, "%d %s %s %d %d %d %d %s\n",
$wagon['wagon_id'], /*iconv('UTF-8', 'windows-1251', */$cargo_cat[0]['category']/*)*/,
/*iconv('UTF-8', 'windows-1251', */$wagon['cargo_name']/*)*/,
$wagon['net'], $wagon['gross'], $wagon['departure'], $wagon['destination'], $wagon['urgency'] ? 'СРОЧНО' : 'ОБЫКН');
}
fclose($F);
} /* End of 'writeTrainInfoToFile' function */
/* Form train function
* ARGUMENTS:
* the way where just arrived train - $busyWay.
* RETURNS: None.
*/
private function formTrains() {
$r = &$GLOBALS['r'];
foreach ($this->trains as $next => $hi) {
$way = $this->getFreeWay();
$train = $r->selectWhatFromTableWhere('trains', 'number', 'destination = ' . (int)$next . ' and way != 0 and departure = ' . $this->stationInfo['code']);
$length = 0;
$mass = 0;
// no train
if ($train == null) {
$train_number = $this->getFreeTrain();
$r->insertIntoTable('trains', array($train_number, $this->stationInfo['code'], (int)$next, $way * -1, '\'' . date('Y-m-d') . '\''));
// len = mass = 0
}
else {
$train_number = $train[0]['number'];
$forming = $r->selectWhatFromTableWhere('wagon_turnover', 'gross', 'train_id = ' . $train_number . ' and date = ' .
$this->fake_date);
// evaluate len and mass
foreach ($forming as $w) {
$length++;
$mass += $w['gross'];
}
}
$depart = function($wagon, &$length, &$mass) use ($r, &$train_number, $next, &$way) {
// depart train
$r->sqlQuery('UPDATE trains SET way = 0 WHERE number = ' . $train_number);
$r->sqlQuery('UPDATE wagon_turnover SET date = \''. date('Y-m-d') .
'\', direction = ' . $wagon['direction'] .
' WHERE train_id = ' . $train_number .
' and date = ' . $this->fake_date . ' and station_id = ' . $this->stationInfo['code']);
$this->writeTrainInfoToFile($train_number);
$length = $mass = 0;
$train_number = $this->getFreeTrain();
$way = $this->getFreeWay();
$r->insertIntoTable('trains', array($train_number, $this->stationInfo['code'], $next, $way * -1, '\'' . date('Y-m-d') . '\''));
};
// urgent
foreach ($this->trains[$next] as $wagon) {
if ($wagon['urgency']) {
$wagon['train_id'] = $train_number;
$r->addWagonEvent($wagon);
$length++;
$mass += $wagon['gross'];
}
if ($length > $this->trainLength || $mass > $this->trainMass)
$depart($wagon, $length, $mass);
}
// not urgent
foreach ($this->trains[$next] as $wagon) {
if (!$wagon['urgency']) {
$wagon['train_id'] = $train_number;
$r->addWagonEvent($wagon);
$length++;
$mass += $wagon['gross'];
}
// depart train
if ($length > $this->trainLength || $mass > $this->trainMass) {
$depart($wagon, $length, $mass);
}
}
}
} /* End of 'formTrains' function */
/* Report about preparing trains function.
* ARGUMENTS: None.
* RETURNS: None.
*/
public function reportPreparingTrains() {
$today_trains = $GLOBALS['r']->selectWhatFromTableWhere('wagon_turnover', '*', 'date = ' . $this->fake_date . ' and station_id = ' . $this->stationInfo['code'] .
' and direction != ' . $this->stationInfo['code'] . ' ORDER BY train_id ASC');
if ($today_trains == null)
echo '<h4>На сегодня подготавливаемых поездов нет</h4>';
else
echo '<h4>Подготавливаемые сегодня поезда:</h4>';
foreach ($today_trains as $train)
$train['date'] = '`' . date('Y-m-d') . '`';
$this->drawTrainsTable($today_trains, 1, 1);
} /* End of 'reportPreparingTrains' function */
/* Report about ready trains function.
* ARGUMENTS: None.
* RETURNS: None.
*/
public function reportReadyTrains() {
$today_trains = $GLOBALS['r']->selectWhatFromTableWhere('wagon_turnover', '*', 'date = \'' . date('Y-m-d') . '\' and station_id = ' . $this->stationInfo['code'] .
' and direction != ' . $this->stationInfo['code'] . ' ORDER BY train_id ASC');
if ($today_trains == null)
echo '<h4>Пока готовых поездов нет</h4>';
else
echo '<h4>Отправленные сегодня поезда:</h4>';
$this->drawTrainsTable($today_trains, 1, 0);
} /* End of 'reportPreparingTrains' function */
}
?>