public static Tuple <int, GameState> GetGameStatus(GameIDType gameID) { var gidNode = new JObject(new JProperty("gameID", (int)gameID)); var response = Communication.Call("GetGameStatus", gidNode); return(new Tuple <int, GameState>((int)response["turnNumber"], (GameState)(int)response["gameState"])); }
public static void AcceptGame(GameIDType gameID) { var input = new JObject(new JProperty("gameID", (int)gameID)); var response = Communication.Call("AcceptGame", input); Assert.Fatal(response["success"] != null); }
public static void PlayForTurn(string botName, GameIDType gameID, int playForTurn) { AILog.Log("Generating orders for game " + gameID + " turn " + playForTurn); var settings = HumanGameAPI.GetGameSettings(gameID); var game = HumanGameAPI.GetGameInfo(gameID, playForTurn); EntryPoint.PlayGame(botName, game, MeID, settings.Item1, settings.Item2, picks => { }, orders => { }); }
public static void SendPicks(GameIDType gameID, IEnumerable <TerritoryIDType> picks) { var input = new JObject(new JProperty("gameID", (int)gameID)); input.Add("territoryIDs", new JArray(picks.Select(o => (int)o))); var response = Communication.Call("SendPicks", input); Assert.Fatal(response["success"] != null); }
public static Tuple <GameSettings, MapDetails> GetGameSettings(GameIDType gameID) { var gidNode = new JObject(new JProperty("gameID", (int)gameID)); var response = Communication.Call("GetBotGameSettings", gidNode); var settings = Communication.ReadSettings(response["settings"]); var map = Communication.ReadMap(response["map"]); return(new Tuple <GameSettings, MapDetails>(settings, map)); }
/// <summary> /// Save it off in case we want to look at it later. To look at it, go to https://www.warlight.net/Play, press Ctrl+Shift+E then click Import /// </summary> private static void ExportGame(GameIDType gameID) { var export = BotGameAPI.ExportGame(gameID); var dir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "PlayBots"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.WriteAllText(Path.Combine(dir, gameID + ".txt"), export); }
public static void PlayLoop(string botName, GameIDType gameID, PlayerIDType playerID) { var settings = HumanGameAPI.GetGameSettings(gameID); int turnNumber = int.MinValue; bool checkedAccept = false; while (true) { var status = HumanGameAPI.GetGameStatus(gameID); if (status.Item2 == GameState.WaitingForPlayers) { if (!checkedAccept) { var game = HumanGameAPI.GetGameInfo(gameID, null); if (game.Players[playerID].State == GamePlayerState.Invited) { AILog.Log("Accepting invite..."); HumanGameAPI.AcceptGame(gameID); AILog.Log("Accepted invite"); } checkedAccept = true; } } else if (status.Item2 == GameState.Finished) { AILog.Log("Game finished"); break; } else if (status.Item1 > turnNumber) { var game = HumanGameAPI.GetGameInfo(gameID, null); if (!EntryPoint.PlayGame(botName, game, MeID, settings.Item1, settings.Item2, picks => { HumanGameAPI.SendPicks(game.ID, picks); AILog.Log("Sent picks"); }, orders => { HumanGameAPI.SendOrders(game.ID, orders, game.NumberOfTurns + 1); AILog.Log("Sent orders"); })) { AILog.Log("We're no longer alive"); break; } turnNumber = status.Item1; } Thread.Sleep(10000); } }
public static GameObject GetGameInfo(GameIDType gameID, PlayerIDType?playerID) { var input = new JObject(); input["gameID"] = (int)gameID; if (playerID.HasValue) { input["playerID"] = (int)playerID.Value; } var response = Communication.Call("GetBotGameInfo", input); return(Communication.ReadGameObject(response, gameID)); }
public static GameObject GetGameInfo(GameIDType gameID, int?turnNumber) { var input = new JObject(); input["gameID"] = (int)gameID; if (turnNumber.HasValue) { input["turnNumber"] = turnNumber.Value; } var response = Communication.Call("GetGameInfo", input); return(Communication.ReadGameObject(response, gameID)); }
public static GameObject ReadGameObject(JToken response, GameIDType gameID) { var ret = new GameObject(); var gameNode = response["game"]; ret.ID = gameID; ret.NumberOfTurns = (int)gameNode["numberOfTurns"]; ret.State = (GameState)Enum.Parse(typeof(GameState), (string)gameNode["state"]); ret.Players = ((JArray)gameNode["players"]).Select(Communication.ReadGamePlayer).ToDictionary(o => o.ID, o => o); var gameInfo = response["gameInfo"]; if (gameInfo != null && gameInfo.Type == JTokenType.Object) { ret.LatestInfo = Communication.ReadLatestInfo(gameInfo); } return(ret); }
public static void SendOrders(GameIDType gameID, IEnumerable <GameOrder> orders, int turnNumber) { var ordersArray = new JArray(); foreach (var order in orders) { var jOrder = new JObject(); Communication.WriteOrder(jOrder, order); ordersArray.Add(jOrder); } var input = new JObject(); input["gameID"] = (int)gameID; input["orders"] = ordersArray; input["turnNumber"] = turnNumber; var response = Communication.Call("SendOrders", input); Assert.Fatal(response["success"] != null); }
private static void GameFinished(GameIDType gameID) { ExportGame(gameID); BotGameAPI.DeleteGame(gameID); }
public static Tuple <GameSettings, MapDetails, GameObject> GetBotExportedGame(GameIDType gameID, string exportedGame, PlayerIDType playerID, int?turnNumber) { var input = new JObject(); input["exportedGame"] = exportedGame; input["playerID"] = (int)playerID; if (turnNumber.HasValue) { input["turnNumber"] = turnNumber; } var response = Communication.Call("GetBotExportedGameInfo", input); var game = Communication.ReadGameObject(response, gameID); var settings = Communication.ReadSettings(response["settings"]); var map = Communication.ReadMap(response["map"]); return(new Tuple <GameSettings, MapDetails, GameObject>(settings, map, game)); }
public static void DeleteGame(GameIDType gameID) { var gidNode = new JObject(new JProperty("gameID", (int)gameID)); Communication.Call("DeleteBotGame", gidNode); }
public static string ExportGame(GameIDType gameID) { var gidNode = new JObject(new JProperty("gameID", (int)gameID)); return((string)Communication.Call("ExportBotGame", gidNode)["result"]); }
public static string ReadExported(string folder, GameIDType gameID) { var dir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), folder); return(File.ReadAllText(Path.Combine(dir, gameID + ".txt"))); }