From 1e6e6aafae1079e5ee2b645eb3ecc68abc61d701 Mon Sep 17 00:00:00 2001 From: azw Date: Wed, 1 Nov 2023 12:57:02 +0000 Subject: [PATCH] 1 --- server/notifyserver/src/main.rs | 156 +++++++++++++++++++++++++++++++- 1 file changed, 152 insertions(+), 4 deletions(-) diff --git a/server/notifyserver/src/main.rs b/server/notifyserver/src/main.rs index ae0823e..3ddf699 100755 --- a/server/notifyserver/src/main.rs +++ b/server/notifyserver/src/main.rs @@ -1,19 +1,167 @@ +use std::rc::{Rc, Weak}; +use std::cell::RefCell; +use std::collections::HashMap; +use std::any::{Any}; + use f9::app::App; -use protobuf::Message; +//use protobuf::Message; +use r9_macro::SharedFromSelf; +use r9_macro_derive::SharedFromSelf; +//use r9_macro::Singleton; +use r9_macro_derive::Singleton; pub mod app; pub mod mt; pub mod cs_msgid; pub mod cs_proto; //pub mod cs_auto_gen; -pub mod mtb; +//pub mod mtb; + +pub trait MsgHandler { + + fn cm_join(_: &f9::MsgHdr, _: Rc::>) { + } + + fn cm_move(_: &f9::MsgHdr, _: Rc::>) { + + } + + fn cm_emote(_: &f9::MsgHdr, _: Rc::>) { + } + +} + +#[derive(SharedFromSelf)] +pub struct Player { + _self_wp: Weak::>, + account_id: String, + nick_name: String, + //net_handler: Rc::>, +} + +pub struct PlayerNetHandler { + self_wp: Weak::>, +} + +impl MsgHandler for PlayerNetHandler { +} + +impl PlayerNetHandler { + + pub fn cm_move(hdr: &f9::MsgHdr, + msg: Rc::>) { + println!("seq:{}", msg.borrow().get_seq()); + } + +} + +#[derive(SharedFromSelf)] +#[derive(Singleton)] +pub struct PlayerMgr { + _self_wp: Weak::>, + account_id_hash: HashMap>>, +} + +impl Player { + + pub fn new() -> Rc::> { + let p = Rc::new(RefCell::new( + Self { + _self_wp: Default::default(), + account_id: String::from(""), + nick_name: String::from(""), + } + )); + return p; + } + + pub fn init(&mut self, account_id: &String, nick_name: &String) { + self.account_id = account_id.clone(); + self.nick_name = nick_name.clone(); + } + + pub fn get_account_id(&self) -> String { + return self.account_id.clone(); + } + + pub fn get_nick_name(&self) -> String { + return self.nick_name.clone(); + } + + pub fn cm_move(self_p :&Rc::>, + hdr: &f9::MsgHdr, + msg: Rc::>) { + println!("seq:{}", msg.borrow().get_seq()); + } + +} + +impl PlayerMgr { + + fn new() -> Self { + let p = Self { + _self_wp: Default::default(), + account_id_hash: HashMap::new(), + }; + return p; + } + + pub fn add_player(&mut self, hum: &Rc::>) { + self.account_id_hash.insert( + hum.borrow().get_account_id(), + hum.clone()); + } + + pub fn get_player_by_account_id(&self, account_id: &String) -> + Option>> { + match self.account_id_hash.get(account_id) { + Some(v) => { + return Some(v.clone()); + } + None => { + return None; + } + } + } + +} fn main() { + { + let hum = Player::new(); + hum.borrow_mut().init(&"test1".to_string(), &"name1".to_string()); + PlayerMgr::instance().borrow_mut().add_player(&hum); + } + { + let hum = Player::new(); + hum.borrow_mut().init(&"test2".to_string(), &"name2".to_string()); + PlayerMgr::instance().borrow_mut().add_player(&hum); + } + { + + let hum = PlayerMgr::instance().borrow().get_player_by_account_id( + &String::from("test1") + ); + println!("account_id:{} name:{}", + hum.as_ref().unwrap().borrow().get_account_id(), + hum.as_ref().unwrap().borrow().get_nick_name()); + let msg = Rc::new(RefCell::new(cs_proto::CMMove::new())); + msg.borrow_mut().set_seq(100); + let msg_hdr = f9::MsgHdr { + //msg: Box::new(msg.clone()) + msg_id: 0, + seq_id: 0, + socket_handle: 0 + }; + Player::cm_move( + &hum.unwrap(), + &msg_hdr, + msg + ) + } App::instance().borrow_mut().init( crate::app::app::UserApp::instance() ); App::instance().borrow_mut().run(); App::instance().borrow_mut().uninit(); - let data: Vec = Vec::new(); -// let psystem = crate::mt::AliKeyConf::parse_from_bytes(&data).unwrap(); }