-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattack.php
More file actions
354 lines (297 loc) · 9.42 KB
/
attack.php
File metadata and controls
354 lines (297 loc) · 9.42 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
<?php
require_once 'includes/bootstrap.php';
require_login();
$pdo = db();
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
if (!is_post()) {
redirect('war.php');
}
verify_csrf();
$char = get_character();
if (!$char || !$char['turf_id']) {
redirect('actions.php');
}
// =========================
// NORMALISE TURF
// =========================
function normalise_turf($pdo, $turfId) {
$stmt = $pdo->prepare("
SELECT faction_id, control_percent
FROM turf_control
WHERE turf_id = ?
");
$stmt->execute([$turfId]);
$rows = $stmt->fetchAll();
$total = array_sum(array_column($rows, 'control_percent'));
if ($total <= 0) {
return;
}
foreach ($rows as $row) {
$newPercent = round(($row['control_percent'] / $total) * 100);
$pdo->prepare("
UPDATE turf_control
SET control_percent = ?
WHERE turf_id = ? AND faction_id = ?
")->execute([$newPercent, $turfId, $row['faction_id']]);
}
}
// =========================
// LOAD ACTIVE WAR
// =========================
$stmt = $pdo->prepare("
SELECT * FROM turf_wars
WHERE turf_id = ? AND status = 'active'
LIMIT 1
");
$stmt->execute([$char['turf_id']]);
$war = $stmt->fetch();
if (!$war) {
redirect('actions.php');
}
// =========================
// LOAD PLAYER PARTICIPATION
// =========================
$stmt = $pdo->prepare("
SELECT * FROM turf_war_participants
WHERE war_id = ? AND char_id = ?
LIMIT 1
");
$stmt->execute([$war['id'], $char['id']]);
$player = $stmt->fetch();
if (!$player || !$player['is_alive']) {
redirect('war.php');
}
// =========================
// COOLDOWN CHECK
// =========================
$cooldown = 5; // seconds
$now = time();
if ($char['last_war_attack'] > 0 && ($now - $char['last_war_attack']) < $cooldown) {
redirect('war.php');
}
// =========================
// DETERMINE SIDES
// =========================
$isAttacker = ($char['faction_id'] == $war['attacker_faction_id']);
$enemyFactionId = $isAttacker
? $war['defender_faction_id']
: $war['attacker_faction_id'];
// =========================
// BEGIN TRANSACTION
// =========================
$pdo->beginTransaction();
// =========================
// GENERATE POINTS
// =========================
$points = rand(8, 18) + (int)floor($char['strength'] / 5) + (int)($char['war_points_bonus'] ?? 0);
// =========================
// ADD SCORE
// =========================
if ($isAttacker) {
$pdo->prepare("
UPDATE turf_wars
SET attacker_score = attacker_score + ?
WHERE id = ?
")->execute([$points, $war['id']]);
} else {
$pdo->prepare("
UPDATE turf_wars
SET defender_score = defender_score + ?
WHERE id = ?
")->execute([$points, $war['id']]);
}
// =========================
// RANDOM TARGET
// =========================
$stmtEnemy = $pdo->prepare("
SELECT p.id, p.char_id, p.current_hp, c.name AS target_name, c.endurance
FROM turf_war_participants p
JOIN characters c ON c.id = p.char_id
WHERE p.war_id = ?
AND p.faction_id = ?
AND p.is_alive = 1
ORDER BY RAND()
LIMIT 1
");
$stmtEnemy->execute([$war['id'], $enemyFactionId]);
$target = $stmtEnemy->fetch();
// =========================
// DEAL DAMAGE
// =========================
$weaponDamage = (int)($char['weapon_damage'] ?? 10);
$damage = 0;
$killed = false;
$newHp = null;
if ($target) {
$damage = max(1,
rand(5, 12)
+ $weaponDamage
+ (int)floor($char['strength'] / 8)
- (int)floor((int)($target['endurance'] ?? 10) / 8)
);
$newHp = max(0, (int)$target['current_hp'] - $damage);
if ($newHp <= 0) {
$pdo->prepare("
UPDATE turf_war_participants
SET is_alive = 0, current_hp = 0, killed_at = NOW()
WHERE id = ?
")->execute([$target['id']]);
$killed = true;
} else {
$pdo->prepare("
UPDATE turf_war_participants
SET current_hp = ?
WHERE id = ?
")->execute([$newHp, $target['id']]);
}
}
// =========================
// UPDATE PLAYER COOLDOWN
// =========================
$pdo->prepare("
UPDATE characters
SET last_war_attack = ?
WHERE id = ?
")->execute([$now, $char['id']]);
// =========================
// LOG EVENT
// =========================
if ($target && $killed) {
$message = "{$char['name']} dealt {$damage} damage and eliminated {$target['target_name']}! (+{$points} pts)";
} elseif ($target) {
$message = "{$char['name']} dealt {$damage} damage to {$target['target_name']} (+{$points} pts)";
} else {
$message = "{$char['name']} gained +{$points} points";
}
$pdo->prepare("
INSERT INTO turf_war_logs (war_id, message)
VALUES (?, ?)
")->execute([$war['id'], $message]);
// =========================
// CHECK WIN CONDITIONS
// =========================
$stmt = $pdo->prepare("
SELECT attacker_score, defender_score, target_score
FROM turf_wars
WHERE id = ?
");
$stmt->execute([$war['id']]);
$updatedWar = $stmt->fetch();
$winnerFactionId = null;
// Score win
if ($updatedWar['attacker_score'] >= $updatedWar['target_score']) {
$winnerFactionId = $war['attacker_faction_id'];
}
elseif ($updatedWar['defender_score'] >= $updatedWar['target_score']) {
$winnerFactionId = $war['defender_faction_id'];
}
// Death wipe check
if (!$winnerFactionId) {
$stmt = $pdo->prepare("
SELECT faction_id, COUNT(*) as alive
FROM turf_war_participants
WHERE war_id = ? AND is_alive = 1
GROUP BY faction_id
");
$stmt->execute([$war['id']]);
$aliveCounts = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
// Ensure both factions are checked
$attackerAlive = $aliveCounts[$war['attacker_faction_id']] ?? 0;
$defenderAlive = $aliveCounts[$war['defender_faction_id']] ?? 0;
if ($attackerAlive == 0) {
$winnerFactionId = $war['defender_faction_id'];
}
elseif ($defenderAlive == 0) {
$winnerFactionId = $war['attacker_faction_id'];
}
}
// =========================
// END WAR IF WINNER
// =========================
if ($winnerFactionId) {
$pdo->prepare("
UPDATE turf_wars
SET status = 'finished',
winner_faction_id = ?,
ended_at = NOW()
WHERE id = ?
")->execute([$winnerFactionId, $war['id']]);
// Set new turf owner
$pdo->prepare("
UPDATE turfs
SET faction_id = ?
WHERE id = ?
")->execute([$winnerFactionId, $char['turf_id']]);
$loserFactionId = ($winnerFactionId == $war['attacker_faction_id'])
? $war['defender_faction_id']
: $war['attacker_faction_id'];
// Fetch current control for winner and loser
$stmtCtrl = $pdo->prepare("SELECT faction_id, control_percent FROM turf_control WHERE turf_id = ?");
$stmtCtrl->execute([$char['turf_id']]);
$controls = $stmtCtrl->fetchAll(PDO::FETCH_KEY_PAIR);
$loserCurrent = (int)($controls[$loserFactionId] ?? 0);
$winnerCurrent = (int)($controls[$winnerFactionId] ?? 0);
// Loser retains a floor: 35% of current control, minimum 15%
$loserNew = max(15, (int)round($loserCurrent * 0.35));
// Winner gains a +20% boost from the victory, capped at 70% pre-normalise
$winnerNew = min(70, $winnerCurrent + 20);
$pdo->prepare("
UPDATE turf_control SET control_percent = ? WHERE turf_id = ? AND faction_id = ?
")->execute([$loserNew, $char['turf_id'], $loserFactionId]);
$pdo->prepare("
UPDATE turf_control SET control_percent = ? WHERE turf_id = ? AND faction_id = ?
")->execute([$winnerNew, $char['turf_id'], $winnerFactionId]);
// Apply cooldown
$pdo->prepare("
UPDATE turfs
SET war_cooldown_until = DATE_ADD(NOW(), INTERVAL 24 HOUR)
WHERE id = ?
")->execute([$char['turf_id']]);
// Normalise turf control after war
normalise_turf($pdo, $char['turf_id']);
// Reset all participants (respawn everyone, restore HP)
$pdo->prepare("
UPDATE turf_war_participants
SET is_alive = 1,
current_hp = max_hp,
killed_at = NULL
WHERE war_id = ?
")->execute([$war['id']]);
$pdo->prepare("
UPDATE characters
SET last_war_attack = 0
WHERE faction_id IN (?, ?)
")->execute([
$war['attacker_faction_id'],
$war['defender_faction_id']
]);
}
$pdo->commit();
// =========================
// RESPOND
// =========================
if ($isAjax) {
$stmt = $pdo->prepare("SELECT attacker_score, defender_score, status FROM turf_wars WHERE id = ?");
$stmt->execute([$war['id']]);
$finalWar = $stmt->fetch();
$mySide = ($char['faction_id'] == $war['attacker_faction_id']) ? 'attacker' : 'defender';
header('Content-Type: application/json');
echo json_encode([
'points' => $points,
'damage' => $damage,
'killed' => $killed,
'target_name' => $target['target_name'] ?? null,
'target_hp' => $newHp,
'message' => $message,
'attacker_score' => (int)$finalWar['attacker_score'],
'defender_score' => (int)$finalWar['defender_score'],
'target_score' => (int)$war['target_score'],
'war_ended' => $finalWar['status'] === 'finished',
'winner' => $winnerFactionId,
'my_side' => $mySide,
'cooldown' => 5,
]);
exit;
}
redirect('war.php');