-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCurrentTroops.php
86 lines (69 loc) · 3.51 KB
/
getCurrentTroops.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
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/connect.php');
//Get village and time data
$villageID = $_SESSION['idPlayer'];
$currentTime = time();
//Get current troops
$getCurrentTroops = $connection->prepare('SELECT * FROM villageowntroops WHERE idVillage = ?');
$getCurrentTroops->bind_param('i', $villageID);
$getCurrentTroops->execute();
$resultCurrentTroops = $getCurrentTroops->get_result();
$getCurrentTroops->close();
$currentTroops = $resultCurrentTroops->fetch_row();
//Get troop production
$getTroopProduction = $connection->prepare('SELECT * FROM barracksproduction WHERE idVillage= ?');
$getTroopProduction->bind_param('i', $villageID);
$getTroopProduction->execute();
$resultTroopProduction = $getTroopProduction->get_result();
$getTroopProduction->close();
$troopProduction = $resultTroopProduction->fetch_row();
if($troopProduction){
$troopID = (int)$troopProduction[2];
//Calculate the time difference
$timeDiff = $currentTime-$troopProduction[8];
//Calculate new troops produced
$troopsProduced = ($timeDiff/$troopProduction[4]);
if($troopsProduced > 0){
$deleteLater = false;
//Check if there were too many troops produced
if($troopsProduced >= $troopProduction[3] - $troopProduction[9]){
$troopsProduced = $troopProduction[3] - $troopProduction[9];
$deleteLater = true;
}
$troopsProducedCombined = $troopsProduced + $troopProduction[9];
$currentTroops[$troopID] += $troopsProduced;
if($deleteLater){
//Delete the production entry
$deleteBarracksProduction = $connection->prepare("DELETE FROM barracksproduction WHERE idvillage = ? AND barrprodid = ?");
$deleteBarracksProduction->bind_param("ii", $villageID, $troopProduction[7]);
$deleteBarracksProduction->execute();
$deleteBarracksProduction->close();
//Update the troops to int value
$updateCurrentTroops = $connection->prepare("UPDATE villageowntroops SET troop".$troopID." = ? WHERE idVillage = ?");
$updateCurrentTroops->bind_param("ii", $currentTroops[$troopID],$villageID);
$updateCurrentTroops->execute();
$updateCurrentTroops->close();
//Rerun the file to check if there are more troops that were trained
include('getCurrentTroops.php');
}
else{
//Update troopsdonealready and the last update
$updateBarracksProduction = $connection->prepare("UPDATE barracksproduction SET lastupdate = ?, troopsdonealready = ? WHERE idVillage = ? AND barrprodid = ?");
$updateBarracksProduction->bind_param("idii", $currentTime, $troopsProducedCombined, $villageID, $troopProduction[7]);
$updateBarracksProduction->execute();
$updateBarracksProduction->close();
//Update the troops
$updateCurrentTroops = $connection->prepare("UPDATE villageowntroops SET troop".$troopID." = ? WHERE idVillage = ?");
$updateCurrentTroops->bind_param("di", $currentTroops[$troopID],$villageID);
$updateCurrentTroops->execute();
$updateCurrentTroops->close();
}
}
}
/*
class CurrentTroops{
public static function removeTroops($villageID,$troops){
}
}
*/
?>