Browse Source

only save exp instead of entire character. fix db issues

set_char_exp
andy 2 years ago
parent
commit
9fb2da6518
  1. 4
      src/entity/gateway/entitygateway.rs
  2. 10
      src/entity/gateway/inmemory.rs
  3. 10
      src/entity/gateway/postgres/migrations/V0001__initial.sql
  4. 2
      src/entity/gateway/postgres/migrations/V0003__item_notes.sql
  5. 11
      src/entity/gateway/postgres/migrations/V0004__meseta.sql
  6. 26
      src/entity/gateway/postgres/postgres.rs
  7. 3
      src/ship/packet/handler/message.rs

4
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!();
}
}

10
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)
}
}
}

10
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

2
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,

11
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;

26
src/entity/gateway/postgres/postgres.rs

@ -182,7 +182,7 @@ impl EntityGateway for PostgresGateway {
async fn create_character(&mut self, char: NewCharacterEntity) -> Result<CharacterEntity, GatewayError> {
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<Meseta, GatewayError> {
#[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<Meseta, GatewayError> {
#[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(())
}
}

3
src/ship/packet/handler/message.rs

@ -57,7 +57,8 @@ pub async fn request_exp<EG: EntityGateway>(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)
}

Loading…
Cancel
Save