From d99b7d6f2f5f6411a44818542612c2a91cbad37f Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 1 May 2022 12:10:29 +0700 Subject: [PATCH] WIP stock --- Bot/DbService.fs | 12 ++++++++++++ Bot/GameHelpers.fs | 11 +++++------ Bot/Games/Store.fs | 19 ++++++++++++++----- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Bot/DbService.fs b/Bot/DbService.fs index baa7c76..dec6bb4 100644 --- a/Bot/DbService.fs +++ b/Bot/DbService.fs @@ -108,6 +108,18 @@ let getStoreItems (channelId : uint64) = }) |> Async.AwaitTask +let decrementItemStock (item : Item) = + connStr + |> Sql.connect + |> Sql.parameters [ ( "iid" , Sql.int item.Id) ] + |> Sql.query """ + UPDATE store_item SET stock = GREATEST(stock - 1, 0) + WHERE store_item.item_id = @iid + """ + |> Sql.executeNonQueryAsync + |> Async.AwaitTask + |> Async.Ignore + let getWeapons () = connStr |> Sql.connect diff --git a/Bot/GameHelpers.fs b/Bot/GameHelpers.fs index fc1f0d8..3cb66d1 100644 --- a/Bot/GameHelpers.fs +++ b/Bot/GameHelpers.fs @@ -4,17 +4,16 @@ open System open DSharpPlus open DSharpPlus.Entities open Degenz -open Newtonsoft.Json module Inventory = let getItemsByType itemType inventory = match itemType with - | ItemType.Hack -> inventory |> List.filter (fun item -> match item.Type with ItemType.Hack _ -> true | _ -> false) - | ItemType.Shield -> inventory |> List.filter (fun item -> match item.Type with ItemType.Shield _ -> true | _ -> false) - | ItemType.Food -> inventory |> List.filter (fun item -> match item.Type with ItemType.Food _ -> true | _ -> false) + | ItemType.Hack -> inventory |> List.filter (fun item -> match item.Type with ItemType.Hack _ -> true | _ -> false) + | ItemType.Shield -> inventory |> List.filter (fun item -> match item.Type with ItemType.Shield _ -> true | _ -> false) + | ItemType.Food -> inventory |> List.filter (fun item -> match item.Type with ItemType.Food _ -> true | _ -> false) | ItemType.Accessory -> inventory |> List.filter (fun item -> match item.Type with ItemType.Accessory _ -> true | _ -> false) - | ItemType.Jpeg -> inventory |> List.filter (fun item -> match item.Type with ItemType.Jpeg _ -> true | _ -> false) - | ItemType.Misc -> inventory |> List.filter (fun item -> match item.Type with ItemType.Misc _ -> true | _ -> false) + | ItemType.Jpeg -> inventory |> List.filter (fun item -> match item.Type with ItemType.Jpeg _ -> true | _ -> false) + | ItemType.Misc -> inventory |> List.filter (fun item -> match item.Type with ItemType.Misc _ -> true | _ -> false) let findItemById id (inventory : Inventory) = inventory |> List.find (fun item -> item.Id = id) diff --git a/Bot/Games/Store.fs b/Bot/Games/Store.fs index f531ed1..b16ef58 100644 --- a/Bot/Games/Store.fs +++ b/Bot/Games/Store.fs @@ -63,9 +63,12 @@ let getBuyItemsEmbed (playerInventory : Inventory) (storeInventory : StoreItem l let buttons = storeInventory |> List.map (fun item -> - if playerInventory |> List.exists (fun i -> i.Id = item.Item.Id) - then DiscordButtonComponent(WeaponClass.getClassButtonColor item.Item, $"Buy-{item.Item.Id}", $"Own {item.Item.Name}", true) - else DiscordButtonComponent(WeaponClass.getClassButtonColor item.Item, $"Buy-{item.Item.Id}", $"Buy {item.Item.Name}") + let owned = playerInventory |> List.exists (fun i -> i.Id = item.Item.Id) + let inStock = item.Available && (item.Stock > 0 || item.LimitStock = false) + match owned , inStock with + | false , true -> DiscordButtonComponent(WeaponClass.getClassButtonColor item.Item, $"Buy-{item.Item.Id}", $"Buy {item.Item.Name}") + | false , false -> DiscordButtonComponent(WeaponClass.getClassButtonColor item.Item, $"Buy-{item.Item.Id}", $"{item.Item.Name} (Out of Stock)", true) + | true , _ -> DiscordButtonComponent(WeaponClass.getClassButtonColor item.Item, $"Buy-{item.Item.Id}", $"Own {item.Item.Name}", true) :> DiscordComponent) let builder = @@ -101,6 +104,11 @@ let getSellEmbed (items : Inventory) = .AddComponents(buttons) .AsEphemeral(true) +let checkHasStock (item : StoreItem) player = + if item.Stock > 0 || item.LimitStock = false + then Ok player + else Error $"{item.Item.Name} is out of stock! Check back later to purchase" + let checkHasSufficientFunds (item : Item) player = match item.Attributes with | CanBuy price -> @@ -176,16 +184,17 @@ let purchaseItemEmbed (item : Item) = let handleBuyItem (ctx : IDiscordContext) itemId = executePlayerAction ctx (fun player -> async { let! storeInventory = DbService.getStoreItems (ctx.GetChannel().Id) -// let item = storeInventory |> Inventory.findItemById itemId |> fun i -> { Item = i ; Stock = 1 ; LimitStock = false ; Available = true } + let storeItem = storeInventory |> List.find (fun si -> si.Item.Id = itemId) let item = storeInventory |> List.map (fun i -> i.Item) |> Inventory.findItemById itemId do! player |> checkHasSufficientFunds item + >>= checkHasStock storeItem >>= checkDoesntExceedStackCap item |> handleResultWithResponse ctx (fun player -> async { let price = match item.Attributes with CanBuy price -> price | _ -> 0 do! DbService.updatePlayerCurrency -price player |> Async.Ignore do! DbService.addToPlayerInventory player.DiscordId item |> Async.Ignore -// do! ctx.FollowUp $"Successfully purchased {item.Name}! You now have {player.Bank - price} 💰$GBT remaining" + do! DbService.decrementItemStock item let builder = DiscordFollowupMessageBuilder().AsEphemeral(true) builder.AddEmbed(purchaseItemEmbed (item)) |> ignore do! ctx.FollowUp builder |> Async.AwaitTask