1
This commit is contained in:
parent
79e3944fbd
commit
1e6e6aafae
@ -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::<RefCell::<cs_proto::CMJoin>>) {
|
||||
}
|
||||
|
||||
fn cm_move(_: &f9::MsgHdr, _: Rc::<RefCell::<cs_proto::CMMove>>) {
|
||||
|
||||
}
|
||||
|
||||
fn cm_emote(_: &f9::MsgHdr, _: Rc::<RefCell::<cs_proto::CMEmote>>) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(SharedFromSelf)]
|
||||
pub struct Player {
|
||||
_self_wp: Weak::<RefCell::<Self>>,
|
||||
account_id: String,
|
||||
nick_name: String,
|
||||
//net_handler: Rc::<RefCell::<PlayerNetHandler>>,
|
||||
}
|
||||
|
||||
pub struct PlayerNetHandler {
|
||||
self_wp: Weak::<RefCell::<Player>>,
|
||||
}
|
||||
|
||||
impl MsgHandler for PlayerNetHandler {
|
||||
}
|
||||
|
||||
impl PlayerNetHandler {
|
||||
|
||||
pub fn cm_move(hdr: &f9::MsgHdr,
|
||||
msg: Rc::<RefCell::<cs_proto::CMMove>>) {
|
||||
println!("seq:{}", msg.borrow().get_seq());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(SharedFromSelf)]
|
||||
#[derive(Singleton)]
|
||||
pub struct PlayerMgr {
|
||||
_self_wp: Weak::<RefCell::<Self>>,
|
||||
account_id_hash: HashMap<String, Rc::<RefCell::<Player>>>,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
|
||||
pub fn new() -> Rc::<RefCell::<Self>> {
|
||||
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::<RefCell<Player>>,
|
||||
hdr: &f9::MsgHdr,
|
||||
msg: Rc::<RefCell::<cs_proto::CMMove>>) {
|
||||
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::<RefCell::<Player>>) {
|
||||
self.account_id_hash.insert(
|
||||
hum.borrow().get_account_id(),
|
||||
hum.clone());
|
||||
}
|
||||
|
||||
pub fn get_player_by_account_id(&self, account_id: &String) ->
|
||||
Option<Rc::<RefCell::<Player>>> {
|
||||
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<u8> = Vec::new();
|
||||
// let psystem = crate::mt::AliKeyConf::parse_from_bytes(&data).unwrap();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user