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