Esempio n. 1
0
        public static async Task AddAsync(long playerId, Databases.personal.PersonalDBContext db, List <Model> models)
        {
            var now = DateTime.UtcNow;

            foreach (var model in models)
            {
                var record = new Databases.personal.GivenHistory
                {
                    playerId     = playerId,
                    obtainedDate = now,
                    type         = model.type,
                    presentId    = model.id,
                    amount       = model.amount,
                    giveType     = model.giveType,
                    text         = model.text,
                };

                await db.GivenHistorys.AddAsync(record);
            }

            await db.SaveChangesAsync();

            // テーブル更新用にUpdate(件数制限制御)
            await UpdateAsync(playerId, db);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public static async Task <BalanceList> BalanceAsync(
            Services.MasterData.IMasterData masterData,
            Databases.personal.PersonalDBContext db,
            string accountAccessToken,
            long playerId //playerIdの正当性は呼び出し元で保証してください
            )
        {
            var balanceList = new BalanceList();

            ////////////////////////
            // Coins
            var requester = new Multiplatform.Inky.GetAccountPlatinumBalance();
            var response  = await requester.GetAsync(accountAccessToken);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var coinModel = new BalanceModel
                {
                    type     = Type.Coin,
                    assetsId = "",
                    amount   = response.Payload.data.total_platinum,
                };
                balanceList.Add(coinModel);
            }

            ////////////////////////
            // Assets
            var query = db.AssetsInventories.Where(a =>
                                                   a.playerId == playerId
                                                   );

            var inventorySrc = await query.ToListAsync();

            inventorySrc.ForEach(
                a => balanceList.Add(new BalanceModel
            {
                type     = Type.Assets,
                assetsId = a.assetsId,
                amount   = a.amount,
            })
                );

            return(balanceList);
        }
Esempio n. 4
0
        public static async Task <GiveResult> GiveAsync(
            Services.MasterData.IMasterData masterData,
            Databases.personal.PersonalDBContext db,
            string accountAccessToken,
            long playerId, //playerIdの正当性は呼び出し元で保証してください
            GiveModel reward
            )
        {
            var rewards = new List <GiveModel>();

            rewards.Add(reward);

            var results = await GiveAsync(
                masterData, db, accountAccessToken,
                playerId, rewards);

            if (results.Count > 0)
            {
                return(results[0].result);
            }

            return(GiveResult.Err);
        }
Esempio n. 5
0
 public static async Task <List <Databases.personal.GivenHistory> > GetAsync(long playerId, Databases.personal.PersonalDBContext db)
 {
     return(await UpdateAsync(playerId, db));
 }
Esempio n. 6
0
        static async Task <List <Databases.personal.GivenHistory> > UpdateAsync(long playerId, Databases.personal.PersonalDBContext db)
        {
            var changed = false;

            var source = await db.GivenHistorys.Where(r => r.playerId == playerId).ToListAsync();

            var list = source.OrderBy(x => x.obtainedDate).ThenBy(x => x.Id).ToList();

            var historyList = new List <Databases.personal.GivenHistory>();

            var count = list.Count;

            if (count > HistoryNum)
            {
                historyList.AddRange(list.Skip(count - HistoryNum));

                var removeList = new List <Databases.personal.GivenHistory>();
                removeList.AddRange(list.Take(count - HistoryNum));

                db.GivenHistorys.RemoveRange(removeList);

                changed = true;
            }
            else
            {
                historyList.AddRange(list);
            }

            if (changed)
            {
                await db.SaveChangesAsync();
            }

            return(historyList);
        }
Esempio n. 7
0
        public static async Task <List <GiveResultModel> > GiveAsync(
            Services.MasterData.IMasterData masterData,
            Databases.personal.PersonalDBContext db,
            string accountAccessToken,
            long playerId, //playerIdの正当性は呼び出し元で保証してください
            long[] ids
            )
        {
            var results = new List <GiveResultModel>();

            var update = false;

            var list = await db.PresentBoxs.Where(r => r.playerId == playerId).ToListAsync();

            var now = DateTime.UtcNow;

            for (int i = 0; i < list.Count;)
            {
                var m = list[i];

                // 期限が切れたプレゼントを削除しておく(期限には少しだけ余裕を持たせる)
                if (m.endDate + TimeSpan.FromMinutes(1) <= now)
                {
                    Logger.Logging(
                        new LogObj().AddChild(new LogModels.GivePresent
                    {
                        PlayerId       = playerId,
                        Date           = now,
                        Type           = m.type,
                        Id             = m.presentId,
                        Amount         = m.amount,
                        ExpirationDate = m.endDate,
                        RouteType      = m.giveType,
                        Code           = Code.Expired,
                        GivenCode      = GiveAndTake.GiveResult.Ok,
                    })
                        );

                    db.PresentBoxs.Remove(m);
                    list.RemoveAt(i);
                    update = true;

                    continue;
                }

                i++;
            }

            var presentList = new List <Databases.personal.PresentBox>();

            foreach (var id in ids)
            {
                var present = list.Find(r => r.Id == id);

                if (present == null)
                {
                    var r = new GiveResultModel
                    {
                        id     = id,
                        result = GiveResult.Err,
                    };
                    results.Add(r);

                    Logger.Logging(
                        new LogObj().AddChild(new LogModels.GivePresent
                    {
                        PlayerId       = playerId,
                        Date           = now,
                        Type           = GiveAndTake.Type.Coin,
                        Id             = "",
                        Amount         = 0,
                        ExpirationDate = now,
                        RouteType      = Type.Management,
                        Code           = Code.Err,
                        GivenCode      = GiveAndTake.GiveResult.Ok,
                    })
                        );

                    continue;
                }

                presentList.Add(present);

                db.PresentBoxs.Remove(present);

                update = true;
            }

            if (update)
            {
                await db.SaveChangesAsync();
            }

            var rewards = new List <GiveAndTake.GiveModel>();

            foreach (var present in presentList)
            {
                var reward = new GiveAndTake.Model
                {
                    assetsId = "",
                    itemId   = "",
                    amount   = present.amount,
                };

                if (present.type == GiveAndTake.Type.Coin)
                {
                    reward.type = GiveAndTake.Type.Coin;
                }
                else if (present.type == GiveAndTake.Type.Assets)
                {
                    reward.type     = GiveAndTake.Type.Assets;
                    reward.assetsId = present.presentId;
                }
                else if (present.type == GiveAndTake.Type.Item)
                {
                    reward.type   = GiveAndTake.Type.Item;
                    reward.itemId = present.presentId;
                }

                rewards.Add(new GiveAndTake.GiveModel
                {
                    id           = present.Id,
                    model        = reward,
                    historyModel = new GiveAndTake.HistoryModel
                    {
                        giveType = present.giveType,
                        text     = present.text,
                    },
                });
            }

            // プレゼント獲得
            var giveModels = await GiveAndTake.GiveAsync(
                masterData,
                db,
                accountAccessToken,
                playerId,
                rewards);

            // 獲得結果集計
            foreach (var reward in rewards)
            {
                var giveModel = giveModels.Find(i => i.id == reward.id);
                if (giveModel == null)
                {
                    Logger.Logging(
                        new LogObj().AddChild(new LogModels.GivePresent
                    {
                        PlayerId       = playerId,
                        Date           = now,
                        Type           = GiveAndTake.Type.Coin,
                        Id             = "",
                        Amount         = 0,
                        ExpirationDate = now,
                        RouteType      = Type.Management,
                        Code           = Code.Err,
                        GivenCode      = GiveAndTake.GiveResult.Ok,
                    })
                        );
                    continue;
                }

                var r = new GiveResultModel
                {
                    id    = reward.id,
                    model = reward.model,
                };

                if (giveModel.result == GiveAndTake.GiveResult.Ok)
                {
                    r.result = GiveResult.Ok;
                }
                else if (giveModel.result == GiveAndTake.GiveResult.AlreadyOwned)
                {
                    r.result = GiveResult.Ok;

                    var convert = ItemConversion.GiveConvertModel(masterData, reward.model.itemId);
                    if (convert.result == ItemConversion.Result.Ok)
                    {
                        // 変換したモデルに差し替える
                        r.model = convert.model;
                    }
                }
                else if (giveModel.result == GiveAndTake.GiveResult.Err)
                {
                    r.result = GiveResult.Err;
                }

                var present = presentList.Find(i => i.Id == reward.id);
                Logger.Logging(
                    new LogObj().AddChild(new LogModels.GivePresent
                {
                    PlayerId       = playerId,
                    Date           = now,
                    Type           = reward.model.type,
                    Id             = (reward.model.type == GiveAndTake.Type.Item) ? reward.model.itemId : reward.model.assetsId,
                    Amount         = reward.model.amount,
                    ExpirationDate = (present != null) ? present.endDate: now,
                    RouteType      = (present != null) ? present.giveType : Type.Management,
                    Code           = Code.Ok,
                    GivenCode      = giveModel.result,
                })
                    );

                results.Add(r);
            }

            return(results);
        }
Esempio n. 8
0
        public static async Task <TakeResult> TakeAsync(
            Services.MasterData.IMasterData masterData,
            Databases.personal.PersonalDBContext db,
            long playerId, //playerIdの正当性は呼び出し元で保証してください
            Model present
            )
        {
            ////////////////////////
            // Assets
            if (present.type == GiveAndTake.Type.Assets)
            {
                var assetsInfo = masterData.GetAssetsInfo(present.id);
                if (assetsInfo == null)
                {
                    return(TakeResult.Err);
                }
            }

            ////////////////////////
            // Item
            if (present.type == GiveAndTake.Type.Item)
            {
                var itemInfo = masterData.GetItemFromItemId(present.id);

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

                if (masterData.CheckDefaultOwnedItem(present.id))
                {
                    return(TakeResult.Err);
                }
            }

            var addDate = TimeSpan.FromDays(10);

            var presentBoxInfo = masterData.GetPresentBox(present.presentType);

            if (presentBoxInfo != null)
            {
                addDate = TimeSpan.FromDays(presentBoxInfo.value);
            }

            var beginDate = DateTime.UtcNow;
            var endDate   = DateTime.UtcNow + addDate;
            var text      = "";

            if (present.presentType == Type.Management)
            {
                // Management = GMツール。以下を任意に設定できるようにする
                beginDate = present.setting.beginDate;
                endDate   = present.setting.endDate;
                text      = (present.setting.text != null) ? present.setting.text : "";
            }

            var record = new Databases.personal.PresentBox
            {
                playerId  = playerId,
                beginDate = beginDate,
                endDate   = endDate,
                type      = present.type,
                presentId = present.id,
                amount    = present.amount,
                giveType  = present.presentType,
                text      = text,
            };
            await db.PresentBoxs.AddAsync(record);

            await db.SaveChangesAsync();

            return(TakeResult.Ok);
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
        public static async Task <List <GiveResultModel> > GiveAsync(
            Services.MasterData.IMasterData masterData,
            Databases.personal.PersonalDBContext db,
            string accountAccessToken,
            long playerId,             //playerIdの正当性は呼び出し元で保証してください
            List <GiveModel> rewards
            )
        {
            bool update = false;

            var results = new List <GiveResultModel>();

            // 獲得履歴
            var givenHistorys = new List <GivenHistory.Model>();
            // アイテム重複確認
            var alreadyOwnedItems = new List <string>();

            foreach (var reward in rewards)
            {
                ////////////////////////
                // Coins
                if (reward.model.type == Type.Coin)
                {
                    var requester = new Multiplatform.Inky.GiveFreePlatinum();
                    requester.request.amount = (int)reward.model.amount;

                    var response = await requester.PostAsync(accountAccessToken);

                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        givenHistorys.Add(new GivenHistory.Model
                        {
                            type     = Type.Coin,
                            id       = "",
                            amount   = reward.model.amount,
                            giveType = reward.historyModel.giveType,
                            text     = reward.historyModel.text,
                        });

                        results.Add(new GiveResultModel
                        {
                            id     = reward.id,
                            result = GiveResult.Ok,
                        });
                        continue;
                    }
                    else
                    {
                        results.Add(new GiveResultModel
                        {
                            id     = reward.id,
                            result = GiveResult.Err,
                        });
                        continue;
                    }
                }

                ////////////////////////
                // Assets
                if (reward.model.type == Type.Assets)
                {
                    var assetsResult = await FirstOrDefaultAssetsAsync(masterData, db, playerId, reward.model);

                    if (assetsResult == GiveAssetsResult.Err || assetsResult == GiveAssetsResult.MaxValue)
                    {
                        results.Add(new GiveResultModel
                        {
                            id     = reward.id,
                            result = GiveResult.Err,
                        });
                        continue;
                    }

                    if (assetsResult == GiveAssetsResult.Add)
                    {
                        // サロゲートキーなテーブルにレコードaddした場合はSaveChangesAsyncする
                        await db.SaveChangesAsync();

                        // SaveChangesAsyncしたので更新フラグ解除
                        update = false;
                    }
                    else if (assetsResult == GiveAssetsResult.Update)
                    {
                        // 更新フラグを立てる
                        update = true;
                    }

                    givenHistorys.Add(new GivenHistory.Model
                    {
                        type     = Type.Assets,
                        id       = reward.model.assetsId,
                        amount   = reward.model.amount,
                        giveType = reward.historyModel.giveType,
                        text     = reward.historyModel.text,
                    });

                    results.Add(new GiveResultModel
                    {
                        id     = reward.id,
                        result = GiveResult.Ok,
                    });
                    continue;
                }

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

                    if (itemInfo == null)
                    {
                        results.Add(new GiveResultModel
                        {
                            id     = reward.id,
                            result = GiveResult.Err,
                        });
                        continue;
                    }

                    var alreadyOwnedItem = false;

                    if (alreadyOwnedItems.Contains(reward.model.itemId))
                    {
                        alreadyOwnedItem = true;
                    }
                    else
                    {
                        alreadyOwnedItems.Add(reward.model.itemId);

                        if (masterData.CheckDefaultOwnedItem(reward.model.itemId))
                        {
                            alreadyOwnedItem = true;
                        }
                        else
                        {
                            var rec = await db.ItemInventories.FirstOrDefaultAsync(r =>
                                                                                   r.playerId == playerId && r.itemId == reward.model.itemId);

                            if (rec != null)
                            {
                                alreadyOwnedItem = true;
                            }
                        }
                    }

                    if (alreadyOwnedItem)
                    {
                        // マテリアル変換
                        var convert = ItemConversion.GiveConvertModel(masterData, reward.model.itemId);
                        if (convert.result == ItemConversion.Result.Err)
                        {
                            results.Add(new GiveResultModel
                            {
                                id     = reward.id,
                                result = GiveResult.Err,
                            });
                            continue;
                        }

                        var assetsResult = await FirstOrDefaultAssetsAsync(masterData, db, playerId, convert.model);

                        if (assetsResult == GiveAssetsResult.Err || assetsResult == GiveAssetsResult.MaxValue)
                        {
                            results.Add(new GiveResultModel
                            {
                                id     = reward.id,
                                result = GiveResult.Err,
                            });
                            continue;
                        }

                        if (assetsResult == GiveAssetsResult.Add)
                        {
                            // サロゲートキーなテーブルにレコードaddした場合はSaveChangesAsyncする
                            await db.SaveChangesAsync();

                            // SaveChangesAsyncしたので更新フラグ解除
                            update = false;
                        }
                        else if (assetsResult == GiveAssetsResult.Update)
                        {
                            // 更新フラグを立てる
                            update = true;
                        }

                        givenHistorys.Add(new GivenHistory.Model
                        {
                            type     = Type.Assets,
                            id       = convert.model.assetsId,
                            amount   = convert.model.amount,
                            giveType = reward.historyModel.giveType,
                            text     = reward.historyModel.text,
                        });

                        results.Add(new GiveResultModel
                        {
                            id     = reward.id,
                            result = GiveResult.AlreadyOwned,
                        });
                        continue;
                    }
                    else
                    {
                        // アイテム獲得
                        await db.ItemInventories.AddAsync(new Databases.personal.ItemInventory
                        {
                            playerId     = playerId,
                            itemId       = reward.model.itemId,
                            itemType     = itemInfo.itemType,
                            obtainedDate = DateTime.UtcNow,
                            obtainedWay  = Item.ObtainedWay.Game,
                            isNew        = true,
                        });

                        update = true;

                        givenHistorys.Add(new GivenHistory.Model
                        {
                            type     = Type.Item,
                            id       = reward.model.itemId,
                            amount   = 0,
                            giveType = reward.historyModel.giveType,
                            text     = reward.historyModel.text,
                        });

                        results.Add(new GiveResultModel
                        {
                            id     = reward.id,
                            result = GiveResult.Ok,
                        });
                        continue;
                    }
                }

                ////////////////////////
                // BattlePassExp
                if (reward.model.type == Type.BattlePassExp)
                {
                    // TODO
                    results.Add(new GiveResultModel
                    {
                        id     = reward.id,
                        result = GiveResult.Err,
                    });
                    continue;
                }

                ////////////////////////
                // Unknown
                results.Add(new GiveResultModel
                {
                    id     = reward.id,
                    result = GiveResult.Err,
                });
            }

            if (update)
            {
                await db.SaveChangesAsync();
            }

            ////////////////////////
            // 獲得履歴追加
            if (givenHistorys.Count > 0)
            {
                await GivenHistory.AddAsync(playerId, db, givenHistorys);
            }

            return(results);
        }