|
@ -1,14 +1,17 @@ |
|
|
|
|
|
use async_std::sync::{Arc, Mutex};
|
|
|
use std::convert::{From, TryFrom, Into};
|
|
|
use std::convert::{From, TryFrom, Into};
|
|
|
use futures::TryStreamExt;
|
|
|
|
|
|
|
|
|
use futures::{Future, TryStreamExt};
|
|
|
use async_std::stream::StreamExt;
|
|
|
use async_std::stream::StreamExt;
|
|
|
use libpso::character::guildcard;
|
|
|
use libpso::character::guildcard;
|
|
|
use crate::entity::account::*;
|
|
|
use crate::entity::account::*;
|
|
|
use crate::entity::character::*;
|
|
|
use crate::entity::character::*;
|
|
|
use crate::entity::gateway::{EntityGateway, GatewayError};
|
|
|
|
|
|
|
|
|
use crate::entity::gateway::{EntityGateway, EntityGatewayTransaction, GatewayError};
|
|
|
use crate::entity::item::*;
|
|
|
use crate::entity::item::*;
|
|
|
use super::models::*;
|
|
|
use super::models::*;
|
|
|
|
|
|
|
|
|
use sqlx::postgres::PgPoolOptions;
|
|
|
use sqlx::postgres::PgPoolOptions;
|
|
|
|
|
|
use sqlx::Connection;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mod embedded {
|
|
|
mod embedded {
|
|
|
use refinery::embed_migrations;
|
|
|
use refinery::embed_migrations;
|
|
@ -16,6 +19,24 @@ mod embedded { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct PostgresTransaction<'t> {
|
|
|
|
|
|
pgtransaction: sqlx::Transaction<'t, sqlx::Postgres>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
|
impl<'t> EntityGatewayTransaction for PostgresTransaction<'t> {
|
|
|
|
|
|
fn gateway<'b>(&'b mut self) -> &'b mut dyn EntityGateway {
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn commit(self: Box<Self>) -> Result<(), GatewayError> {
|
|
|
|
|
|
self.pgtransaction.commit().await?;
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
#[derive(Clone)]
|
|
|
pub struct PostgresGateway {
|
|
|
pub struct PostgresGateway {
|
|
|
pool: sqlx::Pool<sqlx::Postgres>,
|
|
|
pool: sqlx::Pool<sqlx::Postgres>,
|
|
@ -539,6 +560,28 @@ async fn get_bank_meseta(conn: &mut sqlx::PgConnection, char_id: &CharacterEntit |
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
#[async_trait::async_trait]
|
|
|
impl EntityGateway for PostgresGateway {
|
|
|
impl EntityGateway for PostgresGateway {
|
|
|
|
|
|
async fn transaction(&'static mut self) -> Result<Box<dyn EntityGatewayTransaction + 'static>, GatewayError>
|
|
|
|
|
|
{
|
|
|
|
|
|
Ok(Box::new(PostgresTransaction {
|
|
|
|
|
|
pgtransaction: self.pool.begin().await?,
|
|
|
|
|
|
}))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn with_transaction<F, Fut, R, E>(&'static mut self, func: F) -> Result<R, E>
|
|
|
|
|
|
where
|
|
|
|
|
|
Fut: Future<Output = Result<(Box<dyn EntityGatewayTransaction>, R), E>> + Send,
|
|
|
|
|
|
F: FnOnce(Box<dyn EntityGatewayTransaction>) -> Fut + Send,
|
|
|
|
|
|
R: Send,
|
|
|
|
|
|
E: From<GatewayError>,
|
|
|
|
|
|
{
|
|
|
|
|
|
let mut transaction = Box::new(PostgresTransaction {
|
|
|
|
|
|
pgtransaction: self.pool.begin().await.map_err(|_| ()).unwrap()
|
|
|
|
|
|
});
|
|
|
|
|
|
let (mut transaction, result) = func(transaction).await.map_err(|_| ()).unwrap();
|
|
|
|
|
|
transaction.commit().await.map_err(|_| ()).unwrap();
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
async fn create_user(&mut self, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError> {
|
|
|
async fn create_user(&mut self, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError> {
|
|
|
create_user(&mut *self.pool.acquire().await?, user).await
|
|
|
create_user(&mut *self.pool.acquire().await?, user).await
|
|
|
}
|
|
|
}
|
|
@ -653,3 +696,118 @@ impl EntityGateway for PostgresGateway { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
|
|
impl<'c> EntityGateway for PostgresTransaction<'c> {
|
|
|
|
|
|
async fn create_user(&mut self, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError> {
|
|
|
|
|
|
create_user(&mut *self.pgtransaction, user).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_user_by_id(&mut self, id: UserAccountId) -> Result<UserAccountEntity, GatewayError> {
|
|
|
|
|
|
get_user_by_id(&mut *self.pgtransaction, id).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_user_by_name(&mut self, username: String) -> Result<UserAccountEntity, GatewayError> {
|
|
|
|
|
|
get_user_by_name(&mut *self.pgtransaction, username).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn save_user(&mut self, user: &UserAccountEntity) -> Result<(), GatewayError> {
|
|
|
|
|
|
save_user(&mut *self.pgtransaction, user).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn create_user_settings(&mut self, settings: NewUserSettingsEntity) -> Result<UserSettingsEntity, GatewayError> {
|
|
|
|
|
|
create_user_settings(&mut *self.pgtransaction, settings).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_user_settings_by_user(&mut self, user: &UserAccountEntity) -> Result<UserSettingsEntity, GatewayError> {
|
|
|
|
|
|
get_user_settings_by_user(&mut *self.pgtransaction, user).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn save_user_settings(&mut self, settings: &UserSettingsEntity) -> Result<(), GatewayError> {
|
|
|
|
|
|
save_user_settings(&mut *self.pgtransaction, settings).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn create_character(&mut self, char: NewCharacterEntity) -> Result<CharacterEntity, GatewayError> {
|
|
|
|
|
|
create_character(&mut *self.pgtransaction, char).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_characters_by_user(&mut self, user: &UserAccountEntity) -> Result<[Option<CharacterEntity>; 4], GatewayError> {
|
|
|
|
|
|
get_characters_by_user(&mut *self.pgtransaction, user).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn save_character(&mut self, char: &CharacterEntity) -> Result<(), GatewayError> {
|
|
|
|
|
|
save_character(&mut *self.pgtransaction, char).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_guild_card_data_by_user(&mut self, user: &UserAccountEntity) -> Result<GuildCardDataEntity, GatewayError> {
|
|
|
|
|
|
Ok(GuildCardDataEntity {
|
|
|
|
|
|
id: GuildCardDataId(0),
|
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
|
guildcard: guildcard::GuildCardData::default(),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn create_item(&mut self, item: NewItemEntity) -> Result<ItemEntity, GatewayError> {
|
|
|
|
|
|
create_item(&mut *self.pgtransaction, item).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn add_item_note(&mut self, item_id: &ItemEntityId, item_note: ItemNote) -> Result<(), GatewayError> {
|
|
|
|
|
|
add_item_note(&mut *self.pgtransaction, item_id, item_note).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn feed_mag(&mut self, mag_item_id: &ItemEntityId, tool_item_id: &ItemEntityId) -> Result<(), GatewayError> {
|
|
|
|
|
|
feed_mag(&mut *self.pgtransaction, mag_item_id, tool_item_id).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn change_mag_owner(&mut self, mag_item_id: &ItemEntityId, character: &CharacterEntity) -> Result<(), GatewayError> {
|
|
|
|
|
|
change_mag_owner(&mut *self.pgtransaction, mag_item_id, character).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn use_mag_cell(&mut self, mag_item_id: &ItemEntityId, mag_cell_id: &ItemEntityId) -> Result<(), GatewayError> {
|
|
|
|
|
|
use_mag_cell(&mut *self.pgtransaction, mag_item_id, mag_cell_id).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn add_weapon_modifier(&mut self, item_id: &ItemEntityId, modifier: weapon::WeaponModifier) -> Result<(), GatewayError> {
|
|
|
|
|
|
add_weapon_modifier(&mut *self.pgtransaction, item_id, modifier).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_character_inventory(&mut self, char_id: &CharacterEntityId) -> Result<InventoryEntity, GatewayError> {
|
|
|
|
|
|
get_character_inventory(&mut *self.pgtransaction, char_id).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_character_bank(&mut self, char_id: &CharacterEntityId, bank_name: BankName) -> Result<BankEntity, GatewayError> {
|
|
|
|
|
|
get_character_bank(&mut *self.pgtransaction, char_id, bank_name).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn set_character_inventory(&mut self, char_id: &CharacterEntityId, inventory: &InventoryEntity) -> Result<(), GatewayError> {
|
|
|
|
|
|
set_character_inventory(&mut *self.pgtransaction, char_id, inventory).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn set_character_bank(&mut self, char_id: &CharacterEntityId, bank: &BankEntity, bank_name: BankName) -> Result<(), GatewayError> {
|
|
|
|
|
|
set_character_bank(&mut *self.pgtransaction, char_id, bank, bank_name).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_character_equips(&mut self, char_id: &CharacterEntityId) -> Result<EquippedEntity, GatewayError> {
|
|
|
|
|
|
get_character_equips(&mut *self.pgtransaction, char_id).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn set_character_equips(&mut self, char_id: &CharacterEntityId, equips: &EquippedEntity) -> Result<(), GatewayError> {
|
|
|
|
|
|
set_character_equips(&mut *self.pgtransaction, char_id, equips).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn set_character_meseta(&mut self, char_id: &CharacterEntityId, meseta: Meseta) -> Result<(), GatewayError> {
|
|
|
|
|
|
set_character_meseta(&mut *self.pgtransaction, char_id, meseta).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_character_meseta(&mut self, char_id: &CharacterEntityId) -> Result<Meseta, GatewayError> {
|
|
|
|
|
|
get_character_meseta(&mut *self.pgtransaction, char_id).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: BankName, meseta: Meseta) -> Result<(), GatewayError> {
|
|
|
|
|
|
set_bank_meseta(&mut *self.pgtransaction, char_id, bank, meseta).await
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: BankName) -> Result<Meseta, GatewayError> {
|
|
|
|
|
|
get_bank_meseta(&mut *self.pgtransaction, char_id, bank).await
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|