Advertisement
PEMapModder

LegionPE MySQL query cast storm

Dec 26th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.59 KB | None | 0 0
  1. diff --git a/LegionPE-Core/src/legionpe/games/kitpvp/PvpGame.php b/LegionPE-Core/src/legionpe/games/kitpvp/PvpGame.php
  2. index 0638efc..5a5b69f 100644
  3. --- a/LegionPE-Core/src/legionpe/games/kitpvp/PvpGame.php
  4. +++ b/LegionPE-Core/src/legionpe/games/kitpvp/PvpGame.php
  5. @@ -164,7 +164,7 @@ class PvpGame implements Game{
  6.                 ) AS sel;", MysqlConnection::RAW);
  7.             $i = 1;
  8.             while(is_array($row = $result->fetch_assoc())){
  9. -               $output .= sprintf("#%d) %s (%d kills, %d deaths)\n", $i++, $row["name"], $row["kills"], $row["deaths"]); // I'm not sure about the associative names
  10. +               $output .= sprintf("#%d) %s (%d kills, %d deaths)\n", $i++, $row["name"], (int) $row["kills"], (int) $row["deaths"]); // I'm not sure about the associative names
  11.             }
  12.             $result->close();
  13.             return $output;
  14. @@ -275,7 +275,7 @@ class PvpGame implements Game{
  15.         }
  16.         $res = $this->main->getMySQLi()->query("SELECT kills FROM kitpvp WHERE uid = $uid;", MysqlConnection::ASSOC);
  17.         if(is_array($res)){
  18. -           return $res["kills"];
  19. +           return (int) $res["kills"];
  20.         }
  21.         return 0;
  22.     }
  23. @@ -292,7 +292,7 @@ class PvpGame implements Game{
  24.         }
  25.         $res = $this->main->getMySQLi()->query("SELECT deaths FROM kitpvp WHERE uid = $uid;", MysqlConnection::ASSOC);
  26.         if(is_array($res)){
  27. -           return $res["deaths"];
  28. +           return (int) $res["deaths"];
  29.         }
  30.         return 0;
  31.     }
  32. @@ -313,7 +313,7 @@ class PvpGame implements Game{
  33.         else{
  34.             $res = $this->main->getMySQLi()->query("SELECT kills,deaths FROM kitpvp WHERE uid = $uid;", MysqlConnection::ASSOC);
  35.             if(is_array($res)){
  36. -               list($kills, $deaths) = [$res["kills"], $res["deaths"]];
  37. +               list($kills, $deaths) = [(int) $res["kills"], (int) $res["deaths"]];
  38.             }
  39.         }
  40.         list($kills, $deaths) = [(int) $kills, (int) $deaths];
  41. @@ -371,6 +371,7 @@ class PvpGame implements Game{
  42.         if($from === $to){
  43.             return "You cannot add yourself as friend!";
  44.         }
  45. +       var_dump($from, $to);
  46.         if($toLarger = $to > $from){
  47.             $large = $to;
  48.             $small = $from;
  49. @@ -382,17 +383,23 @@ class PvpGame implements Game{
  50.         $result = $db->query("SELECT * FROM kitpvp_friends WHERE smalluid = $small AND largeuid = $large;", MysqlConnection::ASSOC);
  51.         if(!is_array($result)){
  52.             $result = ["smalluid" => $small, "largeuid" => $large, "type" => self::TYPE_BROKE];
  53. -           $null = true;
  54. +           $null = true; // insert
  55. +       }
  56. +       else{
  57. +           foreach(["smalluid", "largeuid", "type"] as $key){
  58. +               $result[$key] = (int) $result[$key];
  59. +           }
  60.         }
  61.         switch($result["type"]){
  62.             case self::TYPE_BROKE:
  63.                 // send
  64.                 $type = $toLarger ? self::TYPE_FROM_SMALL:self::TYPE_FROM_LARGE;
  65. +               var_dump($type, $small, $large);
  66.                 if(isset($null) and $null === true){
  67.                     $db->query("INSERT INTO kitpvp_friends (smalluid, largeuid, type) VALUES (%d, %d, %d);", MysqlConnection::RAW, $small, $large, $type);
  68.                 }
  69.                 else{
  70. -                   $db->query("UPDATE kitpvp_friends SET type = %d WHERE smalluid = %d AND largeuid = %d", $type, $small, $large);
  71. +                   $db->query("UPDATE kitpvp_friends SET type = %d WHERE smalluid = %d AND largeuid = %d;", $type, $small, $large);
  72.                 }
  73.                 $otherSession = $this->main->getSessions()->getSessionByUID($to);
  74.                 if($otherSession instanceof Session){
  75. @@ -429,7 +436,7 @@ class PvpGame implements Game{
  76.         if($rank === false){
  77.             $result = $this->main->getMySQLi()->query("SELECT rank FROM ranks WHERE uid = $uid;", MysqlConnection::ASSOC);
  78.             if(is_array($result)){
  79. -               $rank = $result["rank"];
  80. +               $rank = (int) $result["rank"];
  81.             }
  82.             else{
  83.                 $rank = Settings::RANK_IMPORTANCE_DEFAULT;
  84. @@ -438,6 +445,7 @@ class PvpGame implements Game{
  85.         $max = Settings::kitpvp_maxFriends($rank);
  86.         $result = $this->main->getMySQLi()->query("SELECT COUNT(*) AS cnt FROM kitpvp_friends WHERE (smalluid = $uid OR largeuid = $uid) AND type = %d", MysqlConnection::ASSOC, self::TYPE_FRIEND);
  87.         if(is_array($result)){
  88. +           $result["cnt"] = (int) $result["cnt"];
  89.             if($result["cnt"] >= $max){
  90.                 return false; // cannot have more
  91.             }
  92. @@ -497,9 +505,10 @@ class PvpGame implements Game{
  93.                         ) AS friends
  94.             ) AS sel;", MysqlConnection::ALL);
  95.         $names = [];
  96. -       foreach($result as $row){
  97. +       foreach($result as &$row){
  98. +           $row["theirFriendsCnt"] = (int) $row["theirFriendsCnt"];
  99.             $name = $row["name"];
  100. -           if($row["theirFriendsCnt"] >= Settings::kitpvp_maxFriends($row["rank"])){
  101. +           if($row["theirFriendsCnt"] >= Settings::kitpvp_maxFriends((int) $row["rank"])){
  102.                 $name = TextFormat::RED . "$name (full)" . TextFormat::RESET;
  103.             }
  104.             $names[] = $name;
  105. @@ -540,4 +549,9 @@ class PvpGame implements Game{
  106.     public function getPlayerData(){
  107.         return $this->playerData;
  108.     }
  109. +   public function updateAllToMySQL(){
  110. +       foreach($this->playerData as $data){
  111. +           $data->update(false);
  112. +       }
  113. +   }
  114.  }
  115. diff --git a/LegionPE-Core/src/legionpe/games/kitpvp/PvpSessionData.php b/LegionPE-Core/src/legionpe/games/kitpvp/PvpSessionData.php
  116. index f1d391e..b604249 100644
  117. --- a/LegionPE-Core/src/legionpe/games/kitpvp/PvpSessionData.php
  118. +++ b/LegionPE-Core/src/legionpe/games/kitpvp/PvpSessionData.php
  119. @@ -23,6 +23,9 @@ class PvpSessionData{
  120.         $this->origGm = $origGm;
  121.         $this->uid = $uid;
  122.         if(is_array($data)){
  123. +           foreach(array_keys($data) as $key){
  124. +               $data[$key] = (int) $data[$key];
  125. +           }
  126.             $this->data = $data;
  127.             $this->isUpdate = true;
  128.         }
  129. @@ -63,29 +66,30 @@ class PvpSessionData{
  130.     }
  131.     public function close(){
  132.         $this->game->getMain()->getLogger()->debug("KitPvP player data close");
  133. -       var_dump($this);
  134. -       if($this->needSave){
  135. -           $this->game->getMain()->getLogger()->debug("Saving...");
  136. -           if($this->isUpdate){
  137. -               $this->game->getMain()->getLogger()->debug("with UPDATE");
  138. -               $this->game->getMain()->getMySQLi()->query(
  139. -                   "UPDATE kitpvp SET kills = %d, deaths = %d, kit = %d WHERE uid = %d;",
  140. -                   MysqlConnection::RAW,
  141. -                   $this->getKills(), $this->getDeaths(), $this->getKit(), $this->getUID()
  142. -               );
  143. -           }
  144. -           else{
  145. -               $this->game->getMain()->getLogger()->debug("with INSERT");
  146. -               $this->game->getMain()->getMySQLi()->query(
  147. -                   "INSERT INTO kitpvp (uid, kills, deaths, kit) VALUES (%d, %d, %d, %d)",
  148. -                   MysqlConnection::RAW,
  149. -                   $this->getUID(), $this->getKills(), $this->getDeaths(), $this->getKit()
  150. -               );
  151. -           }
  152. +       $this->update();
  153. +   }
  154. +   public function update($force = false){
  155. +       if(!$force and !$this->needSave){
  156. +           return;
  157. +       }
  158. +       if($this->isUpdate){
  159. +           $this->game->getMain()->getLogger()->debug("with UPDATE");
  160. +           $this->game->getMain()->getMySQLi()->query(
  161. +               "UPDATE kitpvp SET kills = %d, deaths = %d, kit = %d WHERE uid = %d;",
  162. +               MysqlConnection::RAW,
  163. +               $this->getKills(), $this->getDeaths(), $this->getKit(), $this->getUID()
  164. +           );
  165.         }
  166.         else{
  167. -           $this->game->getMain()->getLogger()->debug("does not need save");
  168. +           $this->game->getMain()->getLogger()->debug("with INSERT");
  169. +           $this->game->getMain()->getMySQLi()->query(
  170. +               "INSERT INTO kitpvp (uid, kills, deaths, kit) VALUES (%d, %d, %d, %d)",
  171. +               MysqlConnection::RAW,
  172. +               $this->getUID(), $this->getKills(), $this->getDeaths(), $this->getKit()
  173. +           );
  174.         }
  175. +       $this->isUpdate = true;
  176. +       $this->needSave = false;
  177.     }
  178.     /**
  179.      * @return int
  180. diff --git a/LegionPE-Core/src/legionpe/games/parkour/ParkourSessionData.php b/LegionPE-Core/src/legionpe/games/parkour/ParkourSessionData.php
  181. index d587c77..be92f5a 100644
  182. --- a/LegionPE-Core/src/legionpe/games/parkour/ParkourSessionData.php
  183. +++ b/LegionPE-Core/src/legionpe/games/parkour/ParkourSessionData.php
  184. @@ -21,6 +21,9 @@ class ParkourSessionData{
  185.         $mysqli = $this->main->getMySQLi();
  186.         $result = $mysqli->query("SELECT uid, progress, completions FROM parkour WHERE uid = $this->uid;", MysqlConnection::ASSOC);
  187.         if(is_array($result)){
  188. +           foreach(array_keys($result) as $key){
  189. +               $result[$key] = (int) $result[$key];
  190. +           }
  191.             $this->setValuesToArray($result);
  192.         }
  193.         else{
  194. diff --git a/LegionPE-Core/src/legionpe/session/MysqlSession.php b/LegionPE-Core/src/legionpe/session/MysqlSession.php
  195. index 563b528..29df1af 100644
  196. --- a/LegionPE-Core/src/legionpe/session/MysqlSession.php
  197. +++ b/LegionPE-Core/src/legionpe/session/MysqlSession.php
  198. @@ -19,6 +19,9 @@ class MysqlSession{
  199.  //         "SELECT * FROM players WHERE INSTR(names, %s);",
  200.  //         MysqlConnection::ASSOC, $session->getPlayer()->getName() . "|");
  201.         $this->data = $this->session->getMain()->getMySQLi()->query("SELECT * FROM players WHERE primaryname = %s;", MysqlConnection::ASSOC, strtolower($session->getPlayer()->getName()));
  202. +       foreach(["uid", "coins", "lastonline", "registry", "ipconfig"] as $key){
  203. +           $this->data[$key] = (int) $this->data[$key];
  204. +       }
  205.         if(!is_array($this->data)){
  206.             $this->rank = Settings::RANK_IMPORTANCE_DEFAULT | Settings::RANK_PERM_DEFAULT | Settings::RANK_PREC_STD | Settings::shiftTeamLimit(Settings::defaultTeamLimit());
  207.             $session->getMain()->getStats()->increment(LegionPE::TITLE_LEGIONPE_NEW_JOINS);
  208. @@ -26,7 +29,7 @@ class MysqlSession{
  209.         $rk = $this->session->getMain()->getMySQLi()->query(
  210.             "SELECT rank FROM ranks WHERE uid = %d;", MysqlConnection::ASSOC, $this->data["uid"]);
  211.         if(is_array($rk) and isset($rk["rank"])){
  212. -           $this->rank = $rk["rank"];
  213. +           $this->rank = (int) $rk["rank"];
  214.         }
  215.         else{
  216.             $this->rank = Settings::RANK_IMPORTANCE_DEFAULT | Settings::RANK_PERM_DEFAULT | Settings::RANK_PREC_STD | Settings::shiftTeamLimit(Settings::defaultTeamLimit());
  217. @@ -34,8 +37,8 @@ class MysqlSession{
  218.         $prch = $this->session->getMain()->getMySQLi()->query("SELECT * FROM purchases WHERE owner = %d AND expiry >= %d;", MysqlConnection::ALL, $this->data["uid"], time());
  219.         if(is_array($prch)){
  220.             foreach($prch as $p){
  221. -               $purchase = new Purchase($p["primary_id"], $p["product"], $p["expiry"]);
  222. -               $this->purchases[$p["primary_id"]] = $purchase;
  223. +               $purchase = new Purchase((int) $p["primary_id"], (int) $p["product"], (int) $p["expiry"]);
  224. +               $this->purchases[(int) $p["primary_id"]] = $purchase;
  225.             }
  226.         }
  227.     }
  228. @@ -67,6 +70,10 @@ class MysqlSession{
  229.     public function setData(array $data){
  230.         $data["lastonline"] = time();
  231.         $this->data = $data;
  232. +       var_dump($this->data);
  233. +       var_dump($this->session);
  234. +       var_dump($this->session->getUID());
  235. +       var_dump($data["uid"]);
  236.         $result = $this->session->getMain()->getMySQLi()->query("INSERT INTO players
  237.                 (uid, names, hash, coins, lastonline, registry,
  238.                     lastip, histip, ipconfig, ignoring, primaryname)
  239. @@ -77,7 +84,7 @@ class MysqlSession{
  240.                 names = %s, hash = %s, coins = %d, lastonline = %d, registry = %d,
  241.                         lastip = %s, histip = %s, ipconfig = %d, ignoring = %s, primaryname = %s;",
  242.             MysqlConnection::RAW,
  243. -           $data["uid"],
  244. +           (int) $data["uid"],
  245.             $data["names"], $data["hash"], $data["coins"], $data["lastonline"], $data["registry"],
  246.             $data["lastip"], $data["histip"], $data["ipconfig"], $data["ignoring"], $data["primaryname"],
  247.             $data["names"], $data["hash"], $data["coins"], $data["lastonline"], $data["registry"],
  248. diff --git a/LegionPE-Core/src/legionpe/session/Session.php b/LegionPE-Core/src/legionpe/session/Session.php
  249. index a3695d3..7e0febf 100644
  250. --- a/LegionPE-Core/src/legionpe/session/Session.php
  251. +++ b/LegionPE-Core/src/legionpe/session/Session.php
  252. @@ -563,7 +563,7 @@ class Session extends PluginTask implements SessionEvents, ChannelSubscriber{
  253.             $this->mysqlSession->data["ignoring"] = $this->ignoring;
  254.             $this->mysqlSession->setData($this->mysqlSession->data);
  255.         }
  256. -       unset($this->player);
  257. +       unset($this->player); // garbage player
  258.     }
  259.  
  260.     public function h_onPreCmd(PlayerCommandPreprocessEvent $event){
  261. diff --git a/build/LegionPE_Eta.phar b/build/LegionPE_Eta.phar
  262. index 46eae3c..f6bcff4 100644
  263. Binary files a/build/LegionPE_Eta.phar and b/build/LegionPE_Eta.phar differ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement