Example #1
0
        public void HandleRequest(IRequest request)
        {
            using (var scope = Db.CreateTransaction())
            {
                var account = _accountManager.Repository.Get(request.Session.AccountId).ThrowIfNull(ErrorCodes.AccountNotFound);

                var character   = request.Session.Character;
                var extensionId = request.Data.GetOrDefault <int>(k.extensionID);
                var isAdmin     = request.Session.AccessLevel.IsAdminOrGm();

                character.IsDocked.ThrowIfFalse(ErrorCodes.CharacterHasToBeDocked);

                var extensionInfo = _extensionReader.GetExtensionByID(extensionId).ThrowIfNull(ErrorCodes.ExtensionNotFound);

                // req extension check
                if (!extensionInfo.RequiredExtensions.All(pr => character.CheckLearnedExtension(pr)))
                {
                    isAdmin.ThrowIfFalse(ErrorCodes.PrerequireExtensionError);
                }

                //the level he/she wants to learn
                var extensionLevel = character.GetExtensionLevel(extensionId) + 1;
                extensionLevel.ThrowIfGreater(10, ErrorCodes.ExtensionFullyLearnt);

                var extensionRank      = extensionInfo.rank;
                var extensionPointCost = _extensionPoints.GetNominalExtensionPoints(extensionLevel, extensionRank);

                //extension price
                if (extensionLevel == 1)
                {
                    // ha lvl 1 akkor leszedjuk penzzel a komat
                    character.SubtractFromWallet(TransactionType.extensionLearn, extensionInfo.price);
                }

                //training character -> no EP pool usage
                //                   -> free extension start
                if (!character.IsInTraining())
                {
                    //points check
                    var availablePoints = _accountManager.CalculateCurrentEp(account);
                    if (availablePoints < extensionPointCost)
                    {
                        isAdmin.ThrowIfFalse(ErrorCodes.NotEnoughExtensionPoints);
                    }

                    //spent extension
                    _accountManager.AddExtensionPointsSpent(account, character, extensionPointCost, extensionId, extensionLevel);
                }

                //write to sql
                character.IncreaseExtensionLevel(extensionId, extensionLevel);

                var result = _accountManager.GetEPData(account, character);
                result.Add(k.extensionID, extensionId);
                result.Add(k.extensionLevel, extensionLevel);

                Message.Builder.FromRequest(request).WithData(result).Send();

                scope.Complete();
            }
        }
        public void HandleRequest(IRequest request)
        {
            using (var scope = Db.CreateTransaction())
            {
                var account     = _accountManager.Repository.Get(request.Session.AccountId).ThrowIfNull(ErrorCodes.AccountNotFound);
                var character   = request.Session.Character;
                var extensionId = request.Data.GetOrDefault <int>(k.extensionID);
                var isAdmin     = request.Session.AccessLevel.IsAdminOrGm();


                //only docked
                character.IsDocked.ThrowIfFalse(ErrorCodes.CharacterHasToBeDocked);

                var extensionInfo = _extensionReader.GetExtensionByID(extensionId).ThrowIfNull(ErrorCodes.ExtensionNotFound);

                //validate extension

                var currentExtensionLevel = character.GetExtensionLevel(extensionId);

                //this extension is not learnt for this character yet
                currentExtensionLevel.ThrowIfLessOrEqual(0, ErrorCodes.ItemNotFound);

                var defaultExtensionHandler = new CharacterDefaultExtensionHelper(character);

                if (defaultExtensionHandler.IsStartingExtension(extensionInfo, out int minimumLevel))
                {
                    (currentExtensionLevel == minimumLevel).ThrowIfTrue(ErrorCodes.ExtensionMinimumReached);
                }


                if (currentExtensionLevel == 1)
                {
                    //extension is getting deleted
                }


                // these extensions need the current one on a specific level as a requirement
                var prerequiredOf = _extensionReader.GetPrerequiredExtensionsOf(extensionId);

                var learntExtensions = character.GetExtensions().ToList();

                foreach (var requiresExtension in prerequiredOf)
                {
                    foreach (var learntExtension in learntExtensions)
                    {
                        if (learntExtension.id != requiresExtension.id)
                        {
                            continue;
                        }

                        if (requiresExtension.level < currentExtensionLevel)
                        {
                            continue;
                        }

                        //extension cant be downgraded because
                        Logger.DebugWarning($"extension:{_extensionReader.GetExtensionName(requiresExtension.id)} requires:{_extensionReader.GetExtensionName(extensionId)} on level:{requiresExtension.level} current level:{currentExtensionLevel}  requests:{requiresExtension.id}->this:{extensionId}");
                        throw PerpetuumException.Create(ErrorCodes.ExtensionIsRequired)
                              .SetData("thisNeeds", requiresExtension.id)
                              .SetData("level", requiresExtension.level)
                              .SetData("downgrading", extensionId);
                    }
                }

                var spentId     = 0;
                var spentPoints = 0;
                character.GetTableIndexForAccountExtensionSpent(extensionId, currentExtensionLevel, ref spentId, ref spentPoints);

                if (spentId == 0)
                {
                    isAdmin.ThrowIfFalse(ErrorCodes.ExtensionLevelCantBeRemoved);
                }

                //add negative spent points
                var spentPoints1   = -1 * spentPoints;
                var extensionLevel = currentExtensionLevel - 1;
                _accountManager.AddExtensionPointsSpent(account, character, spentPoints1, extensionId, extensionLevel);

                //insert log
                _accountManager.InsertExtensionRemoveLog(account, character, extensionId, currentExtensionLevel, spentPoints);
                character.SetExtension(new Extension(extensionId, currentExtensionLevel - 1));

                var price = spentPoints / 60;

                //var priceBase = AccountWallet.GetProductPrice(AccountTransactionType.extensionRemoveLevel.ToString().ToLower());
                //var price = currentExtensionLevel * priceBase; //current level * base price

                var wallet = _accountManager.GetWallet(account, AccountTransactionType.ExtensionRemoveLevel);
                wallet.Balance -= price;
                var e = new AccountTransactionLogEvent(account, AccountTransactionType.ExtensionRemoveLevel)
                {
                    Credit = wallet.Balance, CreditChange = -price
                };
                _accountManager.LogTransaction(e);

                _accountRepository.Update(account);

                var result = _accountManager.GetEPData(account, character);

                //The amount of EP gained
                result.Add("pointsReturned", spentPoints);
                result.Add(k.extensionID, extensionId);
                result.Add(k.extensionLevel, currentExtensionLevel - 1);

                Message.Builder.FromRequest(request).WithData(result).Send();

                scope.Complete();
            }
        }