I have a problem. Server use 2 GB Ram, but it stopped by lacks memory. How to optimize this code? PHP: public function setPlayer($p){ $pos = array("1","2","3","4","5","6","7","8"); $rand = array_rand($pos); $r = $pos[$rand]; if($this->pos[$r] == "0"){ $name = $p->getName(); $this->pos[$r] = "$name"; if($r == 1){ $p->teleport(new Vector3(135, 32, 103)); } if($r == 2){ $p->teleport(new Vector3(135, 33, 139)); } if($r == 3){ $p->teleport(new Vector3(144, 33, 161)); } if($r == 4){ $p->teleport(new Vector3(111, 33, 159)); } if($r == 5){ $p->teleport(new Vector3(89, 33, 120)); } if($r == 6){ $p->teleport(new Vector3(114, 32, 85)); } if($r == 7){ $p->teleport(new Vector3(157, 30, 75)); } if($r == 8){ $p->teleport(new Vector3(172, 35, 120)); } } else { $this->setPlayer($p); } }
Ohh yeah, I didn't even notice. xD But true ↑, that code is like a never-ending loop, that's why the server is stopping.
Okey, thanks. Now right? PHP: public function setPlayer($p){ $rand = array_rand($this->pos_array); $r = $this->pos_array[$rand]; unset($this->pos_array[array_search($r, $this->pos_array)]); if($this->pos[$r] == "0"){ $name = $p->getName(); $this->pos[$r] = "$name"; //unset($this->pos_array[array_search("$r", $this->pos_array)]);//$this->pos_array = array_flip($this->pos_array);unset($this->pos_array[$r]); switch ($r) { case 1: $p->teleport(new Vector3(135, 32, 103)); break; case 2: $p->teleport(new Vector3(135, 33, 139)); break; case 3: $p->teleport(new Vector3(144, 33, 161)); break; case 4: $p->teleport(new Vector3(111, 33, 159)); break; case 5: $p->teleport(new Vector3(89, 33, 120)); break; case 6: $p->teleport(new Vector3(114, 32, 85)); break; case 7: $p->teleport(new Vector3(157, 30, 75)); break; case 8: $p->teleport(new Vector3(172, 35, 120)); break; } } }
Player vasa1 join on pos1, player vasa2 on pos2, player vasa3 on pos3. After vasa1 left to the game. Player John join on pos3, but there was vasa3! PHP: <?php$pos = 8;$pos = $pos-3; //3 players$pos = $pos+1; //+1 player (vasa1)
Sorry for bad English, my main language is Russian) Your code set players on $var++; or $var--; If some players left the game position will empty!
Code: var $posts = [ "1" => [ "x" => "xpos", "y" => "ypos", "z" => "zpos", "player" = null ] "2" => [ "x" => "xpos", "y" => "ypos", "z" => "zpos", "player" = null ] //... ] function tpPos($player){ foreach ($this->$posts as key=>post){ if !$post['player'] instanceof Player OR $player instanceof Player AND isoffline{ $this->posts[$key]['player'] = $player //do stuff like tp since it means there no player on that spot yet break; } } } sorry with the horrible indents i was just me coding on the fly also do not just copy and paste the code but you get the idea and the reasons to not use magic numbers also remember to do something if the array is full like breaking out and tell the player about it calling the function itself wont make it slove itself
I'm sure if everything i right in this if: PHP: if !$post['player'] instanceof Player OR $player instanceof Player AND isoffline{ (Not sure, could be right too) P.S.: You can use [PHP] instead of [CODE] to get synthax highlighting.
well not directly i often just give them the idea in a form of code to do what they wish not the code it self basically on that line check if player is NOT instanceof player OR player is instance of Player (kind of redundant code)AND is offline