add Parameter.csv reader

This commit is contained in:
aozhiwei 2019-03-14 09:57:48 +08:00
parent 6197a080af
commit db26e044ca
16 changed files with 207 additions and 48 deletions

View File

@ -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<std::mutex> lk(*loop_mutex_);
loop_cond_->wait_for(lk, std::chrono::milliseconds(1));
}
#else
std::unique_lock<std::mutex> 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()

View File

@ -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;

View File

@ -5,6 +5,11 @@
namespace MetaData
{
struct Parameter
{
const metatable::Parameter* i = nullptr;
};
struct Map
{
const metatable::Map* i = nullptr;

View File

@ -10,6 +10,8 @@
class MetaDataLoader
{
public:
std::list<metatable::Parameter> parameter_meta_list;
std::list<MetaData::Parameter> parameter_list;
std::list<metatable::Map> map_meta_list;
std::list<MetaData::Map> map_list;
std::list<metatable::SafeArea> safearea_meta_list;
@ -19,6 +21,7 @@ public:
std::list<metatable::Equip> equip_meta_list;
std::list<MetaData::Equip> equip_list;
std::map<std::string, MetaData::Parameter*> parameter_hash;
std::map<int, MetaData::Map*> gamemap_hash;
std::map<int, MetaData::SafeArea*> safearea_hash;
std::map<int, MetaData::Item*> 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;
}

View File

@ -16,6 +16,9 @@ class MetaMgr : public a8::Singleton<MetaMgr>
void UnInit();
void Reload();
std::string GetSysParam(const std::string& param_name);
MetaData::Map* GetMap(int map_id);
private:
MetaDataLoader* loader_ = nullptr;
};

View File

@ -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)
{

View File

@ -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;
};

View File

@ -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;
}

View File

@ -1,5 +1,10 @@
#pragma once
namespace cs
{
class CMJoin;
}
class Player;
class PlayerMgr : public a8::Singleton<PlayerMgr>
{
@ -15,7 +20,9 @@ class PlayerMgr : public a8::Singleton<PlayerMgr>
void UnInit();
void Update();
Player* GetPlayerSocket(int socket);
Player* CreatePlayerByCMJoin(unsigned short obj_uniid, const cs::CMJoin& msg);
private:
std::map<int, Player*> id_hash_;
std::map<int, Player*> socket_hash_;
};

View File

@ -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;
}

View File

@ -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<std::string, Player*> accountid_hash_;
std::map<int, Player*> uniid_hash_;
std::map<unsigned short, Player*> uniid_hash_;
};

View File

@ -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,17 +19,45 @@ 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);
}
}
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;
}

View File

@ -18,11 +18,13 @@ class RoomMgr : public a8::Singleton<RoomMgr>
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<long long, Room*> inactive_room_hash_;

View File

@ -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;
}

View File

@ -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;
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; //
}

View File

@ -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