private void OnInvalidPlayerName(SnakeBot snake, JObject json) { var reason = (string)json["reasonCode"]; throw new InvalidOperationException( $"The given player name '{snake.Name}' is not valid because. Reason: {reason}"); }
private void OnGameStarting(SnakeBot snake, JObject json) { var settings = json["gameSettings"].ToObject <GameSettings>(); _observer.OnGameStart(settings); snake.OnGameStart(settings); }
/// <summary> /// Registers the specified <see cref="SnakeBot"/> with the server and /// tries to initiate a new game using the specified <see cref="GameSettings"/>. /// The specified <see cref="SnakeBot"/> instance will receive calls to /// <see cref="SnakeBot.GetNextMove"/> when the server requests a new move /// from this client. /// </summary> /// <remarks>This method will throw an exception if the web socket is not open.</remarks> /// <param name="snake">The specified <see cref="SnakeBot"/></param> /// <param name="isTrainingMode">Indicates if the game mode is training or not</param> /// <param name="settings">The specified <see cref="GameSettings"/>, can be null.</param> /// <exception cref="ArgumentNullException" /> /// <exception cref="InvalidOperationException"> /// If the socket is not opened, or if the specified <see cref="SnakeBot"/> has an invalid name. /// </exception> public void Start(SnakeBot snake, bool isTrainingMode, GameSettings settings) { _isTrainingMode = isTrainingMode; if (snake == null) { throw new ArgumentNullException(nameof(snake)); } if (_socket.State != WebSocketState.Open) { throw new InvalidOperationException("Cannot start a new game without connecting the Snake server. " + $"The current state of the connection is { _socket.State}."); } SendRegisterPlayerRequest(snake.Name, settings); SendClientInfoMessage(); while (_socket.State == WebSocketState.Open) { var message = ReceiveString(); var json = JObject.Parse(message); OnMessageReceived(snake, json); } }
private void OnMessageReceived(SnakeBot snake, JObject json) { string messageType = (string)json?["type"] ?? String.Empty; switch (messageType) { case MessageType.GameStarting: OnGameStarting(); break; case MessageType.GameEnded: OnGameEnded(json); break; case MessageType.MapUpdated: OnMapUpdated(snake, json); break; case MessageType.SnakeDead: OnSnakeDead(json); break; case MessageType.PlayerRegistered: OnPlayerRegistered(); break; case MessageType.InvalidPlayerName: OnInvalidPlayerName(snake, json); break; case MessageType.HeartBeatResponse: break; } }
private void OnMapUpdated(SnakeBot snake, JObject json) { var map = Map.FromJson((JObject)json["map"], (string)json["receivingPlayerId"]); _observer.OnUpdate(map); var direction = snake.GetNextMove(map); SendRegisterMoveRequest(direction, map.Tick, (string)json["gameId"]); }
private void OnMessageReceived(SnakeBot snake, JObject json) { var messageType = (string)json?["type"] ?? string.Empty; switch (messageType) { case MessageType.GameStarting: OnGameStarting(snake, json); break; case MessageType.GameEnded: OnGameEnded(json); break; case MessageType.MapUpdated: OnMapUpdated(snake, json); break; case MessageType.SnakeDead: OnSnakeDead(json); break; case MessageType.InvalidPlayerName: OnInvalidPlayerName(snake, json); break; case MessageType.PlayerRegistered: if (snake.AutoStart) { SendStartGameRequest(); } break; case MessageType.GameLink: OnGameLink(json); break; case MessageType.HeartBeatResponse: break; } }
/// <summary> /// Registers the specified <see cref="SnakeBot" /> with the server and /// tries to initiate a game. The specified <see cref="SnakeBot" /> instance /// will receive calls to <see cref="SnakeBot.GetNextMove" /> when the server /// requests a new move from this client. /// </summary> /// <remarks>This method will throw an exception if the web socket is not open.</remarks> /// <param name="snake">The specified <see cref="SnakeBot" /></param> /// <exception cref="ArgumentNullException" /> /// <exception cref="InvalidOperationException"> /// If the socket is not opened, or if the specified <see cref="SnakeBot" /> has an invalid name. /// </exception> public void Start(SnakeBot snake) { Start(snake, null); }
/// <summary> /// Registers the specified <see cref="SnakeBot"/> with the server and /// tries to initiate a game. The specified <see cref="SnakeBot"/> instance /// will receive calls to <see cref="SnakeBot.GetNextMove"/> when the server /// requests a new move from this client. /// </summary> /// <remarks>This method will throw an exception if the web socket is not open.</remarks> /// <param name="snake">The specified <see cref="SnakeBot"/></param> /// <param name="isTrainingMode">Indicates if the game mode is training or not</param> /// <exception cref="ArgumentNullException" /> /// <exception cref="InvalidOperationException"> /// If the socket is not opened, or if the specified <see cref="SnakeBot"/> has an invalid name. /// </exception> public void Start(SnakeBot snake, bool isTrainingMode) { Start(snake, isTrainingMode, null); }
/// <summary> /// Initializes a new instanse of the <see cref="SnakeBot"/> class /// with the specified name. /// </summary> /// <param name="name">The specified name.</param> /// <param name="backupBot">A backup algorithm to use when the first one results in certain death.</param> protected SnakeBot(string name, SnakeBot backupBot) { Name = name; BackupBot = backupBot; }