/// <summary> /// Send a command to client connection. /// </summary> /// <param name="target">The user target.</param> /// <param name="command">The command to send.</param> /// <param name="arguments">Additional arguments to send.</param> public async Task <string> SendCommand(ERemoteCommand command, string arguments) { // If no client found, player is the host // Then trigger local command // Else send command request to remote player if (Client == null) { return(ExecuteCommandsLocally(command, arguments)); } else { using (NetworkStream ns = Client.GetStream()) { using (StreamWriter sw = new StreamWriter(ns)) { await sw.WriteLineAsync(command.ToString()); await sw.WriteLineAsync(arguments); sw.Flush(); } using (StreamReader sr = new StreamReader(ns)) { return(await sr.ReadLineAsync()); } } } }
/// <summary> /// Execute incomming commands on host player. /// </summary> /// <param name="command">The command to execute.</param> /// <param name="arguments">Additional arguments to the command.</param> /// <returns>Returns the command results.</returns> private string ExecuteCommandsLocally(ERemoteCommand command, string arguments) { Hider hider = this as Hider; switch (command) { case ERemoteCommand.StartGame: StartGame(); break; case ERemoteCommand.EndGame: EndGame(); break; case ERemoteCommand.GuessRoom: return(hider?.CheckRoom(arguments).ToString()); case ERemoteCommand.AddPoints: AddPoints(int.Parse(arguments)); break; case ERemoteCommand.GetMap: return(JsonConvert.SerializeObject(hider?.Map)); case ERemoteCommand.GetPosition: return(JsonConvert.SerializeObject(hider?.GetPosition())); default: return(string.Empty); } return(string.Empty); }
/// <summary> /// Send a command to all client connections. /// </summary> /// <param name="command">The command to send.</param> /// <param name="arguments">Additional arguments to send.</param> private async Task SendCommandToAllRemote(ERemoteCommand command, string arguments) { foreach (Hider hider in _hiders) { await hider.SendCommand(command, arguments); } foreach (Seeker seeker in _seekers) { await seeker.SendCommand(command, arguments); } }