public HostCommunicator() { _manager.ParticipantConnected += (async(sender, e) => { // A player has joined the game. _participantCommunicationChannels.Add(_manager.CreateCommunicationChannel(e.Id)); HostCommandData data = new HostCommandData { PlayerName = e.Message.ToString(), Data = null, Command = Command.Join }; await OnPlayerJoinedAsync(data, e.Id); }); _managerCommunicationChannel.MessageReceived += ((sender, e) => { // Decode the message. The command of the message is the first phrase before a '-' character. string[] message = e.Message.ToString().Split('-'); switch (message[0]) { case LEAVE_COMMAND: // The participant sent a message to leave the game. OnPlayerDeparted( new HostCommandData { PlayerName = message[1], Command = Command.Leave, }, _playerToParticipantMap[message[1]]); break; case ANSWER_COMMAND: // The participant sent a message to answer a question. OnAnswerReceived( new HostCommandData { PlayerName = message[1], Command = Command.Answer, Data = int.Parse(message[2]) }, _playerToParticipantMap[message[1]]); break; } }); }
public HostCommunicator() { _manager.ParticipantConnected += (async(sender, e) => { // A player has joined the game. _participantCommunicationChannels.Add(_manager.CreateCommunicationChannel(e.Id)); HostCommand data = new HostCommand { PlayerName = e.Message.ToString(), QuestionAnswer = 0, Command = Command.Join }; await OnPlayerJoinedAsync(data, e.Id); }); _managerCommunicationChannel.MessageReceived += ((sender, e) => { // De serialize the message and put it in a command variable. object data = new HostCommand(); e.GetDeserializedMessage(ref data); var command = data as HostCommand; // Place the indicated actions in an array, so we can index them based on the Command enum value. Action[] actions = new Action[] { // Do nothing if Command == Join, because we already do this on ParticipantConnected. () => { }, // if Command == Leave () => { OnPlayerDeparted(command, _playerToParticipantMap[command.PlayerName]); }, // if Command == Answer () => { OnAnswerReceived(command, _playerToParticipantMap[command.PlayerName]); } }; // Index the array from the Command and call the associated Action lambda. actions[(int)command.Command](); }); }