public async Task <StopGameResponse> StopGameAsync(StopGameRequest request) { LogInfo($"Stop game: ConnectionId = {CurrentRequest.RequestContext.ConnectionId}"); // fetch game record from table var gameRecord = await _dataClient.GetGameRecordAsync(request.GameId); if (gameRecord is null) { LogInfo("No game found to stop"); // game is already stopped, nothing further to do return(new StopGameResponse()); } // delete game record LogInfo($"Deleting game record: ID = {request.GameId}"); await _dataClient.DeleteGameRecordAsync(request.GameId); // update game state to indicated it was stopped gameRecord.GameSession.Status = GameStatus.Finished; ++gameRecord.GameSession.CurrentGameTurn; gameRecord.GameSession.Messages.Add(new Message { GameTurn = gameRecord.GameSession.CurrentGameTurn, Text = "Game stopped." }); // return final game state return(new StopGameResponse { GameSession = gameRecord.GameSession }); }
public async Task <ScanResponse> ScanEnemiesAsync(string gameId, ScanEnemiesRequest request) { // fetch game record from table var gameRecord = await _dataClient.GetGameRecordAsync(gameId); if (gameRecord is null) { throw AbortNotFound($"could not find a game session: ID = {gameId ?? "<NULL>"}"); } var gameLogic = new GameLogic(gameRecord.GameSession, this); // identify scanning bot var bot = gameRecord.GameSession.Bots.FirstOrDefault(r => r.Id == request.BotId); if (bot is null) { throw AbortNotFound($"could not find a bot: ID = {request.BotId}"); } // find nearest enemy within scan resolution var found = gameLogic.ScanBots(bot, request.Heading, request.Resolution); if (found != null) { var distance = GameMath.Distance(bot.X, bot.Y, found.X, found.Y); var angle = GameMath.NormalizeAngle(MathF.Atan2(found.X - bot.X, found.Y - bot.Y) * 180.0f / MathF.PI); LogInfo($"Scanning: Heading = {GameMath.NormalizeAngle(request.Heading):N2}, Resolution = {request.Resolution:N2}, Found = R{found.Index}, Distance = {distance:N2}, Angle = {angle:N2}"); return(new ScanResponse { Found = true, Distance = distance }); } else { LogInfo($"Scanning: Heading = {GameMath.NormalizeAngle(request.Heading):N2}, Resolution = {request.Resolution:N2}, Found = nothing"); return(new ScanResponse { Found = false, Distance = 0.0f }); } }