// -------------------------------------------------------------------- private static Status GetStatus(Client client, int timeout = 0, string msg = "") { Status status = new Status(); string message = client.ReadString(timeout); foreach (string mess in message.Split('\n')) { if (mess.StartsWith(@"{""status"":")) { status = JsonConvert.DeserializeObject<Status>(mess); Console.WriteLine(JsonConvert.SerializeObject(status, Formatting.Indented)); if (status.status.Equals("command_no_cmd_during_game")) //throw new Exception("command_no_cmd_during_game"); return null; if (!msg.Equals(status.status)) //throw new Exception( // "Expected status '" + status.status + "'. Actual message '" + msg + "'"); return null; } else if (mess.StartsWith(@"{""game"":")) { gameInfo = JsonConvert.DeserializeObject<Game>(mess).game; Console.WriteLine(JsonConvert.SerializeObject(gameInfo, Formatting.Indented)); } else if (mess.StartsWith(@"{""play"":")) { try { gameUpdate = JsonConvert.DeserializeObject<Game>(mess).play; //Console.WriteLine("Last cmd ID: " + gameUpdate.lastCmdId); } catch (Exception) { // ignore TODO } //Console.WriteLine(JsonConvert.SerializeObject(gameUpdate, Formatting.Indented)); } else if (mess.StartsWith(@"{""result"":")) { gameResult = JsonConvert.DeserializeObject<Game>(mess).result; Console.WriteLine(JsonConvert.SerializeObject(gameResult, Formatting.Indented)); //Console.ReadKey(); return null; } } return status; }
// -------------------------------------------------------------------- static void Main(string[] args) { while (true) { gameInfo = null; gameUpdate = null; gameResult = null; lastCmdID = -1; currentCmdID = 0; stopwatch.Start(); try { using (Client client = new Client()) { // handshake Status status = GetStatus(client, 0, "socket_connected"); Connect connect = new Connect { login = new Login() { nickname = NICKNAME, hash = GenerateHashValue(status.random, TOKEN) } }; // send login info client.Send(connect); GetStatus(client, 0, "login_ok"); // main loop while (true) { // get game data info GetStatus(client, 0, ""); // loop for one game while (GetStatus(client, DELAY - (int)stopwatch.ElapsedMilliseconds) != null) { if (stopwatch.ElapsedMilliseconds > DELAY) { // send comand on server MoveBots(client); Console.WriteLine("Before restart: " + stopwatch.ElapsedMilliseconds); stopwatch.Restart(); } } } } } catch (Exception e) { Console.WriteLine("Exception:\n"); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } } }
// -------------------------------------------------------------------- private static void MoveBots(Client client) { if (gameInfo == null) return; if (gameUpdate == null) return; IList<Bot> bots = gameUpdate.players.Single( player => player.nickname.Equals(NICKNAME)).bots; Log("GameUpdate"); Log(JsonConvert.SerializeObject(gameUpdate, Formatting.Indented)); foreach (Bot bot in bots) { Bot enemyBot = SearchNearestBotID(bot); double angleToEnemy = GetAngleToEnemy(bot, enemyBot); double betweenAngle = BetweenAngle(bot.angle, angleToEnemy); Log("MyBot: " + bot.x + ", " + bot.y + " , angle: " + bot.angle); Log("EnemyBot: " + enemyBot.x + ", " + enemyBot.y); Log("AngleToEnemy: " + angleToEnemy); Log("Between angle: " + betweenAngle); if (Math.Abs(betweenAngle) < 5 && !DetectEndOfWorld(bot)) { bot.SetCommand(BotCommand.accelerate); } else if (bot.speed > gameInfo.speedLevels.Min(speedLevel => speedLevel.speed)) { bot.SetCommand(BotCommand.brake); } else { double maxAngle = gameInfo.speedLevels.First(speedLevel => speedLevel.speed >= bot.speed).maxAngle; if (Math.Abs(betweenAngle) > maxAngle) betweenAngle = maxAngle; if (DetectEndOfWorld(bot)) betweenAngle = maxAngle; if (betweenAngle > 0) { bot.angle = betweenAngle; } else if (betweenAngle <= 0) { bot.angle = -betweenAngle; } bot.SetCommand(BotCommand.steer); } } Command command = new Command(++currentCmdID, bots); Console.WriteLine("Before sending: " + stopwatch.ElapsedMilliseconds); client.Send(command); Log("Command"); Log(JsonConvert.SerializeObject(command, Formatting.Indented)); }