rename ->game to tictactoe to support multiple games

This commit is contained in:
Ben Harris 2017-04-28 19:10:14 -04:00
parent 442ba030fd
commit e05fded0a6
2 changed files with 29 additions and 22 deletions

View File

@ -31,7 +31,8 @@ class BenBot extends Discord
public $yomamajokes;
public $copypastas;
public $devbot;
public $game;
public $tictactoe;
public $hangman;
protected $help;
protected $banner;
@ -55,7 +56,8 @@ class BenBot extends Discord
$this->copypastas = explode("---", file_get_contents("$dir/copypasta.txt"));
$this->yomamajokes = file("$dir/yomamajokes.txt");
$this->banner = file_get_contents("{$this->dir}/banner.txt");
$this->game = [];
$this->tictactoe = [];
$this->hangman = [];
try {
$this->defs = new PersistentArray("$dir/bot_data/defs.mp");

View File

@ -12,7 +12,7 @@ class TicTacToe
{
self::$bot = $that;
self::$bot->game[] = [
self::$bot->tictactoe[] = [
'active' => false
];
@ -20,6 +20,11 @@ class TicTacToe
'description' => 'play tic tac toe!',
'usage' => '<@user>',
'registerHelp' => true,
'aliases' => [
'tictactoe',
'tictac',
'ttt',
],
]);
$tic->registerSubCommand('stop', [__CLASS__, 'stopGame'], [
'description' => 'stops current game',
@ -49,7 +54,7 @@ class TicTacToe
return "mention someone who you would like to play with!";
} elseif (count($msg->mentions) === 1) {
$gameid = $msg->channel->id;
self::$bot->game[$gameid] = [
self::$bot->tictactoe[$gameid] = [
'board' => [
[":one:", ":two:", ":three:"],
[":four:", ":five:", ":six:"],
@ -63,9 +68,9 @@ class TicTacToe
'active' => true,
];
foreach ($msg->mentions as $mention) {
self::$bot->game[$gameid]['players'][":o:"] = $mention->id;
self::$bot->tictactoe[$gameid]['players'][":o:"] = $mention->id;
}
Utils::send($msg, self::printBoard($gameid) . "\n<@" . self::$bot->game[$gameid]['players'][self::$bot->game[$gameid]['turn']] . ">, it's your turn!");
Utils::send($msg, self::printBoard($gameid) . "\n<@" . self::$bot->tictactoe[$gameid]['players'][self::$bot->tictactoe[$gameid]['turn']] . ">, it's your turn!");
} else {
return "can't play tictactoe with more than two people!";
}
@ -76,16 +81,16 @@ class TicTacToe
{
$gameid = $msg->channel->id;
return isset(self::$bot->game[$gameid])
&& self::$bot->game[$gameid]['active']
&& $msg->author->id === self::$bot->game[$gameid]['players'][self::$bot->game[$gameid]['turn']];
return isset(self::$bot->tictactoe[$gameid])
&& self::$bot->tictactoe[$gameid]['active']
&& $msg->author->id === self::$bot->tictactoe[$gameid]['players'][self::$bot->tictactoe[$gameid]['turn']];
}
public static function handleMove($msg)
{
$gameid = $msg->channel->id;
$player = self::$bot->game[$gameid]['turn'];
$player = self::$bot->tictactoe[$gameid]['turn'];
$text = $msg->content;
$move = intval($text);
@ -108,14 +113,14 @@ class TicTacToe
if (self::placePieceAt($gameid, $move, $player)) {
if (self::checkWin($gameid)) {
self::$bot->game[$gameid]['active'] = false;
$response = "<@" . self::$bot->game[$gameid]['players'][self::$bot->game[$gameid]['turn']] . "> (" . self::$bot->game[$gameid]['turn'] . ") won!";
} elseif (isset(self::$bot->game[$gameid]['tied']) && self::$bot->game[$gameid]['tied']) {
self::$bot->game[$gameid]['active'] = false;
self::$bot->tictactoe[$gameid]['active'] = false;
$response = "<@" . self::$bot->tictactoe[$gameid]['players'][self::$bot->tictactoe[$gameid]['turn']] . "> (" . self::$bot->tictactoe[$gameid]['turn'] . ") won!";
} elseif (isset(self::$bot->tictactoe[$gameid]['tied']) && self::$bot->tictactoe[$gameid]['tied']) {
self::$bot->tictactoe[$gameid]['active'] = false;
$response = "it's a tie... game over";
} else {
self::$bot->game[$gameid]['turn'] = self::$bot->game[$gameid]['turn'] == ":x:" ? ":o:" : ":x:";
$response = "<@" . self::$bot->game[$gameid]['players'][self::$bot->game[$gameid]['turn']] . ">, it's your turn! (you're " . self::$bot->game[$gameid]['turn'] . "'s)";
self::$bot->tictactoe[$gameid]['turn'] = self::$bot->tictactoe[$gameid]['turn'] == ":x:" ? ":o:" : ":x:";
$response = "<@" . self::$bot->tictactoe[$gameid]['players'][self::$bot->tictactoe[$gameid]['turn']] . ">, it's your turn! (you're " . self::$bot->tictactoe[$gameid]['turn'] . "'s)";
}
} else {
@ -129,7 +134,7 @@ class TicTacToe
{
Utils::deleteMessage($msg);
$gameid = $msg->channel->id;
self::$bot->game[$gameid] = [
self::$bot->tictactoe[$gameid] = [
'active' => false,
];
Utils::send($msg, "game stopped")->then(function ($result) {
@ -170,8 +175,8 @@ class TicTacToe
}
}
if ($tmp) {
self::$bot->game[$gameid]['active'] = false;
self::$bot->game[$gameid]['tied'] = true;
self::$bot->tictactoe[$gameid]['active'] = false;
self::$bot->tictactoe[$gameid]['tied'] = true;
}
return false;
}
@ -181,7 +186,7 @@ class TicTacToe
private static function printBoard($gameid)
{
$response = "";
foreach (self::$bot->game[$gameid]['board'] as $row) {
foreach (self::$bot->tictactoe[$gameid]['board'] as $row) {
foreach ($row as $col) {
$response .= $col;
}
@ -193,7 +198,7 @@ class TicTacToe
private static function getPieceAt($gameid, $i)
{
return self::$bot->game[$gameid]['board'][intval(($i - 1) / 3)][($i - 1) % 3];
return self::$bot->tictactoe[$gameid]['board'][intval(($i - 1) / 3)][($i - 1) % 3];
}
private static function placePieceAt($gameid, $i, $piece)
@ -201,7 +206,7 @@ class TicTacToe
if (self::getPieceAt($gameid, $i) == ":x:" || self::getPieceAt($gameid, $i) == ":o:") {
return false;
} else {
self::$bot->game[$gameid]['board'][intval(($i - 1) / 3)][($i - 1) % 3] = $piece;
self::$bot->tictactoe[$gameid]['board'][intval(($i - 1) / 3)][($i - 1) % 3] = $piece;
return true;
}
}