time emojis and hangman basic functionality

This commit is contained in:
Ben Harris 2017-05-01 11:43:22 -04:00
parent 93cc7cab8f
commit 6ba0ab4446
2 changed files with 67 additions and 12 deletions

View File

@ -3,6 +3,8 @@ namespace BenBot\Commands;
use BenBot\Utils;
use function Stringy\create as s;
final class Hangman
{
private static $bot;
@ -34,16 +36,30 @@ final class Hangman
public static function isGameOriginator($msg)
{
return Utils::isDM($msg)
&& self::$bot->hangman['readygame']['originator']->id === $msg->author->id;
&& self::$bot->hangman['readymsg']->author->id === $msg->author->id;
}
public static function initGameWithWord($msg)
{
$gameid = self::$bot->hangman['readygame']['gameid'];
$gameid = self::$bot->hangman['readymsg']->channel->id;
self::$bot->hangman[$gameid]['secret_word'] = $msg->content;
self::$bot->hangman[$gameid]['active'] = true;
Utils::send(self::$bot->hangman['readygame']['origmsg'], "Game ready\n```" . self::$gallows . "```");
Utils::send(self::$bot->hangman['readymsg'], self::showGameState($gameid));
self::$bot->hangman['readymsg'] = null;
}
public static function handleMove($msg)
{
$gameid = $msg->channel->id;
if (strlen($msg->content) === 1) {
self::$bot->hangman[$gameid]['guessed_letters'][] = $msg->content;
if (!in_array($msg->content, str_split(self::$bot->hangman[$gameid]['secret_word']))) {
self::$bot->hangman[$gameid]['state']++;
}
Utils::send($msg, self::showGameState($gameid));
}
}
@ -53,12 +69,9 @@ final class Hangman
self::$bot->hangman[$gameid] = [
'active' => false,
'state' => 0,
'guessed_letters' => [],
];
self::$bot->hangman['readygame'] = [
'originator' => $msg->author,
'gameid' => $gameid,
'origmsg' => $msg,
];
self::$bot->hangman['readymsg'] = $msg;
$msg->author->user->sendMessage("enter the secret word")->otherwise(function ($e) {
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
@ -66,4 +79,24 @@ final class Hangman
return "waiting for {$msg->author} to enter a word";
}
private static function showSecretWord($gameid)
{
$ret = "Word: ";
foreach (s(self::$bot->hangman[$gameid]['secret_word']) as $char) {
$ret .= in_array($char, self::$bot->hangman[$gameid]['guessed_letters']) ? $char : "_";
$ret .= " ";
}
return $ret;
}
private static function showGameState($gameid)
{
return "```" . self::$gallows[self::$bot->hangman[$gameid]['state']] . "\n" .
self::showSecretWord($gameid) . "\n" .
"Incorrect letters: " . implode(" ", array_diff(self::$bot->hangman[$gameid]['guessed_letters'], str_split(self::$bot->hangman[$gameid]['secret_word']))) . "\n" .
"Guessed letters: " . implode(" ", self::$bot->hangman[$gameid]['guessed_letters']) . "```";
}
}

View File

@ -29,6 +29,7 @@ final class Time
echo __CLASS__ . " registered", PHP_EOL;
}
public static function getUserTime($msg, $args)
{
$id = Utils::getUserIDFromMsg($msg);
@ -37,9 +38,12 @@ final class Time
// look up the user's time
if (isset(self::$bot->cities[$id])) {
$city = self::$bot->cities[$id];
return "It's " . Carbon::now($city["timezone"])->format('g:i A \o\n l F j, Y') . " in {$city["city"]}.";
return self::sayTime($city);
} else {
return "It's " . Carbon::now()->format('g:i A \o\n l F j, Y') . " Eastern Time (USA).\nyou can set a preferred city with `;time save <city>` or `;weather save <city>`";
return self::sayTime([
'timezone' => 'America/Detroit',
'city' => 'Eastern Time (USA)'
]) . "\n you can set a preferred city with `;time save <city>`";
}
} else {
@ -48,7 +52,7 @@ final class Time
foreach ($msg->mentions as $mention) {
if (isset(self::$bot->cities[$mention->id])) {
$city = self::$bot->cities[$mention->id];
return "It's " . Carbon::now($city["timezone"])->format('g:i A \o\n l F j, Y') . " in {$city["city"]}.";
return self::sayTime($city);
} else {
return "No city found for {$mention}.\nset a preferred city with `;time save <city> <@user>`";
}
@ -66,7 +70,10 @@ final class Time
$geonamesurl = "http://api.geonames.org/timezoneJSON?username=benharri&lat={$coord->lat}&lng={$coord->lon}";
self::$bot->http->get($geonamesurl)->then(function ($json) use ($msg, $weatherjson) {
Utils::send($msg, "It's " . Carbon::now($json->timezoneId)->format('g:i A \o\n l F j, Y') . " in {$weatherjson->name}.");
Utils::send($msg, self::sayTime([
'timezone' => $json->timezoneId,
'city' => $weatherjson->name
]));
});
});
}
@ -74,4 +81,19 @@ final class Time
}
public static function sayTime($city)
{
$time = Carbon::now($city['timezone']);
return "It's " . $time->format('g:i A \o\n l F j, Y') . " in {$city["city"]}. " . self::clockEmojiForTime($time);
}
public static function clockEmojiForTime(Carbon $emojitime)
{
$hour = $emojitime->hour % 12;
$minute = $emojitime->minute >= 30 ? "30" : "";
return ":clock$hour$minute:";
}
}