-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Player.php
298 lines (260 loc) · 10.5 KB
/
Player.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
<?php
/**
* eBot - A bot for match management for CS:GO
* @license http://creativecommons.org/licenses/by/3.0/ Creative Commons 3.0
* @author Julien Pardons <julien.pardons@esport-tools.net>
* @version 3.0
* @date 21/10/2012
*/
namespace eBot\Match;
use eTools\Utils\Logger;
class Player {
private $id = 0;
private $match_id = 0;
private $map_id = 0;
private $save_mode = null;
private $steamid = "";
private $ip = "";
private $online = true;
private $kill = 0;
private $assist = 0;
private $death = 0;
private $hs = 0;
private $bombe = 0;
private $defuse = 0;
private $tk = 0;
private $point = 0;
private $name = "";
private $mysql_id = 0;
private $currentSide = "";
private $killRound = 0;
private $alive = true;
private $firstKill = 0;
private $v1 = 0;
private $v2 = 0;
private $v3 = 0;
private $v4 = 0;
private $v5 = 0;
private $k1 = 0;
private $k2 = 0;
private $k3 = 0;
private $k4 = 0;
private $k5 = 0;
private $firstSide = "";
private $checkBDD = false;
private $gotFirstKill = false;
private $needSave = false;
public function __construct($match_id, $map_id, $steamid) {
$this->map_id = $map_id;
$this->match_id = $match_id;
$this->steamid = $steamid;
Logger::debug("Creating objet Player $match_id $map_id $steamid");
$sql = \mysql_query("SELECT * FROM players WHERE match_id='" . $this->match_id . "' AND map_id='" . $this->map_id . "' AND steamid = '" . $this->steamid . "'") or dir(mysql_error());
$req = \mysql_fetch_array($sql);
if ($req) {
Logger::log("Restoring player " . $this->steamid . " from match " . $this->match_id);
$this->mysql_id = $req['id'];
$this->firstSide = $req['first_side'];
$this->currentSide = $req['current_side'];
$this->name = $req['pseudo'];
$this->kill = $req['nb_kill'];
$this->assist = $req['assist'];
$this->death = $req['death'];
$this->point = $req['point'];
$this->hs = $req['hs'];
$this->defuse = $req['defuse'];
$this->bombe = $req['bombe'];
$this->tk = $req['tk'];
$this->v1 = $req['nb1'];
$this->v2 = $req['nb2'];
$this->v3 = $req['nb3'];
$this->v4 = $req['nb4'];
$this->v5 = $req['nb5'];
$this->k1 = $req['nb1kill'];
$this->k2 = $req['nb2kill'];
$this->k3 = $req['nb3kill'];
$this->k4 = $req['nb4kill'];
$this->k5 = $req['nb5kill'];
$this->firstKill = $req['firstkill'];
} else {
Logger::log("Creating players " . $this->steamid . " on match " . $this->match_id);
\mysql_query("INSERT INTO `players` (`match_id`,`map_id`,`steamid`,`first_side`,`created_at`, `updated_at`) VALUES ('{$this->match_id}','{$this->map_id}', '{$this->steamid}', 'other', NOW(), NOW())") or die(mysql_error());
$this->mysql_id = \mysql_insert_id();
}
$this->needSave = true;
}
private $team = null;
public function setTeam($team, $teamDefault = null) {
if ($this->team == null) {
if (($teamDefault != null) && ($teamDefault == "a") || ($teamDefault == "b")) {
Logger::debug("Already got the team");
$this->team = $teamDefault;
} else {
if ($team == "CT") {
$this->team = "a";
} elseif ($team == "TERRORIST") {
$this->team = "b";
} elseif ($team == "ct") {
$this->team = "a";
} elseif ($team == "t") {
$this->team = "b";
} else {
$this->team = null;
}
}
if ($this->team != null) {
mysql_query("UPDATE `players` SET team = '{$this->team}', updated_at = NOW() WHERE id='{$this->mysql_id}'");
} else {
mysql_query("UPDATE `players` SET team = 'other' WHERE id='{$this->mysql_id}'");
}
}
}
public function roundStart() {
$this->killRound = 0;
$this->alive = true;
$this->gotFirstKill = false;
// Snapshotting player
}
public function getId() {
return $this->mysql_id;
}
public function setOnline($online) {
$this->online = $online;
}
public function inc($var, $nb = 1) {
$this->$var += $nb;
}
public function deinc($var, $nb = 1) {
$this->$var -= $nb;
}
public function __set($name, $val) {
$this->$name = $val;
}
public function __get($name) {
return $this->$name;
}
public function set($name, $val) {
$this->$name = $val;
$this->needSave = true;
}
public function get($name) {
return $this->$name;
}
public function setIp($ip) {
$this->ip = $ip;
if ($this->ip != $ip) {
Logger::debug("Setting $ip to " . $this->steamid . " (players #" . $this->mysql_id . ")");
mysql_query("UPDATE `player` SET ip='{$ip}' WHERE id='{$this->mysql_id}'");
}
}
public function setCurrentTeam($team, $teamDefault = null) {
if ($team == "CT") {
if ($this->currentSide != "ct") {
$this->needSave = true;
$this->currentSide = "ct";
}
} elseif ($team == "TERRORIST") {
if ($this->currentSide != "t") {
$this->needSave = true;
$this->currentSide = "t";
}
} elseif ($team == "ct") {
if ($this->currentSide != "ct") {
$this->needSave = true;
$this->currentSide = "ct";
}
} elseif ($team == "t") {
if ($this->currentSide != "t") {
$this->needSave = true;
$this->currentSide = "t";
}
} else {
if ($this->currentSide != "other") {
$this->needSave = true;
$this->currentSide = "other";
}
}
$this->setTeam($this->currentSide, $teamDefault);
}
public function setUserName($name) {
if ($this->name != $name) {
if ($this->name == "") {
Logger::log("Setting nickname to $name");
} else {
Logger::log("Changing nickname from {$this->name} to $name");
}
$this->name = $name;
$this->needSave = true;
}
}
public function getSteamid() {
return $this->steamid;
}
public function save() {
if ($this->needSave)
mysql_query("UPDATE `players` SET pseudo='" . \mysql_real_escape_string($this->name) . "', current_side='" . $this->currentSide . "' WHERE id='{$this->mysql_id}'") or Logger::error(mysql_error());
$this->needSave = false;
}
public function saveScore() {
$query = "UPDATE players SET
nb_kill = '" . $this->kill . "',
death = '" . $this->death . "',
assist = '" . $this->assist . "',
hs = '" . $this->hs . "',
defuse = '" . $this->defuse . "',
bombe = '" . $this->bombe . "',
point = '" . $this->point . "',
tk = '" . $this->tk . "',
firstkill='" . $this->firstKill . "',
updated_at = NOW()
WHERE id='" . $this->mysql_id . "'";
if (!mysql_query($query)) {
Logger::error(mysql_error());
}
}
public function saveKillRound() {
if ($this->killRound > 0) {
if ($this->killRound <= 5) {
mysql_query("UPDATE players SET nb" . $this->killRound . "kill = nb" . $this->killRound . "kill + 1 WHERE id='" . $this->mysql_id . "'");
$k = "k" . $this->killRound;
$this->inc($k);
}
}
}
public function snapshot($round) {
@\mysql_query("DELETE FROM players_snapshot WHERE player_id = '" . $this->mysql_id . "' AND round_id='" . $round . "'");
\mysql_query("INSERT INTO players_snapshot
(`player_id`,`nb_kill`,`death`,`assist`,`point`,`hs`,`defuse`,`bombe`,`tk`,`nb1`,`nb2`,`nb3`,`nb4`,`nb5`,`nb1kill`,`nb2kill`,`nb3kill`,`nb4kill`,`nb5kill`,`firstkill`,`round_id`,`created_at`,`updated_at`)
VALUES
({$this->mysql_id}, {$this->kill}, {$this->death}, {$this->assist}, {$this->point}, {$this->hs}, {$this->defuse}, {$this->bombe}, {$this->tk}, {$this->v1}, {$this->v2}, {$this->v3}, {$this->v4}, {$this->v5}, {$this->k1}, {$this->k2}, {$this->k3}, {$this->k4}, {$this->k5}, {$this->firstKill}, {$round}, NOW(), NOW())") or Logger::error("Error while snapshoting");
}
public function restoreSnapshot($round) {
$sql = \mysql_query("SELECT * FROM players_snapshot WHERE player_id ='" . $this->mysql_id . "' AND round_id='" . $round . "' ") or dir(mysql_error());
$req = \mysql_fetch_array($sql);
if ($req) {
Logger::log("Restoring player " . $this->steamid . " from match " . $this->match_id . " for round " . $round);
$this->kill = $req['nb_kill'];
$this->assist = $req['assist'];
$this->death = $req['death'];
$this->point = $req['point'];
$this->hs = $req['hs'];
$this->defuse = $req['defuse'];
$this->bombe = $req['bombe'];
$this->tk = $req['tk'];
$this->v1 = $req['nb1'];
$this->v2 = $req['nb2'];
$this->v3 = $req['nb3'];
$this->v4 = $req['nb4'];
$this->v5 = $req['nb5'];
$this->k1 = $req['nb1kill'];
$this->k2 = $req['nb2kill'];
$this->k3 = $req['nb3kill'];
$this->k4 = $req['nb4kill'];
$this->k5 = $req['nb5kill'];
$this->firstKill = $req['firstkill'];
} else {
Logger::log("Snapshot not found for " . $this->steamid . " from match " . $this->match_id . " for round " . $round);
}
}
}
?>