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); } }
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); } }
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); } }
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); } }
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); }
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); } }
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); } }
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); } }
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)); } } } }