public void OnAnswerReceived(HostCommand commandData, Guid guid)
 {
     AnswerReceived(this, new AnswerReceivedEventArgs
     {
         PlayerName  = commandData.PlayerName,
         AnswerIndex = (int)commandData.QuestionAnswer
     });
 }
 public void OnPlayerDeparted(HostCommand commandData, Guid guid)
 {
     PlayerDeparted(this, new PlayerEventArgs {
         PlayerName = commandData.PlayerName
     });
     _manager.RemoveParticipant(guid);
     _playerToParticipantMap.Remove(commandData.PlayerName);
 }
        public async Task LeaveGameAsync(string playerName)
        {
            HostCommand command = new HostCommand()
            {
                Command    = Command.Leave,
                PlayerName = playerName
            };

            await this._managerCommunicationChannel
            .SendRemoteMessageAsync(command);
        }
        public async Task AnswerQuestionAsync(string playerName, int option)
        {
            HostCommand command = new HostCommand()
            {
                Command        = Command.Answer,
                PlayerName     = playerName,
                QuestionAnswer = option
            };

            await this._managerCommunicationChannel
            .SendRemoteMessageAsync(command);
        }
        /// <summary>
        /// When a message is received from the participant, this method will attempt to add the participant.
        /// </summary>
        private async Task OnPlayerJoinedAsync(HostCommand commandData, Guid guid)
        {
            Guid duplicatePlayerID;

            if (!_playerToParticipantMap.TryGetValue(commandData.PlayerName, out duplicatePlayerID))
            {
                _playerToParticipantMap.Add(commandData.PlayerName, guid);
                PlayerJoined(this, new PlayerEventArgs {
                    PlayerName = commandData.PlayerName
                });
            }
        }
        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]();
            });
        }