Exemplo n.º 1
0
        public override bool CanHappen(string msg, [NotNull] Viewer viewer)
        {
            var worker = ArgWorker.CreateInstance(CommandFilter.Parse(msg).Skip(2));

            if (!PurchaseHelper.TryGetPawn(viewer.username, out Pawn pawn) || !pawn !.Spawned)
            {
                return(false);
            }

            if (!worker.TryGetNextAsItem(out ArgWorker.ItemProxy proxy) || !proxy.IsValid() || proxy !.Thing.Thing.race != null)
            {
                MessageHelper.ReplyToUser(viewer.username, "TKUtils.InvalidItemQuery".LocalizeKeyed(worker.GetLast()));

                return(false);
            }

            if (proxy.TryGetError(out string error))
            {
                MessageHelper.ReplyToUser(viewer.username, error);

                return(false);
            }

            if (!worker.TryGetNextAsInt(out int amount, 1, viewer.GetMaximumPurchaseAmount(proxy !.Thing.Cost)))
            {
                amount = 1;
            }

            if (PurchaseHelper.TryGetUnfinishedPrerequisites(proxy.Thing.Thing, out List <ResearchProjectDef> projects))
            {
                MessageHelper.ReplyToUser(
                    viewer.username,
                    "TKUtils.ResearchRequired".LocalizeKeyed(proxy.Thing.Thing !.LabelCap.RawText, projects.Select(p => p.LabelCap.RawText).SectionJoin())
                    );

                return(false);
            }

            if (worker.GetLast().Equals("*"))
            {
                amount = viewer.GetMaximumPurchaseAmount(proxy.Thing.Cost);
            }

            if (proxy.Thing.ItemData?.HasQuantityLimit == true)
            {
                amount = Mathf.Clamp(amount, 1, proxy.Thing.ItemData.QuantityLimit);
            }

            _purchaseRequest = new PurchaseBackpackRequest {
                Proxy = proxy, Quantity = amount, Purchaser = viewer, Pawn = pawn
            };

            if (_purchaseRequest.Price < ToolkitSettings.MinimumPurchasePrice)
            {
                MessageHelper.ReplyToUser(
                    viewer.username,
                    "TKUtils.Item.MinimumViolation".LocalizeKeyed(_purchaseRequest.Price.ToString("N0"), ToolkitSettings.MinimumPurchasePrice.ToString("N0"))
                    );

                return(false);
            }

            if (_purchaseRequest.Overflowed)
            {
                MessageHelper.ReplyToUser(viewer.username, "TKUtils.Overflowed".Localize());

                return(false);
            }

            if (viewer.CanAfford(_purchaseRequest.Price))
            {
                return(_purchaseRequest.Map != null);
            }

            MessageHelper.ReplyToUser(
                viewer.username,
                "TKUtils.InsufficientBalance".LocalizeKeyed(_purchaseRequest.Price.ToString("N0"), viewer.GetViewerCoins().ToString("N0"))
                );

            return(false);
        }