Browse Source

pick the newest character for a slot when recreating

pull/127/head
jake 2 years ago
parent
commit
aa019d4ea9
  1. 2
      src/entity/gateway/postgres/migrations/V0010__char_create_timestamp.sql
  2. 16
      src/entity/gateway/postgres/postgres.rs

2
src/entity/gateway/postgres/migrations/V0010__char_create_timestamp.sql

@ -0,0 +1,2 @@
alter table player_character
add created_at timestamptz default current_timestamp not null;

16
src/entity/gateway/postgres/postgres.rs

@ -276,17 +276,17 @@ async fn create_character(conn: &mut sqlx::PgConnection, char: NewCharacterEntit
async fn get_characters_by_user(conn: &mut sqlx::PgConnection, user: &UserAccountEntity) -> Result<[Option<CharacterEntity>; 4], GatewayError> async fn get_characters_by_user(conn: &mut sqlx::PgConnection, user: &UserAccountEntity) -> Result<[Option<CharacterEntity>; 4], GatewayError>
{ {
let mut stream = sqlx::query_as::<_, PgCharacter>("select * from player_character where user_account = $1 and slot < 4 order by slot")
let stream = sqlx::query_as::<_, PgCharacter>("select * from player_character where user_account = $1 and slot < 4 order by created_at;")
.bind(user.id.0) .bind(user.id.0)
.fetch(conn); .fetch(conn);
const NONE: Option<CharacterEntity> = None;
let mut result = [NONE; 4];
while let Some(character) = stream.try_next().await? {
let index = character.slot as usize;
result[index] = Some(character.into())
}
Ok(result)
Ok(stream.fold(core::array::from_fn(|_| None), |mut acc, char| {
if let Ok(char) = char {
let slot = char.slot as usize;
acc[slot] = Some(char.into())
}
acc
}).await)
} }
async fn save_character(conn: &mut sqlx::PgConnection, char: &CharacterEntity) -> Result<(), GatewayError> async fn save_character(conn: &mut sqlx::PgConnection, char: &CharacterEntity) -> Result<(), GatewayError>

Loading…
Cancel
Save