//public TcpClient GetSecondPlayer(TcpClient client) public ICClientHandler GetSecondPlayer(ICClientHandler client) { if (client1.Equals(client)) { return(client2); } return(client1); }
//public string Execute(string[] args, TcpClient client) public string Execute(string[] args, ICClientHandler client) { string move = args[0]; String s = model.Play(move, client); return(s); }
//public string Play(string move, TcpClient client) public string Play(string move, ICClientHandler client) { StringBuilder sb = new StringBuilder(); sb.Append("{\n"); sb.Append("\"Name\": " + "\"" + this.myMaze.Name + "\",\n"); sb.Append("\"Direction\": " + move + "\n"); sb.Append("}"); return(sb.ToString()); }
//public string Execute(string[] args, TcpClient client) public string Execute(string[] args, ICClientHandler client) { string name = args[0]; string ans = model.Close(name, client); return(ans); }
//public string Execute(string[] args, TcpClient client) public string Execute(string[] args, ICClientHandler client) { string name = args[0]; int algo = int.Parse(args[1]); SolutionDetails <Direction> sol = model.SolveMaze(name, algo); PasrseSolve p = new PasrseSolve(name, sol.solv.getSolve(), sol.NodesEvaluated); Console.WriteLine(JsonConvert.SerializeObject(p)); return(JsonConvert.SerializeObject(p)); }
public ClientHandler(TcpClient client1) { this.client = client1; stream = client.GetStream(); //to accept data from client reader = new BinaryReader(stream); //to send data from client writer = new BinaryWriter(stream); //save point to this object cclient = new CClientHandler(this); }
//public string Execute(string[] args, TcpClient client) public string Execute(string[] args, ICClientHandler client) { string name = args[0]; int rows = int.Parse(args[1]); int cols = int.Parse(args[2]); string ans = model.StartGame(name, rows, cols, client); return(ans); }
//public void AddClient(TcpClient c) public void AddClient(ICClientHandler c) { if (numOfClient == 0) { client1 = c; } else { client2 = c; //client1.Client.Send() - myMaze.ToJSON() } numOfClient++; }
// public string Execute(string[] args, TcpClient client) public string Execute(string[] args, ICClientHandler client) { string name = args[0]; Game g = model.JoinGame(name, client); if (g != null) { return(g.ToJSON()); } else { return("no such available game"); } }
//public string StartGame(string name, int rows, int cols, TcpClient client) public string StartGame(string name, int rows, int cols, ICClientHandler client) { if (games.ContainsKey(name)) { return("there is such game"); } else { Game g = new Game(name, rows, cols); g.AddClient(client); clientInGames.Add(client, name); this.games.Add(name, g); return("wait for the second player"); } }
//public string ExecuteCommand(string commandLine, TcpClient client) public string ExecuteCommand(string commandLine, ICClientHandler client) { string[] arr = commandLine.Split(' '); string commandKey = arr[0]; Console.WriteLine("controller:" + commandKey); if (!commands.ContainsKey(commandKey)) { return("Command not found"); } string[] args = arr.Skip(1).ToArray(); ICommand command = commands[commandKey]; return(command.Execute(args, client)); }
//public string Close(string name, TcpClient client) public string Close(string name, ICClientHandler client) { Game g; ICClientHandler secondClient = null; if (games.TryGetValue(name, out g)) { secondClient = g.GetSecondPlayer(client); controller.SendToClient(secondClient, "close"); } //update the DB this.games.Remove(name); this.clientInGames.Remove(client); this.clientInGames.Remove(secondClient); return("close"); }
//public string Play(string move, TcpClient client) public string Play(string move, ICClientHandler client) { string name; Game g; if (clientInGames.TryGetValue(client, out name)) { if (games.TryGetValue(name, out g)) { string s = g.Play(move, client); controller.SendToClient(g.GetSecondPlayer(client), s); return("got your move, and sent it to the second client"); } } return("the game not found"); }
//public string Execute(string[] args, TcpClient client) public string Execute(string[] args, ICClientHandler client) { string name = args[0]; int rows = int.Parse(args[1]); int cols = int.Parse(args[2]); Maze maze = model.GenerateMaze(name, rows, cols); if (maze != null) { return(maze.ToJSON()); } else { return("alredy exist"); } }
//public Game JoinGame(string name, TcpClient client) public Game JoinGame(string name, ICClientHandler client) { Game g; if (games.TryGetValue(name, out g)) { if (g.numOfClient == 1) { g.AddClient(client); this.clientInGames.Add(client, name); // Console.WriteLine("check" + g.ToJSON()); controller.SendToClient(g.GetSecondPlayer(client), g.ToJSON()); return(g); } else { return(null); } } else { return(null); } }
//public string Execute(string[] args, TcpClient client) public string Execute(string[] args, ICClientHandler client) { String ans = model.list(); return(ans); }
//public void SendToClient(TcpClient client, string msg) //{ //sender.SendToClient(client, msg); //} public void SendToClient(ICClientHandler client, string msg) { client.sendMssage(msg); }