v1.0.1 changes #11

Manually merged
Raven merged 54 commits from betterauth into main 2025-03-21 23:36:08 -04:00
21 changed files with 141 additions and 30 deletions
Showing only changes of commit a9a6c52b00 - Show all commits

View File

@@ -1,6 +1,6 @@
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure() tonic_build::configure()
.compile_well_known_types(true) .compile_well_known_types(true)
.compile_protos(&["../proto/auth.proto"], &["../proto"])?; .compile_protos(&["../proto/common.proto", "../proto/auth.proto"], &["../proto"])?;
Ok(()) Ok(())
} }

View File

@@ -16,6 +16,9 @@ use auth::{LoginRequest, RegisterRequest};
use log::{error, info}; use log::{error, info};
use tokio::sync::Mutex; use tokio::sync::Mutex;
pub mod common {
tonic::include_proto!("common");
}
pub mod auth { pub mod auth {
tonic::include_proto!("auth"); tonic::include_proto!("auth");
} }

View File

@@ -1,6 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use crate::auth::auth_service_server::AuthService; use crate::auth::auth_service_server::AuthService;
use crate::auth::{LoginRequest, LoginResponse, PasswordResetRequest, PasswordResetResponse, RegisterRequest, RegisterResponse, ResetPasswordRequest, ResetPasswordResponse, ValidateTokenRequest, ValidateTokenResponse, ValidateSessionRequest, ValidateSessionResponse, Empty, LogoutRequest}; use crate::auth::{LoginRequest, LoginResponse, PasswordResetRequest, PasswordResetResponse, RegisterRequest, RegisterResponse, ResetPasswordRequest, ResetPasswordResponse, ValidateTokenRequest, ValidateTokenResponse, ValidateSessionRequest, ValidateSessionResponse, LogoutRequest};
use crate::common::{Empty};
use crate::database_client::{DatabaseClient, DatabaseClientTrait}; use crate::database_client::{DatabaseClient, DatabaseClientTrait};
use crate::session::session_service_client::SessionServiceClient; use crate::session::session_service_client::SessionServiceClient;
use crate::session::{CreateSessionRequest, GetSessionRequest, DeleteSessionRequest}; use crate::session::{CreateSessionRequest, GetSessionRequest, DeleteSessionRequest};

View File

@@ -3,6 +3,11 @@ pub mod jwt;
pub mod database_client; pub mod database_client;
pub mod users; pub mod users;
pub mod common {
tonic::include_proto!("common");
}
pub mod auth { pub mod auth {
tonic::include_proto!("auth"); tonic::include_proto!("auth");
} }

View File

@@ -82,6 +82,7 @@ impl CharacterService for MyCharacterService {
position: serde_json::from_str(&get_character_response.position).unwrap(), position: serde_json::from_str(&get_character_response.position).unwrap(),
looks: serde_json::from_str(&get_character_response.looks).unwrap(), looks: serde_json::from_str(&get_character_response.looks).unwrap(),
stats: serde_json::from_str(&get_character_response.stats).unwrap(), stats: serde_json::from_str(&get_character_response.stats).unwrap(),
skills: serde_json::from_str(&get_character_response.skills).unwrap(),
items: serde_json::from_str(&get_character_response.inventory).unwrap(), items: serde_json::from_str(&get_character_response.inventory).unwrap(),
}; };

View File

@@ -11,6 +11,7 @@ pub struct Character {
pub name: String, pub name: String,
pub inventory: serde_json::Value, pub inventory: serde_json::Value,
pub stats: serde_json::Value, pub stats: serde_json::Value,
pub skills: serde_json::Value,
pub looks: serde_json::Value, pub looks: serde_json::Value,
pub position: serde_json::Value, pub position: serde_json::Value,
pub created_at: chrono::NaiveDateTime, pub created_at: chrono::NaiveDateTime,
@@ -39,7 +40,7 @@ impl CharacterRepository {
// Fetch from database // Fetch from database
let character = sqlx::query_as::<_, Character>( let character = sqlx::query_as::<_, Character>(
"SELECT id, user_id, name, inventory, stats, looks, position, \ "SELECT id, user_id, name, inventory, stats, skills, looks, position, \
created_at, updated_at, extract(epoch from (deleted_at - now()))::BIGINT as deleted_at, is_active \ created_at, updated_at, extract(epoch from (deleted_at - now()))::BIGINT as deleted_at, is_active \
FROM characters WHERE id = $1 AND is_active = true", FROM characters WHERE id = $1 AND is_active = true",
) )

View File

@@ -1,5 +1,5 @@
use serde_json::Value::Null; use serde_json::Value::Null;
use crate::grpc::{Character, CharacterRequest, CharacterListRequest, CharacterListResponse, CreateCharacterRequest, CreateCharacterResponse, DeleteCharacterRequest, DeleteCharacterResponse, Empty}; use crate::grpc::{Character, CharacterRequest, CharacterListRequest, CharacterListResponse, CreateCharacterRequest, CreateCharacterResponse, DeleteCharacterRequest, DeleteCharacterResponse};
use crate::grpc::character_service_server::CharacterService; use crate::grpc::character_service_server::CharacterService;
use crate::grpc::database_service::MyDatabaseService; use crate::grpc::database_service::MyDatabaseService;
use tonic::{Request, Response, Status}; use tonic::{Request, Response, Status};
@@ -29,6 +29,7 @@ impl CharacterService for MyDatabaseService {
name: character.name, name: character.name,
inventory: character.inventory.to_string(), inventory: character.inventory.to_string(),
stats: character.stats.to_string(), stats: character.stats.to_string(),
skills: character.skills.to_string(),
looks: character.looks.to_string(), looks: character.looks.to_string(),
position: character.position.to_string(), position: character.position.to_string(),
created_at: character.created_at.to_string(), created_at: character.created_at.to_string(),
@@ -66,6 +67,7 @@ impl CharacterService for MyDatabaseService {
name: character.name, name: character.name,
inventory: character.inventory.to_string(), inventory: character.inventory.to_string(),
stats: character.stats.to_string(), stats: character.stats.to_string(),
skills: character.skills.to_string(),
looks: character.looks.to_string(), looks: character.looks.to_string(),
position: character.position.to_string(), position: character.position.to_string(),
created_at: character.created_at.to_string(), created_at: character.created_at.to_string(),

View File

@@ -1,5 +1,6 @@
use crate::auth::auth_service_client::AuthServiceClient; use crate::auth::auth_service_client::AuthServiceClient;
use crate::auth::{Empty, LoginRequest, LoginResponse, LogoutRequest, ValidateSessionRequest, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse}; use crate::auth::{LoginRequest, LoginResponse, LogoutRequest, ValidateSessionRequest, ValidateSessionResponse, ValidateTokenRequest, ValidateTokenResponse};
use crate::common::{Empty};
use tonic::transport::Channel; use tonic::transport::Channel;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

View File

@@ -189,9 +189,16 @@ pub(crate) async fn handle_select_char_req(stream: &mut TcpStream, packet: Packe
let looks = character.looks.unwrap(); let looks = character.looks.unwrap();
let position = character.position.unwrap(); let position = character.position.unwrap();
let stats = character.stats.unwrap(); let stats = character.stats.unwrap();
let skills = character.skills;
let items = character.items; let items = character.items;
let mut equipped_item_list: [EquippedItem; (MAX_VISIBLE_ITEMS as usize)] = core::array::from_fn(|i| EquippedItem::default()); let mut equipped_item_list: [EquippedItem; (MAX_VISIBLE_ITEMS as usize)] = core::array::from_fn(|i| EquippedItem::default());
let mut inventory: [srv_inventory_data::Item; (MAX_ITEMS as usize)] = core::array::from_fn(|i| srv_inventory_data::Item::default()); let mut inventory: [srv_inventory_data::Item; (MAX_ITEMS as usize)] = core::array::from_fn(|i| srv_inventory_data::Item::default());
let mut skill_list: [u16; (MAX_SKILL_COUNT as usize)] = core::array::from_fn(|i| {
if i < skills.len() {
return skills[i] as u16;
}
0
});
for item in items { for item in items {
if item.slot < MAX_VISIBLE_ITEMS as i32 { if item.slot < MAX_VISIBLE_ITEMS as i32 {
@@ -265,7 +272,7 @@ pub(crate) async fn handle_select_char_req(stream: &mut TcpStream, packet: Packe
effects: effect_list, effects: effect_list,
pat_hp: stats.pat_hp as u16, pat_hp: stats.pat_hp as u16,
pat_cooldown_time: stats.pat_cooldown_time as u32, pat_cooldown_time: stats.pat_cooldown_time as u32,
skills: [0u16; (MAX_SKILL_COUNT as usize)], skills: skill_list,
hotbar: hotbar_list, hotbar: hotbar_list,
tag: user_id as u32, tag: user_id as u32,
name: request.name, name: request.name,

View File

@@ -37,6 +37,9 @@ mod character_client;
mod connection_state; mod connection_state;
mod connection_service; mod connection_service;
pub mod common {
tonic::include_proto!("common");
}
pub mod auth { pub mod auth {
tonic::include_proto!("auth"); // Path matches the package name in auth.proto tonic::include_proto!("auth"); // Path matches the package name in auth.proto
} }

View File

@@ -2,9 +2,11 @@ syntax = "proto3";
package auth; package auth;
import "common.proto";
service AuthService { service AuthService {
rpc Login(LoginRequest) returns (LoginResponse); rpc Login(LoginRequest) returns (LoginResponse);
rpc Logout(LogoutRequest) returns (Empty); rpc Logout(LogoutRequest) returns (common.Empty);
rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse); rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse);
rpc ValidateSession(ValidateSessionRequest) returns (ValidateSessionResponse); rpc ValidateSession(ValidateSessionRequest) returns (ValidateSessionResponse);
rpc RefreshSession(ValidateSessionRequest) returns (ValidateSessionResponse); rpc RefreshSession(ValidateSessionRequest) returns (ValidateSessionResponse);
@@ -74,5 +76,3 @@ message ResetPasswordRequest {
message ResetPasswordResponse { message ResetPasswordResponse {
string message = 1; string message = 1;
} }
message Empty {}

View File

@@ -78,5 +78,6 @@ message CharacterFull {
Location position = 3; // Character's position Location position = 3; // Character's position
Looks looks = 4; // Character's Looks Looks looks = 4; // Character's Looks
Stats stats = 5; // Character's stats Stats stats = 5; // Character's stats
repeated Item items = 6; // Character inventory repeated int32 skills = 6; // Character's skills
repeated Item items = 7; // Character inventory
} }

View File

@@ -53,12 +53,11 @@ message Character {
string name = 3; string name = 3;
string inventory = 6; string inventory = 6;
string stats = 7; string stats = 7;
string looks = 8; string skills = 8;
string position = 9; string looks = 9;
string created_at = 10; string position = 10;
string updated_at = 11; string created_at = 11;
string deleted_at = 12; string updated_at = 12;
bool is_active = 13; string deleted_at = 13;
bool is_active = 14;
} }
message Empty {}

25
proto/chat.proto Normal file
View File

@@ -0,0 +1,25 @@
syntax = "proto3";
package chat;
import "common.proto";
service ChatService {
rpc SendMessage(ChatMessage) returns (common.Empty);
}
enum MessageType {
MESSAGE_TYPE_UNSPECIFIED = 0;
MESSAGE_TYPE_NORMAL = 1;
MESSAGE_TYPE_SHOUT = 2;
MESSAGE_TYPE_PARTY = 3;
MESSAGE_TYPE_WHISPER = 4;
MESSAGE_TYPE_CLAN = 5;
MESSAGE_TYPE_ALLIED = 6;
}
message ChatMessage {
MessageType type = 1;
string message = 2;
string target = 3;
}

5
proto/common.proto Normal file
View File

@@ -0,0 +1,5 @@
syntax = "proto3";
package common;
message Empty {}

View File

@@ -2,11 +2,13 @@ syntax = "proto3";
package session_service_api; package session_service_api;
import "common.proto";
service SessionService { service SessionService {
rpc CreateSession (CreateSessionRequest) returns (SessionResponse); rpc CreateSession (CreateSessionRequest) returns (SessionResponse);
rpc GetSession (GetSessionRequest) returns (SessionResponse); rpc GetSession (GetSessionRequest) returns (SessionResponse);
rpc RefreshSession (GetSessionRequest) returns (SessionResponse); rpc RefreshSession (GetSessionRequest) returns (SessionResponse);
rpc DeleteSession (DeleteSessionRequest) returns (Empty); rpc DeleteSession (DeleteSessionRequest) returns (common.Empty);
} }
message CreateSessionRequest { message CreateSessionRequest {
@@ -34,4 +36,3 @@ message SessionResponse {
string ip_address = 6; string ip_address = 6;
} }
message Empty {}

View File

@@ -1,17 +1,67 @@
syntax = "proto3"; syntax = "proto3";
package character; package world;
service CharacterService { service WorldService {
rpc GetCharacter(CharacterRequest) returns (CharacterResponse); rpc GetCharacter(CharacterRequest) returns (CharacterResponse);
rpc ChangeMap(ChangeMapRequest) returns (ChangeMapResponse);
rpc MoveCharacter(CharacterMoveRequest) returns (CharacterMoveResponse);
} }
message CharacterRequest { message CharacterRequest {
string token = 1; string token = 1;
string user_id = 2; string user_id = 2;
string char_id = 3; string char_id = 3;
string session_id = 4;
} }
message CharacterResponse { message CharacterResponse {
int32 count = 1; int32 count = 1;
} }
message CharacterMoveRequest {
string session_id = 1;
uint32 target_id = 2;
float x = 3;
float y = 4;
float z = 5;
}
message CharacterMoveResponse {
int32 id = 1;
int32 target_id = 2;
int32 distance = 3;
float x = 4;
float y = 5;
float z = 6;
}
message ChangeMapRequest {
int32 id = 1;
float x = 2;
float y = 3;
}
message ChangeMapResponse {
int32 id = 1;
int32 map_id = 2;
float x = 3;
float y = 4;
int32 move_mode = 5;
int32 ride_mode = 6;
}
message AttackRequest {
string session_id = 1;
uint32 target_id = 2;
}
message ObjectHpRequest {
string session_id = 1;
uint32 target_id = 2;
}
message ObjectHpResponse {
uint32 target_id = 1;
int32 hp = 2;
}

View File

@@ -4,7 +4,7 @@ fn main() {
.build_server(true) // Generate gRPC server code .build_server(true) // Generate gRPC server code
.compile_well_known_types(true) .compile_well_known_types(true)
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]") .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.compile_protos(&["../proto/session_service_api.proto"], &["../proto"]) .compile_protos(&["../proto/common.proto", "../proto/session_service_api.proto"], &["../proto"])
.unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e)); .unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e));
// gRPC Client code // gRPC Client code

View File

@@ -15,6 +15,10 @@ use utils::service_discovery::get_service_address;
use crate::api::session_service_server::SessionServiceServer; use crate::api::session_service_server::SessionServiceServer;
use crate::session_service::SessionServiceImpl; use crate::session_service::SessionServiceImpl;
pub mod common {
tonic::include_proto!("common");
}
pub mod api { pub mod api {
tonic::include_proto!("session_service_api"); tonic::include_proto!("session_service_api");
} }

View File

@@ -3,8 +3,9 @@ use tokio::sync::Mutex;
use tonic::{Request, Response, Status}; use tonic::{Request, Response, Status};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::api::{ use crate::api::{
CreateSessionRequest, SessionResponse, GetSessionRequest, DeleteSessionRequest, Empty, CreateSessionRequest, SessionResponse, GetSessionRequest, DeleteSessionRequest,
}; };
use crate::common::{Empty};
use crate::api::session_service_server::SessionService; use crate::api::session_service_server::SessionService;
use utils::redis_cache::{Cache, RedisCache}; use utils::redis_cache::{Cache, RedisCache};

View File

@@ -41,6 +41,7 @@ create table characters
name varchar(50) not null, name varchar(50) not null,
inventory jsonb default '{}'::jsonb, inventory jsonb default '{}'::jsonb,
stats jsonb default '{}'::jsonb, stats jsonb default '{}'::jsonb,
skills jsonb default '{}'::jsonb,
looks jsonb default '{}'::jsonb, looks jsonb default '{}'::jsonb,
position jsonb default '{}'::jsonb, position jsonb default '{}'::jsonb,
created_at timestamp default CURRENT_TIMESTAMP, created_at timestamp default CURRENT_TIMESTAMP,