Exemplo n.º 1
0
 //public TcpClient GetSecondPlayer(TcpClient client)
 public ICClientHandler GetSecondPlayer(ICClientHandler client)
 {
     if (client1.Equals(client))
     {
         return(client2);
     }
     return(client1);
 }
Exemplo n.º 2
0
        //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);
        }
Exemplo n.º 3
0
        //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());
        }
Exemplo n.º 4
0
        //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);
        }
Exemplo n.º 5
0
        //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));
        }
Exemplo n.º 6
0
 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);
 }
Exemplo n.º 7
0
        //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);
        }
Exemplo n.º 8
0
 //public void AddClient(TcpClient c)
 public void AddClient(ICClientHandler c)
 {
     if (numOfClient == 0)
     {
         client1 = c;
     }
     else
     {
         client2 = c;
         //client1.Client.Send() - myMaze.ToJSON()
     }
     numOfClient++;
 }
Exemplo n.º 9
0
        // 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");
            }
        }
Exemplo n.º 10
0
 //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");
     }
 }
Exemplo n.º 11
0
        //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));
        }
Exemplo n.º 12
0
        //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");
        }
Exemplo n.º 13
0
        //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");
        }
Exemplo n.º 14
0
        //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");
            }
        }
Exemplo n.º 15
0
        //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);
            }
        }
Exemplo n.º 16
0
        //public string Execute(string[] args, TcpClient client)
        public string Execute(string[] args, ICClientHandler client)
        {
            String ans = model.list();

            return(ans);
        }
Exemplo n.º 17
0
 //public void SendToClient(TcpClient client, string msg)
 //{
 //sender.SendToClient(client, msg);
 //}
 public void SendToClient(ICClientHandler client, string msg)
 {
     client.sendMssage(msg);
 }