Skip to content

Commit 03edca5

Browse files
author
Pardons Julien
committed
Less SQL call for players update
1 parent 9917609 commit 03edca5

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

src/eBot/Match/Player.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Player {
4848
private $firstSide = "";
4949
private $checkBDD = false;
5050
private $gotFirstKill = false;
51+
private $needSave = false;
5152

5253
public function __construct($match_id, $map_id, $steamid) {
5354
$this->map_id = $map_id;
@@ -88,6 +89,8 @@ public function __construct($match_id, $map_id, $steamid) {
8889
\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());
8990
$this->mysql_id = \mysql_insert_id();
9091
}
92+
93+
$this->needSave = true;
9194
}
9295

9396
private $team = null;
@@ -153,6 +156,7 @@ public function __get($name) {
153156

154157
public function set($name, $val) {
155158
$this->$name = $val;
159+
$this->needSave = true;
156160
}
157161

158162
public function get($name) {
@@ -161,21 +165,39 @@ public function get($name) {
161165

162166
public function setIp($ip) {
163167
$this->ip = $ip;
164-
Logger::debug("Setting $ip to " . $this->steamid . " (players #" . $this->mysql_id . ")");
165-
mysql_query("UPDATE `player` SET ip='{$ip}' WHERE id='{$this->mysql_id}'");
168+
if ($this->ip != $ip) {
169+
Logger::debug("Setting $ip to " . $this->steamid . " (players #" . $this->mysql_id . ")");
170+
mysql_query("UPDATE `player` SET ip='{$ip}' WHERE id='{$this->mysql_id}'");
171+
}
166172
}
167173

168174
public function setCurrentTeam($team, $teamDefault = null) {
175+
169176
if ($team == "CT") {
170-
$this->currentSide = "ct";
177+
if ($this->currentSide != "ct") {
178+
$this->needSave = true;
179+
$this->currentSide = "ct";
180+
}
171181
} elseif ($team == "TERRORIST") {
172-
$this->currentSide = "t";
182+
if ($this->currentSide != "t") {
183+
$this->needSave = true;
184+
$this->currentSide = "t";
185+
}
173186
} elseif ($team == "ct") {
174-
$this->currentSide = "ct";
187+
if ($this->currentSide != "ct") {
188+
$this->needSave = true;
189+
$this->currentSide = "ct";
190+
}
175191
} elseif ($team == "t") {
176-
$this->currentSide = "t";
192+
if ($this->currentSide != "t") {
193+
$this->needSave = true;
194+
$this->currentSide = "t";
195+
}
177196
} else {
178-
$this->currentSide = "other";
197+
if ($this->currentSide != "other") {
198+
$this->needSave = true;
199+
$this->currentSide = "other";
200+
}
179201
}
180202

181203
$this->setTeam($this->currentSide, $teamDefault);
@@ -189,6 +211,7 @@ public function setUserName($name) {
189211
Logger::log("Changing nickname from {$this->name} to $name");
190212
}
191213
$this->name = $name;
214+
$this->needSave = true;
192215
}
193216
}
194217

@@ -197,7 +220,9 @@ public function getSteamid() {
197220
}
198221

199222
public function save() {
200-
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());
223+
if ($this->needSave)
224+
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());
225+
$this->needSave = false;
201226
}
202227

203228
public function saveScore() {

0 commit comments

Comments
 (0)