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); }
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); }
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); }