예제 #1
0
        public ActionResult Unlock(int id, bool useKudos = false)
        {
            using (var db = new ZkDataContext())
                using (var scope = new TransactionScope())
                {
                    List <Unlock> unlocks;
                    List <Unlock> future;

                    GetUnlockLists(db, out unlocks, out future);

                    if (unlocks.Any(x => x.UnlockID == id))
                    {
                        Unlock unlock = db.Unlocks.FirstOrDefault(x => x.UnlockID == id);
                        if (!useKudos && unlock.IsKudosOnly == true)
                        {
                            return(Content("That unlock cannot be bought using XP"));
                        }
                        if (useKudos && unlock.KudosCost == null)
                        {
                            return(Content("That unlock cannot be bought using Kudos"));
                        }

                        if (useKudos)
                        {
                            var acc            = db.Accounts.Find(Global.AccountID);
                            var kudosRemaining = acc.KudosGained - acc.KudosSpent;
                            if (kudosRemaining < unlock.KudosCost)
                            {
                                return(Content("Not enough kudos to unlock this"));
                            }
                            acc.KudosPurchases.Add(new KudosPurchase()
                            {
                                Time = DateTime.UtcNow, Unlock = unlock, Account = acc, KudosValue = unlock.KudosCost ?? 0
                            });
                            db.SaveChanges();
                        }

                        var au = db.AccountUnlocks.SingleOrDefault(x => x.AccountID == Global.AccountID && x.UnlockID == id);
                        if (au == null)
                        {
                            au = new AccountUnlock()
                            {
                                AccountID = Global.AccountID, UnlockID = id, Count = 1
                            };
                            db.AccountUnlocks.InsertOnSubmit(au);
                        }
                        else
                        {
                            au.Count++;
                        }
                        db.SaveChanges();
                    }
                    scope.Complete();
                }
            return(RedirectToAction("UnlockList"));
        }
        public ActionResult Unlock(int id, bool useKudos = false)
        {
            using (var db = new ZkDataContext())
            using (var scope = new TransactionScope())
            {

                List<Unlock> unlocks;
                List<Unlock> future;

                GetUnlockLists(db, out unlocks, out future);

                if (unlocks.Any(x => x.UnlockID == id))
                {
                    Unlock unlock = db.Unlocks.FirstOrDefault(x => x.UnlockID == id);
                    if (!useKudos && unlock.IsKudosOnly == true) return Content("That unlock cannot be bought using XP");

                    if (useKudos) {
                        var acc = db.Accounts.Find(Global.AccountID);
                        if (acc.Kudos < unlock.KudosCost) return Content("Not enough kudos to unlock this");
                        acc.KudosPurchases.Add(new KudosPurchase() {Time = DateTime.UtcNow, Unlock = unlock, Account = acc, KudosValue = unlock.KudosCost??0});
                        db.SubmitAndMergeChanges();
                        acc.Kudos = acc.KudosGained - acc.KudosSpent;
                        db.SubmitAndMergeChanges();
                    }
                    
                    var au = db.AccountUnlocks.SingleOrDefault(x => x.AccountID == Global.AccountID && x.UnlockID == id);
                    if (au == null)
                    {
                        au = new AccountUnlock() { AccountID = Global.AccountID, UnlockID = id, Count = 1 };
                        db.AccountUnlocks.InsertOnSubmit(au);
                    }
                    else au.Count++;
                    db.SubmitAndMergeChanges();
                }
                scope.Complete();
            }
            return RedirectToAction("UnlockList");
        }