private void BroadcastMessage(GameAction action, XmlUser skipUser) { GameEventArgs e = new GameEventArgs(); e.Person = action.Sender; e.Action = action; BroadcastMessage(e, skipUser); }
private void ExecuteAction(GameAction action) { // the user should exist in our list of loggedin users XmlUser user = (from u in _Users.Keys where u.ID == action.Sender select u).Single(); if (user == null) { SayError(_CurrentPerson.ID, "Hey! I thought you are someone else. Tampering with input info aint cool. If you aint, go hunt some developer"); return; } if (action is InGameAction) { ExecuteInGameAction((InGameAction)action); } if (action is LobbyAction) { ExecuteLobbyAction((LobbyAction)action); } }
/// <summary> /// Broadcasts the input msg parameter to all connected chatters /// by using the BroadcastMessage() method /// </summary> /// <param name="msg">The message to broadcast to all chatters</param> public void Say(GameAction action) { if (!action.IsValid()) { SayError(action.Sender, "Invalid action! The action object said: _ /r/n/r/n" + action.InvalidMessage); } else { ExecuteAction(action); } }
/// <summary> /// Broadcasts the input msg parameter to all the <see cref="Common.Person"> /// Person</see> whos name matches the to input parameter /// by looking up the person from the internal list of chatters /// and invoking their ChatEventHandler delegate asynchronously. /// Where the MyEventHandler() method is called at the start of the /// asynch call, and the EndAsync() method at the end of the asynch call /// </summary> /// <param name="to">The persons name to send the message to</param> /// <param name="msg">The message to broadcast to all chatters</param> public void Whisper(int userID, GameAction action) { GameEventArgs e = new GameEventArgs(); e.Person = _ServerUser.ID; e.Action = action; try { GameEventHandler chatterTo; //carry out a critical section, that attempts to find the //correct Person in the list of chatters. //if a person match is found, the matched chatters //ChatEventHandler delegate is invoked asynchronously lock (threadSync) { chatterTo = GetPersonHandler(userID); if (chatterTo == null) { throw new KeyNotFoundException("The person \" " + GetUser(userID).Name + "\" could not be found"); } } //do a async invoke on the chatter (call the MyEventHandler() method, and the //EndAsync() method at the end of the asynch call chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); } catch (KeyNotFoundException) { Console.WriteLine("whispering failed, could not find the player with ID " + e.Person); } }
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); } }
private void Receive(string name, GameAction action) { }
/// <summary> /// Broadcasts the input msg parameter to all connected chatters /// by using the BroadcastMessage() method /// </summary> /// <param name="msg">The message to broadcast to all chatters</param> public void Say(GameAction action) { ExecuteAction(action); }
public void Say(GameAction action) { InGameAction inGameAction = action as InGameAction; if (inGameAction != null && !(action is HostStartsGameAction)) { if (!inGameAction.IsValid(_Game)) { _CallBack.Receive(new MessageFromServerAction() { Message = String.Format("Invalid action! \r\n Reason: {0}", inGameAction.InvalidMessage), Sender = Core.Instance.ServerUser.ID, DateTime = DateTime.Now }); return; } if (_Game.ActionsQueue.Count > 0) // we have some expected actions in the queue, process them first { if (_Game.ActionsQueue.IsExpected(inGameAction, _Game)) { // we encountered an expected action on the queue _Game.ActionsQueue.Dequeue(); } else { // Grab the expected action InGameAction expected = _Game.ActionsQueue.Peek(); // Notify we did not xpect that action _CallBack.Receive(new MessageFromServerAction() { Message = String.Format("Invalid action! \r\n Reason: Expected {0} from player {1}, but instead got {2} from player {3}", expected.GetType().ToString(), _Game.GetPlayer(expected.Sender), inGameAction.GetType().ToString(), _Game.GetPlayer(inGameAction.Sender)), Sender = Core.Instance.ServerUser.ID, DateTime = DateTime.Now }); return; } } } HostStartsGameAction startGame = action as HostStartsGameAction; if (startGame != null) { CreateNewGame(startGame); StartGameAction startIt = new StartGameAction() { Game = _Game.Copy(), Sender = Core.Instance.ServerUser.ID, }; inGameAction = startIt; _Game.Phase = _Game.Phase.Next(_Game); _Game.Phase.Start(_Game); } // Make sure to check if we need to switch a phase ProcessAction(inGameAction); // A phase switches when the first action on the queue equals the action type specified // by the current phase if (_Game.ActionsQueue.Count() > 0 && _Game.ActionsQueue.Peek().GetType() == _Game.Phase.EndAction()) { // First process the action switching phases InGameAction actionToProcess = _Game.ActionsQueue.Dequeue(); ProcessAction(actionToProcess); // Swith to next phase and then start the phase _Game.Phase = _Game.Phase.Next(_Game); _Game.Phase.Start(_Game); } // If we have a notification, such as longest road, largest army or traderoutes changed, // server should initiate sending message if (_Game.ActionsQueue.Count > 0) { // Notify players of the next phase if present if (_Game.ActionsQueue.Peek().Sender == 0) { InGameAction serverNotification = _Game.ActionsQueue.Dequeue(); _CallBack.Receive(serverNotification); _Game.Phase.ProcessAction(serverNotification, _Game); } } }
public void Say(GameAction action) { _Proxy.Say(action); }
void OnGameActionReceived(GameAction action) { if (GameActionReceived != null) GameActionReceived(action); }
/// <summary> /// Calls by either the Receive() or ReceiveWhisper() <see cref="IChatCallback">IChatCallback</see> /// method implementations, and simply raises the OnProxyCallBackEvent() event /// to any subscribers /// </summary> /// <param name="sender">The <see cref="Common.Person">current chatter</see></param> /// <param name="message">The message</param> /// <param name="callbackType">Could be <see cref="CallBackType">CallBackType.Receive</see> or /// <see cref="CallBackType">CallBackType.ReceiveWhisper</see></param> public void Receive(GameAction action) { TurnAction turnAction = action as TurnAction; if (turnAction != null) { OnTurnActionReceived(turnAction); return; } InGameAction inGameAction = action as InGameAction; if (inGameAction != null) { OnInGameActionReceived(inGameAction); return; } LobbyAction lobbyAction = action as LobbyAction; if (lobbyAction != null) { OnLobbyActionReceived(lobbyAction); return; } //Fall through, raise default event OnGameActionReceived(action); }
public IAsyncResult BeginSay(GameAction action, AsyncCallback callback, object asyncState) { return _Proxy.BeginSay(action, callback, asyncState); }
public IAsyncResult BeginReceive(GameAction action, AsyncCallback callback, object asyncState) { throw new NotImplementedException(); }
private void SayToLobby(GameAction action) { LobbyChatAction ca = new LobbyChatAction(); ca.Sender = action.Sender; ca.ChatMessage = ((LobbyChatAction)action).ChatMessage; //_LobbyChatLog.Say((LobbyChatAction)action); BroadcastMessage(ca, null); }
/// <summary> /// Broadcasts the input msg parameter to all the <see cref="Common.Person"> /// Person</see> whos name matches the to input parameter /// by looking up the person from the internal list of chatters /// and invoking their ChatEventHandler delegate asynchronously. /// Where the MyEventHandler() method is called at the start of the /// asynch call, and the EndAsync() method at the end of the asynch call /// </summary> /// <param name="to">The persons name to send the message to</param> /// <param name="msg">The message to broadcast to all chatters</param> public void Whisper(string to, GameAction action) { GameEventArgs e = new GameEventArgs(); e.MessageType = MessageType.ReceiveWhisper; e.Person = _ServerUser; e.Action = action; try { GameEventHandler chatterTo; //carry out a critical section, that attempts to find the //correct Person in the list of chatters. //if a person match is found, the matched chatters //ChatEventHandler delegate is invoked asynchronously lock (syncObj) { chatterTo = getPersonHandler(to); if (chatterTo == null) { throw new KeyNotFoundException("The person whos name is " + to + " could not be found"); } } //do a async invoke on the chatter (call the MyEventHandler() method, and the //EndAsync() method at the end of the asynch call chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); } catch (KeyNotFoundException) { Console.WriteLine("whispering failed, could not find the player with name" + e.Person); } }
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); }
public void Receive(GameAction action) { }