public void getMessageFromPlayerClient(Messages.GameMessages message,List<object> data, playerClient client) { switch (message) { case Messages.GameMessages.matchCreated: broadcast(Messages.GameMessages.gamesUpdated); break; case Messages.GameMessages.gamesUpdated: broadcast(Messages.GameMessages.gamesUpdated); break; case Messages.GameMessages.SendActiveMatches: broadcast(Messages.GameMessages.SendActiveMatches); break; case Messages.GameMessages.JoinMatch: String gameName = (String)data[0]; Match m = this.getMatch(gameName); if (m != null) m.Player2.PlayerName = client.playerName; broadcast(Messages.GameMessages.gamesUpdated); break; default: break; } updateServerForm(); }
void IGameObservable.notifyObservers(Messages.GameMessages msg, DataPlayer player, List <object> data) { foreach (IGameObserver obs in observers) { obs.update(player, msg, data); } }
public void broadcast(Messages.GameMessages msg) { foreach (KeyValuePair<String, playerClient> current in playerSocketList) { current.Value.getMessageFromServerManager(msg, null); } }
public void broadcast(Messages.GameMessages message) { WCFMessage newMessage = null; //Interpet message switch (message) { case Messages.GameMessages.PlayerConnected: newMessage = new WCFMessage() { Type = Messages.DTOType.lobby, Data = getLobbyDTO() }; break; case Messages.GameMessages.matchCreated: newMessage = new WCFMessage() { Type = Messages.DTOType.lobby, Data = getLobbyDTO() }; break; default: break; } //Send to all if (newMessage != null && players != null) { foreach (var player in players) { newMessage.Player = player; MyMessages[player.PlayerName].Add(newMessage); } } }
/// <summary> /// Adds WCFMessage to player's queue containing only the network message in the dto /// </summary> /// <param name="msg"></param> /// <param name="player"></param> private void addSignalMessage(Messages.GameMessages msg, DataPlayer player) { MyMessages[player.PlayerName].Add(new WCFMessage() { Player = player, Data = new DisputeDTO() { Message = msg }, Type = Messages.DTOType.message }); }
public void getMessageFromServerManager(Messages.GameMessages message, List <NamedParameter> data) { switch (message) { case Messages.GameMessages.gamesUpdated: //Tell client that game was created so they can react sendData(message.ToString()); break; default: break; } }
public void parseRequest(Messages.GameMessages header, List <object> args, object sender) { if (!isConnected() && header != Messages.GameMessages.connect) { return; } List <NamedParameter> pars = null; switch (header) { case Messages.GameMessages.connect: pars = DataTypes.createList(args, DataType.name); this.connect((String)pars[0].data, (String)pars[1].data, (String)pars[2].data); break; case Messages.GameMessages.CreateMatch: sendFeedback("sendMessage", "Begin Create Game"); this.sendData(header.ToString()); break; case Messages.GameMessages.JoinMatch: sendFeedback("sendMessage", "Begin Join Game"); pars = DataTypes.createList(args, DataType.name); String name = (String)pars[0].data; sendData(Messages.GameMessages.JoinMatch.ToString()); sendData(name); break; case Messages.GameMessages.LeaveMatch: sendFeedback("sendMessage", "Begin Join Game"); sendData(Messages.GameMessages.LeaveMatch.ToString()); break; case Messages.GameMessages.UpdateMatches: sendData(Messages.GameMessages.SendActiveMatches); break; case Messages.GameMessages.playerQuit: this.isRunning = false; sendFeedback("sendMessage", "Player quit"); this.sendData(header.ToString()); break; case Messages.GameMessages.SendActiveMatches: this.sendData(header.ToString()); break; default: break; } }
/// <summary> /// Updates data from server and notifies observers of new data /// </summary> /// <param name="code"></param> void IConnectionObservable.notifyObservers(object code = null) { getNewMessages(); foreach (WCFMessage msg in serverMessages) { parseNewMessage(msg); foreach (IConnectionObserver obs in myObservers) { obs.update(createDataPackage()); serverMessage = Messages.GameMessages.UpdateMatch; viewMessage = Messages.LobbyViewMessage.UpdateAll; } } serverMessages.Clear(); }
/// <summary> /// This method is used to get messages from the Model layer (GameDispute), and pass them on to the players. /// /// </summary> /// <param name="msg"></param> /// <param name="player"></param> /// <param name="data"></param> void IGameObserver.update(DataPlayer player, Messages.GameMessages msg, List <object> data) { DisputeDTO dto = null; switch (msg) { case Messages.GameMessages.GetSOM: dto = new DisputeDTO() { SomRequest = (SoMRequest)data[0], Message = Messages.GameMessages.GetSOM }; goto case Messages.GameMessages.none; case Messages.GameMessages.ArgumentDone: break; case Messages.GameMessages.GameOver: dto = new DisputeDTO() { Message = Messages.GameMessages.GameOver }; goto case Messages.GameMessages.none; case Messages.GameMessages.NotPlayerTurn: dto = new DisputeDTO() { Message = Messages.GameMessages.NotPlayerTurn }; goto case Messages.GameMessages.none; case Messages.GameMessages.none: this.MyMessages[player.PlayerName].Add(new WCFMessage() { Player = player, Type = Messages.DTOType.match, Data = dto }); break; default: break; } }
public void sendMessage(Messages.GameMessages header, List<object> args, object sender) { switch (header) { case Messages.GameMessages.matchCreated: broadcast(Messages.GameMessages.gamesUpdated); break; case Messages.GameMessages.SendActiveMatches: broadcast(Messages.GameMessages.SendActiveMatches); break; case Messages.GameMessages.connect: break; case Messages.GameMessages.GameOver: //Tell everyclient the game is over break; default: break; }; }
void parseMessage(String header) { if (!Enum.IsDefined(typeof(Messages.GameMessages), header)) { Console.WriteLine("Bad Header:" + header); return; } sendFeedback("parseMessage", header); Messages.GameMessages data = (Messages.GameMessages)Enum.Parse(typeof(Messages.GameMessages), header); switch (data) { case Messages.GameMessages.dataReceived: break; case Messages.GameMessages.CreateMatch: tryCreateMatch(); break; case Messages.GameMessages.JoinMatch: String gameName = getData(); tryJoinGame(gameName); break; case Messages.GameMessages.LeaveMatch: tryLeaveGame(); break; case Messages.GameMessages.SendActiveMatches: SendActiveMatches(); break; case Messages.GameMessages.playerQuit: tryLeaveGame(); //Quit player from game if it's in one ClientSocket.Close(); serverManager.disconnect(this.playerName); playerConnected = false; break; default: break; } }
/// <summary> /// Decision Maker when game is playing.Gets messages from server and decides what to do with them /// </summary> /// <param name="disputeDTO"></param> private void parseMatchDTO(DisputeDTO disputeDTO) { Messages.GameMessages dtoMessage = disputeDTO.Message; serverMessage = dtoMessage; switch (dtoMessage) { case Messages.GameMessages.StartingMatch: this.inMatch = disputeDTO.Match; viewMessage = Messages.LobbyViewMessage.StartGame; break; case Messages.GameMessages.ArgumentDone: this.inMatch = disputeDTO.Match; break; case Messages.GameMessages.GetSOM: serverMessage = dtoMessage; //this was already done above but just emphasize myDTO = disputeDTO; break; case Messages.GameMessages.NotPlayerTurn: sendFeedback("parseMatchDTO", dtoMessage); break; case Messages.GameMessages.UpdateMatch: this.inMatch = disputeDTO.Match; break; case Messages.GameMessages.stillAlive: this.inMatch = disputeDTO.Match; break; case Messages.GameMessages.GameOver: serverMessage = dtoMessage; //this was already done above but just emphasize break; } }
void IConnectionObserver.update(object code) { if (code != null) { List <NamedParameter> dataList = (List <NamedParameter>)code; List <NamedParameter> m = DataTypes.findAll(dataList, DataType.serverMessage); if (m != null && m.Count > 0) { Messages.GameMessages msg = (Messages.GameMessages)m[0].data; switch (msg) { case Messages.GameMessages.UpdateMatch: updateData(dataList); break; case Messages.GameMessages.UpdateAll: updateData(dataList); break; case Messages.GameMessages.GetSOM: List <NamedParameter> somRequest = DataTypes.findAll(dataList, DataType.SoMRequest); SOM selected = myView.selectSoM((somRequest[0].data as SoMRequest).Allowed, (somRequest[0].data as SoMRequest).Result); MessageSentFromView(MatchViewMessage.SoM, new List <object>() { selected }, this); break; case Messages.GameMessages.GameOver: //Determine who won bool won = Match.Turn == 0 ? player.Equals(Match.Player1) : player.Equals(Match.Player2); myView.gameOver(won); break; } //End Switch } //End MEssage if } //End Code if }
public void sendData(Messages.GameMessages msg) { sendData(msg.ToString()); }
public void addDataMessage(DataPlayer player, Messages.GameMessages msg, String data) { manager.addNewMessage(player, msg, data); }
/// <summary> /// This method looks at the type of request the player sent, /// and puts the appropriate data and type in to msg.data before adding it to MyMessages /// </summary> /// <param name="player"></param> /// <param name="msg"></param> public void addNewMessage(DataPlayer player, Messages.GameMessages msg, String data = null) { player = players.Find(n => n.Equals(player)); if (player == null) { sendFeedback("addNewMessage", "Player Not Found\n PlayerName:" + player.PlayerName + "\n" + msg.ToString()); return; } if (player.InGame)//Use in game handler { IPlayable game = games.Find(n => n.Player1.Equals(player) || n.Player2.Equals(player)); DataPlayer player1 = players.Find(n => n.Equals(game.Player1)), player2 = players.Find(n => n.Equals(game.Player2)); switch (msg) { case Messages.GameMessages.UpdateMatch: updatePlayerInMatch(player); break; case Messages.GameMessages.somChosen: SOM som = (SOM)Enum.Parse(typeof(SOM), data); game.getMessageFromConnection(Messages.GameMessages.somChosen, player, new List <object> { som }); game.Match.Transcript += "SoM chosen:" + som.ToString() + "\n"; break; case Messages.GameMessages.GameOver: //Reset players,remove finished match from list player1.InGame = false; player2.InGame = false; Matches.Remove(player1.ActiveMatch); player1.ActiveMatch = new Match(); player1.ActiveMatch = null; player2.ActiveMatch = null; game = null; break; case Messages.GameMessages.SendActiveMatches: updatePlayerInMatch(player); break; default: //Covers all argument requests game.getMessageFromConnection(msg, player, null); break; } } else //Use lobby handler { switch (msg) { case Messages.GameMessages.SendActiveMatches: updatePlayer(player); break; case Messages.GameMessages.CreateMatch: createMatch(player); break; case Messages.GameMessages.LeaveMatch: leaveMatch(player); break; case Messages.GameMessages.UpdateAll: updatePlayer(player); break; case Messages.GameMessages.JoinMatch: if (data == null) { addSignalMessage(Messages.GameMessages.InvalidMatch, player); } else { joinMatch(data, player); } break; case Messages.GameMessages.PlayerReady: lockInPlayer(player); break; case Messages.GameMessages.PlayerNotReady: lockOutPlayer(player); break; case Messages.GameMessages.UpdateMatch: updatePlayerInMatch(player); break; default: break; } } }
/// <summary> /// Gets request from view controller and creates appropriate message for it /// </summary> /// <param name="header"></param> /// <param name="args"></param> /// <param name="sender"></param> public void parseRequest(Messages.GameMessages header, List <object> args, object sender) { if (!isConnected() && header != Messages.GameMessages.connect) { return; } List <NamedParameter> pars = null; switch (header) { #region LobbyRequests case Messages.GameMessages.connect: pars = DataTypes.createList(args, DataType.name); this.connect(null, null, pars[0].data.ToString()); //this.connect((String)pars[0].data, (String)pars[1].data, (String)pars[2].data); break; case Messages.GameMessages.CreateMatch: sendFeedback("parseRequest", "Begin Create Match"); createMatch(); break; case Messages.GameMessages.JoinMatch: sendFeedback("parseRequest", "Begin Join Match"); pars = DataTypes.createList(args, DataType.name); if (pars == null || pars.Count < 1) { sendFeedback("parseRequest", "Error in match name"); return; } String name = ((Match)pars[0].data).Player1.PlayerName; joinGame(name); break; case Messages.GameMessages.LeaveMatch: sendFeedback("parseRequest", "Begin Leave Game"); leaveGame(); break; case Messages.GameMessages.UpdateMatches: updateMatches(); break; case Messages.GameMessages.playerQuit: this.connected = false; sendFeedback("sendMessage", "Player quit"); doQuit(); break; case Messages.GameMessages.GameOver: sendFeedback("sendMessage", "Game over"); remoteProxy.addNewMessage(player, Messages.GameMessages.GameOver); break; case Messages.GameMessages.UpdateAll: //Do nothing, notify will update anyway, but we don't // want it to look like we forgot this case break; case Messages.GameMessages.SendCharacterToServer: pars = DataTypes.createList(args, DataType.character); remoteProxy.addCharacter((CharacterData)pars[0].data, player); break; case Messages.GameMessages.PlayerReady: remoteProxy.addNewMessage(player, Messages.GameMessages.PlayerReady); break; case Messages.GameMessages.SendGoalToServer: pars = DataTypes.createList(args, DataType.Goal); remoteProxy.addGoal((Goal)pars[0].data, player); break; #endregion #region DATA REQUESTS case Messages.GameMessages.SendActiveMatches: updateMatches(); break; case Messages.GameMessages.sendPlayers: getPlayers(); break; #endregion #region ACTIONREQUESTS case Messages.GameMessages.doAction: remoteProxy.addNewMessage(player, (Messages.GameMessages)args[0]); break; case Messages.GameMessages.UpdateMatch: remoteProxy.addNewMessage(player, Messages.GameMessages.UpdateMatch); break; case Messages.GameMessages.somChosen: remoteProxy.addNewMessage(player, Messages.GameMessages.somChosen, args[0].ToString()); break; #endregion default: break; } if (connected) { (this as IConnectionObservable).notifyObservers(); } }
public void sendFeedback(string functionName, Messages.GameMessages feedback) { FeedbackWriter.WriteLine("Method:" + functionName + " Feedback:" + feedback.ToString()); }
public void addNewMessage(DataPlayer player, Messages.GameMessages message) { manager.addNewMessage(player, message); }
void sendData(Messages.GameMessages data) { sendData(data.ToString()); }
/// <summary> /// This method looks at the type of request the player sent, /// and puts the appropriate data and type in to msg.data before adding it to MyMessages /// </summary> /// <param name="player"></param> /// <param name="msg"></param> public void addNewMessage(DataPlayer player, Messages.GameMessages msg, String data = null) { player = players.Find(n => n.Equals(player)); if (player == null) { sendFeedback("addNewMessage", "Player Not Found\n PlayerName:" + player.PlayerName + "\n" + msg.ToString()); return; } if (player.InGame)//Use in game handler { DisputeGame game = games.Find(n => n.Player1.Equals(player) || n.Player2.Equals(player)); DataPlayer player1 = players.Find(n => n.Equals(game.Player1)), player2 = players.Find(n => n.Equals(game.Player2)); switch (msg) { case Messages.GameMessages.UpdateMatch: updatePlayerInMatch(player); break; case Messages.GameMessages.somChosen: int i = data.IndexOf("$"); String name = data.Substring(0, i); double value = Double.Parse(data.Substring(i + 1)); game.getMessageFromConnection(Messages.GameMessages.somChosen, player, new List <object> { name, value }); game.Match.Transcript += "SoM chosen:" + name + "\n"; break; case Messages.GameMessages.GameOver: player1.InGame = false; player2.InGame = false; Matches.Remove(player1.ActiveMatch); player1.ActiveMatch = new Match(); player1.ActiveMatch = null; player2.ActiveMatch = null; game = null; break; default: //Covers all argument requests game.getMessageFromConnection(msg, player, null); break; } } else //Use lobby handler { switch (msg) { case Messages.GameMessages.SendActiveMatches: updatePlayer(player); break; case Messages.GameMessages.CreateMatch: createMatch(player); break; case Messages.GameMessages.LeaveMatch: leaveMatch(player); break; case Messages.GameMessages.UpdateAll: updatePlayer(player); break; case Messages.GameMessages.JoinMatch: if (data == null) { addSignalMessage(Messages.GameMessages.InvalidMatch, player); } else { joinMatch(data, player); } break; case Messages.GameMessages.PlayerReady: lockInPlayer(player); break; case Messages.GameMessages.PlayerNotReady: lockOutPlayer(player); break; default: break; } } }
public void parseMessage(String header) { if (!Enum.IsDefined(typeof(Messages.GameMessages), header)) { sendFeedback("parseMessage:", "Bad Header:" + header); return; } sendFeedback("parseMessage", header); Messages.GameMessages data = (Messages.GameMessages)Enum.Parse(typeof(Messages.GameMessages), header); switch (data) { case Messages.GameMessages.connected: connected = true; sendFeedback("parseMessage", "Connected"); break; case Messages.GameMessages.playerExists: //This send data needs to be here because the message came from the connect from server //not the listen from playerClient, the connect is expecting an ack sendData(Messages.GameMessages.dataReceived.ToString()); MessageBox.Show("Player Exists"); break; case Messages.GameMessages.SendActiveMatches: //Get active games getGames(); getActiveMatch(); break; case Messages.GameMessages.matchCreated: //Game was successfully created, so it is my Active game now sendData(Messages.GameMessages.dataReceived); sendFeedback("parseMessage", "Game Created"); break; case Messages.GameMessages.gamesUpdated: sendData(Messages.GameMessages.SendActiveMatches.ToString()); break; case Messages.GameMessages.playerLeftMatch: sendFeedback("parseMessage", this.PlayerName + " has left the game"); sendData(Messages.GameMessages.SendActiveMatches.ToString()); break; case Messages.GameMessages.PlayerNotInMatch: sendFeedback("parseMessage", this.PlayerName + " is not in a game"); break; case Messages.GameMessages.PlayerInMatch: sendFeedback("parseMessage", this.PlayerName + " is already in a game"); break; case Messages.GameMessages.MatchIsFull: sendFeedback("parseMessage", "Game already has two players"); break; case Messages.GameMessages.PlayerJoinedGame: sendFeedback("parseMessage", playerName + " joined game"); sendData(Messages.GameMessages.SendActiveMatches.ToString()); break; case Messages.GameMessages.emptyDataReceived: sendFeedback("parseMessage", " Bad data sent"); break; default: break; }//End case notifyObservers(); }
/// <summary> /// Game decision point. This method gets messages from connection and decides what arguments to call, as well /// as taking care of runtime handlers for SoM requests /// </summary> /// <param name="msg"></param> /// <param name="player"></param> /// <param name="data"></param> public void getMessageFromConnection(Messages.GameMessages msg, DataPlayer player, List <object> data) { if (!checkTurn(player))//Not right turn? { (this as IGameObservable).notifyObservers(Messages.GameMessages.NotPlayerTurn, player, null); Match.updateTranscript("Not your turn"); return; } CharacterData attacker = Turn == 0?Player1.Character:Player2.Character, defender = Turn == 0?Player2.Character:Player1.Character; ArgNames actionName = 0; ArgumentFeedback argFeedback = null; if (waiting && msg != Messages.GameMessages.somChosen) { Match.updateTranscript("Waiting for SOM"); return; } switch (msg) { ///This case handles the response from a request to get SoM from the user ///We need to translate the response (SoMName,value) into values the Handler needs (JoySorrowValue,AngerFearValue) case Messages.GameMessages.somChosen: SOM name = (SOM)data[0]; switch (name) { case SOM.Joy: argFeedback = SoMHandler(1, 0); break; case SOM.Sorrow: argFeedback = SoMHandler(-1, 0); break; case SOM.Anger: argFeedback = SoMHandler(0, 1); break; case SOM.Fear: argFeedback = SoMHandler(0, -1); break; } waiting = false; //Just in case break; case Messages.GameMessages.Trick: actionName = ArgNames.Trick; argFeedback = arguments[actionName].doArgument(attacker, defender, World); if ((arguments[actionName] as Trick).repeatTurn()) { Turn--; //We turn it back then the regular turn counter turns it back to my turn } break; case Messages.GameMessages.Manipulate: actionName = ArgNames.Manipulate; goto case Messages.GameMessages.SoMDependentArgument; case Messages.GameMessages.Taunt: actionName = ArgNames.Taunt; goto case Messages.GameMessages.SoMDependentArgument; case Messages.GameMessages.Focus: actionName = ArgNames.Focus; goto case Messages.GameMessages.SoMDependentArgument; case Messages.GameMessages.Empathy: actionName = ArgNames.Empathy; argFeedback = (arguments[actionName] as Empathy).doArgument(attacker, defender, World); if (argFeedback != null && argFeedback.result == Result.None) { Match.updateTranscript("Empathy cannot be used\n"); } break; case Messages.GameMessages.Bluff: actionName = ArgNames.Bluff; goto case Messages.GameMessages.GenericArgument; case Messages.GameMessages.Charm: actionName = ArgNames.Charm; goto case Messages.GameMessages.GenericArgument; case Messages.GameMessages.Coerce: actionName = ArgNames.Coerce; goto case Messages.GameMessages.GenericArgument; case Messages.GameMessages.Convince: actionName = ArgNames.Convince; goto case Messages.GameMessages.GenericArgument; case Messages.GameMessages.Scare: actionName = ArgNames.Scare; goto case Messages.GameMessages.GenericArgument; case Messages.GameMessages.GenericArgument: argFeedback = arguments[actionName].doArgument(attacker, defender, World); break; case Messages.GameMessages.SoMDependentArgument: (arguments[actionName] as SoMDependentArgument).Selector = this; argFeedback = (arguments[actionName] as SoMDependentArgument).doArgument(attacker, defender, World); //If true then it succeeded so it needs to get SoM if (argFeedback == null) { waiting = true; } break; }//End Switch //Update transcript if there is feedback if (argFeedback != null) { argFeedback.playerName = player.PlayerName; Match.updateTranscript(argFeedback); } if (!waiting) { //Check Game over if (Match.Goal.isGoalReached(defender)) { (this as IGameObservable).notifyObservers(Messages.GameMessages.GameOver, Match.Player1, null); (this as IGameObservable).notifyObservers(Messages.GameMessages.GameOver, Match.Player2, null); } else//Not over { //Tell players to update and go to next turn (this as IGameObservable).notifyObservers(Messages.GameMessages.ArgumentDone, player, null); Turn = (Turn + 1) % 2; } } }
public void sendMessage(Messages.GameMessages message) { manager.broadcast(message); }