Hey Guys.....All of you know, how to save (for a short time) numbers...Like this... PHP: public $test = 10; And you can change the number to a other number... So....You also know, how to save Playernames for a short time in the Code.....like... PHP: public $players = []; ...So now my Question... How I can combinate this.... So how I can save (for a short time) in the Code the Playername with the Coins from the Player....
Thats called "Multidimensional arrays" PHP: public $players["Steve" => 23, "Alex" => 382]; To get the coins of a player do: PHP: $this->players["Steve"]; It returns 23.
PHP: $this->players["NewPlayerName"] = $this->players["OldPlayerName"];unset($this->player["OldPlayerName"]);
Straightforward answer to your question. PHP: public $players = [];public function setCoins(Player $player, int $amount) { $this->players[$player->getName()] = $amount;}public function getCoins(Player $player) { return $this->players[$player->getName()];} Now you can do things like: PHP: $this->setCoins($player, $this->getCoins($player) + 1); // Adds 1 to the players coin amount.
PHP: arsort($this->players);$secondBestName = array_keys($this->players)[1];$secondBestAmount = array_values($this->players)[1];$bestFive = array_slice($this->players, 0, 5, true);$number = 1;foreach($bestFive as $name => $amount){ echo "#$number: $name (\$$amount)", PHP_EOL; $number++;} Test run: http://bit.ly/1Uowltk
I think that a for loop should be faster and more efficient in this case. http://php.net/manual/en/control-structures.for.php And also, what if he's not organizing his players array as you? I don't think your method will be the best idea, it totally depends on how his code is. @PEMapModder won this time.
The nostalgia got to you, didn't it? I don't see @PEMapModder anywhere here, only @SOFe. But how much would the difference be when it comes to speed? How much more efficient? How many more lines?
Off-topic alarm. You may become new @PEMapModder, or you already are? Anyway good work creating these great posts from which even I can learn new things Back on-topic This is nohow related with PocketMine plugin development except that plugins are in written in php which obviously you don't know well. Please learn that first, it's essential in creating plugins, seriously just take this day and go through CodeAcademy PHP courses or DerekBanas video and you'll be good to go
I don't get what you mean. More effectively, do it like this: PHP: private $top10 = array();function onDataUpdate($playerName, $newAmount){ $this->top10[$playerName] = $newAmount; arsort($this->top10); if(count($this->top10) > 10){ $this->top10 = array_slice($this->top10, 0, 10, true); }} In this way, you don't need to recalculate a lot of data every query, but sort a really small amount of data every update.
No. Execute this every time any data are changed. Check $this->top10 when you want to query the top data.
Your given help/tips/info are really helpful and you're active as @PEMapModder was once. I'm getting little suspicious. Maybe @PEMapModder decided to return in new form (profile). I'd wish that would be true. P.S. If something sounds insane, well that's because of my grammar (English isn't my native language)