/// <summary> /// Executes the command. /// </summary> /// <param name="args">the arguments for the command.</param> /// <param name="client">the client that gave the command</param> /// <returns></returns> public TaskResult Execute(string[] args, TcpClient client = null) { // needs to be only one argument. if (args.Length != 1) { return(new TaskResult("bad args", false)); } try { string name = args[0]; // find the game to close. MultiPlayerGame game = model.GetGame(client); // get the client we need to send him the close msg. TcpClient opp = game.GetOpponent(client); // error - eather no opp or no game. if (game == null || opp == null) { return(new TaskResult(null, false)); } // send the opp the game is over. ch.SendMessage(opp, "game over"); // disconnect the current client. return(new TaskResult("disconnect", false)); } catch (Exception e) { Console.WriteLine(e.Message); return(new TaskResult("error occured", false)); } }
/// <summary> /// get a multiplayer game. /// </summary> /// <param name="player">the player</param> /// <returns>the game</returns> public MultiPlayerGame GetGame(TcpClient player) { MultiPlayerGame game = null; // go over the game for (int i = 0; i < multiGames.Count; i++) { game = multiGames.ElementAt(i).Value; // check if that is the players game. if (game.IsPlayer(player)) { return(game); } } // game wasn't found. return(null); }
/// <summary> /// start a multi player game. /// </summary> /// <param name="client">the client that will be player1</param> /// <param name="name">the name of the game</param> /// <param name="x">number of rows.</param> /// <param name="y">number off cols</param> /// <returns>returns the game maze</returns> public Maze Start(TcpClient client, string name, int x, int y) { // the game exist. if (toJoinMazes.ContainsKey(name)) { // error. return(null); } // generate the IMazeGenerator gen = new DFSMazeGenerator(); Maze maze = gen.Generate(x, y); maze.Name = name; // add the game, toJoinMazes.Add(name, maze); // buils a muli player game. MultiPlayerGame game = new MultiPlayerGame(client, name); multiGames.Add(name, game); game.WaitForJoin(); // we got another player. return(playingMazes[name]); }
/// <summary> /// Executes the command. /// </summary> /// <param name="args">the arguments for the command.</param> /// <param name="client">the client that gave the command</param> /// <returns></returns> public TaskResult Execute(string[] args, TcpClient client = null) { // needs to be only one argument. if (args.Length != 1) { return(new TaskResult("bad args", false)); } try { string move = args[0]; // find the playing game. MultiPlayerGame game = model.GetGame(client); // get the client we need to send him the close msg. TcpClient opp = game.GetOpponent(client); if (game == null || opp == null) { return(new TaskResult(null, false)); } // json solution. JObject sol = new JObject { { "Name", game.Name }, { "Direction", move } }; string Jsol = JsonConvert.SerializeObject(sol); // send the opp the players move. ch.SendMessage(opp, Jsol); // stay connected. return(new TaskResult("", true)); } catch (Exception e) { Console.WriteLine(e.Message); return(new TaskResult("error occured", false)); } }