diff --git a/server/gameserver/app.cc b/server/gameserver/app.cc index 7476053..0e5e867 100755 --- a/server/gameserver/app.cc +++ b/server/gameserver/app.cc @@ -136,16 +136,19 @@ int App::Run() } int ret = 0; a8::UdpLog::Instance()->Info("gameserver running", {}); + last_run_tick_ = a8::XGetTickCount(); while (!terminated) { a8::tick_t begin_tick = a8::XGetTickCount(); Global::g_nowtime = time(nullptr); - QuickExecute(); - SlowerExecute(); + int delta_time = a8::XGetTickCount() - last_run_tick_; + QuickExecute(delta_time); + SlowerExecute(delta_time); a8::tick_t end_tick = a8::XGetTickCount(); if (end_tick - begin_tick > perf.max_run_delay_time) { perf.max_run_delay_time = end_tick - begin_tick; } Schedule(); + last_run_tick_ = a8::XGetTickCount(); } return ret; } @@ -202,14 +205,15 @@ void App::AddIMMsg(unsigned short imcmd, a8::XParams params) NotifyLoopCond(); } -void App::QuickExecute() +void App::QuickExecute(int delta_time) { ProcessIMMsg(); DispatchMsg(); a8::Timer::Instance()->Update(); + RoomMgr::Instance()->Update(delta_time); } -void App::SlowerExecute() +void App::SlowerExecute(int delta_time) { } @@ -221,6 +225,12 @@ void App::NotifyLoopCond() void App::Schedule() { + #if 1 + { + std::unique_lock lk(*loop_mutex_); + loop_cond_->wait_for(lk, std::chrono::milliseconds(1)); + } + #else std::unique_lock lk(*loop_mutex_); if (!HasTask()) { int sleep_time = a8::Timer::Instance()->GetIdleableMillSeconds(); @@ -229,6 +239,7 @@ void App::Schedule() perf.max_timer_idle = sleep_time; } } + #endif } bool App::HasTask() diff --git a/server/gameserver/app.h b/server/gameserver/app.h index d8c662c..d4da03f 100644 --- a/server/gameserver/app.h +++ b/server/gameserver/app.h @@ -34,8 +34,8 @@ public: a8::XParams* GetContext(long long context_id); private: - void QuickExecute(); - void SlowerExecute(); + void QuickExecute(int delta_time); + void SlowerExecute(int delta_time); void Schedule(); bool HasTask(); @@ -61,6 +61,7 @@ public: bool is_test_mode = false; private: + long long last_run_tick_ = 0; std::mutex *loop_mutex_ = nullptr; std::condition_variable *loop_cond_ = nullptr; diff --git a/server/gameserver/metadata.h b/server/gameserver/metadata.h index d3e0308..7d14e11 100755 --- a/server/gameserver/metadata.h +++ b/server/gameserver/metadata.h @@ -5,6 +5,11 @@ namespace MetaData { + struct Parameter + { + const metatable::Parameter* i = nullptr; + }; + struct Map { const metatable::Map* i = nullptr; diff --git a/server/gameserver/metamgr.cc b/server/gameserver/metamgr.cc index faa09fd..a808ef0 100755 --- a/server/gameserver/metamgr.cc +++ b/server/gameserver/metamgr.cc @@ -10,6 +10,8 @@ class MetaDataLoader { public: + std::list parameter_meta_list; + std::list parameter_list; std::list map_meta_list; std::list map_list; std::list safearea_meta_list; @@ -19,6 +21,7 @@ public: std::list equip_meta_list; std::list equip_list; + std::map parameter_hash; std::map gamemap_hash; std::map safearea_hash; std::map item_hash; @@ -33,6 +36,7 @@ public: res_path = "../res/"; } + f8::ReadCsvMetaFile(res_path + "parameter@parameter.csv", parameter_meta_list); f8::ReadCsvMetaFile(res_path + "map@map.csv", map_meta_list); f8::ReadCsvMetaFile(res_path + "safearea@safearea.csv", safearea_meta_list); f8::ReadCsvMetaFile(res_path + "item@item.csv", item_meta_list); @@ -45,6 +49,12 @@ private: void BindToMetaData() { + for (auto& meta : parameter_meta_list) { + MetaData::Parameter& item = a8::FastAppend(parameter_list); + item.i = &meta; + parameter_hash[item.i->param_name()] = &item; + } + for (auto& meta : map_meta_list) { MetaData::Map& item = a8::FastAppend(map_list); item.i = &meta; @@ -93,3 +103,15 @@ void MetaMgr::Reload() loader_ = new MetaDataLoader(); loader_->Load(); } + +std::string MetaMgr::GetSysParam(const std::string& param_name) +{ + auto itr = loader_->parameter_hash.find(param_name); + return itr != loader_->parameter_hash.end() ? itr->second->i->param_value() : ""; +} + +MetaData::Map* MetaMgr::GetMap(int map_id) +{ + auto itr = loader_->gamemap_hash.find(map_id); + return itr != loader_->gamemap_hash.end() ? itr->second : nullptr; +} diff --git a/server/gameserver/metamgr.h b/server/gameserver/metamgr.h index f8bc62e..4c7b108 100755 --- a/server/gameserver/metamgr.h +++ b/server/gameserver/metamgr.h @@ -16,6 +16,9 @@ class MetaMgr : public a8::Singleton void UnInit(); void Reload(); + std::string GetSysParam(const std::string& param_name); + MetaData::Map* GetMap(int map_id); + private: MetaDataLoader* loader_ = nullptr; }; diff --git a/server/gameserver/player.cc b/server/gameserver/player.cc index a55fe39..44c3e84 100644 --- a/server/gameserver/player.cc +++ b/server/gameserver/player.cc @@ -4,6 +4,11 @@ #include "player.h" #include "cs_proto.pb.h" +void Player::Update(int delta_time) +{ + +} + void Player::_CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg) { diff --git a/server/gameserver/player.h b/server/gameserver/player.h index b5f4d8c..feb590b 100644 --- a/server/gameserver/player.h +++ b/server/gameserver/player.h @@ -9,35 +9,43 @@ namespace cs class CMVoice; } +class Room; class Player { public: enum { HID = HID_Player }; public: + Room* room = nullptr; std::string account_id; - int obj_uniid = 0; + std::string team_uniid; + int team_mode = 0; + int player_count = 0; + bool auto_fill = false; + bool use_touch = false; + std::string avatar_url; + + unsigned short obj_uniid = 0; float health = 0.0; + bool dead = false; + bool downed = false; + bool disconnected = false; + int anim_type = 0; + int anim_seq = 0; + int action_type = 0; + int skin = 0; + int helmet = 0; + int chest = 0; + int weapon = 0; + int energy_shield = 0; + int vip = 0; + int sdmg = 0; + + void Update(int delta_time); void _CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg); void _CMDropItem(f8::MsgHdr& hdr, const cs::CMDropItem& msg); void _CMEmote(f8::MsgHdr& hdr, const cs::CMEmote& msg); void _CMSpectate(f8::MsgHdr& hdr, const cs::CMSpectate& msg); void _CMVoice(f8::MsgHdr& hdr, const cs::CMVoice& msg); - -private: - bool dead_ = false; - bool downed_ = false; - bool disconnected_ = false; - int anim_type_ = 0; - int anim_seq_ = 0; - int action_type_ = 0; - int skin_ = 0; - int helmet_ = 0; - int chest_ = 0; - int weapon_ = 0; - int energy_shield_ = 0; - int vip_ = 0; - int sdmg_ = 0; - }; diff --git a/server/gameserver/playermgr.cc b/server/gameserver/playermgr.cc index 1703c32..10770a8 100644 --- a/server/gameserver/playermgr.cc +++ b/server/gameserver/playermgr.cc @@ -2,6 +2,7 @@ #include "playermgr.h" #include "player.h" +#include "cs_proto.pb.h" void PlayerMgr::Init() { @@ -17,3 +18,29 @@ void PlayerMgr::Update() { } + +Player* PlayerMgr::GetPlayerSocket(int socket) +{ + auto itr = socket_hash_.find(socket); + return itr != socket_hash_.end() ? itr->second : nullptr; +} + +Player* PlayerMgr::CreatePlayerByCMJoin(unsigned short obj_uniid, const cs::CMJoin& msg) +{ + Player* hum = new Player(); + hum->obj_uniid = obj_uniid; + hum->account_id = msg.account_id(); + hum->health = 100; + hum->team_uniid = msg.team_uuid(); + hum->team_mode = msg.team_mode(); + hum->player_count = msg.player_count(); + hum->auto_fill = msg.auto_fill(); + hum->use_touch = msg.use_touch(); + hum->avatar_url = msg.avatar_url(); + hum->energy_shield = msg.energy_shield(); + // hum->baseskin = msg.baseskin(); + // hum->basemelee = msg.basemelee(); + // hum->elo_score = msg.elo_score(); + // hum->gmode = msg.gmode(); + return hum; +} diff --git a/server/gameserver/playermgr.h b/server/gameserver/playermgr.h index a7c252a..fd388f6 100644 --- a/server/gameserver/playermgr.h +++ b/server/gameserver/playermgr.h @@ -1,5 +1,10 @@ #pragma once +namespace cs +{ + class CMJoin; +} + class Player; class PlayerMgr : public a8::Singleton { @@ -15,7 +20,9 @@ class PlayerMgr : public a8::Singleton void UnInit(); void Update(); + Player* GetPlayerSocket(int socket); + Player* CreatePlayerByCMJoin(unsigned short obj_uniid, const cs::CMJoin& msg); + private: - std::map id_hash_; std::map socket_hash_; }; diff --git a/server/gameserver/room.cc b/server/gameserver/room.cc index 1e20abe..7a9782f 100644 --- a/server/gameserver/room.cc +++ b/server/gameserver/room.cc @@ -7,6 +7,13 @@ const int ROOM_MAX_PLAYER_NUM = 50; +void Room::Update(int delta_time) +{ + for (auto& pair : uniid_hash_) { + pair.second->Update(delta_time); + } +} + bool Room::IsFull() { return accountid_hash_.size() >= ROOM_MAX_PLAYER_NUM; @@ -23,8 +30,20 @@ Player* Room::GetPlayerByAccountId(const std::string& accountid) return itr != accountid_hash_.end() ? itr->second : nullptr; } -Player* Room::GetPlayerByUniId(int uniid) +Player* Room::GetPlayerByUniId(unsigned short uniid) { auto itr = uniid_hash_.find(uniid); return itr != uniid_hash_.end() ? itr->second : nullptr; } + +void Room::AddPlayer(Player* hum) +{ + hum->room = this; + uniid_hash_[hum->obj_uniid] = hum; + accountid_hash_[hum->account_id] = hum; +} + +unsigned short Room::AllocUniid() +{ + return ++current_uniid; +} diff --git a/server/gameserver/room.h b/server/gameserver/room.h index c1570e8..bf11c8f 100644 --- a/server/gameserver/room.h +++ b/server/gameserver/room.h @@ -16,17 +16,21 @@ class Player; class Room { public: + long long room_uuid = 0; MetaData::Map* map_meta = nullptr; + void Update(int delta_time); bool IsFull(); int GetPlayerNum(); Player* GetPlayerByAccountId(const std::string& accountid); - Player* GetPlayerByUniId(int uniid); + Player* GetPlayerByUniId(unsigned short uniid); + void AddPlayer(Player* hum); + unsigned short AllocUniid(); private: - long long room_id_ = 0; + unsigned short current_uniid = 0; RoomState_e state_ = RS_Inactive; std::map accountid_hash_; - std::map uniid_hash_; + std::map uniid_hash_; }; diff --git a/server/gameserver/roommgr.cc b/server/gameserver/roommgr.cc index cff4901..20ac6b5 100644 --- a/server/gameserver/roommgr.cc +++ b/server/gameserver/roommgr.cc @@ -4,6 +4,10 @@ #include "room.h" #include "cs_proto.pb.h" #include "GGListener.h" +#include "player.h" +#include "playermgr.h" +#include "app.h" +#include "metamgr.h" void RoomMgr::Init() { @@ -15,16 +19,44 @@ void RoomMgr::UnInit() } +void RoomMgr::Update(int delta_time) +{ + for (auto& pair : room_hash_) { + pair.second->Update(delta_time); + } +} + void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg) { - cs::SMJoinedNotify notifymsg; Room* room = GetJoinableRoom(msg.account_id()); - if (room) { - - } else { - + if (!room) { + room = new Room(); + room->room_uuid = App::Instance()->NewUuid(); + assert(GetRoomByUuid(room->room_uuid)); + if (GetRoomByUuid(room->room_uuid)) { + abort(); + } + room->map_meta = MetaMgr::Instance()->GetMap(1001); + room_hash_[room->room_uuid] = room; + } + unsigned short new_uniid = room->AllocUniid(); + Player* hum = PlayerMgr::Instance()->CreatePlayerByCMJoin(new_uniid, msg); + room->AddPlayer(hum); + + { + cs::SMJoinedNotify notifymsg; + notifymsg.set_team_mode(msg.team_mode()); + notifymsg.set_player_id(hum->obj_uniid); + notifymsg.set_started(false); + GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg); + } + { + cs::SMMapInfo notifymsg; + notifymsg.set_map_id(room->map_meta->i->map_id()); + notifymsg.set_width(room->map_meta->i->width()); + notifymsg.set_height(room->map_meta->i->height()); + GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg); } - GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg); } Room* RoomMgr::GetJoinableRoom(const std::string& account_id) @@ -36,3 +68,9 @@ Room* RoomMgr::GetJoinableRoom(const std::string& account_id) } return nullptr; } + +Room* RoomMgr::GetRoomByUuid(long long uuid) +{ + auto itr = room_hash_.find(uuid); + return itr != room_hash_.end() ? itr->second : nullptr; +} diff --git a/server/gameserver/roommgr.h b/server/gameserver/roommgr.h index a9f9781..a17afd4 100644 --- a/server/gameserver/roommgr.h +++ b/server/gameserver/roommgr.h @@ -18,11 +18,13 @@ class RoomMgr : public a8::Singleton public: void Init(); void UnInit(); + void Update(int delta_time); void _CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg); private: Room* GetJoinableRoom(const std::string& account_id); + Room* GetRoomByUuid(long long uuid); private: std::map inactive_room_hash_; diff --git a/server/tools/protobuild/cs_msgid.proto b/server/tools/protobuild/cs_msgid.proto index 765d504..3f9e070 100644 --- a/server/tools/protobuild/cs_msgid.proto +++ b/server/tools/protobuild/cs_msgid.proto @@ -20,11 +20,12 @@ enum SMMessageId_e _SMRpcError = 102; _SMLogin = 103; - _SMVoiceNotify = 1001; - _SMJoinedNotify = 1002; + _SMJoinedNotify = 1001; + _SMMapInfo = 1002; _SMPlayerInfo = 1003; _SMUpdate = 1004; _SMKillMsg = 1005; _SMPickup = 1006; - _SMDisconnectNotify = 1007; + _SMVoiceNotify = 1007; + _SMDisconnectNotify = 1008; } diff --git a/server/tools/protobuild/cs_proto.proto b/server/tools/protobuild/cs_proto.proto index 691f8f9..fe518fc 100755 --- a/server/tools/protobuild/cs_proto.proto +++ b/server/tools/protobuild/cs_proto.proto @@ -83,7 +83,7 @@ message MFMapObject message MFPlayerInfo { - optional int32 id = 1; + optional int32 player_id = 1; optional int32 team_id = 2; optional string name = 3; } @@ -394,7 +394,7 @@ message MFEmote message MFPlayerStats { optional int32 player_id = 1; - optional string player_icon = 2; + optional string player_avatar_url = 2; optional int32 time_alive = 3; optional int32 kills = 4; optional int32 dead = 5; @@ -452,20 +452,19 @@ message CMJoin optional string team_uuid = 2; //队伍唯一id (没组队时为空字符串) optional int32 team_mode = 3; //队伍模式 0:单人 1:多人 optional int32 player_count = 4; //玩家数(单人时为1) - optional int32 auto_fill = 5; //是否自动填充玩家 + optional bool auto_fill = 5; //是否自动填充玩家 optional int32 bot = 6; //是否机器人 optional string name = 7; //角色名 - optional int32 use_touch = 8; //zzz + optional bool use_touch = 8; //zzz optional string account_id = 9; //账号id account_id repeated int32 emotes = 10; //表情列表 - optional string icon = 11; //头像 + optional string avatar_url = 11; //头像 optional int32 energy_shield = 12; //能量护盾 optional int32 baseskin = 13; //皮肤id optional int32 basemelee = 14; //xx - optional int32 elo_score = 15; + optional int32 elo_score = 15; repeated MFPlug plugs = 16; - optional string channel = 20; //渠道编号 optional string gmode = 21; } @@ -546,8 +545,9 @@ message SMJoinedNotify optional int32 player_id = 2; //玩家id optional bool started = 3; //游戏是否已开始 repeated MFPlayerInfo player_infos = 4; //玩家信息 - optional int32 map_type = 5; - optional bool elo_start = 6; + + optional int32 map_type = 5; //目前没用到 + optional bool elo_start = 6; //目前没用到 } //地图信息 @@ -556,8 +556,8 @@ message SMMapInfo optional int32 map_id = 1; //地图id optional int32 width = 2; //地图宽度 optional int32 height = 3; //地图高度 - optional int32 seed = 4; //zzz - repeated MFPlace places = 5; //zzz + optional int32 seed = 4; //没用到 + repeated MFPlace places = 5; //没用到 repeated MFMapObject objects = 6; //地图对象 } diff --git a/server/tools/protobuild/metatable.proto b/server/tools/protobuild/metatable.proto index 9ff574a..44ee0ed 100755 --- a/server/tools/protobuild/metatable.proto +++ b/server/tools/protobuild/metatable.proto @@ -1,5 +1,11 @@ package metatable; +message Parameter +{ + optional string param_name = 1; + optional string param_value = 2; +} + message Map { optional int32 map_id = 1; //地图id