Browse Source

fix this keyconfig nonsense

pull/127/head
jake 2 years ago
parent
commit
d495ec97f2
  1. 4
      src/bin/main.rs
  2. 84
      src/entity/character.rs
  3. 2
      src/entity/gateway/inmemory.rs
  4. 3
      src/entity/gateway/postgres/migrations/V0009__no_player_keyconfig.sql
  5. 8
      src/entity/gateway/postgres/models.rs
  6. 15
      src/entity/gateway/postgres/postgres.rs
  7. 2
      src/login/character.rs
  8. 2
      src/ship/items/actions.rs
  9. 4
      src/ship/packet/handler/lobby.rs
  10. 8
      src/ship/packet/handler/settings.rs
  11. 3
      tests/common.rs
  12. 29
      tests/test_character.rs

4
src/bin/main.rs

@ -64,12 +64,12 @@ fn main() {
}; };
let fake_user = entity_gateway.create_user(fake_user).await.unwrap(); let fake_user = entity_gateway.create_user(fake_user).await.unwrap();
entity_gateway.create_user_settings(NewUserSettingsEntity::new(fake_user.id)).await.unwrap(); entity_gateway.create_user_settings(NewUserSettingsEntity::new(fake_user.id)).await.unwrap();
let mut character = NewCharacterEntity::new(fake_user.id, 1);
let mut character = NewCharacterEntity::new(fake_user.id);
character.name = format!("Test Char {}", i*2); character.name = format!("Test Char {}", i*2);
let character = entity_gateway.create_character(character).await.unwrap(); let character = entity_gateway.create_character(character).await.unwrap();
entity_gateway.set_character_meseta(&character.id, item::Meseta(999999)).await.unwrap(); entity_gateway.set_character_meseta(&character.id, item::Meseta(999999)).await.unwrap();
entity_gateway.set_bank_meseta(&character.id, &item::BankName("".into()), item::Meseta(999999)).await.unwrap(); entity_gateway.set_bank_meseta(&character.id, &item::BankName("".into()), item::Meseta(999999)).await.unwrap();
let mut character = NewCharacterEntity::new(fake_user.id, 1);
let mut character = NewCharacterEntity::new(fake_user.id);
character.slot = 2; character.slot = 2;
character.name = "ItemRefactor".into(); character.name = "ItemRefactor".into();
character.exp = 80000000; character.exp = 80000000;

84
src/entity/character.rs

@ -264,82 +264,6 @@ pub struct CharacterMaterials {
pub tp: u32, pub tp: u32,
} }
#[derive(Clone, Debug)]
pub struct CharacterKeyboardConfig {
pub keyboard_config: [u8; 0x16C],
}
impl Default for CharacterKeyboardConfig {
fn default() -> CharacterKeyboardConfig {
CharacterKeyboardConfig {
keyboard_config: DEFAULT_KEYBOARD_CONFIG1,
}
}
}
impl CharacterKeyboardConfig {
fn new(preset: usize) -> CharacterKeyboardConfig {
match preset {
1 => {
CharacterKeyboardConfig {
keyboard_config: DEFAULT_KEYBOARD_CONFIG1,
}
},
2 => {
CharacterKeyboardConfig {
keyboard_config: DEFAULT_KEYBOARD_CONFIG2,
}
},
3 => {
CharacterKeyboardConfig {
keyboard_config: DEFAULT_KEYBOARD_CONFIG3,
}
},
4 => {
CharacterKeyboardConfig {
keyboard_config: DEFAULT_KEYBOARD_CONFIG4,
}
},
_ => {
CharacterKeyboardConfig {
keyboard_config: DEFAULT_KEYBOARD_CONFIG1,
}
},
}
}
pub fn update(&mut self, new_config: &KeyboardConfig) {
self.keyboard_config = new_config.keyboard_config;
}
pub fn as_bytes(&self) -> [u8; 0x16C] {
self.keyboard_config
}
}
#[derive(Clone, Debug)]
pub struct CharacterGamepadConfig {
pub gamepad_config: [u8; 0x38],
}
impl Default for CharacterGamepadConfig {
fn default() -> CharacterGamepadConfig {
CharacterGamepadConfig {
gamepad_config: DEFAULT_GAMEPAD_CONFIG,
}
}
}
impl CharacterGamepadConfig {
pub fn update(&mut self, new_config: &GamepadConfig) {
self.gamepad_config = new_config.gamepad_config;
}
pub fn as_bytes(&self) -> [u8; 0x38] {
self.gamepad_config
}
}
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default, derive_more::Display)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default, derive_more::Display)]
pub struct CharacterEntityId(pub u32); pub struct CharacterEntityId(pub u32);
@ -363,12 +287,10 @@ pub struct NewCharacterEntity {
pub tech_menu: CharacterTechMenu, pub tech_menu: CharacterTechMenu,
pub option_flags: u32, pub option_flags: u32,
pub keyboard_config: CharacterKeyboardConfig,
pub gamepad_config: CharacterGamepadConfig,
} }
impl NewCharacterEntity { impl NewCharacterEntity {
pub fn new(user: UserAccountId, keyboard_config_preset: usize,) -> NewCharacterEntity {
pub fn new(user: UserAccountId) -> NewCharacterEntity {
NewCharacterEntity { NewCharacterEntity {
user_id: user, user_id: user,
slot: 0, slot: 0,
@ -384,8 +306,6 @@ impl NewCharacterEntity {
materials: CharacterMaterials::default(), materials: CharacterMaterials::default(),
tech_menu: CharacterTechMenu::default(), tech_menu: CharacterTechMenu::default(),
option_flags: 0, option_flags: 0,
keyboard_config: CharacterKeyboardConfig::new(keyboard_config_preset),
gamepad_config: CharacterGamepadConfig::default(),
} }
} }
} }
@ -411,8 +331,6 @@ pub struct CharacterEntity {
pub tech_menu: CharacterTechMenu, pub tech_menu: CharacterTechMenu,
pub option_flags: u32, pub option_flags: u32,
pub keyboard_config: CharacterKeyboardConfig,
pub gamepad_config: CharacterGamepadConfig,
pub playtime: u32, pub playtime: u32,
} }

2
src/entity/gateway/inmemory.rs

@ -452,8 +452,6 @@ impl EntityGateway for InMemoryGateway {
materials: character.materials, materials: character.materials,
tech_menu: character.tech_menu, tech_menu: character.tech_menu,
option_flags: character.option_flags, option_flags: character.option_flags,
keyboard_config: character.keyboard_config,
gamepad_config: character.gamepad_config,
playtime: 0, playtime: 0,
}; };
characters.insert(new_character.id, new_character.clone()); characters.insert(new_character.id, new_character.clone());

3
src/entity/gateway/postgres/migrations/V0009__no_player_keyconfig.sql

@ -0,0 +1,3 @@
alter table player_character
drop column keyboard_config,
drop column gamepad_config;

8
src/entity/gateway/postgres/models.rs

@ -217,8 +217,6 @@ pub struct PgCharacter {
tp: i16, tp: i16,
tech_menu: Vec<u8>, tech_menu: Vec<u8>,
keyboard_config: Vec<u8>,
gamepad_config: Vec<u8>,
playtime: i32, playtime: i32,
} }
@ -270,12 +268,6 @@ impl From<PgCharacter> for CharacterEntity {
tech_menu: CharacterTechMenu { tech_menu: CharacterTechMenu {
tech_menu: vec_to_array(other.tech_menu) tech_menu: vec_to_array(other.tech_menu)
}, },
keyboard_config: CharacterKeyboardConfig {
keyboard_config: vec_to_array(other.keyboard_config)
},
gamepad_config: CharacterGamepadConfig {
gamepad_config: vec_to_array(other.gamepad_config)
},
playtime: other.playtime as u32, playtime: other.playtime as u32,
} }
} }

15
src/entity/gateway/postgres/postgres.rs

@ -229,8 +229,7 @@ async fn create_character(conn: &mut sqlx::PgConnection, char: NewCharacterEntit
hair, hair_r, hair_g, hair_b, prop_x, hair, hair_r, hair_g, hair_b, prop_x,
prop_y, techs, config, infoboard, guildcard, prop_y, techs, config, infoboard, guildcard,
power, mind, def, evade, luck, power, mind, def, evade, luck,
hp, tp, tech_menu, option_flags, keyboard_config,
gamepad_config, playtime)
hp, tp, tech_menu, option_flags, playtime)
values values
($1, $2, $3, $4, $5, ($1, $2, $3, $4, $5,
$6, $7, $8, $9, $10, $6, $7, $8, $9, $10,
@ -270,8 +269,6 @@ async fn create_character(conn: &mut sqlx::PgConnection, char: NewCharacterEntit
.bind(char.materials.tp as i16) .bind(char.materials.tp as i16)
.bind(char.tech_menu.tech_menu.to_vec()) .bind(char.tech_menu.tech_menu.to_vec())
.bind(char.option_flags as i32) .bind(char.option_flags as i32)
.bind(&char.keyboard_config.keyboard_config.to_vec())
.bind(&char.gamepad_config.gamepad_config.to_vec())
.bind(0) .bind(0)
.fetch_one(conn).await?; .fetch_one(conn).await?;
@ -298,8 +295,8 @@ async fn save_character(conn: &mut sqlx::PgConnection, char: &CharacterEntity) -
let q = r#"update player_character set let q = r#"update player_character set
user_account=$1, slot=$2, name=$3, exp=$4, class=$5, section_id=$6, costume=$7, skin=$8, face=$9, head=$10, hair=$11, hair_r=$12, user_account=$1, slot=$2, name=$3, exp=$4, class=$5, section_id=$6, costume=$7, skin=$8, face=$9, head=$10, hair=$11, hair_r=$12,
hair_g=$13, hair_b=$14, prop_x=$15, prop_y=$16, techs=$17, config=$18, infoboard=$19, guildcard=$20, power=$21, mind=$22, def=$23, hair_g=$13, hair_b=$14, prop_x=$15, prop_y=$16, techs=$17, config=$18, infoboard=$19, guildcard=$20, power=$21, mind=$22, def=$23,
evade=$24, luck=$25, hp=$26, tp=$27, tech_menu=$28, option_flags=$29, keyboard_config=$30, gamepad_config=$31, playtime=$32
where id=$33;"#;
evade=$24, luck=$25, hp=$26, tp=$27, tech_menu=$28, option_flags=$29, playtime=$30
where id=$31;"#;
sqlx::query(q) sqlx::query(q)
.bind(char.user_id.0) // $1 .bind(char.user_id.0) // $1
.bind(char.slot as i16) // $2 .bind(char.slot as i16) // $2
@ -330,10 +327,8 @@ async fn save_character(conn: &mut sqlx::PgConnection, char: &CharacterEntity) -
.bind(char.materials.tp as i16) // $27 .bind(char.materials.tp as i16) // $27
.bind(char.tech_menu.tech_menu.to_vec()) // $28 .bind(char.tech_menu.tech_menu.to_vec()) // $28
.bind(char.option_flags as i32) // $29 .bind(char.option_flags as i32) // $29
.bind(&char.keyboard_config.keyboard_config.to_vec()) // $30
.bind(&char.gamepad_config.gamepad_config.to_vec()) // $31
.bind(char.playtime as i32) // $32
.bind(char.id.0 as i32) // $33
.bind(char.playtime as i32) // $30
.bind(char.id.0 as i32) // $31
.execute(conn).await?; .execute(conn).await?;
Ok(()) Ok(())
} }

2
src/login/character.rs

@ -747,7 +747,7 @@ impl<EG: EntityGateway + Clone> InterserverActor for CharacterServerState<EG> {
fn new_character_from_preview(user: &UserAccountEntity, preview: &CharacterPreview) -> NewCharacterEntity { fn new_character_from_preview(user: &UserAccountEntity, preview: &CharacterPreview) -> NewCharacterEntity {
let mut character = NewCharacterEntity::new(user.id, 1); // it should not be possible for the client to specify the kbm config preset from the char create screen
let mut character = NewCharacterEntity::new(user.id);
character.slot = preview.slot; character.slot = preview.slot;
character.name = String::from_utf16_lossy(&preview.character.name).trim_matches(char::from(0)).into(); character.name = String::from_utf16_lossy(&preview.character.name).trim_matches(char::from(0)).into();
character.section_id = preview.character.section_id.into(); character.section_id = preview.character.section_id.into();

2
src/ship/items/actions.rs

@ -113,7 +113,7 @@ where
move |(mut item_state, mut transaction), _| { move |(mut item_state, mut transaction), _| {
Box::pin(async move { Box::pin(async move {
let mut inventory = item_state.inventory(&character_id).await?; let mut inventory = item_state.inventory(&character_id).await?;
let item = inventory.take_item(&item_id, amount).ok_or_else(|| ItemStateError::NoFloorItem(item_id))?;
let item = inventory.take_item(&item_id, amount).ok_or_else(|| ItemStateError::NoInventoryItem(item_id))?;
transaction.gateway().set_character_inventory(&character_id, &inventory.as_inventory_entity(&character_id)).await?; transaction.gateway().set_character_inventory(&character_id, &inventory.as_inventory_entity(&character_id)).await?;
item_state.set_inventory(inventory).await; item_state.set_inventory(inventory).await;

4
src/ship/packet/handler/lobby.rs

@ -34,8 +34,8 @@ pub async fn block_selected(id: ClientId,
.meseta(inventory.meseta) .meseta(inventory.meseta)
.inventory(&inventory) .inventory(&inventory)
.bank(&bank) .bank(&bank)
.keyboard_config(&client.character.keyboard_config.as_bytes())
.gamepad_config(&client.character.gamepad_config.as_bytes())
.keyboard_config(&client.settings.settings.keyboard_config)
.gamepad_config(&client.settings.settings.gamepad_config)
.symbol_chat(&client.settings.settings.symbol_chats) .symbol_chat(&client.settings.settings.symbol_chats)
.tech_menu(&client.character.tech_menu.as_bytes()) .tech_menu(&client.character.tech_menu.as_bytes())
.option_flags(client.character.option_flags) .option_flags(client.character.option_flags)

8
src/ship/packet/handler/settings.rs

@ -48,8 +48,8 @@ where
clients.with_mut(id, |client| { clients.with_mut(id, |client| {
let mut entity_gateway = entity_gateway.clone(); let mut entity_gateway = entity_gateway.clone();
Box::pin(async move { Box::pin(async move {
client.character.keyboard_config.update(&keyboard_config);
entity_gateway.save_character(&client.character).await
client.settings.settings.keyboard_config = keyboard_config.keyboard_config;
entity_gateway.save_user_settings(&client.settings).await
})}).await??; })}).await??;
Ok(Vec::new()) Ok(Vec::new())
} }
@ -65,8 +65,8 @@ where
clients.with_mut(id, |client| { clients.with_mut(id, |client| {
let mut entity_gateway = entity_gateway.clone(); let mut entity_gateway = entity_gateway.clone();
Box::pin(async move { Box::pin(async move {
client.character.gamepad_config.update(&gamepad_config);
entity_gateway.save_character(&client.character).await
client.settings.settings.gamepad_config = gamepad_config.gamepad_config;
entity_gateway.save_user_settings(&client.settings).await
})}).await??; })}).await??;
Ok(Vec::new()) Ok(Vec::new())
} }

3
tests/common.rs

@ -13,6 +13,7 @@ use libpso::packet::login::{Login, Session};
use libpso::{utf8_to_array, utf8_to_utf16_array}; use libpso::{utf8_to_array, utf8_to_utf16_array};
//TODO: remove kb_conf_preset
pub async fn new_user_character<EG: EntityGateway + Clone>(entity_gateway: &mut EG, username: &str, password: &str, kb_conf_preset: usize) -> (UserAccountEntity, CharacterEntity) { pub async fn new_user_character<EG: EntityGateway + Clone>(entity_gateway: &mut EG, username: &str, password: &str, kb_conf_preset: usize) -> (UserAccountEntity, CharacterEntity) {
let new_user = NewUserAccountEntity { let new_user = NewUserAccountEntity {
email: format!("{}@pso.com", username), email: format!("{}@pso.com", username),
@ -26,7 +27,7 @@ pub async fn new_user_character<EG: EntityGateway + Clone>(entity_gateway: &mut
let user = entity_gateway.create_user(new_user).await.unwrap(); let user = entity_gateway.create_user(new_user).await.unwrap();
let new_settings = NewUserSettingsEntity::new(user.id); let new_settings = NewUserSettingsEntity::new(user.id);
let _settings = entity_gateway.create_user_settings(new_settings).await.unwrap(); let _settings = entity_gateway.create_user_settings(new_settings).await.unwrap();
let new_character = NewCharacterEntity::new(user.id, kb_conf_preset);
let new_character = NewCharacterEntity::new(user.id);
let character = entity_gateway.create_character(new_character).await.unwrap(); let character = entity_gateway.create_character(new_character).await.unwrap();
entity_gateway.set_character_meseta(&character.id, Meseta(0)).await.unwrap(); entity_gateway.set_character_meseta(&character.id, Meseta(0)).await.unwrap();
entity_gateway.set_bank_meseta(&character.id, &BankName("".into()), Meseta(0)).await.unwrap(); entity_gateway.set_bank_meseta(&character.id, &BankName("".into()), Meseta(0)).await.unwrap();

29
tests/test_character.rs

@ -31,26 +31,6 @@ async fn test_save_options() {
assert!(char.option_flags == 12345); assert!(char.option_flags == 12345);
} }
#[async_std::test]
async fn test_default3_keyboard_mappings() {
/*
check if keyboard is set to default3 when specified. this will only occur for things like creating characters from the web page.
normal client behaviour will simply use default1 when creating a character.
gamepad only has 1 default config
*/
let mut entity_gateway = InMemoryGateway::default();
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 3).await;
assert!(char1.keyboard_config.as_bytes() == DEFAULT_KEYBOARD_CONFIG3);
}
#[async_std::test]
async fn test_invalid_keyboard_preset_value() {
// check if keyboard_config self-corrects to DEFAULT1 if an invalid value (>4) is given
let mut entity_gateway = InMemoryGateway::default();
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 10).await;
assert!(char1.keyboard_config.as_bytes() == DEFAULT_KEYBOARD_CONFIG1);
}
#[async_std::test] #[async_std::test]
async fn test_change_keyboard_mappings() { async fn test_change_keyboard_mappings() {
let mut entity_gateway = InMemoryGateway::default(); let mut entity_gateway = InMemoryGateway::default();
@ -63,7 +43,8 @@ async fn test_change_keyboard_mappings() {
log_in_char(&mut ship, ClientId(1), "a1", "a").await; log_in_char(&mut ship, ClientId(1), "a1", "a").await;
join_lobby(&mut ship, ClientId(1)).await; join_lobby(&mut ship, ClientId(1)).await;
assert!(char1.keyboard_config.as_bytes() == DEFAULT_KEYBOARD_CONFIG2);
let settings = entity_gateway.get_user_settings_by_user(&user1).await.unwrap();
assert!(settings.settings.keyboard_config == DEFAULT_KEYBOARD_CONFIG1);
// update from default2 to default4 // update from default2 to default4
// the client simply sends the full 364 bytes... // the client simply sends the full 364 bytes...
@ -95,8 +76,6 @@ async fn test_change_keyboard_mappings() {
], ],
})).await.unwrap(); })).await.unwrap();
let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
let char = characters[0].as_ref().unwrap();
assert!(char.keyboard_config.as_bytes() == DEFAULT_KEYBOARD_CONFIG4);
let settings = entity_gateway.get_user_settings_by_user(&user1).await.unwrap();
assert!(settings.settings.keyboard_config == DEFAULT_KEYBOARD_CONFIG4);
} }
Loading…
Cancel
Save