Ejemplo n.º 1
0
        public static Map GetMap(string map)
        {
            var code = ApiReq.CreateReq()
                       .WithMethod("api/map/" + map, "get")
                       .GetResult(out string json);

            if (code == System.Net.HttpStatusCode.OK)
            {
                var result = DynamicJson.Parse(json);
                Map m      = new Map();
                m.Edge      = (int)result.Edge;
                m.Shift     = (int)result.Shift;
                m.Direction = (int)result.Direction;

                m.Rows = new List <Row>();
                int i = 0;
                foreach (string item in result.Rows)
                {
                    Row row = new Row(i, item.Length);
                    for (int j = 0; j < item.Length; j++)
                    {
                        row.Cells[j].Type = Int32.Parse(item[j].ToString());
                    }
                    m.Rows.Add(row);

                    i++;
                }

                return(m);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        public static Player CreatePlayer(string name, int color)
        {
            string parms   = string.Format("{{\"Name\":\"{0}\", \"Color\":{1}}}", name, color);
            var    content = new StringContent(parms, Encoding.UTF8, "application/json");
            var    code    = ApiReq.CreateReq()
                             .WithMethod("api/player", "post", content)
                             .GetResult(out string json);

            if (code == System.Net.HttpStatusCode.OK)
            {
                var    result = DynamicJson.Parse(json);
                Player player = new Player();
                player.Color  = color;
                player.Energy = (int)result.Energy;
                player.Id     = result.Id.ToString();
                player.Index  = (int)result.Index;
                player.Name   = name;
                player.State  = (int)result.State;
                player.Token  = result.Token.ToString();

                return(player);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        public static string[] GameList()
        {
            var code = ApiReq.CreateReq()
                       .WithMethod("api/game", "get")
                       .GetResult(out string json);

            if (code == System.Net.HttpStatusCode.OK)
            {
                var           result = DynamicJson.Parse(json);
                List <string> games  = new List <string>();
                foreach (var item in result)
                {
                    if ((int)item.state == 0)
                    {
                        games.Add(item.id.ToString());
                    }
                }

                return(games.ToArray());
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
        public static bool GetGame(string gameId, ref Game game)
        {
            var code = ApiReq.CreateReq()
                       .WithMethod("api/game/" + gameId, "get")
                       .GetResult(out string json);

            if (code == System.Net.HttpStatusCode.OK)
            {
                var result = DynamicJson.Parse(json);
                game.State = (int)result.State;

                //load players
                int iPlayer = 0;
                if (result != null)
                {
                    foreach (var item in result.Players)
                    {
                        iPlayer++;
                    }
                }

                if (result.Id.ToString() != game.Id ||
                    game.Players == null ||
                    game.Players.Count != iPlayer)
                {
                    game.Players = new List <Player>(iPlayer);
                    foreach (var item in result.Players)
                    {
                        Player player = new Player();
                        player.Index = (int)item.Index;
                        player.Color = (int)item.Color;
                        player.Name  = item.Name.ToString();

                        game.Players.Add(player);
                    }
                }

                //load cells
                foreach (var row in result.Cells)
                {
                    foreach (var item in row)
                    {
                        int x    = (int)item.X;
                        int y    = (int)item.Y;
                        var cell = game.Locate(x, y);
                        cell.Type       = (int)item.Type;
                        cell.State      = (int)item.State;
                        cell.OwnerIndex = (int)item.Owner;
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
 public static void Attack(string gameId, string playerId, int x, int y)
 {
     string parms = string.Format("{{\"Game\":\"{0}\", \"Player\":\"{1}\", \"X\":{2}, \"Y\":{3}}}",
                                  gameId, playerId, x, y);
     var content = new StringContent(parms, Encoding.UTF8, "application/json");
     var code    = ApiReq.CreateReq()
                   .WithMethod("api/cell", "put", content)
                   .GetResult(out string json);
 }
Ejemplo n.º 6
0
        public static bool StartGame(string gameId)
        {
            var code = ApiReq.CreateReq()
                       .WithMethod("api/game/" + gameId, "put")
                       .GetResult(out string json);

            if (code == System.Net.HttpStatusCode.OK)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 7
0
        public static bool JoinGame(string gameId, string playerId)
        {
            string parms   = string.Format("{{\"Game\":\"{0}\", \"Player\":\"{1}\"}}", gameId, playerId);
            var    content = new StringContent(parms, Encoding.UTF8, "application/json");
            var    code    = ApiReq.CreateReq()
                             .WithMethod("api/game", "patch", content)
                             .GetResult(out string json);

            if (code == System.Net.HttpStatusCode.OK)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 8
0
        public static string CreateGame(string map)
        {
            string parms   = string.Format("{{\"Map\":\"{0}\"}}", map);
            var    content = new StringContent(parms, Encoding.UTF8, "application/json");
            var    code    = ApiReq.CreateReq()
                             .WithMethod("api/game", "post", content)
                             .GetResult(out string json);

            if (code == System.Net.HttpStatusCode.OK)
            {
                return(json);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 9
0
        public static void GetPlayer(ref Player player)
        {
            var code = ApiReq.CreateReq()
                       .WithMethod("api/player/" + player.Id, "get")
                       .GetResult(out string json);

            if (code == System.Net.HttpStatusCode.OK)
            {
                var result = DynamicJson.Parse(json);
                lock (player.Locker)
                {
                    player.Energy = (int)result.Energy;
                    player.State  = (int)result.State;
                    //parse bases
                    player.Bases.Clear();
                    foreach (string pos in result.Bases)
                    {
                        player.Bases.Add(Position.Parse(pos));
                    }
                }
            }
        }