private void HandleWorldState(Message msg) { Stopwatch watch = Stopwatch.StartNew(); var parser = new WorldParser(msg.Data); var world = parser.Parse(); this.Id = world.Players.FindIndex(p => p.Id == Program.TeamId); this.Ai.Id = this.Id; Log.Info("Player {0} round {1}", this.Id, world.Round); Direction direction; try { direction = this.Ai.Decide(world); } catch (Exception e) { Log.Error(e.ToString()); direction = (Direction) this.random.Next(0, 4); Log.Error("Player {0} decides randomly {1}", this.Id, direction); } watch.Stop(); this.Client.SendMove(world.Round, direction); Log.Info("Player {0} decides {1} in {2}ms", this.Id, direction, watch.ElapsedMilliseconds); }
public IEnumerable<Message> Receive() { Log.Debug("Reading message from server"); var lines = this.ReadData(); foreach (var data in lines) { Message msg; if (data.StartsWith("worldstate::")) { msg = new Message(MessageType.WorldState, data); } else if (data.StartsWith("game over")) { msg = new Message(MessageType.GameOver, data); } else if (data.StartsWith("inscription OK", StringComparison.InvariantCultureIgnoreCase)) { msg = new Message(MessageType.InscriptionOk, data); } else if (data.StartsWith("inscription KO", StringComparison.InvariantCultureIgnoreCase)) { msg = new Message(MessageType.InscriptionKo, data); } else if (data.StartsWith("action OK", StringComparison.InvariantCultureIgnoreCase)) { msg = new Message(MessageType.ActionOk, data); } else { Log.Error("Unknown message: " + data); continue; } Log.Debug("Message Recu: " + msg.Type); yield return msg; } }