Beispiel #1
0
        public void OnMessage(Player player, String message)
        {
            Newtonsoft.Json.Linq.JObject obj = Newtonsoft.Json.Linq.JObject.Parse(message);
            JsonTypeReceive messageType      = (JsonTypeReceive)Enum.Parse(typeof(JsonTypeReceive), obj.GetValue("type").ToString());

            Newtonsoft.Json.Linq.JToken token = obj.GetValue("value");

            // If the player is talking
            if (messageType == JsonTypeReceive.MESSAGE)
            {
                HandleChat(player, token.ToString());
            }
            else if (messageType == JsonTypeReceive.WILL && player.IsAlive() && player.GetRole() != null)
            {
                String will = token.ToString().Trim();

                if (will.Length > 10000)
                {
                    will = will.Substring(0, 10000);
                }

                if (will.Length <= 0)
                {
                    lastWills.Remove(player);
                }
                else
                {
                    lastWills.Add(player, System.Web.HttpUtility.HtmlEncode(will));
                }
            }
            else if (messageType == JsonTypeReceive.DEATHNOTE && player.IsAlive() && player.GetRole() != null && player.GetRole().HasDeathNote())
            {
                String deathnote = token.ToString().Trim();

                if (deathnote.Length > 10000)
                {
                    deathnote = deathnote.Substring(0, 10000);
                }

                if (deathnote.Length <= 0)
                {
                    deathNotes.Remove(player);
                }
                else
                {
                    deathNotes.Add(player, System.Web.HttpUtility.HtmlEncode(deathnote));
                }
            }
            else if (messageType == JsonTypeReceive.NAME && GetGamePhase() == GamePhase.PICK_NAME)
            {
                String name = token.ToString().Trim();

                if (!new System.Text.RegularExpressions.Regex("(?!.*[ ]{2})[a-zA-Z ]{2,16}").IsMatch(name))
                {
                    player.SendMessage(Messages.INFO_INVALID_NAME);
                    return;
                }

                if (NameConflicts(name))
                {
                    player.SendMessage(Messages.INFO_NAME_EXISTS);
                    return;
                }

                if (player.GetName() == null)
                {
                    BroadcastMessage(String.Format(Messages.GAME_PICKED_NAME, name));
                }
                else
                {
                    BroadcastMessage(String.Format(Messages.GAME_PICKED_RENAME, player.GetName(), name));
                }

                player.SetName(name);
            }
            else if (messageType == JsonTypeReceive.ACTION)
            {
                RoleAction action = token.ToObject <RoleAction>();

                if (action.GetAbility().Equals("votes"))
                {
                    if (GetGamePhase() == GamePhase.VOTING)
                    {
                        if (action.GetTarget() < 0 || (votes.ContainsKey(player) && votes[player] == action.GetTarget()))
                        {
                            if (votes.ContainsKey(player))
                            {
                                int target = votes[player];
                                votes.Remove(player);

                                BroadcastMessage(String.Format(Messages.VOTING_UNVOTED_PLAYER, player.GetName(), GetAllPlayers()[target]));
                            }
                        }
                        else if (action.GetTarget() < GetAllPlayers().Count)
                        {
                            Player target = GetAllPlayers()[action.GetTarget()];

                            if (!target.IsAlive())
                            {
                                return;
                            }

                            BroadcastMessage(String.Format(votes.ContainsKey(player) ? Messages.VOTING_VOTED_ANOTHER_PLAYER : Messages.VOTING_VOTED_PLAYER, player.GetName(), target.GetName()));

                            votes.Add(player, action.GetTarget());
                        }

                        Dictionary <Player, int> voteCount = new Dictionary <Player, int>();
                        int votesNeeded = (GetLivingPlayers().Count / 2) + 1;

                        foreach (Player voter in votes.Keys)
                        {
                            Player p = GetAllPlayers()[votes[voter]];

                            if (!p.IsAlive())
                            {
                                // TODO Reset votes for this player

                                continue;
                            }

                            int newCount = (voteCount.ContainsKey(p) ? voteCount[p] : 0) + (voter.GetRole() is Mayor ? 3 : 1);

                            voteCount.Add(p, newCount);

                            if (newCount < votesNeeded)
                            {
                                continue;
                            }

                            String targets = new RoleTargets("vote").Serialize();

                            GetLivingPlayers().ForEach(pl => pl.SendTargets(targets));

                            SetGamePhase(GamePhase.VOTING_UP);
                            judged = p;
                        }
                    }
                    else if (GetGamePhase() == GamePhase.VOTING_DECISION)
                    {
                        if (action.GetTarget() < 0 || (votes.ContainsKey(player) && votes[player] == action.GetTarget()))
                        {
                            if (votes.ContainsKey(player))
                            {
                                int target = votes[player];
                                votes.Remove(player);

                                BroadcastMessage(String.Format(Messages.VOTING_JUDGEMENT_UNCAST, player.GetName(), GetAllPlayers()[target]));
                            }
                        }
                        else if (action.GetTarget() < GetAllPlayers().Count)
                        {
                            Player target = GetAllPlayers()[action.GetTarget()];

                            if (!target.IsAlive())
                            {
                                return;
                            }

                            BroadcastMessage(String.Format(votes.ContainsKey(player) ? Messages.VOTING_JUDGEMENT_SWITCH : Messages.VOTING_JUDGEMENT_CAST, player.GetName(), target.GetName()));

                            votes.Add(player, action.GetTarget());
                        }
                    }
                }
                else
                {
                    player.GetRole().DoAction(player, action);
                }
            }
        }