Beispiel #1
0
        public void GiveChiefWord(Guid token, string word)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("GiveChiefWord userId=" + user.Id);
            RoomControll.GiveChiefWord(user, word);
        }
Beispiel #2
0
        public void AskQuestion(Guid token, string question, string word)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("AskQuestion userId=" + user.Id);
            RoomControll.AskQuestion(user, question, word);
        }
Beispiel #3
0
        public void AcceptChiefWord(User user, string word)
        {
            if (user.role != User.Role.Host)
            {
                LogSaver.Log("!!! AcceptChiefWord invoked user " + user.Id + " role " + user.role.ToString());
                GameException.Throw("Вы не можете так делать");
            }

            lock (gameState)
            {
                if (gameState.UsedWords.Contains(word))
                {
                    GameException.Throw("Это слово уже использовалось");
                }

                if (!word.StartsWith(gameState.PrimaryWordKnownLetters))
                {
                    GameException.Throw("Слово должно начинаться с открытых букв");
                }
                if (word == gameState.PrimaryWord)
                {
                    GameException.Throw("Так ты спалишь загаданное слово!");
                }

                gameState.PrepareForVoting(1);
                gameState.ChiefWord = word;
                BroadcastMessage(GameMessage.WeHaveChiefWord(word));
                ChangeState(GameState.State.VotingForHostWord);
            }
        }
Beispiel #4
0
        public void GiveCurrentWordVariant(Guid token, string word)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("GiveCurrentWordVariant userId=" + user.Id);
            RoomControll.GiveCurrentWordVariant(user, word);
        }
Beispiel #5
0
        public GameState GetState(Guid token)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("GetState asked userId=" + user.Id);
            return(RoomControll.GetState(user));
        }
Beispiel #6
0
        public void SetPrimaryWord(Guid token, string primaryWord)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("SetPrimaryWord userId=" + user.Id);
            RoomControll.SetPrimaryWord(user, primaryWord);
        }
Beispiel #7
0
        public void LeaveRoom(Guid token)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("User " + user.Id + "leave room");
            RoomControll.UserLeaveRoom(user);
        }
Beispiel #8
0
        public void VoteForPlayerWord(Guid token, int wordId, bool up)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("VoteForPlayerWord userId=" + user.Id + " word=" + wordId + (up ? "up" : "down"));
            RoomControll.VoteForPlayerWord(user, wordId, up);
        }
Beispiel #9
0
        public void Logoff(Guid token)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("Logoff userId=" + user.Id);
            RoomControll.DeleteOnlineUser(user);
        }
Beispiel #10
0
        public void VoteForChiefWord(Guid token, bool up)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("VoteForChiefWord userId=" + user.Id + (up ? "up" : "down"));
            RoomControll.VoteForChifWord(user, up);
        }
Beispiel #11
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);
            }
        }
Beispiel #12
0
        public void AddRoom(Guid token, string roomName)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("AddRoom userId=" + user.Id + " roomName=" + roomName);

            RoomControll.AddRoom(roomName);
        }
Beispiel #13
0
        public UserData Login(string name, string password)
        {
            LogSaver.Log("Attempt to login. name=" + name + " password="******"Loged in successfully. UserId= " + user.Id);
            return(user);
        }
Beispiel #14
0
        public void GotoRoom(Guid token, int roomId)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("GotoRoom userId=" + user.Id + " to room " + roomId);

            RoomControll.GotoRoom(user, roomId);
        }
Beispiel #15
0
        public List <Room> GetRoomsList(Guid token)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("GetRoomsList userId=" + user.Id);

            return(RoomControll.GetRoomsList(user));
        }
Beispiel #16
0
        public void StartGame(Guid token)
        {
            var user = RoomControll.GetUserByToken(token);

            LogSaver.Log("Start Game userId=" + user.Id);
            //ведущим становится взывавший эту операцию (пока пусть так будет)
            user.role = User.Role.Host;
            RoomControll.StartGame(user);
        }
Beispiel #17
0
 public static int Duration(GameState.State state)
 {
     if (!StateDurationTime.ContainsKey(state))
     {
         LogSaver.Log("[!!!] StateDurationTime не установлено для " + state.ToString());
         throw new Exception("StateDurationTime не установлено");
     }
     return(StateDurationTime[state]);
 }
Beispiel #18
0
        public void ReceiveChatMessage(Guid token, string message)
        {
            var user = RoomControll.GetUserByToken(token);

            if (message == "")
            {
                return;
            }
            LogSaver.Log("Chat message from " + user.Name + ": " + message);
            RoomControll.BroadcastChatMessageToRoom(user, message);
        }
Beispiel #19
0
        private void HaveCurrentWordVariantTimeout()
        {
            LogSaver.Log("HaveCurrentWordVariant Timeout");

            lock (gameState)
            {
                gameState.PrepareForVoting(2);
            }

            ChangeState(GameState.State.VotingForPlayersWords);
        }
Beispiel #20
0
 private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     // TODO: check that state that ended is the state that was sheduled
     //Invoke appropriate function
     if (!timeoutFunctions.ContainsKey(gameState.state))
     {
         LogSaver.Log("[!!!] timeoutFunctions не задан для " + gameState.state.ToString());
         throw new Exception("timeoutFunctions не задан");
     }
     timeoutFunctions[gameState.state]();
 }
Beispiel #21
0
        public static User GetUserByToken(Guid token)
        {
            lock (OnlineUsers)
            {
                foreach (var user in OnlineUsers.Where(user => user.Token == token))
                {
                    return(user);
                }
            }

            LogSaver.Log("Asked for user with unknown GUID");
            throw new AccessViolationException("Asked for user with unknown GUID");
        }
Beispiel #22
0
        static void Main(string[] args)
        {
            // TODO: remove this and do normal room management
            RoomControll.AddRoom("Lobby");
            RoomControll.AddRoom("First and only room");

            var service = new ServiceHost(typeof(GameService));

            service.Open();
            LogSaver.Log("Server started");
            Console.ReadLine();
            service.Close();
            LogSaver.Log("Server shut down");
        }
Beispiel #23
0
        public void AcceptPrimaryWord(User user, string primaryWord)
        {
            if (user.role != User.Role.Host)
            {
                LogSaver.Log("!!! AcceptPrimaryWord invoked user " + user.Id + " role " + user.role.ToString());
                GameException.Throw("Вы не можете этого делать!");
            }

            lock (gameState)
            {
                gameState.PrimaryWord = primaryWord.ToLower();
                BroadcastMessage(GameMessage.PrimaryWordGiven(gameState.PrimaryWord));
                ChangeState(GameState.State.HaveNoCurrentWord);
            }
        }
Beispiel #24
0
        public static void GotoRoom(User user, int roomId)
        {
            if (!Rooms.ContainsKey(roomId))
            {
                LogSaver.Log("!?! Trying to go to nonexistent room " + roomId + " userId=" + user.Id);
                GameException.Throw("Комнаты с таким номером нет");
            }

            if (roomId == user.RoomId)
            {
                return;
            }

            Rooms[user.RoomId].LeaveRoom(user);
            user.RoomId = roomId;
            Rooms[roomId].EnterRoom(user);
        }
Beispiel #25
0
        public void AcceptCurrentWordVariant(User user, string word)
        {
            if (user.role != User.Role.None)
            {
                LogSaver.Log("!!! AcceptCurrentWordVariant invoked user " + user.Id + " role " + user.role.ToString());
                GameException.Throw("Вы не можете так делать");
            }

            bool epicWin = false; // угадал primaryWord

            lock (gameState)
            {
                if (gameState.UsedWords.Contains(word))
                {
                    GameException.Throw("Это слово уже использовалось");
                }

                if (!word.StartsWith(gameState.PrimaryWordKnownLetters))
                {
                    GameException.Throw("Слово должно начинаться с открытых букв");
                }

                if (word == gameState.PrimaryWord)
                {
                    epicWin = true;
                }
                else
                {
                    gameState.VarOfCurWord = word;
                    user.role = User.Role.Contacter;
                }
            }

            if (epicWin)
            {
                ResetRoles();
                ChangeState(GameState.State.GameOver);
            }
            else
            {
                BroadcastMessage(GameMessage.UserRoleChangedMessage(user, User.Role.Contacter));
                BroadcastMessage(GameMessage.VarOfCurWordChangedMessage(word));
                ChangeState(GameState.State.HaveCurrentWordVariant);
            }
        }
Beispiel #26
0
        public static void DeleteOnlineUser(User user)
        {
            lock (OnlineUsers)
            {
                //TODO: error handling?
                try
                {
                    OnlineUsers.Remove(user);
                }
                catch (Exception e)
                {
                    LogSaver.Log("Tried to delete user " + user.Id + " but it is not online");
                    //throw;
                }
            }

            Rooms[user.RoomId].LeaveRoom(user);
        }
Beispiel #27
0
        public void AcceptQuestion(User user, string question, string word)
        {
            if (user.role != User.Role.None)
            {
                LogSaver.Log("!!! AcceptQuestion invoked user " + user.Id + " role " + user.role.ToString());
                GameException.Throw("Вы не можете этого делать!");
            }

            bool epicWin = false; //угадал primaryWord

            lock (gameState)
            {
                gameState.Question = question;

                if (!word.StartsWith(gameState.PrimaryWordKnownLetters))
                {
                    GameException.Throw("Слово должно начинаться с открытых букв");
                }

                if (word == gameState.PrimaryWord)
                {
                    epicWin = true;
                }
                else
                {
                    gameState.CurrentWord = word;
                    user.role             = User.Role.Qwestioner;
                }
            }

            if (epicWin)
            {
                ResetRoles();
                ChangeState(GameState.State.GameOver);
            }
            else
            {
                BroadcastMessage(GameMessage.QuestionAsked(question, word));
                BroadcastMessage(GameMessage.UserRoleChangedMessage(user, User.Role.Qwestioner));
                ChangeState(GameState.State.HaveCurrentWord);
            }
        }
Beispiel #28
0
        private static User GetUserById(int userId)
        {
            User user;

            lock (OnlineUsers)
            {
                try
                {
                    user = OnlineUsers.Find(curUser => curUser.Id == userId);
                }
                catch (Exception e)
                {
                    //It should be ok even if this happens
                    LogSaver.Log("Tried to GetUserById wher user not online. userId=" + userId);
                    throw;
                }
            }

            return(user);
        }
Beispiel #29
0
 private void HaveCurrentWordTimeout()
 {
     LogSaver.Log("HaveCurrentWord Timeout");
     //TODO: remove stub and do actual logic
     ChangeState(GameState.State.HaveNoCurrentWord);
 }
Beispiel #30
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");
        }