Fix some functions still using integers, and use division for selling res

This commit is contained in:
Joseph Ferano 2023-04-21 20:00:35 +07:00
parent 40060686a0
commit 9f01712230

View File

@ -8,6 +8,7 @@ as $$
declare declare
new_uuid uuid; new_uuid uuid;
res_id integer; res_id integer;
currency_start_amount float8;
begin begin
if p_user_id is null then if p_user_id is null then
insert into users(name, wallet) values (p_name, p_wallet) insert into users(name, wallet) values (p_name, p_wallet)
@ -17,7 +18,11 @@ begin
insert into users(id, name, wallet) values (p_user_id, p_name, p_wallet); insert into users(id, name, wallet) values (p_user_id, p_name, p_wallet);
end if; end if;
insert into bank_account(user_id, balance) values (new_uuid, 500); select value into currency_start_amount
from game_constants
where key = 'MoonbucksStartAmount';
insert into bank_account(user_id, balance) values (new_uuid, currency_start_amount);
insert into resource_account (resource_id, user_id, balance) insert into resource_account (resource_id, user_id, balance)
select resource.id, new_uuid, 50 select resource.id, new_uuid, 50
@ -31,7 +36,7 @@ $$ language plpgsql;
create or replace function get_accounts(user_id uuid) create or replace function get_accounts(user_id uuid)
returns table ( returns table (
id uuid, id uuid,
"primaryBalance" integer, "primaryBalance" float8,
"resourceAccounts" json "resourceAccounts" json
) )
as $$ as $$
@ -61,23 +66,23 @@ create or replace function sell_resources(
) )
returns table ( returns table (
"resourceName" text, "resourceName" text,
"resourceAmount" integer, "resourceAmount" float8,
"returnAmount" integer, "returnAmount" float8,
"saleResult" text "saleResult" text
) )
as $$ as $$
declare declare
resource jsonb; resource jsonb;
resource_name text; resource_name text;
resource_amount integer; resource_amount float8;
return_amount integer; return_amount float8;
resource_sell_factor real; resource_sell_factor real;
sale_result text; sale_result text;
begin begin
for resource in select * from jsonb_array_elements(p_resources) for resource in select * from jsonb_array_elements(p_resources)
loop loop
resource_name := resource ->> 'resourceType'; resource_name := resource ->> 'resourceType';
resource_amount := (resource ->> 'resourceAmount')::integer; resource_amount := (resource ->> 'resourceAmount')::float8;
sale_result := null; sale_result := null;
begin begin
@ -98,7 +103,7 @@ begin
where key = resource_name || 'ToMoonbucks'; where key = resource_name || 'ToMoonbucks';
update bank_account update bank_account
set balance = balance + resource_amount * resource_sell_factor set balance = balance + resource_amount / resource_sell_factor
where user_id = p_user_id where user_id = p_user_id
returning resource_amount * resource_sell_factor into return_amount; returning resource_amount * resource_sell_factor into return_amount;
@ -218,9 +223,9 @@ declare
new_source_id uuid; new_source_id uuid;
res_id integer; res_id integer;
address varchar; address varchar;
price integer; price float8;
min_res integer; min_res float8;
max_res integer; max_res float8;
begin begin
select value into price from game_constants where key = 'price'; select value into price from game_constants where key = 'price';
@ -262,7 +267,7 @@ returns table (
id uuid, id uuid,
name varchar, name varchar,
wallet varchar, wallet varchar,
balance integer balance float8
) )
as $$ as $$
begin begin
@ -283,7 +288,7 @@ create or replace function purchase_item(
as $$ as $$
declare declare
new_item_id uuid; new_item_id uuid;
item_price integer; item_price float8;
begin begin
select price into item_price from store_item where store_item.id = p_store_item_id; select price into item_price from store_item where store_item.id = p_store_item_id;
@ -314,7 +319,7 @@ as $$
declare declare
upgrade_event_id uuid; upgrade_event_id uuid;
item_insert_id uuid; item_insert_id uuid;
upgrade_price integer; upgrade_price float8;
begin begin
select upgrade_item.price into upgrade_price select upgrade_item.price into upgrade_price
@ -361,7 +366,7 @@ declare
inv_id uuid; inv_id uuid;
stake_id uuid; stake_id uuid;
stake_created_at timestamp; stake_created_at timestamp;
total_stake_amount integer; total_stake_amount float8;
found_resource_well_id uuid; found_resource_well_id uuid;
begin begin
@ -425,7 +430,7 @@ returns table (
"wellId" uuid, "wellId" uuid,
"inventoryItemId" uuid, "inventoryItemId" uuid,
"resourceType" varchar, "resourceType" varchar,
"stakeAmount" integer, "stakeAmount" float8,
"stakeTime" timestamp with time zone, "stakeTime" timestamp with time zone,
"durationInMins" integer, "durationInMins" integer,
unclaimed bool unclaimed bool
@ -459,8 +464,8 @@ declare
stake record; stake record;
claim_event_id uuid; claim_event_id uuid;
stake_end_time timestamp; stake_end_time timestamp;
resource_supply integer; resource_supply float8;
final_supply integer; final_supply float8;
begin begin
select select
staking_event.id, staking_event.id,