forked from thekabal/tki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounty.php
404 lines (351 loc) · 15.1 KB
/
bounty.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
<?php declare(strict_types = 1);
/**
* bounty.php from The Kabal Invasion.
* The Kabal Invasion is a Free & Opensource (FOSS), web-based 4X space/strategy game.
*
* @copyright 2020 The Kabal Invasion development team, Ron Harwood, and the BNT development team
*
* @license GNU AGPL version 3.0 or (at your option) any later version.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
require_once './common.php';
$login = new Tki\Login();
$login->checkLogin($pdo_db, $lang, $tkireg, $tkitimer, $template);
// Database driven language entries
$langvars = Tki\Translate::load($pdo_db, $lang, array('bounty', 'combat',
'common', 'footer', 'insignias', 'news',
'port', 'universal'));
$title = $langvars['l_by_title'];
$header = new Tki\Header();
$header->display($pdo_db, $lang, $template, $title);
// Detect if this variable exists, and filter it. Returns false if anything wasn't right
$response = null;
$response = filter_input(INPUT_POST, 'response', FILTER_SANITIZE_STRING);
if (strlen(trim($response)) === 0)
{
$response = false;
}
if (array_key_exists('response', $_GET))
{
$response = filter_input(INPUT_GET, 'response', FILTER_SANITIZE_STRING);
}
// Detect if this variable exists, and filter it. Returns false if anything wasn't right
$bounty_on = null;
$bounty_on = (int) filter_input(INPUT_POST, 'bounty_on', FILTER_SANITIZE_NUMBER_INT);
if (strlen(trim((string) $bounty_on)) === 0)
{
$bounty_on = false;
}
if (array_key_exists('bounty_on', $_GET))
{
$bounty_on = filter_input(INPUT_GET, 'bounty_on', FILTER_SANITIZE_NUMBER_INT);
}
// Detect if this variable exists, and filter it. Returns false if anything wasn't right
$bid = null;
$bid = (int) filter_input(INPUT_POST, 'bid', FILTER_SANITIZE_NUMBER_INT);
if (strlen(trim((string) $bid)) === 0)
{
$bid = false;
}
// Detect if this variable exists, and filter it. Returns false if anything wasn't right
$amount = 0;
$amount = (int) filter_input(INPUT_POST, 'amount', FILTER_SANITIZE_NUMBER_INT);
if (strlen(trim((string) $amount)) === 0)
{
$amount = 0;
}
// Get playerinfo from database
$players_gateway = new \Tki\Players\PlayersGateway($pdo_db);
$playerinfo = $players_gateway->selectPlayerInfo($_SESSION['username']);
$bounty_details = array();
$bounties = array();
switch ($response) {
case 'display':
echo "<h1>" . $title . "</h1>\n";
$res5 = $old_db->Execute("SELECT * FROM {$old_db->prefix}ships, {$old_db->prefix}bounty WHERE bounty_on = ship_id AND bounty_on = ?;", array($bounty_on));
Tki\Db::logDbErrors($pdo_db, $res5, __LINE__, __FILE__);
$bount_details_count = 0;
if ($res5)
{
while (!$res5->EOF)
{
$bounty_details[$bount_details_count] = $res5->fields;
$bount_details_count++;
$res5->MoveNext();
}
}
$num_details = $bount_details_count;
if ($num_details < 1)
{
echo $langvars['l_by_nobounties'] . "<br>";
}
else
{
echo $langvars['l_by_bountyon'] . " " . $bounty_details[0]['character_name'];
echo '<table border=1 cellspacing=1 cellpadding=2 width="50%" align=center>';
echo "<tr bgcolor=\"$tkireg->color_header\">";
echo "<td><strong>" . $langvars['l_amount'] . "</td>";
echo "<td><strong>" . $langvars['l_by_placedby'] . "</td>";
echo "<td><strong>" . $langvars['l_by_action'] . "</td>";
echo "</tr>";
$color = $tkireg->color_line1;
for ($bount_details_count = 0; $bount_details_count < $num_details; $bount_details_count++)
{
$players_gateway = new \Tki\Players\PlayersGateway($pdo_db);
$details = $players_gateway->selectPlayerInfoById($bounty_details[$bount_details_count]['placed_by']);
echo "<tr bgcolor=\"$color\">";
echo "<td>" . $bounty_details[$bount_details_count]['amount'] . "</td>";
if ($bounty_details[$bount_details_count]['placed_by'] == 0)
{
echo "<td>" . $langvars['l_by_thefeds'] . "</td>";
}
else
{
echo "<td>" . $details['character_name'] . "</td>";
}
if ($bounty_details[$bount_details_count]['placed_by'] == $playerinfo['ship_id'])
{
echo "<td><a href=bounty.php?bid=" . $bounty_details[$bount_details_count]['bounty_id'] . "&response=cancel>" . $langvars['l_by_cancel'] . "</a></td>";
}
else
{
echo "<td>n/a</td>";
}
echo "</tr>";
if ($color == $tkireg->color_line1)
{
$color = $tkireg->color_line2;
}
else
{
$color = $tkireg->color_line1;
}
}
echo "</table>";
}
break;
case 'cancel':
echo "<h1>" . $title . "</h1>\n";
if ($playerinfo['turns'] < 1)
{
echo $langvars['l_by_noturn'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
$res = $old_db->Execute("SELECT * FROM {$old_db->prefix}bounty WHERE bounty_id = ?;", array($bid));
Tki\Db::logDbErrors($pdo_db, $res, __LINE__, __FILE__);
if (!$res || $res->RowCount() == 0)
{
echo $langvars['l_by_nobounty'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
$bty = $res->fields;
if ($bty['placed_by'] != $playerinfo['ship_id'])
{
echo $langvars['l_by_notyours'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
$del = $old_db->Execute("DELETE FROM {$old_db->prefix}bounty WHERE bounty_id = ?;", array($bid));
Tki\Db::logDbErrors($pdo_db, $del, __LINE__, __FILE__);
$cur_time_stamp = date("Y-m-d H:i:s");
$refund = $bty['amount'];
$sql = "UPDATE ::prefix::ships SET last_login = :stamp, turns = turns - 1, turns_used = turns_used + 1, credits = credits + :refund WHERE ship_id = :ship_id";
$stmt = $pdo_db->prepare($sql);
$stmt->bindParam(':stamp', $cur_time_stamp, \PDO::PARAM_INT);
$stmt->bindParam(':refund', $refund, \PDO::PARAM_INT);
$stmt->bindParam(':ship_id', $playerinfo['ship_id'], \PDO::PARAM_INT);
$result = $stmt->execute();
Tki\Db::logDbErrors($pdo_db, $sql, __LINE__, __FILE__);
echo $langvars['l_by_canceled'] . "<br>";
Tki\Text::gotoMain($pdo_db, $lang);
die();
case 'place':
echo "<h1>" . $title . "</h1>\n";
$ex = $old_db->Execute("SELECT * FROM {$old_db->prefix}ships WHERE ship_id = ?;", array($bounty_on));
Tki\Db::logDbErrors($pdo_db, $ex, __LINE__, __FILE__);
if (!$ex)
{
echo $langvars['l_by_notexists'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
$bty = $ex->fields;
if ($bty['ship_destroyed'] == "Y")
{
echo $langvars['l_by_destroyed'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
if ($playerinfo['turns'] < 1)
{
echo $langvars['l_by_noturn'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
if ($amount <= 0)
{
echo $langvars['l_by_zeroamount'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
if ($bounty_on == $playerinfo['ship_id'])
{
echo $langvars['l_by_yourself'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
if ($amount > $playerinfo['credits'])
{
echo $langvars['l_by_notenough'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
if ($tkireg->max_bountyvalue != 0)
{
$percent = $tkireg->max_bountyvalue * 100;
$score = Tki\Score::updateScore($pdo_db, $playerinfo['ship_id'], $tkireg, $playerinfo);
$maxtrans = $score * $score * $tkireg->max_bountyvalue;
$previous_bounty = 0;
$pb = $old_db->Execute("SELECT SUM(amount) AS totalbounty FROM {$old_db->prefix}bounty WHERE bounty_on = ? AND placed_by = ?;", array($bounty_on, $playerinfo['ship_id']));
Tki\Db::logDbErrors($pdo_db, $pb, __LINE__, __FILE__);
if ($pb)
{
$prev = $pb->fields;
$previous_bounty = $prev['totalbounty'];
}
if ($amount + $previous_bounty > $maxtrans)
{
$langvars['l_by_toomuch'] = str_replace("[percent]", (string) $percent, $langvars['l_by_toomuch']);
echo $langvars['l_by_toomuch'] . "<br><br>";
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);
die();
}
}
$insert = $old_db->Execute("INSERT INTO {$old_db->prefix}bounty (bounty_on, placed_by, amount) values (?, ?, ?);", array($bounty_on, $playerinfo['ship_id'], $amount));
Tki\Db::logDbErrors($pdo_db, $insert, __LINE__, __FILE__);
$cur_time_stamp = date("Y-m-d H:i:s");
$sql = "UPDATE ::prefix::ships SET last_login = :stamp, turns = turns - 1, turns_used = turns_used + 1, credits = credits - :amount WHERE ship_id = :ship_id";
$stmt = $pdo_db->prepare($sql);
$stmt->bindParam(':stamp', $cur_time_stamp, \PDO::PARAM_INT);
$stmt->bindParam(':amount', $amount, \PDO::PARAM_INT);
$stmt->bindParam(':ship_id', $playerinfo['ship_id'], \PDO::PARAM_INT);
$result = $stmt->execute();
Tki\Db::logDbErrors($pdo_db, $sql, __LINE__, __FILE__);
echo $langvars['l_by_placed'] . "<br>";
Tki\Text::gotoMain($pdo_db, $lang);
die();
default:
echo "<h1>" . $title . "</h1>\n";
$res = $old_db->Execute("SELECT * FROM {$old_db->prefix}ships WHERE ship_destroyed = 'N' AND ship_id <> ? ORDER BY character_name ASC;", array($playerinfo['ship_id']));
Tki\Db::logDbErrors($pdo_db, $res, __LINE__, __FILE__);
echo "<form accept-charset='utf-8' action=bounty.php method=post>";
echo "<table>";
echo "<tr><td>" . $langvars['l_by_bountyon'] . "</td><td><select name=bounty_on>";
while (!$res->EOF)
{
if ($bounty_on !== null && $bounty_on == $res->fields['ship_id'])
{
$selected = "selected";
}
else
{
$selected = null;
}
$charname = $res->fields['character_name'];
$ship_id = $res->fields['ship_id'];
echo "<option value=$ship_id $selected>$charname</option>";
$res->MoveNext();
}
echo "</select></td></tr>";
echo "<tr><td>" . $langvars['l_by_amount'] . ":</td><td><input type=text name=amount size=20 maxlength=20></td></tr>";
echo "<tr><td></td><td><input type=submit value=" . $langvars['l_by_place'] . "><input type=reset value=Clear></td>";
echo "</table>";
echo "<input type=hidden name=response value=place>";
echo "</form>";
$result3 = $old_db->Execute("SELECT bounty_on, SUM(amount) as total_bounty FROM {$old_db->prefix}bounty GROUP BY bounty_on;");
Tki\Db::logDbErrors($pdo_db, $result3, __LINE__, __FILE__);
$i = 0;
if ($result3)
{
while (!$result3->EOF)
{
$bounties[$i] = $result3->fields;
$i++;
$result3->MoveNext();
}
}
$num_bounties = $i;
if ($num_bounties < 1)
{
echo $langvars['l_by_nobounties'] . "<br>";
}
else
{
echo $langvars['l_by_moredetails'] . "<br><br>";
echo "<table width=\"100%\" border=0 cellspacing=0 cellpadding=2>";
echo "<tr bgcolor=\"$tkireg->color_header\">";
echo "<td><strong>" . $langvars['l_by_bountyon'] . "</strong></td>";
echo "<td><strong>" . $langvars['l_amount'] . "</td>";
echo "</tr>";
$color = $tkireg->color_line1;
for ($i = 0; $i < $num_bounties; $i++)
{
$players_gateway = new \Tki\Players\PlayersGateway($pdo_db);
$tmp_return = $players_gateway->selectPlayerInfoById($bounties[$i]['bounty_on']);
$details = $tmp_return['character_name'];
echo "<tr bgcolor=\"$color\">";
echo "<td><a href=bounty.php?bounty_on=" . $bounties[$i]['bounty_on'] . "&response=display>" . $details['character_name'] . "</a></td>";
echo "<td>" . $bounties[$i]['total_bounty'] . "</td>";
echo "</tr>";
if ($color == $tkireg->color_line1)
{
$color = $tkireg->color_line2;
}
else
{
$color = $tkireg->color_line1;
}
}
echo "</table>";
}
echo "<br><br>";
break;
}
Tki\Text::gotoMain($pdo_db, $lang);
$footer = new Tki\Footer();
$footer->display($pdo_db, $lang, $tkireg, $tkitimer, $template);