- 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:
@@ -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,
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -9,8 +9,6 @@ pub struct Character {
|
||||
pub id: i32,
|
||||
pub user_id: i32,
|
||||
pub name: String,
|
||||
pub level: i16,
|
||||
pub experience: i64,
|
||||
pub inventory: serde_json::Value,
|
||||
pub stats: serde_json::Value,
|
||||
pub looks: serde_json::Value,
|
||||
@@ -57,7 +55,7 @@ impl CharacterRepository {
|
||||
pub async fn create_character(&self, user_id: i32, name: &str, inventory: serde_json::Value, stats: serde_json::Value, looks: serde_json::Value, position: serde_json::Value) -> Result<i32, sqlx::Error> {
|
||||
let result = sqlx::query(
|
||||
"INSERT INTO characters (user_id, name, inventory, stats, looks, position, created_at, updated_at, is_active) \
|
||||
VALUES ($1, $2, 1, 0, $3, $4, $5, $6, NOW(), NOW(), true) RETURNING id",
|
||||
VALUES ($1, $2, $3, $4, $5, $6, NOW(), NOW(), true) RETURNING id",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(name)
|
||||
@@ -68,18 +66,23 @@ impl CharacterRepository {
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
||||
// Invalidate cache
|
||||
let cache_key = format!("character:user:{}", user_id);
|
||||
self.cache.lock().await.delete(&cache_key).await.map_err(|_| sqlx::Error::RowNotFound)?;
|
||||
Ok(result.get("id"))
|
||||
}
|
||||
|
||||
pub async fn delete_character(&self, character_id: i32) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(
|
||||
"UPDATE characters SET deleted_at = NOW(), is_active = false WHERE id = $1",
|
||||
let result = sqlx::query(
|
||||
"UPDATE characters SET deleted_at = NOW(), is_active = false WHERE id = $1 RETURNING user_id",
|
||||
)
|
||||
.bind(character_id)
|
||||
.execute(&self.pool)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
||||
// Invalidate cache
|
||||
let cache_key = format!("character:user:{}", result.get::<i32, &str>("user_id"));
|
||||
self.cache.lock().await.delete(&cache_key).await.map_err(|_| sqlx::Error::RowNotFound)?;
|
||||
let cache_key = format!("character:{}", character_id);
|
||||
self.cache.lock().await.delete(&cache_key).await.map_err(|_| sqlx::Error::RowNotFound)?;
|
||||
Ok(())
|
||||
|
||||
@@ -21,8 +21,6 @@ impl CharacterService for MyDatabaseService {
|
||||
id: character.id,
|
||||
user_id: character.user_id,
|
||||
name: character.name,
|
||||
level: character.level as i32,
|
||||
experience: character.experience,
|
||||
inventory: character.inventory.to_string(),
|
||||
stats: character.stats.to_string(),
|
||||
looks: character.looks.to_string(),
|
||||
@@ -54,8 +52,6 @@ impl CharacterService for MyDatabaseService {
|
||||
id: character.id,
|
||||
user_id: character.user_id,
|
||||
name: character.name,
|
||||
level: character.level as i32,
|
||||
experience: character.experience,
|
||||
inventory: character.inventory.to_string(),
|
||||
stats: character.stats.to_string(),
|
||||
looks: character.looks.to_string(),
|
||||
|
||||
@@ -5,6 +5,7 @@ pub struct ConnectionState {
|
||||
pub user_id: Option<i32>,
|
||||
pub session_id: Option<String>,
|
||||
pub character_id: Option<i8>,
|
||||
pub character_list: Option<Vec<u8>>,
|
||||
pub additional_data: HashMap<String, String>, // Flexible data storage
|
||||
}
|
||||
|
||||
@@ -14,6 +15,7 @@ impl ConnectionState {
|
||||
user_id: None,
|
||||
session_id: None,
|
||||
character_id: None,
|
||||
character_list: None,
|
||||
additional_data: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,25 @@ pub(crate) enum EquippedPosition {
|
||||
Earing,
|
||||
MaxEquipItems,
|
||||
}
|
||||
impl EquippedPosition {
|
||||
pub(crate) fn from_i32(value: i32) -> Option<EquippedPosition> {
|
||||
match value {
|
||||
1 => Some(EquippedPosition::Goggles),
|
||||
2 => Some(EquippedPosition::Helmet),
|
||||
3 => Some(EquippedPosition::Armor),
|
||||
4 => Some(EquippedPosition::Backpack),
|
||||
5 => Some(EquippedPosition::Gauntlet),
|
||||
6 => Some(EquippedPosition::Boots),
|
||||
7 => Some(EquippedPosition::WeaponR),
|
||||
8 => Some(EquippedPosition::WeaponL),
|
||||
9 => Some(EquippedPosition::Necklace),
|
||||
10 => Some(EquippedPosition::Ring),
|
||||
11 => Some(EquippedPosition::Earing),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
|
||||
@@ -15,12 +15,27 @@ use tracing::{debug, error, info, warn};
|
||||
use crate::auth_client::AuthClient;
|
||||
use crate::character_client::CharacterClient;
|
||||
use crate::connection_service::ConnectionService;
|
||||
use crate::enums;
|
||||
use crate::packets::cli_create_char_req::CliCreateCharReq;
|
||||
use crate::packets::cli_delete_char_req::CliDeleteCharReq;
|
||||
use crate::packets::cli_select_char_req::CliSelectCharReq;
|
||||
use crate::packets::srv_create_char_reply::SrvCreateCharReply;
|
||||
use crate::packets::srv_delete_char_reply::SrvDeleteCharReply;
|
||||
|
||||
fn convert_slot(slot: i32) -> srv_char_list_reply::EquippedPosition {
|
||||
match enums::EquippedPosition::from_i32(slot) {
|
||||
Some(enums::EquippedPosition::Goggles) => srv_char_list_reply::EquippedPosition::Googles,
|
||||
Some(enums::EquippedPosition::Helmet) => srv_char_list_reply::EquippedPosition::Helmet,
|
||||
Some(enums::EquippedPosition::Armor) => srv_char_list_reply::EquippedPosition::Armor,
|
||||
Some(enums::EquippedPosition::Backpack) => srv_char_list_reply::EquippedPosition::Backpack,
|
||||
Some(enums::EquippedPosition::Gauntlet) => srv_char_list_reply::EquippedPosition::Gauntlet,
|
||||
Some(enums::EquippedPosition::Boots) => srv_char_list_reply::EquippedPosition::Boots,
|
||||
Some(enums::EquippedPosition::WeaponR) => srv_char_list_reply::EquippedPosition::WeaponR,
|
||||
Some(enums::EquippedPosition::WeaponL) => srv_char_list_reply::EquippedPosition::WeaponL,
|
||||
_ => srv_char_list_reply::EquippedPosition::MaxItems
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_char_list_req(stream: &mut TcpStream, packet: Packet, character_client: Arc<Mutex<CharacterClient>>, connection_service: Arc<ConnectionService>, connection_id: String) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
use crate::packets::srv_char_list_reply::*;
|
||||
let request = CliCharListReq::decode(packet.payload.as_slice());
|
||||
@@ -38,21 +53,22 @@ pub(crate) async fn handle_char_list_req(stream: &mut TcpStream, packet: Packet,
|
||||
let character_list = character_client.get_character_list(&user_id.to_string()).await?;
|
||||
debug!("{:?}", character_list.characters);
|
||||
let mut characters = vec![];
|
||||
let mut character_id_list:Vec<u8> = Vec::new();
|
||||
for character in character_list.characters {
|
||||
let mut item_list: [EquippedItem; (MAX_VISIBLE_ITEMS as usize)] = core::array::from_fn(|i| EquippedItem::default());
|
||||
|
||||
{
|
||||
let mut index = 0;
|
||||
let _ = character.items.iter().map(|item| {
|
||||
item_list[index] = EquippedItem {
|
||||
id: item.id as u16,
|
||||
for item in character.items {
|
||||
if item.slot < 10 {
|
||||
let slot = convert_slot(item.slot) as usize;
|
||||
item_list[slot] = EquippedItem {
|
||||
id: item.item_id as u16,
|
||||
gem_opt: item.gem_option as u16,
|
||||
socket: item.socket as i8,
|
||||
grade: item.grade as u8,
|
||||
};
|
||||
index = index + 1;
|
||||
});
|
||||
}
|
||||
}
|
||||
debug!("Item list is created as: {:?}", item_list);
|
||||
|
||||
let character_info = CharInfo {
|
||||
name: NullTerminatedString(character.name),
|
||||
@@ -65,9 +81,14 @@ pub(crate) async fn handle_char_list_req(stream: &mut TcpStream, packet: Packet,
|
||||
hair: character.looks.unwrap().hair as u32,
|
||||
items: item_list,
|
||||
};
|
||||
character_id_list.push(character.character_id.parse().unwrap());
|
||||
characters.push(character_info);
|
||||
}
|
||||
|
||||
if let Some(mut state) = connection_service.get_connection_mut(&connection_id) {
|
||||
state.character_list = Some(character_id_list); // Save the real character id's for later as the client sends what is selected from 0 index. It does not use the real character idss
|
||||
}
|
||||
|
||||
debug!("{:?}", characters);
|
||||
let data = SrvCharListReply { characters };
|
||||
let response_packet = Packet::new(PacketType::PakccCharListReply, &data)?;
|
||||
@@ -114,13 +135,16 @@ pub(crate) async fn handle_delete_char_req(stream: &mut TcpStream, packet: Packe
|
||||
|
||||
let mut user_id = 0;
|
||||
let session_id;
|
||||
let mut character_id_list:Vec<u8> = Vec::new();
|
||||
|
||||
if let Some(mut state) = connection_service.get_connection(&connection_id) {
|
||||
user_id = state.user_id.expect("Missing user id in connection state");
|
||||
session_id = state.session_id.expect("Missing session id in connection state");
|
||||
character_id_list = state.character_list.expect("Missing character id list");
|
||||
}
|
||||
|
||||
let mut character_client = character_client.lock().await;
|
||||
let delete_response = character_client.delete_character(&user_id.to_string(), &request.char_id.to_string()).await?;
|
||||
let delete_response = character_client.delete_character(&user_id.to_string(), &character_id_list[request.char_id as usize].to_string()).await?;
|
||||
|
||||
let character_name = request.name;
|
||||
let data = SrvDeleteCharReply { remaining_time: delete_response.remaining_time as u32, name: character_name };
|
||||
@@ -137,13 +161,15 @@ pub(crate) async fn handle_select_char_req(stream: &mut TcpStream, packet: Packe
|
||||
debug!("{:?}", request);
|
||||
|
||||
let mut user_id = 0;
|
||||
let mut character_id_list:Vec<u8> = Vec::new();
|
||||
if let Some(mut state) = connection_service.get_connection_mut(&connection_id) {
|
||||
user_id = state.user_id.expect("Missing user id in connection state");
|
||||
character_id_list = state.character_list.clone().expect("Missing character id list");
|
||||
state.character_id = Some(request.char_id as i8);
|
||||
}
|
||||
|
||||
let mut character_client = character_client.lock().await;
|
||||
let character_data = character_client.get_character(&user_id.to_string(), request.char_id).await?;
|
||||
let character_data = character_client.get_character(&user_id.to_string(), character_id_list[request.char_id as usize]).await?;
|
||||
let mut equipped_item_list: [EquippedItem; (MAX_VISIBLE_ITEMS as usize)] = core::array::from_fn(|i| EquippedItem::default());
|
||||
let mut effect_list: [StatusEffect; (MAX_STATUS_EFFECTS as usize)] = core::array::from_fn(|i| StatusEffect::default());
|
||||
let mut hotbar_list: [HotbarItem; (MAX_HOTBAR_ITEMS as usize)] = core::array::from_fn(|i| HotbarItem::default());
|
||||
|
||||
@@ -14,10 +14,11 @@ message Looks {
|
||||
}
|
||||
|
||||
message EquippedItem {
|
||||
int32 id = 1;
|
||||
int32 item_id = 1;
|
||||
int32 gem_option = 2;
|
||||
int32 socket = 3;
|
||||
int32 grade = 4;
|
||||
int32 slot = 5;
|
||||
}
|
||||
|
||||
message Character {
|
||||
|
||||
@@ -10,7 +10,8 @@ service CharacterService {
|
||||
}
|
||||
|
||||
message CharacterRequest {
|
||||
int32 character_id = 1;
|
||||
int32 user_id = 1;
|
||||
int32 character_id = 2;
|
||||
}
|
||||
|
||||
message CharacterListRequest {
|
||||
@@ -49,8 +50,6 @@ message Character {
|
||||
int32 id = 1;
|
||||
int32 user_id = 2;
|
||||
string name = 3;
|
||||
int32 level = 4;
|
||||
int64 experience = 5;
|
||||
string inventory = 6;
|
||||
string stats = 7;
|
||||
string looks = 8;
|
||||
|
||||
Reference in New Issue
Block a user