public string GetTcpCommand(Command command) { string tcpCommand = null; switch (command.Cmd) { case Commands.Hello: if (command.Parameters.Length > 0 && command.Parameters[0].Length > 0) { tcpCommand = $"{TcpCommandsDict[command.Cmd]} {string.Join(' ', command.Parameters)}"; } else { throw new CantCreateCommandException($"Can't create tcp tcp command of command {command.Cmd}. Invalid parameters."); } break; case Commands.Helo: if (command.Parameters.Length > 0 && command.Parameters[0].Length > 0) { tcpCommand = $"{TcpCommandsDict[command.Cmd]} {string.Join(' ', command.Parameters)}"; } else { throw new CantCreateCommandException($"Can't create tcp tcp command of command {command.Cmd}. Invalid parameters."); } break; case Commands.Start: if (command.Parameters == null) { tcpCommand = $"{TcpCommandsDict[command.Cmd]}"; } else { throw new CantCreateCommandException($"Can't create tcp command of command {command.Cmd}. No parameter is allowed for this command."); } break; case Commands.Fire: if (command.Parameters.Length == 2 || command.Parameters.Length == 3) { string yCordinate = command.Parameters[0], message = command.Parameters.Length > 2 ? string.Join(' ', command.Parameters.Skip(2).Take(command.Parameters.Length - 2).ToArray()) : null; bool xCordIsValidNumber = int.TryParse(command.Parameters[1], out int xCordinate); if (YcordinateDict.ContainsKey(yCordinate) && (xCordIsValidNumber && xCordinate >= 1 && xCordinate <= 10)) { tcpCommand = $"{TcpCommandsDict[command.Cmd]} {yCordinate}{xCordinate}"; tcpCommand += message != null ? " " + message : ""; } else { throw new CantCreateCommandException($"Can't create tcp command of command {command.Cmd}. Parameters for cordinates are invalid."); } } else { throw new CantCreateCommandException($"Can't create tcp command of command {command.Cmd}. Number of parameters must be two or three."); } break; case Commands.Quit: tcpCommand = $"{TcpCommandsDict[command.Cmd]}"; break; default: throw new NotImplementedException($"Can't create tcp command. Command: {command.Cmd} is not implemented."); } return(tcpCommand); }
public Command GetCommand(string tcpCommand) { var substrings = tcpCommand.Split(' '); var key = substrings[0].ToUpper(); Command command = null; if (CommandsDict.ContainsKey(key)) { var commandEnum = CommandsDict[key]; switch (commandEnum) { case Commands.Hello: if (substrings.Length > 1 && substrings[1].Length > 0) { command = new Command(commandEnum, substrings.Skip(1).Take(substrings.Length - 1).ToArray()); } else { throw new CantCreateCommandException($"Can't create command of input string {tcpCommand}. Parameter for Name is invalid."); } break; case Commands.Helo: if (substrings.Length > 1 && substrings[1].Length > 0) { command = new Command(commandEnum, substrings.Skip(1).Take(substrings.Length - 1).ToArray()); } else { throw new CantCreateCommandException($"Can't create command of input string {tcpCommand}. Parameter for Name is invalid."); } break; case Commands.Start: command = new Command(commandEnum, null); break; case Commands.Fire: if (substrings.Length > 1) { string yCordinate = substrings[1].Substring(0, 1).ToUpper(), xCordinate = substrings[1].Substring(1); bool xCordIsValidNumber = int.TryParse(xCordinate, out int xCordInt); if (YcordinateDict.ContainsKey(yCordinate) && (xCordIsValidNumber && xCordInt >= 1 && xCordInt <= 10)) { var parameters = new List <string> { yCordinate, xCordinate }; if (substrings.Length > 2) { parameters.AddRange(substrings.Skip(2).Take(substrings.Length - 2).ToList()); } command = new Command(commandEnum, parameters.ToArray()); } else { throw new CantCreateCommandException($"Can't create tcp command of command {command.Cmd}. Parameters for cordinates are invalid."); } } else { throw new CantCreateCommandException($"Can't create command of input string {tcpCommand}. Must be at least one parameter for this command."); } break; case Commands.Quit: command = new Command(commandEnum, null); break; case Commands.Help: command = new Command(commandEnum, null); break; default: throw new NotImplementedException($"Can't create command. Command: {command.Cmd} is not implemented."); } return(command); } else { throw new CantCreateCommandException($"Can't create command of input string {tcpCommand}. Key: {key} doesn't exists."); } }