Skip to content

Commit

Permalink
Merge pull request #57 from GrumpyMurloc/master
Browse files Browse the repository at this point in the history
Adding weather effect sytem (#51)
  • Loading branch information
chrisgillis authored Jan 29, 2017
2 parents ff40ed6 + 5dc1ed5 commit 7b33ca0
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 13 deletions.
26 changes: 22 additions & 4 deletions src/Game/Command/WeatherCommand.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php namespace Slackwolf\Game\Command;

/*
use Exception;
use Slack\Channel;
use Slack\ChannelInterface;
use Slackwolf\Game\Formatter\GameStatusFormatter;
use Slackwolf\Game\Game;

*/
/**
* Defines the WeatherCommand class.
*/
Expand All @@ -14,7 +14,7 @@ class WeatherCommand extends Command

/**
* {@inheritdoc}
*/
*/
public function fire()
{
$client = $this->client;
Expand All @@ -26,8 +26,26 @@ public function fire()
});
return;
}
if ($weather != null){

if($weather == 1){
$this->gameManager->sendMessageToChannel($this->game, ":rain_cloud: It is raining. It is a cold rain, and the freezing drops chill you to the bone." );
}

$this->gameManager->sendMessageToChannel($this->game, ":rain_cloud: It is raining. It is a cold rain, and the freezing drops chill you to the bone." );
// Cloudy
else if($weather == 2){
$this->gameManager->sendMessageToChannel($this->game, ":cloud: The cloud embrace the sky and cover the sun letting only a few glimmer of light");
}

// Sunny
else{
$this->gameManager->sendMessageToChannel($this->game, ":sunny: The warm sun is shining. Its brightness almost blinds you. You take a moment to appreciate its embrace.");
}

}

else{
$this->gameManager->sendMessageToChannel($this->game,"No Game Running");
}
}
}
3 changes: 3 additions & 0 deletions src/Game/Data/day_weather.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:sunrise: The sun rises and the villagers awake. It is still raining, but it slows somewhat, allowing momentary respite from the cold, wet hell that we all live in.
:cloud: There is no sky today, only a thick layer of cloud blocking the sky. The air was cooler, announcing rain in the day to come.
:sunny: The morning came and the sun, high in the sky, gave hope that one day this madness will end.
3 changes: 3 additions & 0 deletions src/Game/Data/first_weather.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:moon: :rain_cloud: The rain comes down in torrents as the village sleeps, unaware of the horror that lurks outside in the wet. It is the middle
:moon: :cloud: The cloud covered the sky blocking even the few glimmering light.
:moon: The village sleeps, unaware of the horror that lurks outside in the dark. It is the middle of the night.
3 changes: 3 additions & 0 deletions src/Game/Data/night_weather.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:moon: :zzz: The sun sets, and the hard rain makes it difficult to hear anything outside. Villagers bar their doors, take long pulls of :beer:, and try not to think of what might lurk beyond the feeble candlelight.
:moon: :fog: A thick fog covered the village blocking all of the light. The Villagers bar their door waiting without rest until sunrise.
:full_moon: The sun set, and the moon lights up the sky giving a glimmer of hope. The Villagers bar their doors waiting in fear until sunrise.
122 changes: 122 additions & 0 deletions src/Game/Formatter/WeatherFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php namespace Slackwolf\Game\Formatter;

use Slackwolf\Game\Game;
use Slackwolf\Game\GameState;

/**
* Defines the WeatherFormatter class.
*/
class WeatherFormatter
{


/**
* @param $weightedValues
*
* @return string
*
* Utility function for getting random values with weighting.
* Pass in an associative array, such as array('A'=>5, 'B'=>45, 'C'=>50)
* An array like this means that "A" has a 5% chance of being selected, "B" 45%, and "C" 50%.
*/

public static function getWeather(array $weightedValues) {

$rand = mt_rand(1, (int) array_sum($weightedValues));

foreach ($weightedValues as $key => $value) {

$rand -= $value;
if ($rand <= 0) {

return $key;

}
}
}

/**
* @param $game
*
* @return string
*/

public static function format(Game $game)
{
// Send message according to GameState and weather
$state = $game->state;
/**
* Create an associate array with the value as weight.
* To modify the weight, just change the value with the integer.
* Raining = 25%, cloudy = 50%, sunny= 25%
*/
$weight = array(
"rainy" => 25,
"cloudy" => 50,
"sunny" => 25,
);

$Weather = WeatherFormatter::getWeather($weight);
$messages = array(":rain_cloud:",":cloud:",":sunny:");

if ($state == GameState::FIRST_NIGHT){

if (file_exists("src/Game/Data/first_weather.txt")){
$messages = file("src/Game/Data/first_weather.txt");
}

if ($Weather == "rainy"){
$msg = $messages[0];
}

else if ($Weather == "cloudy"){
$msg = $messages[1];
}

else{
$msg = $messages[2];
}

}

if ($state == GameState::DAY){
if (file_exists("src/Game/Data/day_weather.txt")){
$messages = file("src/Game/Data/day_weather.txt");
}

if ($Weather == "rainy"){
$msg = $messages[0];
}

else if ($Weather == "cloudy"){
$msg = $messages[1];
}

else{
$msg = $messages[2];
}

}

if ($state == GameState::NIGHT){
if (file_exists("src/Game/Data/night_weather.txt")){
$messages = file("src/Game/Data/night_weather.txt");
}

if ($Weather == "rainy"){
$msg = $messages[0];
}

else if ($Weather == "cloudy"){
$msg = $messages[1];
}

else{
$msg = $messages[2];
}

}

return $msg;
}
}
3 changes: 3 additions & 0 deletions src/Game/Formatter/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:moon: :rain_cloud: The rain comes down in torrents as the village sleeps, unaware of the horror that lurks outside in the wet. It is the middle
:moon: :cloud: The cloud covered the sky blocking even the few glimmering light.
:moon: The village sleeps, unaware of the horror that lurks outside in the dark. It is the middle of the night.
2 changes: 1 addition & 1 deletion src/Game/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Game
public $wolvesVoted;
public $witchHealed;
public $witchPoisoned;


/**
* @param $id
Expand Down Expand Up @@ -605,5 +606,4 @@ public function getWitchPoisonedUserId() {
public function setWitchPoisonedUserId($id) {
$this->witchPoisonedUserId = $id;
}

}
14 changes: 7 additions & 7 deletions src/Game/GameManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Slackwolf\Game\Formatter\RoleListFormatter;
use Slackwolf\Game\Formatter\RoleSummaryFormatter;
use Slackwolf\Game\Formatter\VoteSummaryFormatter;
use Slackwolf\Game\Formatter\WeatherFormatter;
use Slackwolf\Message\Message;
use Slackwolf\Game\OptionsManager;
use Slackwolf\Game\OptionName;
Expand Down Expand Up @@ -467,16 +468,16 @@ private function onFirstNight(Game $game)
}
});
}

;
$playerList = PlayerListFormatter::format($game->getLivingPlayers());
$roleList = RoleListFormatter::format($game->getLivingPlayers());

$msg = ":wolf: It is raining, and a new game of Werewolf is starting! For a tutorial, type !help.\r\n\r\n";
$msg = ":wolf: A new game of Werewolf is starting! For a tutorial, type !help.\r\n\r\n";
$msg .= "Players: {$playerList}\r\n";
$msg .= "Possible Roles: {$game->getRoleStrategy()->getRoleListMsg()}\r\n\r\n";

$msg .= WeatherFormatter::format($game)."\r\n";
if ($this->optionsManager->getOptionValue(OptionName::role_seer)) {
$msg .= ":moon: :rain_cloud: The rain comes down in torrents as the village sleeps, unaware of the horror the lurks outside in the wet. It is the middle of the night.";

$msg .= " The game will begin when the Seer chooses someone.";
}
$this->sendMessageToChannel($game, $msg);
Expand All @@ -492,8 +493,7 @@ private function onFirstNight(Game $game)
private function onDay(Game $game)
{
$remainingPlayers = PlayerListFormatter::format($game->getLivingPlayers());

$dayBreakMsg = ":sunrise: The sun rises and the villagers awake. It is still raining, but it slows somewhat, allowing momentary respite from the cold, wet hell that we all live in.\r\n";
$dayBreakMsg = WeatherFormatter::format($game)."\r\n";
$dayBreakMsg .= "Remaining Players: {$remainingPlayers}\r\n\r\n";
$dayBreakMsg .= "Villagers, find the Werewolves! Type !vote @username to vote to lynch a player.";
if ($this->optionsManager->getOptionValue(OptionName::changevote))
Expand All @@ -514,7 +514,7 @@ private function onDay(Game $game)
private function onNight(Game $game)
{
$client = $this->client;
$nightMsg = ":moon: :zzz: The sun sets, and the hard rain makes it difficult to hear anything outside. Villagers bar their doors, take long pulls of :beer:, and try not to think of what might lurk beyond the feeble candlelight. ";
$nightMsg = WeatherFormatter::format($game);
$this->sendMessageToChannel($game, $nightMsg);

$wolves = $game->getWerewolves();
Expand Down
2 changes: 1 addition & 1 deletion src/Slackwolf.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function run()
'alive' => AliveCommand::class,
'dead' => DeadCommand::class,
'status' => StatusCommand::class,
'weather' => WeatherCommand::class,
// 'weather' => WeatherCommand::class,
];

/*
Expand Down

0 comments on commit 7b33ca0

Please sign in to comment.