public void ExecuteAction(InGameAction action) { //check if the userid of the action is actually playing if ((from u in _Users where u.ID == action.UserID select u).Count() == 0) { if (action is BuildRoadAction) { TryBuildRoad((BuildRoadAction)action); } } else // we received an ingame action from a user which is not in this game { MessageFromServerAction message = new MessageFromServerAction(); message.Message = String.Format( "Hey! You send an action for game with id: {0}, but you with userid {1} are not in the players list of game with id: {0}", action.GameID, action.UserID); } }
private void TryHandleGameAction(InGameAction action) { if (CheckIfPlayerBelongsToGame(action.GameID, action.Sender)) { //HandleGameAction(action); } else { //notify the user some mismatch happened MessageFromServerAction response = new MessageFromServerAction(); response.Message = String.Format( "You, player {0} do not belong to game with ID {1}", action.GameID, _CurrentPerson.Name); response.Sender = _ServerUser.ID; Whisper(action.Sender, response); return; } }
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); }
private void SayError(int userID, string error) { MessageFromServerAction message = new MessageFromServerAction(); message.Message = error; Whisper(userID, message); }
/// <summary> /// Takes a <see cref="Common.Person">Person</see> and allows them /// to join the chat room, if there is not already a chatter with /// the same name /// </summary> /// <param name="person"><see cref="Common.Person">Person</see> joining</param> /// <returns>An array of <see cref="Common.Person">Person</see> objects</returns> public JoinResult Join(XmlUserCredentials person) { MessageFromServerAction message = new MessageFromServerAction(); message.Sender = _ServerUser.ID; if (person == null) { message.Message = "No valid person passed (Usercredentials object == null)"; return new JoinResult() { FailMessage = message }; } if (String.IsNullOrEmpty(person.Name) || String.IsNullOrEmpty(person.Password)) { message.Message = "either password or username isnt valid: empty password or username"; return new JoinResult() { FailMessage = message }; } /* XmlUser user = _UserAdministration.Authenticate(person); // we failed to authenticate, communicate this back to user if (user == null) { message.Message = "either password or username isnt valid, or both"; return new JoinResult() { FailMessage = message }; } */ //Yey! we are authenticated. Add user to list of users and // return the lobby state bool userAdded = false; //create a new ChatEventHandler delegate, pointing to the MyEventHandler() method myEventHandler = new GameEventHandler(MyEventHandler); //carry out a critical section that checks to see if the new chatter //name is already in use, if its not allow the new chatter to be //added to the list of chatters, using the person as the key, and the //ChatEventHandler delegate as the value, for later invocation lock (threadSync) { /* if (!PlayerExists(user.ID)) { _Users.Add(user, MyEventHandler); userAdded = true; _CurrentPerson = user; } else { message.Message = "It seems that you are already logged in."; return new JoinResult() { FailMessage = message }; } */ } //if the new chatter could be successfully added, get a callback instance //create a new message, and broadcast it to all other chatters, and then //return the list of al chatters such that connected clients may show a //list of all the chatters if (userAdded) { _Callback = OperationContext.Current.GetCallbackChannel<IChatCallback>(); //add this newly joined chatters ChatEventHandler delegate, to the global //multicast delegate for invocation ChatEvent += myEventHandler; XmlLobbyState lobbyState = new XmlLobbyState(); //carry out a critical section that copy all chatters to a new list lock (threadSync) { //copy chatlog if (_LobbyChatLog != null) { lobbyState.LobbyChat = _LobbyChatLog.Copy(); } //copy users if (_Users != null) { lobbyState.Users = new List<XmlUser>(); foreach (KeyValuePair<XmlUser, GameEventHandler> lobbyUser in _Users) lobbyState.Users.Add(lobbyUser.Key.Copy()); } //copy games if (_Games != null) { lobbyState.Games = CreateGameList(); } } /* //Say to all other players we have a new player joined in the lobby LobbyJoinedAction lobbyJoined = new LobbyJoinedAction() { NewPlayer = user, Sender = _ServerUser.ID }; BroadcastMessage(lobbyJoined, user); return new JoinResult() { User = user, LobbyState = lobbyState }; */ return null; } else { message.Message = "You are already in the list of logged in players!"; message.Sender = _ServerUser.ID; return null; } }
private void TryJoinGame(JoinGameAction action) { ServerGame gameToFind = null; foreach (ServerGame game in _Games) { if (game.ID == action.GameID) { gameToFind = game; break; } } // whoops, user send a wrong ID or game is removed // notify the user the game is lost into oblivion if (gameToFind == null) { string message = "The game you where trying to join was not found on the server"; MessageFromServerAction messageBack = new MessageFromServerAction(); messageBack.Message = message; messageBack.Sender = _ServerUser; Whisper(action.Sender.Name, messageBack); return; } //game full, notify user if (gameToFind != null) { if (gameToFind.Users.Count == gameToFind.Settings.MaxPlayers) { string message = "The game you where trying to join is already full"; MessageFromServerAction messageBack = new MessageFromServerAction(); messageBack.Message = message; messageBack.Sender = _ServerUser; Whisper(action.Sender.Name, messageBack); return; } if (gameToFind.Users.Count < gameToFind.Settings.MaxPlayers) { gameToFind.Users.Add(action.Sender); GameJoinedAction gameJoinedAction = new GameJoinedAction(); gameJoinedAction.GameID = action.GameID; gameJoinedAction.Player = action.Sender; gameJoinedAction.Sender = _ServerUser; BroadcastMessage(gameJoinedAction, null); } } }
private void ExecuteAction(GameAction action) { if (action is InGameAction) { IValidAction valid = (IValidAction)((InGameAction)action); if (valid != null) { ServerGame serverGame = (from g in _Games where g.Game.ID == ((InGameAction)action).GameID select g).Single(); if (!valid.IsValid(serverGame.Game)) { MessageFromServerAction message = new MessageFromServerAction(); message.Message = "Invalid action!"; Whisper(_CurrentPerson.Name, message); } else { serverGame.ExecuteAction((InGameAction)action); } } } if (action is TryCreateGameAction) { TryCreateNewGame(action); } if (action is LobbyChatAction) { SayToLobby(action); } if (action is JoinGameAction) { TryJoinGame((JoinGameAction)action); } if (action is InGameAction) { HandleGameAction((InGameAction)action); } }
public GameAction ExecuteAction(InGameAction action) { if (action.IsValid(_Game)) { //Execute the game action if the action is valid action.PerformTurnAction(_Game); // Send action back to sender for confirmation //Send action to all other players and spectators //Add action to the gamelog _Game.GameLog.Add(action); return action; } else // we received an illegal ingameaction. // This should only happen when a user deliberately tampers with data { MessageFromServerAction message = new MessageFromServerAction(); message.Message = String.Format( "The game action does not seem to be valid. The reason: action object says \"{0}\"", action.InvalidMessage); return message; } }
public GameAction TryBuildShip(BuildShipAction action) { if (_Game.IsRoadShipPresent(action.Intersection)) // hey, road or ship found, we can't built { MessageFromServerAction message = new MessageFromServerAction(); message.Message = String.Format( "Hey! We cannot build a ship at location {0}. There is already a road or ship.", action.GameID, action.UserID); return message; } else // yey! no ship or road present, let's build there { GamePlayer player = _Game.GetPlayer(action.UserID); if (player != null) { player.BuildShip(action.Intersection); return action; } else { MessageFromServerAction message = new MessageFromServerAction(); message.Message = String.Format( "Hey! We _can_ build a ship at location {0}, but userid {1} is not found in game with id {2}", action.Intersection.ToString(), action.UserID, action.GameID); return message; } } }
public GameAction TryBuildTown(PlaceTownAction action) { if (_Game.IsTownCityPresent(action.Location)) // town or city found, we cannot build there { MessageFromServerAction message = new MessageFromServerAction(); message.Message = String.Format( "Hey! We cannot build a town at location {0}. There is already a town or city.", action.Location); return message; } else // the spot is free to build for the player. { //check if we have a town or city on the side, and check if we have a road or ship // connecting to the location where player wants to put a town GamePlayer player = _Game.GetPlayer(action.UserID); if (!_Game.HasRoadShipAtPoint(action.Location, player)) //sweet, we have a road or ship at a side of the location // and the spot or its neighbours isnt taken yet { player.BuildTown(action.Location); return action; } else { MessageFromServerAction message = new MessageFromServerAction(); message.Message = String.Format( "Hey! We cannot build a town at location {0}. You do not have a road or ship at that point.", action.Location); return message; } } throw new Exception(""); }