Example #1
0
        private static async Task <GiveAssetsResult> FirstOrDefaultAssetsAsync(
            Services.MasterData.IMasterData masterData,
            Databases.personal.PersonalDBContext db,
            long playerId,
            Model model
            )
        {
            var assetsInfo = masterData.GetAssetsInfo(model.assetsId);

            if (assetsInfo == null)
            {
                return(GiveAssetsResult.Err);
            }

            var result = GiveAssetsResult.Err;

            var rec = await db.AssetsInventories.FirstOrDefaultAsync(r =>
                                                                     r.playerId == playerId && r.assetsId == model.assetsId);

            if (rec == null)
            {
                rec = new Databases.personal.AssetsInventory
                {
                    playerId = playerId,
                    assetsId = model.assetsId,
                };
                await db.AssetsInventories.AddAsync(rec);

                result = GiveAssetsResult.Add;
            }
            else
            {
                result = GiveAssetsResult.Update;
            }

            rec.amount = Math.Clamp(rec.amount + model.amount, 0, assetsInfo.maxValue);

            if (rec.amount >= assetsInfo.maxValue)
            {
                result = GiveAssetsResult.MaxValue;
            }

            return(result);
        }
Example #2
0
        public static async Task <TakeResult> TakeAsync(
            Services.MasterData.IMasterData masterData,
            Databases.personal.PersonalDBContext db,
            string accountAccessToken,
            long playerId,             //playerIdの正当性は呼び出し元で保証してください
            Model cost
            )
        {
            ////////////////////////
            // Coins
            if (cost.type == Type.Coin)
            {
                var requester = new Multiplatform.Inky.PurchaseItem();
                requester.request.items = new List <Multiplatform.Inky.PurchaseItem.Request.PurchasedProductItems>()
                {
                    new Multiplatform.Inky.PurchaseItem.Request.PurchasedProductItems
                    {
                        deduct_platinum = (int)cost.amount,
                        item_id         = "testItemId",
                        item_name       = "testItem",
                        item_qty        = 1,
                        item_type       = 1,
                    }
                };

                var response = await requester.PostAsync(accountAccessToken);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return(TakeResult.Ok);
                }

                return(TakeResult.Insufficient);
            }

            ////////////////////////
            // Assets
            if (cost.type == Type.Assets)
            {
                var assetsInfo = masterData.GetAssetsInfo(cost.assetsId);
                if (assetsInfo == null)
                {
                    return(TakeResult.Err);
                }

                var rec = await db.AssetsInventories.FirstOrDefaultAsync(r =>
                                                                         r.playerId == playerId && r.assetsId == cost.assetsId);

                if (rec == null)
                {
                    rec = new Databases.personal.AssetsInventory
                    {
                        playerId = playerId,
                        assetsId = cost.assetsId,
                    };
                    await db.AssetsInventories.AddAsync(rec);
                }

                if (rec.amount < cost.amount)
                {
                    return(TakeResult.Insufficient);
                }

                rec.amount = rec.amount - cost.amount;
                await db.SaveChangesAsync();

                return(TakeResult.Ok);
            }

            ////////////////////////
            // Item
            if (cost.type == Type.Item)
            {
                var itemInfo = masterData.GetItemFromItemId(cost.itemId);

                if (itemInfo == null)
                {
                    return(TakeResult.Err);
                }

                if (masterData.CheckDefaultOwnedItem(cost.itemId))
                {
                    return(TakeResult.Ok);
                }

                var rec = await db.ItemInventories.FirstOrDefaultAsync(r =>
                                                                       r.playerId == playerId && r.itemId == cost.itemId);

                if (rec != null)
                {
                    return(TakeResult.Ok);
                }

                return(TakeResult.Insufficient);
            }

            ////////////////////////
            // Unknown
            return(TakeResult.Err);
        }