/// <summary> /// Executes the specified arguments. /// </summary> /// <param name="args">The arguments.</param> /// <param name="ch">The ch.</param> /// <param name="client">The client.</param> /// <returns>string of the join command</returns> public override string Execute(string[] args, ClientOfServer client = null) { string name = args[0]; Maze maze = model.Join(name, client); return(maze.ToJSON()); }
public ClientOfServer Close(string name, ClientOfServer player) { MultiPlayerInfo mp = multiPlayerOnline[name]; multiPlayerOnline.Remove(name); return(mp.GetTheOtherPlayer(player)); }
/// <summary> /// Contains the player. /// </summary> /// <param name="player">The player.</param> /// <returns>bool if Contains player</returns> public bool ContainPlayer(ClientOfServer player) { if (Guest.Equals(player) || Host.Equals(player)) { return(true); } return(false); }
/// <summary> /// Executes the specified arguments. /// </summary> /// <param name="args">The arguments.</param> /// <param name="ch">The ch.</param> /// <param name="client">The client.</param> /// <returns>string of list command</returns> public override string Execute(string[] args, ClientOfServer client = null) { string list = JsonConvert.SerializeObject(model.List()); client.WriteToClient(list); client.DisconnectFromServer(); return(list); }
public override string Execute(string[] args, ClientOfServer client = null) { Direction move = (Direction)Enum.Parse(typeof(Direction), args[0]); Tuple <ClientOfServer, PlayerDirection> otherPlayerInfo = model.Play(move, client); ClientOfServer otherPlayer = otherPlayerInfo.Item1; otherPlayer.WriteToClient(otherPlayerInfo.Item2.ToJSON()); return(null); }
/// <summary> /// Executes the specified arguments to start. /// </summary> /// <param name="args">The arguments.</param> /// <param name="ch">The ch.</param> /// <param name="client">The client.</param> /// <returns>string of the solve maze</returns> public override string Execute(string[] args, ClientOfServer client = null) { string name = args[0]; int typeSolve = int.Parse(args[1]); MazeSolution s = model.Solve(name, typeSolve); client.WriteToClient(s.ToJSON()); client.DisconnectFromServer(); return(s.ToJSON()); }
/// <summary> /// Starts the specified of maze. /// </summary> /// <param name="name">The name of maze.</param> /// <param name="rows">The rows of maze.</param> /// <param name="cols">The cols of maze.</param> /// <param name="host">The host of maze.</param> public void Start(string name, int rows, int cols, ClientOfServer host) { Maze maze = Generate(name, rows, cols); MultiPlayerInfo mp = new MultiPlayerInfo() { Host = host, Maze = maze }; multiPlayerWaiting.Add(name, mp); }
/// <summary> /// Finds the Multi Player information. /// </summary> /// <param name="player">The player.</param> /// <returns>the current Multi Player </returns> /// <exception cref="System.Exception">This player does not exist.</exception> private MultiPlayerInfo FindMPInfo(ClientOfServer player) { foreach (MultiPlayerInfo mp in multiPlayerOnline.Values) { if (mp.ContainPlayer(player)) { return(mp); } } throw new Exception("This player does not exist."); }
/// <summary> /// Executes the specified arguments Generate Maze Command. /// </summary> /// <param name="args">The arguments.</param> /// <param name="ch">The ch.</param> /// <param name="client">The client.</param> /// <returns></returns> public override string Execute(string[] args, ClientOfServer client = null) { string name = args[0]; int rows = int.Parse(args[1]); int cols = int.Parse(args[2]); Maze maze = model.GenerateMaze(name, rows, cols); client.WriteToClient(maze.ToJSON()); client.DisconnectFromServer(); return(maze.ToJSON()); }
/// <summary> /// Plays the specified move. /// </summary> /// <param name="move">The move.</param> /// <param name="player">The player.</param> /// <returns>map of TcpClient, PlayerDirection </returns> public Tuple <ClientOfServer, PlayerDirection> Play(string move, ClientOfServer player) { MultiPlayerInfo mp = FindMPInfo(player); ClientOfServer otherPlayer = mp.GetTheOtherPlayer(player); PlayerDirection pd = new PlayerDirection() { GameName = mp.Maze.Name, Move = move }; return(new Tuple <ClientOfServer, PlayerDirection>(otherPlayer, pd)); }
/// <summary> /// Executes the specified arguments abstract method. /// </summary> /// <param name="args">The arguments.</param> /// <param name="ch">The ch.</param> /// <param name="client">The client.</param> /// <returns> /// string of the command /// </returns> public override string Execute(string[] args, ClientOfServer client = null) { string name = args[0]; ClientOfServer otherPlayer = model.Close(name, client); if (otherPlayer != null) { otherPlayer.DisconnectFromServer(); } client.DisconnectFromServer(); return(null); }
/// <summary> /// Joins the specified name. /// </summary> /// <param name="name">The name of maze.</param> /// <param name="guest">The guest client.</param> /// <returns the maze></returns> /// <exception cref="System.Exception">This maze does not exist - " + name</exception> public Maze Join(string name, ClientOfServer guest) { if (!multiPlayerWaiting.ContainsKey(name)) { throw new Exception("This maze does not exist - " + name); } MultiPlayerInfo mp = multiPlayerWaiting[name]; mp.Guest = guest; multiPlayerOnline[name] = mp; multiPlayerWaiting.Remove(name); return(mp.Maze); }
/// <summary> /// Executes the specified arguments the start command. /// </summary> /// <param name="args">The arguments.</param> /// <param name="ch">The ch.</param> /// <param name="client">The client.</param> /// <returns>string of json</returns> public override string Execute(string[] args, ClientOfServer client = null) { string name = args[0]; int rows = int.Parse(args[1]); int cols = int.Parse(args[2]); model.Start(name, rows, cols, client); while (!model.IsPair(name)) { Thread.Sleep(2000); } return(model.GetMaze(name).ToJSON()); }
/// <summary> /// Gets the other player. /// </summary> /// <param name="player">The player.</param> /// <returns>the tcp client</returns> /// <exception cref="System.Exception">the player does not exist</exception> public ClientOfServer GetTheOtherPlayer(ClientOfServer player) { if (Host.Equals(player)) { return(Guest); } if (Guest.Equals(player)) { return(Host); } throw new Exception("the player does not exist"); }
public override string Execute(string[] args, ClientOfServer client = null) { Tuple <ClientOfServer, PlayerDirection> otherPlayerInfo = model.Play(args[0], client); ClientOfServer otherPlayer = otherPlayerInfo.Item1; //using (NetworkStream stream = otherPlayer.GetStream()) //using (StreamWriter writer = new StreamWriter(stream)) //{ // writer.AutoFlush = true; // writer.Write(otherPlayerInfo.Item2.ToJSON()); //} otherPlayer.WriteToClient(otherPlayerInfo.Item2.ToJSON()); return(null); }
/// <summary> /// Executes the command. /// </summary> /// <param name="commandLine">The command line.</param> /// <param name="ch">The ClientHandler.</param> /// <param name="client">The client.</param> /// <returns>the string of the command </returns> public string ExecuteCommand(string commandLine, ClientOfServer client = null) { string[] arr = commandLine.Split(' '); string commandKey = arr[0]; if (!commands.ContainsKey(commandKey)) { return("Command not found"); } string[] args = arr.Skip(1).ToArray(); ICommand command = commands[commandKey]; //if (command is SingleCommand || command is CloseCommand) //{ // ch.StopConnetion(); //} return(command.Execute(args, client)); }
/// <summary> /// Executes the specified arguments abstract method. /// </summary> /// <param name="args">The arguments.</param> /// <param name="ch">The ch.</param> /// <param name="client">The client.</param> /// <returns> /// string of the command /// </returns> public override string Execute(string[] args, ClientOfServer client = null) { string name = args[0]; //JObject obj = new JObject(); //obj["close"] = "game-over"; ClientOfServer otherPlayer = model.Close(client); //using (NetworkStream stream = otherPlayer.GetStream()) //using (StreamWriter writer = new StreamWriter(stream)) //{ // writer.AutoFlush = true; // writer.Write(obj.ToString()); //} otherPlayer.DisconnectFromServer(); client.DisconnectFromServer(); //return obj.ToString(); return(null); }
/// <summary> /// Handles the client. /// </summary> /// <param name="client">The client.</param> public void HandleClient(ClientOfServer client) { new Task(() => { while (client.IsConnected()) { // Receive the command from the client. string commandLine = client.ReadFromClient(); if (!string.IsNullOrEmpty(commandLine)) { Console.WriteLine("Received command: {0}", commandLine); // Send the result to the client. string result = controller.ExecuteCommand(commandLine, client); client.WriteToClient(result); } } }).Start(); }
/// <summary> /// Executes the specified arguments abstract method. /// </summary> /// <param name="args">The arguments.</param> /// <param name="ch">The ch.</param> /// <param name="client">The client.</param> /// <returns> /// string of the command /// </returns> public abstract string Execute(string[] args, ClientOfServer client = null);