Ejemplo n.º 1
0
        private void AddMapBotsAsGameBots(Game game, List <MapBot> mapBots, List <BotImage> mapBotImages, List <GameBot> gameBots, Dictionary <int, int> botImages,
                                          Dictionary <int, int> bulletImages, Dictionary <int, int> shotImages, Dictionary <int, int> explosionImages, List <int> botImageIds)
        {
            foreach (var mapBot in mapBots)
            {
                var botImage = mapBotImages.FirstOrDefault(bi => bi.Id == mapBot.BotImageId);
                if (botImage == null)
                {
                    throw new Exception($"BotImage #{mapBot.BotImageId} for MapBot #{mapBot.Id} not found!");
                }

                var gameBot = gameBots.FirstOrDefault(gb => gb.BotId == mapBot.BotId);
                if (gameBot == null)
                {
                    gameBot = GameDataService.AddBot(game.Id, mapBot.BotId);
                    gameBots.Add(gameBot);
                }

                botImages[mapBot.BotId]       = botImage.ImageId;
                bulletImages[mapBot.BotId]    = botImage.BulletImageId;
                shotImages[mapBot.BotId]      = botImage.ShotImageId;
                explosionImages[mapBot.BotId] = botImage.ExplosionImageId;

                botImageIds.Add(botImage.ImageId);
                botImageIds.Add(botImage.BulletImageId);
                botImageIds.Add(botImage.ShotImageId);
                botImageIds.Add(botImage.ExplosionImageId);
            }
        }
Ejemplo n.º 2
0
        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;
                }
            }
        }
Ejemplo n.º 3
0
 public void QueueGame(Game game)
 {
     game.State = (int)GameStates.Queued;
     GameDataService.UpdateGame(game);
 }