Proper AccountUID/PlayerUID implementation, RpcCreatePlayer

This commit is contained in:
xeon 2024-05-31 20:31:22 +03:00
parent f97bc25688
commit 7ef2b76a32
8 changed files with 97 additions and 21 deletions

View file

@ -59,7 +59,7 @@ where
pub async fn send_changes(&mut self, session: &NetworkSession) -> Result<&T> { pub async fn send_changes(&mut self, session: &NetworkSession) -> Result<&T> {
if self.player_info_changes.is_some() { if self.player_info_changes.is_some() {
let ptc_player_info_changed = PtcPlayerInfoChangedArg { let ptc_player_info_changed = PtcPlayerInfoChangedArg {
player_uid: session.get_player_uid().await, player_uid: session.player_uid().0,
player_info: self.player_info_changes.take().unwrap(), player_info: self.player_info_changes.take().unwrap(),
}; };

View file

@ -14,7 +14,7 @@ pub fn cur_timestamp_ms() -> u64 {
pub fn create_default_account(id: u64) -> AccountInfo { pub fn create_default_account(id: u64) -> AccountInfo {
AccountInfo { AccountInfo {
account_name: Some(format!("1_{id}")), account_name: Some(format!("1_{id}")),
players: Some(vec![id]), players: Some(Vec::new()),
gm_level: Some(10), gm_level: Some(10),
account_type: Some(1), account_type: Some(1),
register_cps: Some(String::new()), register_cps: Some(String::new()),

View file

@ -37,7 +37,7 @@ pub async fn on_rpc_hollow_move(
} }
let pos = PtcPositionInHollowChangedArg { let pos = PtcPositionInHollowChangedArg {
player_uid: 1337, player_uid: session.player_uid().0,
hollow_level: arg.hollow_level, hollow_level: arg.hollow_level,
position: destination_pos, position: destination_pos,
}; };
@ -58,7 +58,7 @@ pub async fn on_rpc_end_battle(
) -> Result<RpcEndBattleRet> { ) -> Result<RpcEndBattleRet> {
tracing::info!("RpcEndBattle: {:?}", &arg); tracing::info!("RpcEndBattle: {:?}", &arg);
let player_uid = session.get_player_uid().await; let player_uid = session.player_uid().0;
let (sync_event, hollow_finished) = session.context.hollow_grid_manager.battle_finished().await; let (sync_event, hollow_finished) = session.context.hollow_grid_manager.battle_finished().await;
if !hollow_finished { if !hollow_finished {
@ -75,7 +75,7 @@ pub async fn on_rpc_end_battle(
.await?; .await?;
let ptc_dungeon_quest_finished = PtcDungeonQuestFinishedArg { let ptc_dungeon_quest_finished = PtcDungeonQuestFinishedArg {
player_uid: 1337, player_uid,
quest_id: 1001000101, quest_id: 1001000101,
success: true, success: true,
reward_items: phashmap![], reward_items: phashmap![],
@ -219,7 +219,7 @@ pub async fn on_rpc_run_hollow_event_graph(
.await?; .await?;
let ptc_dungeon_quest_finished = PtcDungeonQuestFinishedArg { let ptc_dungeon_quest_finished = PtcDungeonQuestFinishedArg {
player_uid: 1337, player_uid: session.player_uid().0,
quest_id: 1001000101, quest_id: 1001000101,
success: true, success: true,
reward_items: phashmap![], reward_items: phashmap![],
@ -249,7 +249,7 @@ pub async fn on_rpc_run_hollow_event_graph(
.await?; .await?;
let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg { let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg {
player_uid: 1337, player_uid: session.player_uid().0,
hollow_level: 1, hollow_level: 1,
position: session position: session
.context .context
@ -371,13 +371,13 @@ pub async fn on_rpc_start_hollow_quest(
&session &session
.context .context
.hollow_grid_manager .hollow_grid_manager
.sync_hollow_maps(session.get_player_uid().await, scene_uid) .sync_hollow_maps(session.player_uid().0, scene_uid)
.await, .await,
) )
.await?; .await?;
let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg { let ptc_position_in_hollow_changed = PtcPositionInHollowChangedArg {
player_uid: 1337, player_uid: session.player_uid().0,
hollow_level: 1, hollow_level: 1,
position: session position: session
.context .context

View file

@ -1,18 +1,54 @@
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use super::*; use super::*;
use crate::game::util; use crate::{game::util, net::session::AccountUID};
const DEFAULT_ACCOUNT_ID: u64 = 1337; const DEFAULT_ACCOUNT_ID: u64 = 1;
pub async fn on_rpc_login(session: &NetworkSession, arg: &RpcLoginArg) -> Result<RpcLoginRet> { pub async fn on_rpc_login(session: &NetworkSession, arg: &RpcLoginArg) -> Result<RpcLoginRet> {
tracing::info!("Received rpc login arg: {}", arg.account_name); tracing::info!("Received rpc login arg: {}", arg.account_name);
*session.ns_prop_mgr.account_info.write().await =
util::create_default_account(DEFAULT_ACCOUNT_ID);
Ok(RpcLoginRet::new( match session
.logged_in(
AccountUID(DEFAULT_ACCOUNT_ID),
util::create_default_account(DEFAULT_ACCOUNT_ID),
)
.await
{
Ok(()) => Ok(RpcLoginRet::new(
session.ns_prop_mgr.serialize_account_info().await, session.ns_prop_mgr.serialize_account_info().await,
)) )),
Err(_) => Ok(RpcLoginRet::error(ErrorCode::RepeatedLogin, Vec::new())),
}
}
pub async fn on_rpc_create_player(
session: &NetworkSession,
_arg: &RpcCreatePlayerArg,
) -> Result<RpcCreatePlayerRet> {
let account_uid = session.account_uid();
let player_count = session
.ns_prop_mgr
.account_info
.read()
.await
.players
.as_ref()
.unwrap()
.len() as u64;
let player_uid = account_uid.0 * 100 + player_count + 1;
session
.ns_prop_mgr
.account_info
.write()
.await
.players
.as_mut()
.unwrap()
.push(player_uid);
Ok(RpcCreatePlayerRet::new(player_uid))
} }
pub async fn on_ptc_get_server_timestamp( pub async fn on_ptc_get_server_timestamp(

View file

@ -57,6 +57,7 @@ macro_rules! protocol_handlers {
protocol_handlers! { protocol_handlers! {
RpcLogin; RpcLogin;
RpcCreatePlayer;
PtcGetServerTimestamp; PtcGetServerTimestamp;
PtcPlayerOperation; PtcPlayerOperation;
RpcAdvanceBeginnerProcedure; RpcAdvanceBeginnerProcedure;

View file

@ -5,6 +5,7 @@ use qwer::{
use crate::config::CONFIGURATION; use crate::config::CONFIGURATION;
use crate::data; use crate::data;
use crate::game::util; use crate::game::util;
use crate::net::session::PlayerUID;
use super::*; use super::*;
@ -251,8 +252,10 @@ pub async fn on_rpc_enter_world(
) -> Result<RpcEnterWorldRet> { ) -> Result<RpcEnterWorldRet> {
let account = session.ns_prop_mgr.account_info.read().await; let account = session.ns_prop_mgr.account_info.read().await;
let id = *account.players.as_ref().unwrap().first().unwrap(); // get first id from list let player_uid = *account.players.as_ref().unwrap().first().unwrap(); // get first id from list
*session.ns_prop_mgr.player_info.write().await = create_player(id); session
.set_cur_player(PlayerUID(player_uid), create_player(player_uid))
.await?;
let item_manager = &session.context.item_manager; let item_manager = &session.context.item_manager;
item_manager.add_resource(501, 120).await; item_manager.add_resource(501, 120).await;

View file

@ -1,10 +1,11 @@
use anyhow::Result; use anyhow::Result;
use protocol::{AccountInfo, PlayerInfo};
use qwer::{OctData, ProtocolHeader}; use qwer::{OctData, ProtocolHeader};
use std::io::Cursor; use std::io::Cursor;
use std::sync::Arc; use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream; use tokio::net::TcpStream;
use tokio::sync::{Mutex, MutexGuard}; use tokio::sync::{Mutex, MutexGuard, OnceCell};
use crate::game::manager::net_stream; use crate::game::manager::net_stream;
use crate::game::GameContext; use crate::game::GameContext;
@ -12,11 +13,19 @@ use crate::game::GameContext;
use super::handlers::ProtocolHandler; use super::handlers::ProtocolHandler;
use super::{Packet, RequestBody, ResponseBody}; use super::{Packet, RequestBody, ResponseBody};
#[derive(Clone, Copy, Debug)]
pub struct AccountUID(pub u64);
#[derive(Clone, Copy, Debug)]
pub struct PlayerUID(pub u64);
pub struct NetworkSession { pub struct NetworkSession {
client_socket: Arc<Mutex<TcpStream>>, client_socket: Arc<Mutex<TcpStream>>,
cur_rpc_uid: u64, cur_rpc_uid: u64,
pub ns_prop_mgr: net_stream::PropertyManager, pub ns_prop_mgr: net_stream::PropertyManager,
pub context: GameContext, pub context: GameContext,
account_uid: OnceCell<AccountUID>,
player_uid: OnceCell<PlayerUID>,
} }
impl NetworkSession { impl NetworkSession {
@ -28,6 +37,8 @@ impl NetworkSession {
cur_rpc_uid: 0, cur_rpc_uid: 0,
context: GameContext::new(ns_prop_mgr.player_info.clone()), context: GameContext::new(ns_prop_mgr.player_info.clone()),
ns_prop_mgr, ns_prop_mgr,
account_uid: OnceCell::new(),
player_uid: OnceCell::new(),
} }
} }
@ -35,8 +46,26 @@ impl NetworkSession {
self.client_socket.lock().await self.client_socket.lock().await
} }
pub async fn get_player_uid(&self) -> u64 { pub async fn logged_in(&self, uid: AccountUID, account: AccountInfo) -> Result<()> {
self.ns_prop_mgr.player_info.read().await.uid.unwrap() self.account_uid.set(uid)?;
*self.ns_prop_mgr.account_info.write().await = account;
Ok(())
}
pub async fn set_cur_player(&self, uid: PlayerUID, player: PlayerInfo) -> Result<()> {
self.player_uid.set(uid)?;
*self.ns_prop_mgr.player_info.write().await = player;
Ok(())
}
pub fn account_uid(&self) -> AccountUID {
*self.account_uid.get().unwrap()
}
pub fn player_uid(&self) -> PlayerUID {
*self.player_uid.get().unwrap()
} }
pub async fn run(&mut self) -> Result<()> { pub async fn run(&mut self) -> Result<()> {

View file

@ -61,6 +61,9 @@ pub struct RpcLoginArg {
pub config_sign: String, pub config_sign: String,
} }
#[derive(OctData, Debug)]
pub struct RpcCreatePlayerArg {}
#[derive(OctData, Clone, Debug)] #[derive(OctData, Clone, Debug)]
pub struct PtcEnterSceneArg { pub struct PtcEnterSceneArg {
pub player_uid: u64, pub player_uid: u64,
@ -357,6 +360,10 @@ ret! {
account_info: PropertyBlob, account_info: PropertyBlob,
} }
struct RpcCreatePlayerRet {
player_uid: u64,
}
struct RpcEnterWorldRet { struct RpcEnterWorldRet {
player_info: PropertyBlob, player_info: PropertyBlob,
} }