public async Task <ScanEnemiesResponse> ScanEnemiesAsync(ScanEnemiesRequest request) { // fetch game record from table var gameRecord = await _table.GetAsync <GameRecord>(request.GameId); if (gameRecord == null) { throw AbortNotFound($"could not find a game session: ID = {request.GameId ?? "<NULL>"}"); } var gameLogic = new GameLogic(new GameDependencyProvider( gameRecord.Game, _random, r => throw new NotImplementedException("not implementation for GetBuild"), r => throw new NotImplementedException("not implementation for GetAction") )); // identify scanning robot var robot = gameRecord.Game.Robots.FirstOrDefault(r => r.Id == request.RobotId); if (robot == null) { throw AbortNotFound($"could not find a robot: ID = {request.RobotId}"); } // find nearest enemy within scan resolution var found = gameLogic.ScanRobots(robot, request.Heading, request.Resolution); if (found != null) { var distance = GameLogic.Distance(robot.X, robot.Y, found.X, found.Y); var angle = GameLogic.NormalizeAngle(Math.Atan2(found.X - robot.X, found.Y - robot.Y) * 180.0 / Math.PI); LogInfo($"Scanning: Heading = {GameLogic.NormalizeAngle(request.Heading):N2}, Resolution = {request.Resolution:N2}, Found = R{found.Index}, Distance = {distance:N2}, Angle = {angle:N2}"); return(new ScanEnemiesResponse { Found = true, Distance = distance }); } else { LogInfo($"Scanning: Heading = {GameLogic.NormalizeAngle(request.Heading):N2}, Resolution = {request.Resolution:N2}, Found = nothing"); return(new ScanEnemiesResponse { Found = false, Distance = 0.0 }); } }
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 }); } }