Beispiel #1
0
 public Game(int id, Player overlord)
 {
     Id = id;
     Overlord = overlord;
     MyPlayers.Add(overlord);
     IsInProgress = false;
     Summary = new Summary(this);
 }
 public void Cancel(Player player, Action action, Game game)
 {
     if (game.IsOverlord(player))
     {
         gameLog.Info("Cancelling " + game);
         RemoveGame(game);
         Gmail.MessagePlayer(player, game, game.Title + " has been cancelled.");
     }
 }
Beispiel #3
0
 public static List<Player> GenListOfPlayers(int size)
 {
     List<Player> players = new List<Player>();
     for (int i = 0; i < size; i++)
     {
         string name = "Player" + i;
         string address = "Player" + i + Program.UnitTestAddress;
         Player player = new Player(name, address);
         players.Add(player);
     }
     return players;
 }
Beispiel #4
0
 /// <summary>
 /// Sends a message to the given player listing all their available commands for the given game.
 /// </summary>
 /// <param name="game"></param>
 /// <param name="player"></param>
 /// <returns></returns>
 public static string Help(this Game game, Player player)
 {
     string commands = "<ul>";
     if (player.IsAlive && game.IsInProgress && game.ActiveCycle == Game.Cycle.Day) commands += "<li>To vote for someone to be cast out: <b>Vote</b> <i>name</i></li>";
     if (game.IsOverlord(player) && !game.IsInProgress) commands += "<li>To start the game: <b>Start</b>.</li>";
     if (game.IsOverlord(player)) commands += "<li>To cancel the game: <b>Cancel</b></li>";
     if (game.IsOverlord(player)) commands += "<li>To kick a player from the game: <b>Kick</b> <i>name</i></li>";
     if (game.IsOverlord(player)) commands += "<li>To ban a player from the game: <b>Ban</b> <i>name</i></li>";
     if (player.IsAlive && !game.IsOverlord(player)) commands += "<li>To quit the game: <b>Quit</b></li>";
     commands += "<li>To see the game status: <b>Status</b></li>";
     commands += "</ul>";
     commands += "<br />To use a command, reply to this email as indicated above.";
     return commands;
 }
Beispiel #5
0
 public void AddPlayer(Player player)
 {
     MyPlayers.Add(player);
 }
        private List<Action> ParseActions(SimpleMessage message, Player player, Game game)
        {
            List<Action> actions = new List<Action>();

            foreach (string line in message.BodyAsLines())
            {
                Action action = null;
                if (player != null && game != null && game.IsInProgress)
                {
                    action = ParseGameAction(line, player, game);
                }
                if (action == null)
                {
                    action = ParseSystemAction(line);
                }
                if (action != null) actions.Add(action);
            }

            return actions;
        }
 public void PerformAction(Game game, Player player, Action action)
 {
     if (action.Type == ActionEnum.JoinAs)
     {
         //If JoinAs gets hit here, it's only because the player is already playing in a different game
         Game g = GetGameByPlayer(player);
         if (g != null) HandleBadAction(g, player, action, "Failed to join " + game.Title + ". You are already playing in " + g.Title + ".");
     }
     else if (action.Type == ActionEnum.Status) Status(player, action, game);
     else if (action.Type == ActionEnum.Cancel) Cancel(player, action, game);
     else if (action.Type == ActionEnum.Start) Start(player, action, game);
     else if (action.Type == ActionEnum.Quit) Quit(player, action, game);
     else if (action.Type == ActionEnum.Vote && game.ActiveCycle == Game.Cycle.Day) Vote(player, action, game);
     else if (action.Type == ActionEnum.Help) Help(player, action, game);
     else if (action.Type == ActionEnum.Role) RoleAction(player, action, game);
     //TODO Implement Kick & Ban
     //if (action.Name == ActionEnum.Kick) Kick(player, action, game);
     //if (action.Name == ActionEnum.Ban) Ban(player, action, game);
 }
 private void CreateTheGame(Player player)
 {
     SendAction(null, player, player, "join as");
 }
Beispiel #9
0
 private string ListTeammates(Player player)
 {
     string message = "";
     if (player.Team.KnowsTeammates)
     {
         message += FlavorText.Divider + "Teammates:".li();
         List<string> teammates = new List<string>();
         foreach (Player p in Players)
         {
             if (!player.Equals(p) && player.Team.Equals(p.Team)) teammates.Add(p.Name.b());
         }
         if (teammates.Count == 0) teammates.Add("You have no teammates.");
         message += teammates.HtmlBulletList();
         message = message.tag("ul");
     }
     return message;
 }
Beispiel #10
0
 /// <summary>
 /// Assigns a random role to a player.
 /// <para />
 /// If team is null, it can be any team.  Otherwise, if thisTeam is true it must be the same team as team.  If thisTeam is false, it must be a different team than team.
 /// </summary>
 /// <param name="player">Player to assign the role to</param>
 /// <param name="team">The team the assigned role must match or not match depending on thisTeam.  If null, any team is allowed.</param>
 /// <param name="thisTeam">Whether or not the assigned role should match or mismatch the team parameter</param>
 /// <param name="roleTypes">List of available roles, by class type</param>
 private void AssignRole(Player player, Team team, bool thisTeam, List<Type> roleTypes)
 {
     player.Role = GetRandomRole(roleTypes, team, thisTeam);
 }
Beispiel #11
0
 public bool IsOverlord(Player player)
 {
     if (Overlord.Address.Equals(player.Address)) return true;
     return false;
 }
 /// <summary>
 /// Join game request is valid, process it.
 /// </summary>
 /// <param name="player"></param>
 /// <param name="action"></param>
 /// <param name="game"></param>
 private void ProcessJoinAction(Player player, Action action, Game game)
 {
     game.AddPlayer(player);
     Gmail.MessagePlayer(player, game, "Successfully added you to the game, <b>" + game.Overlord + "</b> is the Overlord.<br />"
         + "I will notify you when the game has started."
         + "<br /><br />"
         + game.Help(player));
     Gmail.MessagePlayer(game.Overlord, game, "<b>" + player + "</b> has joined " + game.Title);
     gameLog.Info("Added player " + player + " to " + game);
 }
 private void HandleBadAction(Game game, Player player, Action action, string error)
 {
     log.Warn("Malformed action " + action.Type + " with target " + action.Target + " and text " + action.Text + " from " + player.Address);
     Gmail.MessagePlayer(player, game, error);
 }
 private void HandleBadAction(Game game, Player player, Action action, string error, Exception e)
 {
     log.Warn("Exception thrown when parsing action: " + e.Message);
     HandleBadAction(game, player, action, error);
 }
 /// <summary>
 /// The given player isn't playing in any games, but they did reference a valid game id.  See if they want to join it.
 /// </summary>
 /// <param name="address"></param>
 /// <param name="actions"></param>
 /// <param name="game"></param>
 public void ValidateJoinAction(string address, List<Action> actions, Game game)
 {
     foreach (Action action in actions)
     {
         if (action.Type == ActionEnum.JoinAs)
         {
             Player player = new Player(action.Text.ToTitleCase(), address);
             if (String.IsNullOrEmpty(player.Name))
             {
                 HandleBadAction(game, player, action, "You didn't specify the name you wished to join the game as. To do this reply to this message with \"Join as <i>name</i>\" where <i>name</i> is your name.");
                 return;
             }
             else if (game.IsInProgress)
             {
                 HandleBadAction(game, player, action, "You cannot join " + game + " because it is in progress.");
             }
             else if (game.GetPlayer(player.Name, "") != null)
             {
                 HandleBadAction(game, player, action, "Someone else is already using " + player.Name.b() + " as their name, please choose a different name.");
             }
             else
             {
                 ProcessJoinAction(player, action, game);
             }
             return;
         }
     }
     //None of the actions were join actions, and they aren't playing in any games so...
     string msg = "You aren't playing any games, the only action you may take is joining a game or creating a game. To start a new game reply to this message with \"Join as <i>name</i>\" where <i>name</i> is your name.";
     log.Warn("Unknown malformed action from " + address);
     Gmail.MessagePerson(address, game.Subject, msg);
 }
 public void Status(Player player, Action action, Game game)
 {
     gameLog.Info("Sending Status of " + game + " to " + player.Address);
     Gmail.MessagePlayer(player, game, game.Status());
 }
 public void Start(Player player, Action action, Game game)
 {
     if (game.IsOverlord(player))
     {
         gameLog.Info("Attempting to start " + game);
         if (!game.Start())
         {
             HandleBadAction(game, player, action, "You need at least 3 players to start a game." + FlavorText.Divider + game.Status());
         }
     }
 }
 public void Quit(Player player, Action action, Game game)
 {
     if (game.IsInProgress)
     {
         player.Quit();
         Gmail.MessagePlayer(player, game, "Your character in " + game.Title + ", " + player.Name + " is now dead.");
         gameLog.Info(player + " is dead because they quit " + game);
     }
     else
     {
         game.RemovePlayer(player);
         Gmail.MessagePlayer(player, game, "You have been removed from " + game);
         gameLog.Info(player + " is quitting " + game);
     }
 }
Beispiel #19
0
 public List<Player> GetLivingPlayersOnMyTeam(Player player)
 {
     return GetLivingPlayersOnTeam(player.Team);
 }
 public void Help(Player player, Action action, Game game)
 {
     gameLog.Info("Sending Help message for " + game + " to " + player.Address);
     Gmail.MessagePlayer(player, game, game.Help(player));
 }
 private void RoleAction(Player player, Action action, Game game)
 {
     //As of now role actions only happen at night
     if (game.ActiveCycle == Game.Cycle.Night)
     {
         string result = player.Role.AddAction(player, action, game);
         if (!String.IsNullOrEmpty(result))
         {
             HandleBadAction(game, player, action, result);
             return;
         }
         game.CheckEndOfCycle();
     }
 }
Beispiel #22
0
 public bool RemovePlayer(Player player)
 {
     return MyPlayers.Remove(player);
 }
 private void Vote(Player player, Action action, Game game)
 {
     Player nominee = action.Target;
     if (nominee == null)
     {
         if (action.Text.Equals("no one") || action.Text.Equals("nobody") || action.Text.Equals("noone"))
         {
             Player NoOne = new Player("No one", "nobody");
             nominee = NoOne;
         }
         else
         {
             HandleBadAction(game, player, action, "You voted for an invalid player <b>" + action.Target + "</b> see the player's names below." + FlavorText.Divider + game.Status());
             return;
         }
     }
     if (nominee.Equals(player))
     {
         HandleBadAction(game, player, action, "You cannot vote for yourself.");
     }
     else if (!nominee.IsAlive)
     {
         HandleBadAction(game, player, action, nominee.Name.b() + " is already dead. See who's alive below:" + FlavorText.Divider + game.Status());
     }
     else
     {
         Action vote = new Action(ActionEnum.Vote);
         vote.Target = nominee;
         player.AddAction(vote);
         Gmail.MessagePlayer(player, game, "Registered your vote for <b>" + nominee.Name + "</b>.");
         game.CheckEndOfCycle();
     }
 }
Beispiel #24
0
 private void AssignRoleMajorTeam(Player player, Team team, bool thisTeam, List<Type> roleTypes)
 {
     AssignRole(player, team, thisTeam, roleTypes);
     while (!player.Team.IsMajor)
     {
         AssignRole(player, team, thisTeam, roleTypes);
     }
 }
 public Game GetGameByPlayer(Player player)
 {
     Game game = null;
     foreach (Game g in Games)
     {
         if (g.Players.Contains(player)) game = g;
     }
     return game;
 }
 private void SendAction(Game game, Player p, Player candidate, string actionText)
 {
     SimpleMessage msg = new SimpleMessage();
     msg.From = p.Address;
     msg.Subject = "New Game";
     if(game != null) msg.Subject = game.FullTitle;
     msg.Body = actionText + " " + candidate.Name.ToLowerInvariant();
     MessageParser.Instance.ParseMessage(msg);
 }
 /// <summary>
 /// Parses a line of the body for game commands.
 /// </summary>
 /// <param name="line"></param>
 /// <param name="player"></param>
 /// <returns></returns>
 private Action ParseGameAction(string line, Player player, Game game)
 {
     string actionText = player.Role.ActionText.ToLowerInvariant();
     if (line.StartsWith(actionText) && game.ActiveCycle == Game.Cycle.Night)
     {
         return StringToAction(line, GameSystem.ActionEnum.Role, actionText, game);
     }
     if (line.StartsWith("vote") && player.IsAlive && game.ActiveCycle == Game.Cycle.Day)
     {
         return StringToAction(line, GameSystem.ActionEnum.Vote, "vote", game);
     }
     return null;
 }
Beispiel #28
0
 public static void MessagePlayer(Player player, Game game, string body)
 {
     string to = player.Address;
     string subject = "Unknown Game";
     if (game != null)
     {
         subject = game.Subject;
         game.Summary.AddEmail(to, subject, body);
     }
     EnqueueMessage(to, subject, body);
 }
 public void NewGame(SimpleMessage message, List<Action> actions)
 {
     string address = message.From;
     string name = "";
     Action joinAction = null;
     foreach (Action action in actions)
     {
         if (action.Type == ActionEnum.JoinAs) joinAction = action;
     }
     name = joinAction.Text;
     Player player = new Player(name, address);
     if (String.IsNullOrEmpty(name))
     {
         HandleBadAction(null, player, joinAction, "You didn't specify the name you wished to join the game as. To do this send a new email with \"Join as <i>name</i>\" where <i>name</i> is your name.");
         return;
     }
     Game game = GetGameByPlayer(player);
     if (game != null)
     {
         HandleBadAction(null, player, joinAction, "You are already playing in " + game + " , you may only play one game at a time.");
         return;
     }
     game = new Game(GetNextGameId(), player);
     Games.Add(game);
     Gmail.MessagePlayer(player, game, game.Title + " has been started, you are the <b>Overlord</b>."
         + "<br /><br />Have your friends join by sending a message with the subject \"" + game.Title + "\" and body \"Join as <i>name</i>\" where <i>name</i> is their name.<br />"
         + game.Help(player)
         + "<br /><hr><br />" + Program.License.Replace('\n'.ToString(), "<br />")
         + "<br /><br />Download this program on " + Program.GitHub);
     gameLog.Info("Started new game: " + game);
 }