-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
377 lines (279 loc) · 11.1 KB
/
Copy pathmain.php
File metadata and controls
377 lines (279 loc) · 11.1 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
<?php
/***********************************************
Zurich Water Game: internet version
Author: Nigel Gilbert and the FIRMA project
--------------------------------------------
main.php
This file despatches to the appropriate
code to display the next web page to the player,
retrieving and updating as necessary.
Version 1.0 6 August 2001
Version 1.1 1 September 2001
Version 2.0 16 November 2001
Version 2.1 7 April 2002
**********************************************/
include("constants.php");
include("common.php");
include("query.php");
include("objects.php");
include("rules.php");
/*============================================
Retrieve the environment state (and as a
side effect, the players and resources)
============================================*/
open_database();
$newplayer = get_param('newplayer');
$action = get_param('action');
$msg = get_param('msg');
$requestor = get_param('requestor');
$reasons = get_param('reasons');
$recipient = get_param('recipient');
$id = get_param('id');
$type = get_param('type');
$price = get_param('price');
$counter_offer = get_param('counter_offer');
$quotep = get_param('quotep');
$quote = get_param('quote');
$offer = get_param('offer');
$haggle_reply = get_param('haggle_reply');
$reject_offer = get_param('reject_offer');
$amount = get_param('amount');
$offer = get_param('offer');
$who = get_param('who');
// water utility
$close_res_number = get_param('close_res_number');
$repair_res_number = get_param('repair_res_number');
// manufacturers
$newfactorytype = get_param('newfactorytype');
$repair_factory_number = get_param('repair_factory_number');
$ch_prod_factory_number = get_param('ch_prod_factory_number');
// waste water
$filter_quality = get_param('filter_quality');
// bank
$frombank = get_param('frombank');
$auto_turns = get_param('auto_turns');
$mintimemins = get_param('mintimemins');
$actionsperyear = get_param('actionsperyear');
/*============================================
Complain if the player has got here without
setting the cookie.
============================================*/
if ($newplayer) { // here only if changing role from bank to other player or vice versa
$player = $newplayer;
setcookie("player", $player, mktime(0,0,0,1,1,2030), '/', '', 0);
$zurich->players[$player]->loggedin = date("g:i a D, d M");
$action = "";
}
$player = $_COOKIE['player'];
// sanity check (in case, e.g. bank has reset the game behind the scenes)
if (!$player or !$zurich->players[$player]->loggedin) header("Location: briefing.html");
$playobj = &$zurich->players[$player]; // the object representing the player
/*============================================
Store a message that the player has written to the Public or
Private fora
============================================*/
$tab_to_display = "bank"; // tab to display when page is shown (default)
if ($msg) {
db_write("INSERT INTO msgs (sender, recipient, timesent, timeread, msg)
VALUES('$player', '$recipient', now(), NULL, '$msg')");
if ($recipient == "All") {
log_act("sent a message to the Public Forum", $playobj->name, 2);
$tab_to_display = "public";
}
else {
$r_name = $playobj->id_to_name($recipient);
log_act("sent a message to $r_name", $playobj->name, 2);
$tab_to_display = "private";
}
}
/*============================================
It is easy to select a sub-action (e.g. amount
to borrow from the bank) without clicking the
corresponding radio button. Set the action
if this was done by mistake.
============================================*/
/* the user's selection (made on the last page the user saw before
s/he arrived here) is recorded in $action by the web server. */
//foreach ($HTTP_POST_VARS as $k => $v) { echo "$k = $v<BR>";}
if (!$action) {
if (get_param('who')) $action = 'logout';
if (get_param('amount')) $action = 'borrow_from_bank';
if (get_param('repayment')) $action = 'repay_bank';
if (get_param('price')) $action = 'petition_politician';
if (get_param('type')) $action = 'request_sanitary_system';
if (get_param('factory_number')) $action = 'change_production';
if (get_param('recipient') and get_param('subsidy')) $action = 'subsidise';
}
/*============================================
Display the game page header
============================================*/
html_header("Zurich Water Game: $playobj->name");
include("display-top.php");
if ($msg) echo "Message sent.\n";
/*============================================
If the user is carrying out an action,
run the softbots (agents representing players
that are not logged in at the moment). While
the softbot is playing, its object is in $softbot
============================================*/
if ($action) {
foreach ($zurich->players as $s) {
$softbot = &$zurich->players[$s->id];
if ($softbot && !$softbot->loggedin) {
log_and_confirm("Robot $softbot->name playing", 'Event', 0);
$softbot->play();
}
}
unset($softbot);
}
/*============================================
Check whether the player has waited a sufficient interval
since the last action. If not, warn and reject
action. (Actions that are a response to another player's
request ($requestor, a variable that is sent when the
user POSTs a form from a dialogue) is not blank) or
involve borrowing from/repaying the bank are permitted anytime)
============================================*/
if ($action and !$requestor and
$action != 'borrow_from_bank' and $action != 'repay_bank') {
if ($playobj->id != 'bank') {
if ($playobj->last_time and
(time() - $playobj->last_time < $zurich->action_gap)) {
alert("You are not permitted to carry out another action yet!");
echo "Not enough time has elapsed since your last action.
You can next carry out an action on " .
date("l, jS F \\a\\t g:ia", $playobj->last_time + $zurich->action_gap) .
".<BR>Request cancelled.<BR>";
$action="";
}
}
}
/*============================================
Check whether the player has provided a reason
for the selected action. If not, object.
If so, store it
============================================*/
if ($action and !$requestor and !($player == 'bank')) {
if (!$reasons) {
alert("You have not provided a reason for selecting this action.");
echo "Please enter a reason in the box at the bottom left
and reselect the action.<BR>";
$action = "";
}
}
/*============================================
Carry out the requested action
============================================*/
if ($action) {
/* branch to the required action. Note that this requires that the value of
$action is the name of a method of Player. */
$playobj->$action();
$zurich->action_count++;
$playobj->last_time = time();
}
/*============================================
Retrieve any queued requests from other
players and carry out the requested actions
============================================*/
/* obtain and process each request in turn */
$query = new Query("SELECT id, request, requestor, param1, param2 FROM requests
WHERE requestee='$player'
ORDER BY id DESC");
while ($query->next_rec()) {
$method = $query->field('request');
$playobj->$method($query->field('id'), $query->field('requestor'),
$query->field('param1'), $query->field('param2'));
echo "<br>";
}
/*============================================
Update the world
============================================*/
/* If the next end of year is now due, carry out the required operations (e.g. collect
maintenance) */
if ($zurich->action_count % $zurich->actsperyear == 0) {
$zurich->end_of_turn();
$zurich->action_count = 1;
}
/*============================================
Send an email to all logged in players listing actions
that have been carried out in the last 24 hours
============================================*/
$now = time();
if ($send_emails) {
if (!$zurich->email_notify_time) $zurich->email_notify_time = $now;
if ($now - $zurich->email_notify_time > $email_notify_interval) {
$query = new query("SELECT name, action, time FROM log
WHERE detail >= '$log_level'");
while ($query->next_rec()) {
$timestamp = strtotime($query->field('time'));
if ($timestamp > $zurich->email_notify_time) {
$msg .= date("l, g:i a", $timestamp) . " " .
$query->field('name') . " " .
stripslashes($query->field('action')) . "\n";
// replace euro symbols
$msg = preg_replace("/€\s*(\d+)/", "$1 euro", $msg);
}
}
if ($msg) {
foreach ($zurich->players as $p) {
if (isset($p->logged_in) && !$p->logged_in && $p->email) {
$last_date = date("l, g:i a", $zurich->email_notify_time);
$mail_sent = mail($p->email, "Zurich water Game: today's news",
"In the Zurich Water Game, the actions carried out
since $last_date have been:\n\n$msg",
"From: \"Zurich Water Game\"<$admin>\n" .
"Reply-To: \"Zurich Water Game\"<$admin>");
if (!$mail_sent) echo "Problem sending mail to $p->email<P>";
}
}
$zurich->email_notify_time = $now;
}
}
}
/*============================================
Display bank balance
============================================*/
include("display-bank.php");
/*============================================
Display any current messages from other players
============================================*/
include("display-msgs.php");
/*============================================
Display the player-specific parts of the page
============================================*/
switch ($player) {
case 'water_utility': include("wspage.php"); break;
case 'waste_water_utility': include("wwupage.php"); break;
case 'housing_assoc_1': include("hapage.php"); break;
case 'housing_assoc_2': include("hapage.php"); break;
case 'manufacturer_1': include("prodpage.php"); break;
case 'manufacturer_2': include("prodpage.php"); break;
case 'politician': include("polpage.php"); break;
case 'bank': include("bankpage.php"); break;
}
/*============================================
Display the actions available to all players
============================================*/
include("display-generic.php");
/*============================================
Display the help messages for these actions
============================================*/
include("display-help.php");
/*============================================
Display a log of recent actions
============================================*/
include("display-log.php");
/*============================================
Display the indicator scales
============================================*/
/* update the water supply and demand indicators */
$zurich->measure_water_indicators();
include("display-scales.php");
html_footer();
/*============================================
Save the player and environment states
============================================*/
$playobj->last_refresh = $now;
// save a serialized copy of all objects
db_save($zurich, "Zurich", "objects");
?>