@ -272,7 +272,22 @@ impl EntityGateway for PostgresGateway {
}
async fn create_item ( & mut self , item : NewItemEntity ) -> Result < ItemEntity , GatewayError > {
unimplemented ! ( ) ;
let mut tx = self . pool . begin ( ) . await ? ;
let new_item = sqlx ::query_as ::< _ , PgItem > ( "insert into item (item) values ($1) returning *;" )
. bind ( sqlx ::types ::Json ( PgItemDetail ::from ( item . item ) ) )
. fetch_one ( & mut tx ) . await ? ;
let location = sqlx ::query_as ::< _ , PgItemLocation > ( "insert into item_location (item, location) values ($1, $2) returning *" )
. bind ( new_item . id )
. bind ( sqlx ::types ::Json ( PgItemLocationDetail ::from ( item . location ) ) )
. fetch_one ( & mut tx ) . await ? ;
tx . commit ( ) . await ? ;
Ok ( ItemEntity {
id : ItemEntityId ( new_item . id as u32 ) ,
item : new_item . item . 0. into ( ) ,
location : location . location . 0. into ( ) ,
} )
/*
let mut tx = self . pool . begin ( ) . await ? ;
let new_item = sqlx ::query_as ::< _ , PgItem > ( "insert into item (item) values ($1) returning *;" )
@ -324,7 +339,12 @@ impl EntityGateway for PostgresGateway {
}
async fn change_item_location ( & mut self , item_id : & ItemEntityId , item_location : ItemLocation ) -> Result < ( ) , GatewayError > {
unimplemented ! ( ) ;
sqlx ::query ( "insert into item_location (item, location) values ($1, $2)" )
. bind ( item_id . 0 )
. bind ( sqlx ::types ::Json ( PgItemLocationDetail ::from ( item_location ) ) )
. execute ( & self . pool ) . await ? ;
Ok ( ( ) )
/*
let mut tx = self . pool . begin ( ) . await ? ;
if let ItemLocation ::Inventory { slot , . . } = & item_location {
@ -417,27 +437,137 @@ impl EntityGateway for PostgresGateway {
) . await )
}
* /
async fn get_character_inventory ( & mut self , _char_id : & CharacterEntityId ) -> Result < InventoryEntity , GatewayError > {
unimplemented ! ( ) ;
async fn get_character_inventory ( & mut self , char_id : & CharacterEntityId ) -> Result < InventoryEntity , GatewayError > {
let inventory = sqlx ::query_as ::< _ , PgInventoryEntity > ( "select * from inventory where pchar = $1" )
. bind ( char_id . 0 )
. fetch_one ( & self . pool ) . await ? ;
// TODO: inefficient
let mut real_inventory = Vec ::new ( ) ;
for inv_item in inventory . items . 0. into_iter ( ) {
match inv_item {
PgInventoryItemEntity ::Individual ( item ) = > {
let entity = sqlx ::query_as ::< _ , PgItemWithLocation > ( "select item.id, item.item, item_location.location from item join item_location on item.id = item_location.item where id = $1" )
. bind ( item )
. fetch_one ( & self . pool ) . await
. map ( | item | item . into ( ) ) ? ;
real_inventory . push ( InventoryItemEntity ::Individual ( entity ) ) ;
} ,
PgInventoryItemEntity ::Stacked ( items ) = > {
let mut stacked_item = Vec ::new ( ) ;
for s_item in items {
stacked_item . push ( sqlx ::query_as ::< _ , PgItemWithLocation > ( "select item.id, item.item, item_location.location from item join item_location on item.id = item_location.item where id = $1" )
. bind ( s_item )
. fetch_one ( & self . pool ) . await
. map ( | item | item . into ( ) ) ? )
}
real_inventory . push ( InventoryItemEntity ::Stacked ( stacked_item ) ) ;
}
}
}
async fn get_character_bank ( & mut self , _char_id : & CharacterEntityId , _bank_name : BankName ) -> Result < BankEntity , GatewayError > {
unimplemented ! ( ) ;
Ok ( InventoryEntity ::new ( real_inventory ) )
}
async fn set_character_inventory ( & mut self , _char_id : & CharacterEntityId , _inventory : & InventoryEntity ) -> Result < ( ) , GatewayError > {
unimplemented ! ( ) ;
async fn get_character_bank ( & mut self , char_id : & CharacterEntityId , bank_name : BankName ) -> Result < BankEntity , GatewayError > {
let bank = sqlx ::query_as ::< _ , PgInventoryEntity > ( "select * from bank where pchar = $1 and name = $2" )
. bind ( char_id . 0 )
. bind ( bank_name . 0 )
. fetch_one ( & self . pool ) . await ? ;
// TODO: inefficient
let mut real_bank = Vec ::new ( ) ;
for bank_item in bank . items . 0. into_iter ( ) {
match bank_item {
PgInventoryItemEntity ::Individual ( item ) = > {
let entity = sqlx ::query_as ::< _ , PgItemWithLocation > ( "select item.id, item.item, item_location.location from item join item_location on item.id = item_location.item where id = $1" )
. bind ( item )
. fetch_one ( & self . pool ) . await
. map ( | item | item . into ( ) ) ? ;
real_bank . push ( BankItemEntity ::Individual ( entity ) ) ;
} ,
PgInventoryItemEntity ::Stacked ( items ) = > {
let mut stacked_item = Vec ::new ( ) ;
for s_item in items {
stacked_item . push ( sqlx ::query_as ::< _ , PgItemWithLocation > ( "select item.id, item.item, item_location.location from item join item_location on item.id = item_location.item where id = $1" )
. bind ( s_item )
. fetch_one ( & self . pool ) . await
. map ( | item | item . into ( ) ) ? )
}
real_bank . push ( BankItemEntity ::Stacked ( stacked_item ) ) ;
}
}
}
Ok ( BankEntity ::new ( real_bank ) )
}
async fn set_character_bank ( & mut self , _char_id : & CharacterEntityId , _inventory : & BankEntity , _bank_name : BankName ) -> Result < ( ) , GatewayError > {
unimplemented ! ( ) ;
async fn set_character_inventory ( & mut self , char_id : & CharacterEntityId , inventory : & InventoryEntity ) -> Result < ( ) , GatewayError > {
let inventory = inventory . items . iter ( )
. map ( | item | {
match item {
InventoryItemEntity ::Individual ( item ) = > {
PgInventoryItemEntity ::Individual ( item . id . 0 as i32 )
} ,
InventoryItemEntity ::Stacked ( items ) = > {
PgInventoryItemEntity ::Stacked ( items . iter ( ) . map ( | i | i . id . 0 as i32 ) . collect ( ) )
} ,
}
} )
. collect ::< Vec < _ > > ( ) ;
async fn get_character_equips ( & mut self , _char_id : & CharacterEntityId ) -> Result < EquippedEntity , GatewayError > {
unimplemented ! ( ) ;
sqlx ::query ( "insert into inventory (pchar, items) values ($1, $2) on conflict (pchar) do update set items = $2" )
. bind ( char_id . 0 )
. bind ( sqlx ::types ::Json ( inventory ) )
. execute ( & self . pool )
. await ? ;
Ok ( ( ) )
}
async fn set_character_equips ( & mut self , _char_id : & CharacterEntityId , _equips : & EquippedEntity ) -> Result < ( ) , GatewayError > {
unimplemented ! ( ) ;
async fn set_character_bank ( & mut self , char_id : & CharacterEntityId , bank : & BankEntity , bank_name : BankName ) -> Result < ( ) , GatewayError > {
let bank = bank . items . iter ( )
. map ( | item | {
match item {
BankItemEntity ::Individual ( item ) = > {
PgInventoryItemEntity ::Individual ( item . id . 0 as i32 )
} ,
BankItemEntity ::Stacked ( items ) = > {
PgInventoryItemEntity ::Stacked ( items . iter ( ) . map ( | i | i . id . 0 as i32 ) . collect ( ) )
} ,
}
} )
. collect ::< Vec < _ > > ( ) ;
sqlx ::query ( "insert into bank (pchar, items, name) values ($1, $2, $3) on conflict (pchar, name) do update set items = $2" )
. bind ( char_id . 0 )
. bind ( sqlx ::types ::Json ( bank ) )
. bind ( bank_name . 0 )
. execute ( & self . pool )
. await ? ;
Ok ( ( ) )
}
async fn get_character_equips ( & mut self , char_id : & CharacterEntityId ) -> Result < EquippedEntity , GatewayError > {
let equips = sqlx ::query_as ::< _ , PgEquipped > ( "select * from equipped where pchar = $1" )
. bind ( char_id . 0 )
. fetch_one ( & self . pool )
. await ? ;
Ok ( equips . into ( ) )
}
async fn set_character_equips ( & mut self , char_id : & CharacterEntityId , equips : & EquippedEntity ) -> Result < ( ) , GatewayError > {
sqlx ::query ( r # " insert into equipped ( pchar , weapon , armor , shield , unit0 , unit1 , unit2 , unit3 , mag ) values ( $ 1 , $ 2 , $ 3 , $ 4 , $ 5 , $ 6 , $ 7 , $ 8 , $ 9 )
on conflict ( pchar ) do update set weapon = $ 2 , armor = $ 3 , shield = $ 4 , unit0 = $ 5 , unit1 = $ 6 , unit2 = $ 7 , unit3 = $ 8 , mag = $ 9 " # )
. bind ( char_id . 0 )
. bind ( equips . weapon . map ( | i | i . 0 as i32 ) )
. bind ( equips . armor . map ( | i | i . 0 as i32 ) )
. bind ( equips . shield . map ( | i | i . 0 as i32 ) )
. bind ( equips . unit [ 0 ] . map ( | i | i . 0 as i32 ) )
. bind ( equips . unit [ 1 ] . map ( | i | i . 0 as i32 ) )
. bind ( equips . unit [ 2 ] . map ( | i | i . 0 as i32 ) )
. bind ( equips . unit [ 3 ] . map ( | i | i . 0 as i32 ) )
. bind ( equips . mag . map ( | i | i . 0 as i32 ) )
. execute ( & self . pool )
. await ? ;
Ok ( ( ) )
}
}