Пример #1
0
        public JsonResult ExecuteAction(String action, int heroId)
        {
            Hero          hero       = Db.Hero.Find(heroId);
            Gameplay      gameplay   = RedisContext.GetByKey <Gameplay>($"hero-gamesave-{heroId}") ?? new Gameplay(hero);
            JsonResult    jsonResult = new JsonResult();
            GameplayModel gameplayModel;

            gameplay.Player = hero;
            try
            {
                List <string> result = new GameplayActions(gameplay).ExecuteAction(action).ToList();
                if (0 == gameplay.Monsters.Count)
                {
                    gameplay.Monsters = gameplay.CurrentLocation.AmbushPlayer();
                    result.AddRange(gameplay.Monsters.Select(m => $"You have been attacked by {m.Name}"));
                }
                gameplayModel = new GameplayModel(hero, result.ToArray());
                if (!RedisContext.Save($"hero-gamesave-{gameplay.Player.ID}", gameplay))
                {
                    gameplayModel.Messages = new String[] { "There was an error while saving game." };
                }
                else
                {
                    hero.Pockets.ToList().ForEach(x => {
                        Db.Entry(x.Item.ItemInfo).State = EntityState.Modified;
                    });
                    hero.LastPlayedAt    = DateTime.UtcNow;
                    Db.Entry(hero).State = EntityState.Modified;
                    Db.SaveChanges();
                }
            }
            catch (InvalidActionException e)
            {
                gameplayModel = new GameplayModel(hero, new string[] { e.Message });
            }
            jsonResult.Data = StringHelper.SerializeObject(gameplayModel);

            return(jsonResult);
        }