Bug fixing sequence initiated.

This commit is contained in:
Ad5001 2017-03-05 13:43:55 +01:00
parent a0a5cfb5ec
commit 42dc29ed27
No known key found for this signature in database
GPG Key ID: ADED7C1DE9B1D7C8
8 changed files with 109 additions and 66 deletions

9
README.md Normal file
View File

@ -0,0 +1,9 @@
# HideAndSeek
The original minigame, free, and better than ever !
Do you know the hide and seek? It's a game where some people (the hiders) hide themselfs and then the other people (the seekers) go seek them and have to find them in a limited time.
This is a recreation of this game for pocketmine !
Please note that this is still under development.
Commands:
- /hideandseek <sub command> [parameters]

View File

@ -13,9 +13,12 @@
name: HideAndSeek
author: Ad5001
version: 1.0
api: [3.0.0, 3.0.0-ALPHA1]
api: [3.0.0, 3.0.0-ALPHA3]
main: Ad5001\HideAndSeek\Main
load: POSTWORLD
commands: []
commands:
hideandseek:
description: Main HideAndSeek management command
usage: /hideandseek <sub command> [parameters]
permissions: []
...

View File

@ -19,5 +19,15 @@
Max players: 16
# Seconds to wait between the time 75% of the maximum players have joined and the games starts.
Waiting time: 16
Waiting time: 60
# Minutes seekers will have to find to find all the hiders before they win.
Seeking time: 3
# Percentage of the players that will be seekers.
Seekers percentage: 10
# Parameters below are set for every game and are not changable by commands
# The world players should be tped to when the game ends.
Lobby world: world
...

View File

@ -31,8 +31,7 @@ class DataBase extends SQLite3 {
$table = $data["table"];
unset($data["table"]);
$str = $this->buildQueryArgs($data);
$result = $this->query("SELECT $dataToGet FROM $table WHERE $str");
if($result !== false) $result = $result->fetchArray();
$result = $this->query("SELECT $dataToGet FROM $table$str");
return $result;
}
@ -47,7 +46,7 @@ class DataBase extends SQLite3 {
$table = $data["table"];
unset($data["table"]);
$str = $this->buildQueryArgs($data);
return $this->query("UPDATE $table SET $key = '$value' WHERE $str");
return $this->query("UPDATE $table SET $key = '$value'$str");
}
@ -68,7 +67,7 @@ class DataBase extends SQLite3 {
*/
public function delete(string $table, array $data) {
$str = $this->buildQueryArgs($data);
return $this->query("DELETE FROM $table WHERE $str");
return $this->query("DELETE FROM $table$str");
}
/*
@ -77,9 +76,9 @@ class DataBase extends SQLite3 {
@return string
*/
public function buildQueryArgs(array $data) : string {
$str = "";
$str = " WHERE ";
foreach($data as $k => $d) $str .= "$k = '$d' AND ";
return substr($str, 0, strlen($str) - 5);
return strlen($str) !== 7 ? substr($str, 0, strlen($str) - 5) : "";
}
/*
@ -88,6 +87,7 @@ class DataBase extends SQLite3 {
@return SQLite3Result|bool
*/
public function query($qry) {
echo $qry . ";;";
$res = parent::query($qry);
if($res instanceof \SQLite3Result) self::setNumRows($res);
return $res;

View File

@ -58,10 +58,10 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
*/
public function __construct(Level $level) {
// Initialisation
$this->level = $level;
$this->initDB();
$level->game = $this;
$this->setLevel($level);
$this->registerEvents();
$this->getMain()->getServer()->getPluginManager()->registerEvents($this,$this->getMain());
// Registering players
foreach($this->getLevel()->getPlayers() as $p) {
@ -70,7 +70,7 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
// Loading timer.
parent::__construct($this->getMain());
$this->getMain()->getServer()->getScheduler()->scheduleRepeatingTask($this, 1);
// $this->getMain()->getServer()->getScheduler()->scheduleRepeatingTask($this, 1);
}
/*
@ -158,16 +158,17 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
/*
Inits the database for the game.
*/
public function initDB() {
protected function initDB() {
$qry = $this->getMain()->getDatabase()->get("*", ["table" => "Games", "name" => $this->getName()]);
if(is_array($qry)) {
if(count($qry) == 0) { // Game not initiated in the db.
if($qry instanceof \SQLite3Result) {
if($qry->num_rows == 0) { // Game not initiated in the db.
$id = $this->getMain()->getDatabase()->get("*", ["table" => "Games"]);
$v3 = $this->getLevel()->getSafeSpawn();
$v3Ser = $v3->x . "," . $v3->y . "," . $v3->z; // V32String
$this->getMain()->getDatabase()->insert("Games", [$this->getName(), $v3Ser, $v3Ser, $this->getMain()->getMaxPlayers(), $this->getMain()->getWaitTime(), $this->getMain()->getSeekTime(), $this->getMain()->getSeekersPercentage()]); // Inserting the db with new queries
$this->getMain()->getDatabase()->insert("Games", [$this->getName(), $v3Ser, $v3Ser, $this->getMain()->getMaxPlayers(), $this->getMain()->getWaitTime(), $this->getMain()->getSeekTime(), $this->getMain()->getSeekersPercentage(), $id->num_rows]); // Inserting the db with new queries
}
} else {
throw new Exception("Could not contact database.");
throw new \Exception("Could not contact database.");
}
}
@ -346,7 +347,7 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
*/
public function setLevel(Level $level) {
$this->level = $level;
return $this->getMain()->getDatabase()->set("level", $level->getName(), ["table" => "Games", "name" => $this->getName()]);
return $this->getMain()->getDatabase()->set("name", $level->getName(), ["table" => "Games", "name" => $this->getName()]);
}
/*
@ -391,8 +392,9 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
$this->getMain()->getServer()->getPluginManager()->getPlugin("SpectatorPlus")->isSpectator($player))) { // Support for spectator Plus
$this->spectators[$player->getName()] = $player;
$player->HideAndSeekRole = self::ROLE_SPECTATE;
} elseif($this->step == self::STEP_WAIT) {
} elseif($this->step == self::STEP_WAIT || $this->step == self::STEP_START) {
// API inside player's class (easilier to get data)
$this->sendMessage("§a" . $player->getName() . " joined (" . count($this->players) . "/" . $this->getMaxPlayers() . "). " . (count($this->players) - round($this->getMaxPlayers() * 0.75)) . "players left before starting");
$player->hideAndSeekGame = $this;
$player->HideAndSeekRole = self::ROLE_WAIT;
$player->playsHideAndSeek = true;
@ -470,9 +472,13 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
@param $event \pocketmine\event\entity\EntityLevelChangeEvent
*/
public function onEntityLevelChange(\pocketmine\event\entity\EntityLevelChangeEvent $event) {
if($event->getTarget()->getName() == $this->getName() && $event->getEntity instanceof Player) {
if($event->getTarget()->getName() == $this->getName() && $event->getEntity() instanceof Player) {
if(count($this->players) >= $this->getMaxPlayers()) {
$event->setCancelled();
$event->getEntity()->sendMessage(Main::PREFIX . "§cThe maximum number of players in this game has been reached.");
}
$this->registerPlayer($event->getEntity());
} elseif($event->getOrigin()->getName() == $this->getName() && $event->getEntity instanceof Player) {
} elseif($event->getOrigin()->getName() == $this->getName() && $event->getEntity() instanceof Player) {
$this->unregisterPlayer($event->getEntity());
}
}
@ -482,7 +488,7 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
@param $event \pocketmine\event\block\BlockBreakEvent
*/
public function onBlockBreak(\pocketmine\event\block\BlockBreakEvent $event) {
if($event->getPlayer()->getLevel() == $this->getName()) {
if($event->getLevel()->getLevel() == $this->getName()) {
$event->setCancelled();
}
}
@ -492,7 +498,7 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
@param $event \pocketmine\event\block\BlockPlaceEvent
*/
public function onBlockPlace(\pocketmine\event\block\BlockPlaceEvent $event) {
if($event->getPlayer()->getLevel() == $this->getName()) {
if($event->getLevel()->getLevel() == $this->getName()) {
$event->setCancelled();
}
}
@ -516,6 +522,19 @@ class Game extends PluginTask /* Allows easy game running */ implements Listener
}
}
/*
Checks when a player dies to prevent it.
@param $event \pocketmine\event\player\PlayerDeathEvent
*/
public function onPlayerDeath(\pocketmine\event\player\PlayerDeathEvent $event) {
if($event->getPlayer()->getLevel() == $this->getName()) {
$event->setCancelled();
}
}
/*
Checks when a player joins in the world to make him rejoin automaticly
@param $event \pocketmine\event\player\PlayerJoinEvent

View File

@ -141,9 +141,10 @@ class GameManager {
$result = $db->get("*", ["table" => "Games"]);
if($result !== false) {
while($row = $result->fetchArray()) {
if(is_null($row["name"])) return;
if(is_null($this->gamesName[$row["name"]]) && ($lvl = $this->getMain()->getServer()->getLevelByName($row["name"])) !== null) { // Game doesn't exists && level is loaded
$this->games[$row["id"]] = new Game($lvl);
$this->gamesNames[$row["name"]] = $row["id"];
$this->games[$row["Id"]] = new Game($lvl);
$this->gamesNames[$row["name"]] = $row["Id"];
$this->getMain()->getLogger()->notice("Succefully registered game level {$row['name']}.");
}
}

View File

@ -36,7 +36,7 @@ class Main extends PluginBase implements Listener {
//Database setup
$this->db = new DataBase($this->getDatafolder() . "base.db");
if($this->db->query("PRAGMA table_info(Games);")->num_rows == 0) {
$this->db->query(<<<A
$this->db->exec(<<<A
CREATE TABLE Games (
name VARCHAR( 128 ) NOT NULL,
spawnpoint VARCHAR( 128 ) NOT NULL,
@ -44,7 +44,8 @@ CREATE TABLE Games (
max_players INT NOT NULL,
waiting_time INT NOT NULL,
seek_time INT NOT NULL,
seekers_percentage INT NOT NULL
seekers_percentage INT NOT NULL,
Id INT PRIMARY KEY
)
A
);
@ -70,14 +71,14 @@ A
case "creategame":
case "addgame":
if($sender->getLevel()->getName() == $this->getLobbyWorld()->getName()) {
$sender->sendMessage(self::PREFIX . "§4Could not create game ! You're in the lobby level.");
$sender->sendMessage(self::PREFIX . "§cCould not create game ! You're in the lobby level.");
return true;
} elseif(!is_null($this->getGameManager()->getGameByLevel($sender->getLevel()))) {
$sender->sendMessage(self::PREFIX . "§4This level is already an hide and seek game.");
$sender->sendMessage(self::PREFIX . "§cThis level is already an hide and seek game.");
return true;
} else {
$this->getGameManager()[] = new Game($sender->getLevel());
$sender->sendMessage(self::PREFIX . "§4Succefully created hide and seek game in level {$sender->getLevel()->getName()}.");
$this->getGameManager()->hey = new Game($sender->getLevel()); // Doesn't care 'bout the name set. It customùly sets it.
$sender->sendMessage(self::PREFIX . "§cSuccefully created hide and seek game in level {$sender->getLevel()->getName()}.");
return true;
}
break;
@ -85,11 +86,11 @@ A
case "delgame":
$game = $this->getGameManager()->getGameByLevel($sender->getLevel());
if(!is_null($game)) {
unset($this->getGameManager()[$sender->getLevel()->getName()]);
$sender->sendMessage(self::PREFIX . "§4Succefully deleted hide and seek game in level {$sender->getLevel()->getName()}.");
unset($this->getGameManager()->{$sender->getLevel()->getName()});
$sender->sendMessage(self::PREFIX . "§cSuccefully deleted hide and seek game in level {$sender->getLevel()->getName()}.");
return true;
} else {
$sender->sendMessage(self::PREFIX . "§4You're not in an hide and seek game world.");
$sender->sendMessage(self::PREFIX . "§cYou're not in an hide and seek game world.");
}
break;
case "setmaxplayers":
@ -98,13 +99,13 @@ A
if(!is_null($game)) {
if(isset($args[1]) && is_int($args[1]) && $args[1] > 1) {
$game->setMaxPlayers($args[1]);
$sender->sendMessage(self::PREFIX . "§4Succefully set maximum amount of players of hide and seek game in level {$sender->getLevel()->getName()} to {$args[1]}.");
$sender->sendMessage(self::PREFIX . "§cSuccefully set maximum amount of players of hide and seek game in level {$sender->getLevel()->getName()} to {$args[1]}.");
} else {
$sender->sendMessage("§4Usage: /hideandseek setmaxplayers <max amount>");
$sender->sendMessage("§cUsage: /hideandseek setmaxplayers <max amount>");
}
return true;
} else {
$sender->sendMessage(self::PREFIX . "§4You're not in an hide and seek game world.");
$sender->sendMessage(self::PREFIX . "§cYou're not in an hide and seek game world.");
}
break;
case "setseekerspercentage":
@ -113,13 +114,13 @@ A
if(!is_null($game)) {
if(isset($args[1]) && is_int($args[1]) && $args[1] > 0 && $args[1] < 100) {
$game->setSeekersPercentage($args[1]);
$sender->sendMessage(self::PREFIX . "§4Succefully set seekers percentage of hide and seek game in level {$sender->getLevel()->getName()} to {$args[1]}.");
$sender->sendMessage(self::PREFIX . "§cSuccefully set seekers percentage of hide and seek game in level {$sender->getLevel()->getName()} to {$args[1]}.");
} else {
$sender->sendMessage("§4Usage: /hideandseek setseekerspercentage <percentage>");
$sender->sendMessage("§cUsage: /hideandseek setseekerspercentage <percentage>");
}
return true;
} else {
$sender->sendMessage(self::PREFIX . "§4You're not in an hide and seek game world.");
$sender->sendMessage(self::PREFIX . "§cYou're not in an hide and seek game world.");
}
break;
case "setwaitingtime":
@ -129,13 +130,13 @@ A
if(!is_null($game)) {
if(isset($args[1]) && is_int($args[1]) && $args[1] > 0) {
$game->setWaitTime($args[1]);
$sender->sendMessage(self::PREFIX . "§4Succefully set waiting time of hide and seek game in level {$sender->getLevel()->getName()} to {$args[1]}.");
$sender->sendMessage(self::PREFIX . "§cSuccefully set waiting time of hide and seek game in level {$sender->getLevel()->getName()} to {$args[1]}.");
} else {
$sender->sendMessage("§4Usage: /hideandseek setwaittime <seconds to wait>");
$sender->sendMessage("§cUsage: /hideandseek setwaittime <seconds to wait>");
}
return true;
} else {
$sender->sendMessage(self::PREFIX . "§4You're not in an hide and seek game world.");
$sender->sendMessage(self::PREFIX . "§cYou're not in an hide and seek game world.");
}
break;
case "setseektime":
@ -144,13 +145,13 @@ A
if(!is_null($game)) {
if(isset($args[1]) && is_int($args[1]) && $args[1] > 0) {
$game->setSeekTime($args[1]);
$sender->sendMessage(self::PREFIX . "§4Succefully set seeking time of hide and seek game in level {$sender->getLevel()->getName()} to {$args[1]}.");
$sender->sendMessage(self::PREFIX . "§cSuccefully set seeking time of hide and seek game in level {$sender->getLevel()->getName()} to {$args[1]}.");
} else {
$sender->sendMessage("§4Usage: /hideandseek setseektime <minutes of seeking>");
$sender->sendMessage("§cUsage: /hideandseek setseektime <minutes of seeking>");
}
return true;
} else {
$sender->sendMessage(self::PREFIX . "§4You're not in an hide and seek game world.");
$sender->sendMessage(self::PREFIX . "§cYou're not in an hide and seek game world.");
}
break;
case "setspawn":
@ -158,23 +159,11 @@ A
$pos = new \pocketmine\math\Vector3($sender->x, $sender->y, $sender->z);
$game = $this->getGameManager()->getGameByLevel($sender->getLevel());
if(!is_null($game)) {
$game->setSpawn($args[1]);
$sender->sendMessage(self::PREFIX . "§4Succefully set spawn of hide and seek game in level {$sender->getLevel()->getName()} to x: $pos->x, y: $pos->y, z: $pos->z.");
$game->setSpawn($pos);
$sender->sendMessage(self::PREFIX . "§cSuccefully set spawn of hide and seek game in level {$sender->getLevel()->getName()} to x: $pos->x, y: $pos->y, z: $pos->z.");
return true;
} else {
$sender->sendMessage(self::PREFIX . "§4You're not in an hide and seek game world.");
}
break;
case "setspawn":
case "ss":
$pos = new \pocketmine\math\Vector3($sender->x, $sender->y, $sender->z);
$game = $this->getGameManager()->getGameByLevel($sender->getLevel());
if(!is_null($game)) {
$game->setSpawn($args[1]);
$sender->sendMessage(self::PREFIX . "§4Succefully set spawn of hide and seek game in level {$sender->getLevel()->getName()} to x: $pos->x, y: $pos->y, z: $pos->z.");
return true;
} else {
$sender->sendMessage(self::PREFIX . "§4You're not in an hide and seek game world.");
$sender->sendMessage(self::PREFIX . "§cYou're not in an hide and seek game world.");
}
break;
case "setseekersspawn":
@ -182,13 +171,25 @@ A
$pos = new \pocketmine\math\Vector3($sender->x, $sender->y, $sender->z);
$game = $this->getGameManager()->getGameByLevel($sender->getLevel());
if(!is_null($game)) {
$game->setSeekersSpawn($args[1]);
$sender->sendMessage(self::PREFIX . "§4Succefully set seekers spawn of hide and seek game in level {$sender->getLevel()->getName()} to x: $pos->x, y: $pos->y, z: $pos->z.");
$game->setSeekersSpawn($pos);
$sender->sendMessage(self::PREFIX . "§cSuccefully set seekers spawn of hide and seek game in level {$sender->getLevel()->getName()} to x: $pos->x, y: $pos->y, z: $pos->z.");
return true;
} else {
$sender->sendMessage(self::PREFIX . "§4You're not in an hide and seek game world.");
$sender->sendMessage(self::PREFIX . "§cYou're not in an hide and seek game world.");
}
break;
default:
$sender->sendMessage(str_ireplace(PHP_EOL, PHP_EOL . self::PREFIX,self::PREFIX. "§cSub-command {$args[0]} not found !
Possible subcommands:
- creategame (or addgame): Creates a hide and seek
- deletegame (or delgame): Deletes the hide and seek
- setmaxplayers <number of players>(or smp): Sets the maximum number of players
- setseekerspercentage <percentage>(or ssp): Sets the percentage of players that will be seekers
- setwaittime <seconds to wait>(or swt): Sets the waiting time of players when 75 percents of the maximum players joined and the game starts
- setseektime <minutes to seek>(or sst): Sets the time seekers have to find all hiders before hiders wins
- setspawn(or ss): Sets the spawn of the place players will wait, hide, and seek
- setseekersspawn(or sss): Sets the place where players will be tped to while hiders are hiding
Please note that all those subcommands are relative to the world where you execute the command in."));
}
return true;
} else {
@ -279,6 +280,6 @@ A
@param $event \pocketmine\event\level\LevelLoadEvent
*/
public function onLevelLoad(\pocketmine\event\level\LevelLoadEvent $event) {
$this->getGameManager()->refreshRegisterGames();
$this->getGameManager()->refreshRegisterGames($this->getDatabase());
}
}

View File

@ -14,7 +14,7 @@
namespace Ad5001\HideAndSeek\tasks;
use pocketmine\Server;
use pocketmine\schedulerPluginTask;
use pocketmine\scheduler\PluginTask;
use pocketmine\Player;
use Ad5001\HideAndSeek\Main;