From 9fb2da6518ec60bff25c1402f0a33b57f86977f7 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 18 Jul 2022 18:42:18 +0000 Subject: [PATCH] only save exp instead of entire character. fix db issues --- src/entity/gateway/entitygateway.rs | 4 +++ src/entity/gateway/inmemory.rs | 10 +++++++ .../postgres/migrations/V0001__initial.sql | 10 +++---- .../postgres/migrations/V0003__item_notes.sql | 2 +- .../postgres/migrations/V0004__meseta.sql | 11 ++++---- src/entity/gateway/postgres/postgres.rs | 26 ++++++++++++++----- src/ship/packet/handler/message.rs | 3 ++- 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/entity/gateway/entitygateway.rs b/src/entity/gateway/entitygateway.rs index fd8b1aa..b1971db 100644 --- a/src/entity/gateway/entitygateway.rs +++ b/src/entity/gateway/entitygateway.rs @@ -131,4 +131,8 @@ pub trait EntityGateway: Send + Sync + Clone { async fn set_bank_meseta(&mut self, _char_id: &CharacterEntityId, _bank: BankName, _amount: Meseta) -> Result<(), GatewayError> { unimplemented!(); } + + async fn set_character_exp(&mut self, _char_id: &CharacterEntityId, _exp: u32) -> Result<(), GatewayError> { + unimplemented!(); + } } diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index 5c7afb4..37e0f67 100644 --- a/src/entity/gateway/inmemory.rs +++ b/src/entity/gateway/inmemory.rs @@ -349,4 +349,14 @@ impl EntityGateway for InMemoryGateway { Err(GatewayError::Error) } } + + async fn set_character_exp(&mut self, char_id: &CharacterEntityId, exp: u32) -> Result<(), GatewayError> { + let mut chars = self.characters.lock().unwrap(); + if let Some(coolhumar) = chars.get_mut(char_id) { + coolhumar.exp = exp; + Ok(()) + } else { + Err(GatewayError::Error) + } + } } diff --git a/src/entity/gateway/postgres/migrations/V0001__initial.sql b/src/entity/gateway/postgres/migrations/V0001__initial.sql index b4cd507..870bd9f 100644 --- a/src/entity/gateway/postgres/migrations/V0001__initial.sql +++ b/src/entity/gateway/postgres/migrations/V0001__initial.sql @@ -19,8 +19,8 @@ create table user_settings ( id serial primary key not null, user_account integer references user_accounts (id) not null, blocked_users bytea not null, - key_config bytea not null, - joystick_config bytea not null, + keyboard_config bytea not null, + gamepad_config bytea not null, option_flags integer not null, shortcuts bytea not null, symbol_chats bytea not null, @@ -65,12 +65,10 @@ create table player_character ( tp smallint not null, tech_menu bytea not null, - meseta integer not null, - bank_meseta integer not null - + keyboard_config bytea not null, + gamepad_config bytea not null ); - create table item ( id serial primary key not null, item jsonb not null diff --git a/src/entity/gateway/postgres/migrations/V0003__item_notes.sql b/src/entity/gateway/postgres/migrations/V0003__item_notes.sql index 3a2f250..a2aae8b 100644 --- a/src/entity/gateway/postgres/migrations/V0003__item_notes.sql +++ b/src/entity/gateway/postgres/migrations/V0003__item_notes.sql @@ -1,4 +1,4 @@ -drop table item_location; +drop table if exists item_location; create table item_note ( item integer references item (id) not null, diff --git a/src/entity/gateway/postgres/migrations/V0004__meseta.sql b/src/entity/gateway/postgres/migrations/V0004__meseta.sql index 97f6b2e..c6deb17 100644 --- a/src/entity/gateway/postgres/migrations/V0004__meseta.sql +++ b/src/entity/gateway/postgres/migrations/V0004__meseta.sql @@ -1,15 +1,16 @@ create table character_meseta ( - pchar integer references character (id) not null unique, - meseta integer not null, + pchar integer references player_character (id) not null unique, + meseta integer not null ); create table bank_meseta ( - pchar integer references character (id) not null, + pchar integer references player_character (id) not null, bank varchar(128) not null, meseta integer not null, unique (pchar, bank) ); -alter table player_character - drop column meseta, bank_meseta; +alter table if exists player_character + drop column if exists meseta, + drop column if exists bank_meseta; diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index 39be9ac..44f5e56 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/src/entity/gateway/postgres/postgres.rs @@ -182,7 +182,7 @@ impl EntityGateway for PostgresGateway { async fn create_character(&mut self, char: NewCharacterEntity) -> Result { let q = r#"insert into player_character (user_account, slot, name, exp, class, section_id, costume, skin, face, head, hair, hair_r, hair_g, hair_b, prop_x, prop_y, techs, - config, infoboard, guildcard, power, mind, def, evade, luck, hp, tp, tech_menu, option_flags) + config, infoboard, guildcard, option_flags, power, mind, def, evade, luck, hp, tp, tech_menu, keyboard_config, gamepad_config) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31) returning *;"#; @@ -207,6 +207,7 @@ impl EntityGateway for PostgresGateway { .bind(&char.config.as_bytes().to_vec()) .bind(String::from_utf16_lossy(&char.info_board.board).trim_matches(char::from(0))) .bind(char.guildcard.description) + .bind(char.option_flags as i32) .bind(char.materials.power as i16) .bind(char.materials.mind as i16) .bind(char.materials.def as i16) @@ -215,7 +216,8 @@ impl EntityGateway for PostgresGateway { .bind(char.materials.hp as i16) .bind(char.materials.tp as i16) .bind(char.tech_menu.tech_menu.to_vec()) - .bind(char.option_flags as i32) + .bind(char.keyboard_config.keyboard_config.to_vec()) + .bind(char.gamepad_config.gamepad_config.to_vec()) .fetch_one(&self.pool).await?; Ok(character.into()) @@ -548,7 +550,7 @@ impl EntityGateway for PostgresGateway { } async fn set_character_meseta(&mut self, char_id: &CharacterEntityId, meseta: Meseta) -> Result<(), GatewayError> { - sqlx::query("insert into character_meseta values ($1, $2) on conflict (pchar) do update set items = $2") + sqlx::query("insert into character_meseta values ($1, $2) on conflict (pchar) do update set meseta = $2") .bind(char_id.0) .bind(meseta.0 as i32) .execute(&self.pool) @@ -559,7 +561,7 @@ impl EntityGateway for PostgresGateway { async fn get_character_meseta(&mut self, char_id: &CharacterEntityId) -> Result { #[derive(sqlx::FromRow)] struct PgMeseta(i32); - let meseta = sqlx::query_as::<_, PgMeseta>(r#"select meseta from character_meseta where id = $1"#) + let meseta = sqlx::query_as::<_, PgMeseta>(r#"select meseta from character_meseta where pchar = $1"#) .bind(char_id.0) .fetch_one(&self.pool) .await?; @@ -567,10 +569,10 @@ impl EntityGateway for PostgresGateway { } async fn set_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: BankName, meseta: Meseta) -> Result<(), GatewayError> { - sqlx::query("insert into bank_meseta values ($1, $2, $3) on conflict (pchar, bank) do update set items = $2") + sqlx::query("insert into bank_meseta values ($1, $2, $3) on conflict (pchar, bank) do update set meseta = $3") .bind(char_id.0) - .bind(meseta.0 as i32) .bind(bank.0) + .bind(meseta.0 as i32) .execute(&self.pool) .await?; Ok(()) @@ -579,11 +581,21 @@ impl EntityGateway for PostgresGateway { async fn get_bank_meseta(&mut self, char_id: &CharacterEntityId, bank: BankName) -> Result { #[derive(sqlx::FromRow)] struct PgMeseta(i32); - let meseta = sqlx::query_as::<_, PgMeseta>(r#"select meseta from character_meseta where id = $1 and bank = $2"#) + let meseta = sqlx::query_as::<_, PgMeseta>(r#"select meseta from bank_meseta where pchar = $1 and bank = $2"#) .bind(char_id.0) .bind(bank.0) .fetch_one(&self.pool) .await?; Ok(Meseta(meseta.0 as u32)) } + + async fn set_character_exp(&mut self, char_id: &CharacterEntityId, exp: u32) -> Result<(), GatewayError> { + let q = r#"update player_character set exp=$1 where id=$2;"#; + sqlx::query(q) + .bind(exp as i32) + .bind(char_id.0 as i32) + .execute(&self.pool) + .await?; + Ok(()) + } } diff --git a/src/ship/packet/handler/message.rs b/src/ship/packet/handler/message.rs index c39ba73..d26f704 100644 --- a/src/ship/packet/handler/message.rs +++ b/src/ship/packet/handler/message.rs @@ -57,7 +57,8 @@ pub async fn request_exp(id: ClientId, } client.character.exp += exp_gain; - entity_gateway.save_character(&client.character).await?; + // entity_gateway.save_character(&client.character).await?; + entity_gateway.set_character_exp(&client.character.id, client.character.exp).await?; Ok(exp_pkts) }