Exemplo n.º 1
0
        private void TryCreateNewGame(GameAction action)
        {
            //check if player has already a game
            foreach (ServerGame game in _Games)
            {
                //check if player is already hosting
                if (game.Host.Equals(action.Sender))
                {
                    string message = "You are already hosting a game, game creation failed";

                    MessageFromServerAction messageBack = new MessageFromServerAction();
                    messageBack.Message = message;
                    messageBack.Sender = _ServerUser.ID;

                    Whisper(action.Sender, messageBack);
                    return;
                }

                //check if player is already in another game
                if (game.Users != null)
                {
                    foreach (XmlUser player in game.Users)
                    {
                        if (player.Equals(action.Sender))
                        {
                            string message = String.Format("You are already in the game '{0}'", game.Game.Settings.Name);

                            MessageFromServerAction messageBack =
                                new MessageFromServerAction();
                            messageBack.Message = message;
                            messageBack.Sender = _ServerUser.ID;

                            Whisper(action.Sender, messageBack);
                            return;
                        }
                    }
                }

            }

            int gameID = GetNewGameID();
            // create game, user is not in another game hosting or playing
            ServerGame serverGame =
                new ServerGame(((TryCreateGameAction)action).GameSettings);
            serverGame.Host = _CurrentPerson;
            serverGame.ID = gameID;
            serverGame.Users.Add(_CurrentPerson);
            _Games.Add(serverGame);

            //create the message
            GameCreatedAction newGameMessage = new GameCreatedAction();
            newGameMessage.Sender = _ServerUser.ID;
            newGameMessage.ID = gameID;
            newGameMessage.Game = ((TryCreateGameAction)action).GameSettings;

            //broadcast the message
            BroadcastMessage(newGameMessage, null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Broadcast the message to all participants of a game
        /// </summary>
        /// <param name="action">Action to broadcast</param>
        /// <param name="game">The game with the participants</param>
        private void BroadcastMessage(InGameAction action, ServerGame game)
        {
            // broadcast message to all users
            foreach (XmlUser user in game.Users)
                BroadcastMessage(action, user);

            // broadcast message to all spectators
            foreach (XmlUser spectator in game.Spectators)
                BroadcastMessage(action, spectator);
        }