Update whitelist and jackpot to use new tables

This commit is contained in:
Joseph Ferano 2022-04-28 18:28:28 +07:00
parent f26d51701d
commit 6dda9dd8a3
5 changed files with 24 additions and 9 deletions

View File

@ -29,7 +29,7 @@ let readItem (reader : RowReader) =
| "Shield" -> ItemType.Shield | "Shield" -> ItemType.Shield
| "Food" -> ItemType.Food | "Food" -> ItemType.Food
| "Accessory" -> ItemType.Accessory | "Accessory" -> ItemType.Accessory
| _ -> ItemType.Accessory | _ -> ItemType.Misc
Item.Attributes = [ Item.Attributes = [
reader.intOrNone "buy_price" |> Option.map (fun a -> Buyable (a * 1<GBT>)) reader.intOrNone "buy_price" |> Option.map (fun a -> Buyable (a * 1<GBT>))
reader.intOrNone "sell_price" |> Option.map (fun a -> Sellable (a * 1<GBT>)) reader.intOrNone "sell_price" |> Option.map (fun a -> Sellable (a * 1<GBT>))
@ -185,7 +185,7 @@ let updatePlayerCurrency (addAmount : int<GBT>) (player : PlayerData) =
"did", Sql.string (string player.DiscordId) "did", Sql.string (string player.DiscordId)
"gbt", Sql.int (int addAmount) "gbt", Sql.int (int addAmount)
] |> Sql.query """ ] |> Sql.query """
UPDATE "user" SET gbt = gbt + @gbt WHERE discord_id = @did; UPDATE "user" SET gbt = gbt + GREATEST(gbt + @gbt, 0) WHERE discord_id = @did;
""" """
|> Sql.executeNonQueryAsync |> Sql.executeNonQueryAsync
|> Async.AwaitTask |> Async.AwaitTask

View File

@ -19,6 +19,7 @@ module Inventory =
| 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.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

@ -88,6 +88,7 @@ type ItemType =
| Shield | Shield
| Food | Food
| Accessory | Accessory
| Misc
type Effect = type Effect =
| Min of int | Min of int

View File

@ -128,7 +128,10 @@ let mutable anyEmoji : DiscordEmoji option = None
let getJackpotAmount () = let getJackpotAmount () =
GuildEnvironment.connectionString GuildEnvironment.connectionString
|> Sql.connect |> Sql.connect
|> Sql.query "SELECT stock FROM item WHERE symbol = 'JACKPOT'" |> Sql.query """
SELECT stock FROM store_item
WHERE store_item.item_id = (SELECT id FROM item WHERE symbol = 'JACKPOT')
"""
|> Sql.executeRowAsync (fun read -> (read.int "stock") * 1<GBT>) |> Sql.executeRowAsync (fun read -> (read.int "stock") * 1<GBT>)
|> Async.AwaitTask |> Async.AwaitTask
@ -136,7 +139,10 @@ let incrementJackpot amount =
GuildEnvironment.connectionString GuildEnvironment.connectionString
|> Sql.connect |> Sql.connect
|> Sql.parameters [ ( "amount" , Sql.int (int amount) ) ] |> Sql.parameters [ ( "amount" , Sql.int (int amount) ) ]
|> Sql.query "UPDATE item SET stock = stock + @amount WHERE symbol = 'JACKPOT'" |> Sql.query """
UPDATE store_item SET stock = stock + @amount
WHERE store_item.item_id = (SELECT id FROM item WHERE symbol = 'JACKPOT')
"""
|> Sql.executeNonQueryAsync |> Sql.executeNonQueryAsync
|> Async.AwaitTask |> Async.AwaitTask
@ -144,7 +150,10 @@ let resetJackpot amount =
GuildEnvironment.connectionString GuildEnvironment.connectionString
|> Sql.connect |> Sql.connect
|> Sql.parameters [ ( "amount" , Sql.int (int amount) ) ] |> Sql.parameters [ ( "amount" , Sql.int (int amount) ) ]
|> Sql.query "UPDATE item SET stock = @amount WHERE symbol = 'JACKPOT'" |> Sql.query """
UPDATE store_item SET stock = @amount
WHERE store_item.item_id = (SELECT id FROM item WHERE symbol = 'JACKPOT')
"""
|> Sql.executeNonQueryAsync |> Sql.executeNonQueryAsync
|> Async.AwaitTask |> Async.AwaitTask

View File

@ -182,9 +182,11 @@ let getWhitelistItem () =
connStr connStr
|> Sql.connect |> Sql.connect
|> Sql.query """ |> Sql.query """
SELECT stock, price FROM item WHERE symbol = 'WHITELIST' SELECT stock, buy_price FROM store_item
JOIN item i on store_item.item_id = i.id
WHERE i.symbol = 'WHITELIST'
""" """
|> Sql.executeRowAsync (fun read -> {| Stock = read.int "stock" ; Price = (read.int "price") * 1<GBT> |}) |> Sql.executeRowAsync (fun read -> {| Stock = read.int "stock" ; Price = (read.int "buy_price") * 1<GBT> |})
|> Async.AwaitTask |> Async.AwaitTask
let updateWhitelistStock () = async { let updateWhitelistStock () = async {
@ -192,7 +194,8 @@ let updateWhitelistStock () = async {
do! connStr do! connStr
|> Sql.connect |> Sql.connect
|> Sql.query """ |> Sql.query """
UPDATE item SET stock = stock - 1 WHERE symbol = 'WHITELIST' UPDATE store_item SET stock = GREATEST(stock - 1, 0)
WHERE store_item.item_id = (SELECT id FROM item WHERE symbol = 'WHITELIST')
""" """
|> Sql.executeNonQueryAsync |> Sql.executeNonQueryAsync
|> Async.AwaitTask |> Async.AwaitTask
@ -207,7 +210,8 @@ let setWhitelistStock amount = async {
|> Sql.connect |> Sql.connect
|> Sql.parameters [ ( "amount" , Sql.int amount ) ] |> Sql.parameters [ ( "amount" , Sql.int amount ) ]
|> Sql.query """ |> Sql.query """
UPDATE item SET stock = @amount WHERE symbol = 'WHITELIST' UPDATE store_item SET stock = @amount
WHERE store_item.item_id = (SELECT id FROM item WHERE symbol = 'WHITELIST')
""" """
|> Sql.executeNonQueryAsync |> Sql.executeNonQueryAsync
|> Async.AwaitTask |> Async.AwaitTask