예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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);
 }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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));
                    }
                }
            }
        }
예제 #10
0
        public ActionResult Api(ApiReq req)
        {
            var res = new ApiRes();

            switch (req.cmd)
            {
            case "LIST":
                var u = new SampleModel();
                res.users = u.GetUsers();
                break;

            case "SELECT":
            {
                user users = db.users.Find(req.id);
                if (users == null)
                {
                    res.res = "NG";
                }
                else
                {
                    res.user = users;
                }
                break;
            }

            case "UPDATE":
                if (!ModelState.IsValid)
                {
                    res.res = "NG";
                }
                else
                {
                    db.Entry(req.user).State = EntityState.Modified;
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        res.res = "NG";
                        res.msg = e.ToString();
                    }
                }
                break;

            case "INSERT":
                if (ModelState.IsValid)
                {
                    db.users.Add(req.user);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        res.res = "NG";
                        res.msg = e.ToString();
                    }
                }
                break;

            default:
                res.res = "NG";
                res.msg = "Bad Request";
                break;
            }
            return(Json(res));
        }