- add: Character service now loads the data from the database and sends it in the character list packet

- add: character id list to the connection state for tracking the real character database id's for when the client requests actions on the character
- fix: sql error when trying to create a character
This commit is contained in:
2025-01-07 22:16:47 -05:00
parent cb6ee657f0
commit db868cc1ac
9 changed files with 117 additions and 62 deletions

View File

@@ -1,7 +1,7 @@
use tonic::transport::Channel;
use serde::{Deserialize, Serialize};
use crate::database::character_service_client::CharacterServiceClient;
use crate::database::{CreateCharacterRequest, CreateCharacterResponse, DeleteCharacterRequest, DeleteCharacterResponse};
use crate::database::{CharacterRequest, Character, CharacterListRequest, CharacterListResponse, CreateCharacterRequest, CreateCharacterResponse, DeleteCharacterRequest, DeleteCharacterResponse};
#[derive(Clone)]
pub struct CharacterDbClient {
@@ -15,6 +15,9 @@ struct Item {
count: i32,
durability: f32,
slot: i32,
gem_option: i32,
socket: i8,
grade: u32,
}
#[derive(Debug, Deserialize, Serialize)]
@@ -63,6 +66,23 @@ impl CharacterDbClient {
Ok(Self { client })
}
pub async fn get_character(&mut self, user_id: &str, char_id: &str) -> Result<Character, Box<dyn std::error::Error>> {
let request = tonic::Request::new(CharacterRequest {
user_id: user_id.parse().unwrap(),
character_id: char_id.parse().unwrap(),
});
let response = self.client.get_character(request).await?;
Ok(response.into_inner())
}
pub async fn get_character_list(&mut self, user_id: &str) -> Result<CharacterListResponse, Box<dyn std::error::Error>> {
let request = tonic::Request::new(CharacterListRequest {
user_id: user_id.parse().unwrap(),
});
let response = self.client.get_character_list(request).await?;
Ok(response.into_inner())
}
pub async fn create_character(&mut self, user_id: &str, name: &str, race: i32, face: i32, hair: i32, stone: i32) -> Result<CreateCharacterResponse, Box<dyn std::error::Error>> {
let mut hatid = 221;
if 0 == race {
@@ -76,6 +96,9 @@ impl CharacterDbClient {
count: 1,
slot: 3,
durability: 45.0,
gem_option: 0,
socket: 0,
grade: 0,
},
Item {
item_id: 1,
@@ -83,6 +106,9 @@ impl CharacterDbClient {
count: 1,
slot: 7,
durability: 45.0,
gem_option: 0,
socket: 0,
grade: 0,
},
Item {
item_id: hatid,
@@ -90,6 +116,9 @@ impl CharacterDbClient {
count: 1,
slot: 12,
durability: 45.0,
gem_option: 0,
socket: 0,
grade: 0,
},
];

View File

@@ -27,44 +27,24 @@ impl CharacterService for MyCharacterService {
let user_id = req.user_id;
debug!("Character list for User ID: {}", user_id);
// todo: get the data from the database
// self.character_db_client.as_ref();
// Simulated database fetch for characters
let characters = vec![
Character {
character_id: "1".to_string(),
name: "Warrior123".to_string(),
last_played: 1633017600,
delete_time: 0,
stats: Option::from(Stats {
job: 0,
level: 0,
}),
looks: Option::from(Looks {
race: 0,
hair: 0,
face: 0,
}),
items: vec![],
},
Character {
character_id: "2".to_string(),
name: "Mage123".to_string(),
last_played: 1633017600,
delete_time: 0,
stats: Option::from(Stats {
job: 211,
level: 20,
}),
looks: Option::from(Looks {
race: 1,
hair: 0,
face: 1,
}),
items: vec![],
},
];
let character_list = self.character_db_client.as_ref().clone().get_character_list(&user_id).await
.map_err(|_| Status::aborted("Unable to get character list"))?;
debug!("{:?}", character_list.characters);
let mut characters: Vec<Character> = vec![];
for character in character_list.characters {
characters.push(
Character {
character_id: character.id.to_string(),
name: character.name,
last_played: 0,
delete_time: 0,
stats: serde_json::from_str(&character.stats).unwrap(),
looks: serde_json::from_str(&character.looks).unwrap(),
items: serde_json::from_str(&character.inventory).unwrap(),
}
)
}
let response = GetCharacterListResponse { characters };
Ok(Response::new(response))