-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
426 lines (321 loc) · 13.4 KB
/
index.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/*==================================================
GONE FISHING
v1.0 April 2001
Nigel Gilbert
====================================================*/
/* main file, includes all the calculations and most database access */
/* this file can be reached:
1. from index.html, when the user has just logged in or has read or sent a message ($action = login)
($loginname contains user's name, $boat and $email may contain user's boat name and email address)
2. from itself, when the user has selected an investment ($action = gofishing)
3. from itself, when the user has logged out ($action = logout)
4. from itself, when the user has just sent or read a message ($action = message)
*/
$debug = 0; /*set to true for debugging output */
include("common.inc");
/* maintain the player's name and boat name between pages */
session_start();
if (isset($_SESSION['name'])) $name = $_SESSION['name']; else $name = "";
if (isset($_SESSION['boat'])) $boat = $_SESSION['boat']; else $boat = "";
if (isset($_REQUEST['action'])) $action = $_REQUEST['action']; else $action = "";
if (isset($_REQUEST['investment'])) $investment = $_REQUEST['investment'];
else $investment = 0;
debug("name", $name);
debug("boat", $boat);
debug("action", $action);
$clockTime = time(); // the time on the simulated clock on the wall
/* declare classes for program objects */
/* Formulae:
quantity fished = investment * stock * k1 * random number
left in sea = stock - quantity fished
market price = k2 - k3 * (sum of fish caught in the last 4 expeditions)
fisher's income = market price * quantity fished
new balance = old balance + fisher's income - maintenance - investment
new stock = left in sea + k5 * (k6 - left in sea) / left in sea
where (these are all set in defines.inc)
k1 relates investment to quantity fished
k2 is maximum market price (price when no fish are in market)
k3 determines slope of demand curve
k4 is minimum price of fish, even when there is an oversupply
k5 is growth rate of fish when sea is half stocked
k6 is maximum capacity of sea
*/
class Sea {
public $stock, $marketPrice, $lastupdate, $maintenance;
function retrieve () {
$query = new query("SELECT stock, price, date_part('epoch', time) as lastupdate, maintenance
FROM sea ORDER BY time DESC LIMIT 1");
if (!$query->next_rec()) $query->error("No records retrieved");
$this->stock = $query->field("stock");
$this->marketPrice = $query->field("price");
$this->lastUpdate = $query->field("lastupdate");
$this->maintenance = $query->field('maintenance');
if ($this->stock < 0) include("deadsea.inc");
}
function save() {
/* $clockTime is number of seconds since the epoch */
global $clockTime;
$timestamp = date('r', $clockTime); debug("timestamp", $timestamp);
db_write("INSERT INTO sea (stock, price, time, maintenance)
VALUES('$this->stock', '$this->marketPrice', '$timestamp', '$this->maintenance')");
}
function growFish() {
global $k5, $k6;
$this->stock = round($this->stock + ($k5 * ($k6 - $this->stock) * $this->stock)/100000);
if ($this->stock < 0) $this->stock = 0;
}
function sellFish($quantityFished) {
global $log, $k2, $k3, $k4;
$this->stock -= $quantityFished;
if ($this->stock < 0) include("deadsea.inc");
debug("leftInSea", $this->stock);
$log->retrieve();
$log->fishInMarket += $quantityFished;
$this->marketPrice = $k2 - $k3 * rand(1, 100) * $log->fishInMarket;
if ($this->marketPrice < $k4) $this->marketPrice = rand(1,100)/100.0 + $k4;
debug("market price", $this->marketPrice);
$this->save();
return $this->marketPrice * $quantityFished - $this->maintenance;
}
}
class Fisher {
public $name, $boat, $type, $logintime, $balance, $email;
function retrieve($myName) {
$this->name = $myName;
$query = new query("SELECT boat, balance, logintime, email FROM people
WHERE name = '$myName'");
if (!$query->next_rec()) $query->error("No records retrieved");
$this->boat = $query->field('boat');
$this->balance = $query->field("balance");
$this->logintime = $query->field('logintime');
$this->email = $query->field('email');
}
function save() {
global $clockTime;
$timestamp = date('r', $clockTime);
db_write("UPDATE people SET balance='$this->balance', lastoptime= '$timestamp'
WHERE name='$this->name'");
}
function catchFish($investment) {
global $sea, $log;
global $k1;
if ($investment < 0) $investment = 0;
if ($investment > $this->balance) $investment = $this->balance; /* no borrowing! */
debug("initial balance", $this->balance);
$quantityFished = round($investment * $sea->stock * rand(1, 10) * $k1);
debug("quantityFished", $quantityFished);
$income = $sea->sellFish($quantityFished);
debug("income", $income);
$this->balance = round($this->balance + $income - $investment);
debug("balance after income", $this->balance);
$log->save($this->name, $investment, $quantityFished, $this->balance);
$this->save();
}
}
class Phantom extends Fisher {
public $code;
function invest() {
global $sea, $k6;
$query = new query("SELECT code FROM phantoms WHERE name='$this->name'");
if (!$query->next_rec()) $query->error("No records retrieved");
$this->code = $query->field('code');
$investment = eval($this->code);
$this->catchFish($investment);
if ($this->balance < 0) $this->bankrupt();
}
function bankrupt() {
mail($this->email, "Gone Fishing! Your agent has gone bankrupt",
"Unfortunately, your fisher agent has run out of money and has been retired from
fishing.
The Fishing Authority");
}
}
class Log {
public $fishInMarket, $averageCatchSize, $averageInvestment;
function retrieve() {
$this->fishInMarket = 0;
$totalRecentInvestment = 0 ;
$catches = 0;
$query = new query("SELECT investment, harvest FROM log WHERE harvest > 0 ORDER BY id DESC LIMIT 4");
while ($query->next_rec()) {
$this->fishInMarket += $query->field("harvest"); debug("in market", $this->fishInMarket);
$totalRecentInvestment += $query->field("investment");
$catches++;
}
if ($catches) {
$this->averageCatchSize = round($this->fishInMarket/$catches);
$this->averageInvestment = round($totalRecentInvestment/$catches);
}
}
function save($name, $investment, $quantityFished, $balance) {
global $clockTime;
$timestamp = date('r', $clockTime);
db_write("INSERT INTO log (name, investment, harvest, balance, time)
VALUES ('$name', '$investment', '$quantityFished', '$balance', '$timestamp')");
}
}
/* create instances for the human player, the sea and the log of operations */
$sea = new Sea();
$fisher = new Fisher();
$log = new Log();
/* open the database */
$db = db_open();
/* update the environment state with whatever actions should have happened during the period since a player
was last here (this is necessary because the program never does anything except when a player tries to
retrieve a web page; so actions which should have taken place while nobody was doing anything are
simulated here as though they happened when they were supposed to). This is the place to run agents and
so on. */
$sea->retrieve();
updateWorld();
/* check if player just logged in. If so, register him or her in the database*/
if ($action == 'login') {
register();
}
else {
$welcome_back = "fishing";
}
/* retrieve previous state before processing action */
$fisher->retrieve($name);
$boat = $fisher->boat;
/* despatch to selected action */
switch ($action) {
case 'login':
case 'message': goFishing();
break;
case 'logout': goodbye();
break;
case 'gofishing':
default: if ($investment > 0 )
$fisher->catchFish($investment);
goFishing();
break;
}
/* logout any inactive players */
checkPlayers();
html_footer();
function updateWorld() {
/* update the environment and run any computational agents
Called every time a human user does anything */
global $sea;
global $clockTime, $updateInterval, $phantomActivityRate;
/* first, retrieve any phantoms that will be operational duringthe updating */
$query = new query("SELECT name FROM people WHERE type = 'agent'");
while ($query->next_rec()) {
$phantom = new Phantom();
$phantom->retrieve($query->field('name'));
if ($phantom->balance > 0) $phantoms[] = $phantom;
}
/* for each interval between the last operation and now, update the world.
Always update at least once, but don't update over more than 24 hours
(86400 secs) */
$clockTime = $sea->lastUpdate;
$now = time();
if ($now - $clockTime > 84600) $clockTime = $now - 84600;
do {
$clockTime += $updateInterval;
if ($clockTime > $now) $clockTime = $now;
$sea->growFish();
$sea->save();
/* give the phantom agents a go, every few turns */
if ($phantoms) {
foreach ($phantoms as $phantom) {
if (rand(0, 100) <= $phantomActivityRate) { /*only activate the phantoms now and then */
$phantom->invest();
}
}
}
include("offish.inc");
} while ($clockTime < $now);
}
function checkPlayers() {
/* log out any player who has not done anything for more than $idleTime seconds */
global $idleTime;
$query = new query("SELECT name, date_part('epoch', lastoptime) as lasttime FROM people
WHERE logintime IS NOT NULL AND type != 'agent'");
while ($query->next_rec()) {
if (time() - $query->field('lasttime') > $idleTime) {
$idler = $query->field('name');
db_write("UPDATE people SET logintime = NULL WHERE name= '$idler'");
}
}
}
function register () {
/* here if user has just logged in;
register name in database if this is a new user
or retrieve name and boat name if returning */
global $name, $boat, $welcome_back;
$name = $_POST['loginname'];
$loginboat = $_POST['loginboat'];
$email = $_POST['email'];
if (!$name) {
alert("You must provide your name.");
exit;
}
$query = new query("SELECT * FROM people WHERE name ~* '$name'");
if ($query->num_recs()) {
/* user is already known */
$query->next_rec();
$name = $query->field('name'); /* standardise name to what is in the DB */
$boat = $query->field('boat');
/* record time when logged in */ debug("name in register", $name);
db_write("UPDATE people SET logintime=now(), lastoptime=now() WHERE name='$name'");
$welcome_back = "old_user";
}
else {
/* new user */
if (!$loginboat) {
alert("You must name your fishing boat.");
exit;
}
$query = new query("SELECT boat FROM people WHERE boat='$loginboat'");
if ($query->num_recs()) {
alert("Sorry. That name is already taken for a boat. Please choose another.");
exit;
}
$boat = $loginboat;
db_write("INSERT INTO people (name, boat, createdtime, logintime, lastoptime, balance, email)
VALUES ('$name', '$boat', now(), now(), now(), 10000, '$email')");
$welcome_back = "new_user";
}
$_SESSION['name'] = $name;
$_SESSION['boat'] = $boat;
}
function goodbye() {
/* here if user has just logged out
note that they are no longer at sea */
global $name;
db_write("UPDATE people SET logintime=null WHERE name='$name'");
include("goodbye.inc");
exit;
}
function goFishing () {
/* here if the user wants to go fishing */
global $fisher, $sea, $log, $name, $welcome_back, $agent_activity, $investment;
if ($fisher->balance < 0) {
include("bankrupt.inc");
}
if (!$fisher->logintime) {
/* user has been logged out for exceeding the idle time */
include("logout.inc");
}
else {
$log->retrieve();
include("fish.inc");
}
}
function radioAll($msg) {
/* send a radio message to all human fishers that are currently logged in */
global $name, $clockTime;
$timestamp = date('r', $clockTime);
$query = new query("SELECT boat FROM people
WHERE name != '$name' AND createdtime IS NOT NULL AND logintime IS NOT NULL");
while ($query->next_rec()) {
$recipient = $query->field("boat");
db_write("INSERT INTO msgs (sender, recipient, timesent, timeread, msg)
VALUES(NULL, '$recipient', '$timestamp', NULL, '$msg')");
}
}
/* ENDS */
?>