예제 #1
0
        private void ResetRoles()
        {
            User host, questioner, contacter;

            lock (gameState)
            {
                host = gameState.Users.SingleOrDefault(user => user.role == User.Role.Host);
                if (host != null)
                {
                    host.role = User.Role.None;
                }
                questioner = gameState.Users.SingleOrDefault(user => user.role == User.Role.Qwestioner);
                if (questioner != null)
                {
                    questioner.role = User.Role.None;
                }
                contacter = gameState.Users.SingleOrDefault(user => user.role == User.Role.Contacter);
                if (contacter != null)
                {
                    contacter.role = User.Role.None;
                }
            }

            if (host != null)
            {
                BroadcastMessage(GameMessage.UserRoleChangedMessage(host, User.Role.None));
            }
            if (contacter != null)
            {
                BroadcastMessage(GameMessage.UserRoleChangedMessage(contacter, User.Role.None));
            }
            if (questioner != null)
            {
                BroadcastMessage(GameMessage.UserRoleChangedMessage(questioner, User.Role.None));
            }
        }
예제 #2
0
        private void VotingForPlayersWordsTimeout()
        {
            LogSaver.Log("VotingForPlayersWords Timeout");
            User contacter, questioner;
            bool openLetter           = false;
            bool winGame              = false;
            bool CurrentWordAccepted  = false;
            bool VarOfCurWordAccepted = false;

            lock (gameState)
            {
                CurrentWordAccepted  = gameState.votings[0].Accepted(gameState.Users.Count);
                VarOfCurWordAccepted = gameState.votings[1].Accepted(gameState.Users.Count);


                // добавить в список используемых
                if (CurrentWordAccepted)
                {
                    gameState.UsedWords.Add(gameState.CurrentWord);
                }

                if (VarOfCurWordAccepted)
                {
                    gameState.UsedWords.Add(gameState.VarOfCurWord);
                }


                if (CurrentWordAccepted && VarOfCurWordAccepted) // игроки выиграли
                {
                    gameState.NumberOfOpenChars++;

                    if (gameState.NumberOfOpenChars == gameState.PrimaryWord.Length)
                    {
                        winGame = true;
                    }
                    else
                    {
                        openLetter = true;
                    }
                }


                // сбросить роли
                contacter      = gameState.Users.Single(user => user.role == User.Role.Contacter);
                contacter.role = User.Role.None;

                questioner      = gameState.Users.Single(user => user.role == User.Role.Qwestioner);
                questioner.role = User.Role.None;
            }

            //разослать изменения
            BroadcastMessage(GameMessage.UserRoleChangedMessage(contacter, User.Role.None));
            BroadcastMessage(GameMessage.UserRoleChangedMessage(questioner, User.Role.None));

            //Сообщить о добавленых словах
            if (CurrentWordAccepted)
            {
                BroadcastMessage(GameMessage.UsedWordAddedMessage(gameState.CurrentWord)); // TODO: а это безопасно?
            }
            if (VarOfCurWordAccepted)
            {
                BroadcastMessage(GameMessage.UsedWordAddedMessage(gameState.VarOfCurWord));
            }


            if (openLetter)
            {
                BroadcastMessage(GameMessage.PrimaryWordCharOpened());
            }

            if (winGame)
            {
                ChangeState(GameState.State.GameOver);
            }
            else
            {
                ChangeState(GameState.State.HaveNoCurrentWord);
            }

            LogSaver.Log("VotingForPlayersWords Timeout end");
        }
예제 #3
0
        private void VotingForHostWordTimeout()
        {
            LogSaver.Log("VotingForChiefWord Timeout");
            bool openLetter = false;
            bool winGame = false;
            bool ChiefWord = false;
            User questioner, contacter;

            lock (gameState)
            {
                ChiefWord = gameState.votings[0].Accepted(gameState.Users.Count);

                if (ChiefWord)
                {
                    gameState.UsedWords.Add(gameState.ChiefWord);
                }
                else
                {
                    gameState.NumberOfOpenChars++;

                    if (gameState.NumberOfOpenChars == gameState.PrimaryWord.Length)
                    {
                        winGame = true;
                    }
                    else
                    {
                        openLetter = true;
                    }
                }

                questioner = gameState.Users.SingleOrDefault(user => user.role == User.Role.Qwestioner);
                contacter  = gameState.Users.SingleOrDefault(user => user.role == User.Role.Contacter);
            }

            if (questioner != null)
            {
                questioner.role = User.Role.None;
                BroadcastMessage(GameMessage.UserRoleChangedMessage(questioner, User.Role.None));
            }

            if (contacter != null)
            {
                contacter.role = User.Role.None;
                BroadcastMessage(GameMessage.UserRoleChangedMessage(contacter, User.Role.None));
            }

            if (ChiefWord)
            {
                BroadcastMessage(GameMessage.UsedWordAddedMessage(gameState.ChiefWord));
            }

            if (openLetter)
            {
                BroadcastMessage(GameMessage.PrimaryWordCharOpened());
            }

            if (winGame)
            {
                ChangeState(GameState.State.GameOver);
            }
            else
            {
                ChangeState(GameState.State.HaveNoCurrentWord);
            }
        }
예제 #4
0
        public void BroadcastMessage(GameMessage message)
        {
            var deadUsers = new List<User>();
            lock (gameState)
            {
                foreach (var user in gameState.Users)
                {
                    try
                    {
                        user.Callback.Notify(message);
                    }
                    catch (CommunicationException e)
                    {
                        // assume that client is down. we can't logof it now - deadlock
                        LogSaver.Log(e.Message);
                        deadUsers.Add(user);
                    }
                    catch (Exception e)
                    {
                        LogSaver.Log("FAIL!! mysterious callback exception: "+e.Message);
                    }
                }
            }

            // logoff all deadUsers
            foreach (var deadUser in deadUsers)
            {
                LogSaver.Log("User "+deadUser.Id+" is dead. Logoff");
                RoomControll.DeleteOnlineUser(deadUser);
            }
        }
예제 #5
0
 /*
  *   If user argument is null then the type of the message is system.
  */
 public void BroadcastChatMessage(string message, User user = null)
 {
     BroadcastMessage(GameMessage.ChatMessage(user != null ? user.Name : null, message));
 }
예제 #6
0
 public void StartGame(User user)
 {
     //TODO: start with normal state
     ChangeState(GameState.State.HaveNoPrimaryWord);
     BroadcastMessage(GameMessage.UserRoleChangedMessage(user, User.Role.Host));
 }
예제 #7
0
 public static void LogoffUser(User user)
 {
     User us = RoomControll.GetUserByName(user.Name);
     GameMessage msg = new GameMessage();
     msg = GameMessage.LogoffUser();
     DeleteOnlineUser(us);
     try { us.Callback.Notify(msg); }
     catch { }
 }