Example #1
0
        public ActionResult Start(long?id)
        {
            using (_dal)
            {
                var claim = User.Claims.Where(x => x.Type == ClaimTypes.PrimarySid).FirstOrDefault();
                if (claim == null)
                {
                    return(Unauthorized());
                }
                var userId = long.Parse(claim.Value);

                var user = (from u in _dal.Users
                            where u.Id == userId
                            select u).FirstOrDefault();
                if (user == null)
                {
                    return(Unauthorized());
                }

                var campaign = (from c in _dal.Campaigns
                                where c.Id == id.Value
                                select c).FirstOrDefault();
                if (campaign == null)
                {
                    return(NotFound());
                }

                var playerUsers = (from p in _dal.Players
                                   where p.CampaignId == id.Value
                                   select p).ToList();

                if (user.Role != UserRole.Admin && campaign.CreatorId != userId && !playerUsers.Any(p => p.UserId == userId))
                {
                    return(Unauthorized());
                }

                var lobbyEngine = new LobbyEngine(_dal);
                lobbyEngine.LaunchGame(campaign);
                playerUsers.ForEach(p => p.Status = PlayerStatus.Ready);
                _dal.SaveChanges();

                return(Ok());
            }
        }
Example #2
0
        public ActionResult ResetCampaign(long?id)
        {
            using (_dal)
            {
                var claim = User.Claims.Where(x => x.Type == ClaimTypes.Sid).FirstOrDefault();
                if (claim == null)
                {
                    return(Unauthorized());
                }
                var idPlayer = long.Parse(claim.Value);

                var user = (from u in _dal.Users
                            join p in _dal.Players on u.Id equals p.UserId
                            join p2 in _dal.Players on u.Id equals p2.UserId
                            join c in _dal.Campaigns on p2.CampaignId equals c.Id
                            where p.Id == idPlayer && c.Id == id.Value
                            select u).FirstOrDefault();
                if (user == null || user.Role != UserRole.Admin)
                {
                    return(Unauthorized());
                }

                var campaign = (from c in _dal.Campaigns
                                where c.Id == id.Value
                                select c).FirstOrDefault();
                if (campaign == null)
                {
                    return(NotFound());
                }

                campaign.CurrentTurn  = 0;
                campaign.CurrentPhase = TurnPhase.End;
                campaign.NextPhase    = DateTime.Now;
                campaign.Status       = CampaignStatus.Preparation;

                _dal.Units.RemoveRange((from u in _dal.Units
                                        join p in _dal.Players on u.PlayerId equals p.Id
                                        where p.CampaignId == campaign.Id
                                        select u).ToArray());

                _dal.Orders.RemoveRange((from o in _dal.Orders
                                         join s in _dal.OrdersSheets on o.OrdersSheetId equals s.Id
                                         join p in _dal.Players on s.PlayerId equals p.Id
                                         where p.CampaignId == campaign.Id
                                         select o).ToArray());

                _dal.OrdersSheets.RemoveRange((from s in _dal.OrdersSheets
                                               join p in _dal.Players on s.PlayerId equals p.Id
                                               where p.CampaignId == campaign.Id
                                               select s).ToArray());

                _dal.MapTiles.RemoveRange((from t in _dal.MapTiles
                                           join m in _dal.Maps on t.MapId equals m.Id
                                           join p in _dal.Players on m.PlayerId equals p.Id
                                           where p.CampaignId == campaign.Id
                                           select t).ToArray());

                _dal.Maps.RemoveRange((from m in _dal.Maps
                                       join p in _dal.Players on m.PlayerId equals p.Id
                                       where p.CampaignId == campaign.Id
                                       select m).ToArray());

                _dal.Messages.RemoveRange((from m in _dal.Messages
                                           join p in _dal.Players on m.PlayerId equals p.Id
                                           where p.CampaignId == campaign.Id && m.IsNotification
                                           select m).ToArray());

                _dal.SaveChanges();

                var lobbyEngine = new LobbyEngine(_dal);
                lobbyEngine.LaunchGame(campaign);
                _dal.SaveChanges();

                return(Ok());
            }
        }