WIP stock

This commit is contained in:
Joseph Ferano 2022-05-01 12:10:29 +07:00
parent d95264743b
commit d99b7d6f2f
3 changed files with 31 additions and 11 deletions

View File

@ -108,6 +108,18 @@ let getStoreItems (channelId : uint64) =
}) })
|> Async.AwaitTask |> 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 () = let getWeapons () =
connStr connStr
|> Sql.connect |> Sql.connect

View File

@ -4,17 +4,16 @@ open System
open DSharpPlus open DSharpPlus
open DSharpPlus.Entities open DSharpPlus.Entities
open Degenz open Degenz
open Newtonsoft.Json
module Inventory = module Inventory =
let getItemsByType itemType inventory = let getItemsByType itemType inventory =
match itemType with match itemType with
| ItemType.Hack -> inventory |> List.filter (fun item -> match item.Type with ItemType.Hack _ -> 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.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.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.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.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.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) let findItemById id (inventory : Inventory) = inventory |> List.find (fun item -> item.Id = id)

View File

@ -63,9 +63,12 @@ let getBuyItemsEmbed (playerInventory : Inventory) (storeInventory : StoreItem l
let buttons = let buttons =
storeInventory storeInventory
|> List.map (fun item -> |> List.map (fun item ->
if playerInventory |> List.exists (fun i -> i.Id = item.Item.Id) let owned = 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) let inStock = item.Available && (item.Stock > 0 || item.LimitStock = false)
else DiscordButtonComponent(WeaponClass.getClassButtonColor item.Item, $"Buy-{item.Item.Id}", $"Buy {item.Item.Name}") 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) :> DiscordComponent)
let builder = let builder =
@ -101,6 +104,11 @@ let getSellEmbed (items : Inventory) =
.AddComponents(buttons) .AddComponents(buttons)
.AsEphemeral(true) .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 = let checkHasSufficientFunds (item : Item) player =
match item.Attributes with match item.Attributes with
| CanBuy price -> | CanBuy price ->
@ -176,16 +184,17 @@ let purchaseItemEmbed (item : Item) =
let handleBuyItem (ctx : IDiscordContext) itemId = let handleBuyItem (ctx : IDiscordContext) itemId =
executePlayerAction ctx (fun player -> async { executePlayerAction ctx (fun player -> async {
let! storeInventory = DbService.getStoreItems (ctx.GetChannel().Id) 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 let item = storeInventory |> List.map (fun i -> i.Item) |> Inventory.findItemById itemId
do! player do! player
|> checkHasSufficientFunds item |> checkHasSufficientFunds item
>>= checkHasStock storeItem
>>= checkDoesntExceedStackCap item >>= checkDoesntExceedStackCap item
|> handleResultWithResponse ctx (fun player -> async { |> handleResultWithResponse ctx (fun player -> async {
let price = match item.Attributes with CanBuy price -> price | _ -> 0<GBT> let price = match item.Attributes with CanBuy price -> price | _ -> 0<GBT>
do! DbService.updatePlayerCurrency -price player |> Async.Ignore do! DbService.updatePlayerCurrency -price player |> Async.Ignore
do! DbService.addToPlayerInventory player.DiscordId item |> 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) let builder = DiscordFollowupMessageBuilder().AsEphemeral(true)
builder.AddEmbed(purchaseItemEmbed (item)) |> ignore builder.AddEmbed(purchaseItemEmbed (item)) |> ignore
do! ctx.FollowUp builder |> Async.AwaitTask do! ctx.FollowUp builder |> Async.AwaitTask