/// <summary> /// Execute. /// </summary> /// <param name="args">The arguments.</param> /// <param name="model"></param> /// <param name="client">The client.</param> /// <returns></returns> public RecieveInfo Execute(string[] args, IServerModel model, TcpClient client = null) { this.client = client; NetworkStream stream = client.GetStream(); StreamReader reader = new StreamReader(stream); string result = SendAndRecieve.RecieveInfo(reader); client.GetStream().Flush(); // the first finished before the other joined if (result.Equals("aborted")) { return(new RecieveInfo("close", false, "close")); } // if the message is invalid else if (!result.Equals("invalid command")) { // save start and end point Maze maze = Maze.FromJSON(result); model.CurrentRow = maze.InitialPos.Row; model.CurrentCol = maze.InitialPos.Col; model.OtherCurrentRow = maze.InitialPos.Row; model.OtherCurrentCol = maze.InitialPos.Col; model.Maze = maze; model.GameName = maze.Name; model.EndRow = maze.GoalPos.Row; model.EndCol = maze.GoalPos.Col; Game.server = client; Game.StartGame(model); } return(new RecieveInfo(result, true, "start")); }
public RecieveInfo Execute(string[] args, IServerModel model, TcpClient client = null) { NetworkStream stream = client.GetStream(); StreamReader reader = new StreamReader(stream); string result = SendAndRecieve.RecieveInfo(reader); client.GetStream().Flush(); client.GetStream().Close(); client.Close(); result = result.Substring(1, result.Length - 2); if (result != "") { List <string> temp = new List <string>(); List <string> arr = result.Split(',').ToList(); foreach (string s in arr) { temp.Add(s.Substring(1, s.Length - 2)); } model.GameMultiPlayerList = temp; } else { model.GameMultiPlayerList = new List <string>(); } return(new RecieveInfo(result, false, "list")); }
/// <summary> /// Execute. /// </summary> /// <param name="args">The arguments.</param> /// <param name="model"></param> /// <param name="client">The client.</param> /// <returns></returns> public RecieveInfo Execute(string[] args, IServerModel model, TcpClient client = null) { NetworkStream stream = client.GetStream(); StreamReader reader = new StreamReader(stream); string result = SendAndRecieve.RecieveInfo(reader); client.GetStream().Flush(); if (!result.Equals("invalid command")) { // save start and end point Maze maze = Maze.FromJSON(result); model.CurrentRow = maze.InitialPos.Row; model.CurrentCol = maze.InitialPos.Col; model.OtherCurrentRow = maze.InitialPos.Row; model.OtherCurrentCol = maze.InitialPos.Col; model.Maze = maze; model.GameName = maze.Name; // update end point model.EndRow = maze.GoalPos.Row; model.EndCol = maze.GoalPos.Col; } Game.server = client; Game.StartGame(model); return(new RecieveInfo(result, true, "join")); }
/// <summary> /// Execute. /// </summary> /// <param name="args">The arguments.</param> /// <param name="model"></param> /// <param name="client">The client.</param> /// <returns></returns> public RecieveInfo Execute(string[] args, IServerModel model, TcpClient client = null) { NetworkStream stream = client.GetStream(); StreamReader reader = new StreamReader(stream); string result = SendAndRecieve.RecieveInfo(reader); client.GetStream().Flush(); client.GetStream().Close(); client.Close(); return(new RecieveInfo(result, false, "solve")); }
/// <summary> /// Starts the game. /// </summary> public static void StartGame(IServerModel model) { gameAlive = true; new Task(() => { // wait for join NetworkStream stream = server.GetStream(); StreamReader reader = new StreamReader(stream); while (gameAlive) { string result = SendAndRecieve.RecieveInfo(reader); if (result != "") { JObject jsonResult = JObject.Parse(result); // if it's the end of the game: if ((string)jsonResult[""] == "game closed") { gameAlive = false; model.GameOver(); model.InitializeGame(); } else { // play one move Play(jsonResult, model); } } else { // model.GameOver(); gameAlive = false; } } }).Start(); }
public void Abort(string gameName) { SendAndRecieve.Send(new StreamWriter(client.GetStream()), "close " + gameName); }
/// <summary> /// Connects the client. /// </summary> public void ConnectClient() { bool isConected = true; while (isConected) { // wait for a message while (message.CompareTo("") == 0) { } Task task = new Task(() => { connectionAlive = true; TcpClient client = new TcpClient(); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ServerIP), ServerPort); // comnect: try { client.Connect(ep); } catch (SocketException e) { connection.Invoke(this, new Recive("connection", "can't find server")); isConected = false; return; } using (NetworkStream stream = client.GetStream()) using (StreamReader reader = new StreamReader(stream)) using (StreamWriter writer = new StreamWriter(stream)) { while (connectionAlive) { string[] arr = message.Split(' '); string commandKey = arr[0]; SendAndRecieve.Send(writer, message); message = ""; // check whats next (close connection or continue) if (commands.ContainsKey(commandKey) || updateCommands.ContainsKey(commandKey)) { System.EventHandler <Recive> temp = (commands.ContainsKey(commandKey)) ? reciveFromServer : updateFromServer; Dictionary <string, ICommand> tempCommands = (commands.ContainsKey(commandKey)) ? commands : updateCommands; string[] arguments = arr.Skip(1).ToArray(); command = tempCommands[commandKey]; RecieveInfo recieveInfo = command.Execute(arguments, this, client); connectionAlive = recieveInfo.connectionAlive; Task viewTask = new Task(() => { if (!recieveInfo.typeCommand.Equals("close")) { temp.Invoke(this, new Recive(recieveInfo.typeCommand, recieveInfo.result)); } }); viewTask.Start(); } if (connectionAlive) { // NEED other input while (message.CompareTo("") == 0) { } } } } client.Close(); message = ""; }); task.Start(); task.Wait(); } }