public GameService(MapDataService mapDataService, BotDataService botDataService, ImageDataService imageDataService, GameDataService gameDataService) { MapDataService = mapDataService; BotDataService = botDataService; ImageDataService = imageDataService; GameDataService = gameDataService; }
public void PlayGame(Game game, bool debugMode = false) { try { // get bots var gameBots = GameDataService.GetBots(game.Id); // get map data from DB var map = MapDataService.Get(game.MapId); var walls = MapDataService.GetMapObjects <Wall>(game.MapId); var bonuses = MapDataService.GetMapObjects <Bonus>(game.MapId); var traps = MapDataService.GetMapObjects <Trap>(game.MapId); var startPoints = MapDataService.GetMapObjects <StartPoint>(game.MapId); var mapBots = MapDataService.GetMapObjects <MapBot>(game.MapId); var allBotImages = MapDataService.GetMapObjects <BotImage>(game.MapId).ToList(); // images var mapBotImages = allBotImages.Where(bi => mapBots.Any(mb => mb.BotImageId == bi.Id)).ToList(); allBotImages.RemoveAll(bi => mapBotImages.Contains(bi)); var wallImages = walls.ToDictionary(o => o.Id, o => o.ImageId); var bonusImages = bonuses.ToDictionary(o => o.Id, o => o.ImageId); var trapImages = traps.ToDictionary(o => o.Id, o => o.ImageId); GetBotImages(gameBots, allBotImages, out var botImages, out var bulletImages, out var shotImages, out var explosionImages, out var botImageIds); // add map bots as gamebots AddMapBotsAsGameBots(game, mapBots, mapBotImages, gameBots, botImages, bulletImages, shotImages, explosionImages, botImageIds); var images = GetImagesForViewModel(map, wallImages, bonusImages, trapImages, botImageIds); // create mapsettings var mapSettings = GetMapSettings(map, walls, bonuses, traps, startPoints, mapBots); // get bots var bots = BotDataService .Get(gameBots.Select(b => b.BotId).ToList()) .ToList(); var botNames = bots.ToDictionary(b => b.Id, b => b.Name); // play the game var gameResult = EngineService.Play(mapSettings, bots.Select(b => new BotSettings { Id = b.Id, Code = b.Code, Type = b.Type }), debugMode).Result; if (gameResult.IsError) { throw new Exception(gameResult.Message); } var gameModel = gameResult.Game; // create viewmodel var viewModel = new GameViewModel( gameModel, map.Scale, map.BackgroundImageId, map.BulletSpeed, wallImages, bonusImages, trapImages, botImages, bulletImages, shotImages, explosionImages, images, botNames); // convert to json var json = GameSerializer.ToJson(viewModel); // calculate bot stats CalculateStats(gameModel, gameBots); // save game.Played = DateTime.UtcNow; game.State = (int)GameStates.Played; game.Json = json; GameDataService.UpdateGame(game, gameBots); } catch (Exception ex) { game.State = (int)GameStates.Error; GameDataService.UpdateGame(game); if (debugMode) { game.Json = GameSerializer.ToJson(ex.Message); GameDataService.UpdateGame(game); } else { throw; } } }