Esempio n. 1
0
        private void SendShopMessage(Message rtn, CommonPlayer player, bool refresh)
        {
            rtn.Add(refresh, player.Energy, player.SecondsToNextEnergy, player.MaxEnergy, player.EnergyDelay);

            if (this.CanUseShop(player, true))
            {
                foreach (var item in this.Items.Where(it => it.Enabled))
                {
                    rtn.Add(item.Key,
                            item.PriceEnergy,
                            item.EnergyPerClick,
                            this.GetEnergyStatus(item.Key),
                            item.PriceCoins,
                            this.GetCount(item),
                            item.Title,
                            item.Description,
                            item.BitmapOffset,
                            item.OnSale,
                            item.IsFeatured,
                            item.IsNew,
                            item.MaxPurchases > 0,
                            item.MaxPurchases,
                            !this.CanBuy(item, player),
                            item.Label,
                            item.LabelColor);
                }
            }
            player.Send(rtn);
        }
Esempio n. 2
0
        public void GotMessage(CommonPlayer player, Message message)
        {
            switch (message.Type)
            {
            case "useEnergy":
            {
                if (!this.CanUseShop(player))
                {
                    player.Send(this.CantUseShopError(message.Type));
                    break;
                }

                var target = message.GetString(0);
                var item   = this.GetShopItem(target);

                if (item != null &&
                    this.CanBuy(item, player) && item.PriceEnergy > 0 &&
                    (item.Enabled || player.IsAdmin || player.IsModerator))
                {
                    player.RefreshPlayerObject(() =>
                        {
                            if (this.EnergyStatus.GetInt(item.Key) > item.PriceEnergy)
                            {
                                this.BuyItem(item.Key);
                                player.PlayerObject.Save(() =>
                                                         this.SendShopMessage(Message.Create(message.Type), player, true));
                            }
                            else if (player.UseEnergy(item.EnergyPerClick))
                            {
                                this.UpdateEnergyStatus(item.Key, item.EnergyPerClick);
                                if (this.EnergyStatus.GetInt(item.Key) >= item.PriceEnergy)
                                {
                                    this.BuyItem(item.Key);
                                    player.PlayerObject.Save(() =>
                                                             this.SendShopMessage(Message.Create(message.Type), player, true));
                                }
                                else
                                {
                                    player.PlayerObject.Save(() =>
                                                             this.SendShopMessage(Message.Create(message.Type), player, false));
                                }
                            }
                            else
                            {
                                player.Send(Shop.NotEnoughEnergyError(message.Type));
                            }
                        });
                }
                break;
            }

            case "useAllEnergy":
            {
                if (!this.CanUseShop(player))
                {
                    player.Send(this.CantUseShopError(message.Type));
                    break;
                }

                var target = message.GetString(0);
                var item   = this.GetShopItem(target);

                if (item != null &&
                    this.CanBuy(item, player) && item.PriceEnergy > 0 &&
                    (item.Enabled || player.IsAdmin || player.IsModerator))
                {
                    player.RefreshPlayerObject(() =>
                        {
                            if (player.UseEnergy(item.PriceEnergy - this.GetEnergyStatus(item.Key)))
                            {
                                this.BuyItem(item.Key);
                                player.PlayerObject.Save(() =>
                                                         this.SendShopMessage(Message.Create(message.Type), player, true));
                            }
                            else
                            {
                                var energyMultiplier = player.Energy / item.EnergyPerClick;
                                if (energyMultiplier > 0)
                                {
                                    var energyToUse = item.EnergyPerClick * energyMultiplier;
                                    if (player.UseEnergy(energyToUse))
                                    {
                                        this.UpdateEnergyStatus(item.Key, energyToUse);
                                        player.PlayerObject.Save(() =>
                                                                 this.SendShopMessage(Message.Create(message.Type), player, false));
                                    }
                                    else
                                    {
                                        player.Send(Shop.NotEnoughEnergyError(message.Type));
                                    }
                                }
                                else
                                {
                                    player.Send(Shop.NotEnoughEnergyError(message.Type));
                                }
                            }
                        });
                }
                else
                {
                    player.Send(message.Type, false);
                }
                break;
            }

            case "useGems":
            {
                if (!this.CanUseShop(player))
                {
                    player.Send(this.CantUseShopError(message.Type));
                    break;
                }

                var target = message.GetString(0);
                var item   = this.GetShopItem(target);

                if (item != null &&
                    this.CanBuy(item, player) &&
                    (item.Enabled || player.IsAdmin || player.IsModerator))
                {
                    player.PayVault.Refresh(() =>
                        {
                            player.PayVault.Debit(item.PriceCoins, "Bought " + item.Title + " for " + this.crew.Name,
                                                  () =>
                            {
                                this.BuyItem(item.Key);
                                this.SendShopMessage(Message.Create(message.Type), player, false);
                            },
                                                  error =>
                            {
                                player.Send(message.Type, "error", "Oops",
                                            "Looks like something went wrong. Do you have enough gems?");
                            });
                        });
                }
                break;
            }

            case "getShop":
            {
                player.RefreshPlayerObject(
                    () =>
                    {
                        player.PayVault.Refresh(
                            () => this.SendShopMessage(Message.Create(message.Type), player, true));
                    });
                break;
            }
            }
        }