/
entity.cpp
135 lines (122 loc) · 3.64 KB
/
entity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "common/entity.h"
#include "server/ffworker.h"
#include "server/script.h"
#include "common/prop.h"
using namespace ff;
using namespace std;
map<long, WeakPtr<Entity> > Entity::EntityPtr2Ref;
SharedPtr<Entity> Entity::genEntity(int ntype, userid_t id, userid_t sid){
SharedPtr<Entity> ret = new Entity(ntype, id, sid);
Entity::EntityPtr2Ref[long(ret.get())] = ret;
ENTITY_MGR.add(ret);
return ret;
}
SharedPtr<Entity> Entity::toEntity(long p){
std::map<long, WeakPtr<Entity> >::iterator it = EntityPtr2Ref.find(p);
if (it != EntityPtr2Ref.end()){
return it->second.lock();
}
return NULL;
}
Entity::~Entity(){
Entity::EntityPtr2Ref.erase(long(this));
clearField();
}
bool Entity::sendMsg(uint16_t cmd, const std::string& msg)
{
if (m_sessionID){
FFWORKER.sessionSendMsg(m_sessionID, cmd, msg);
return true;
}
return false;
}
SharedPtr<Entity> Entity::toPtr(){
return Entity::toEntity(long(this));
}
void Entity::initField(EntityField* ret, const std::string& name){
ret->setFiledName(name);
ret->setOwner(this->toPtr());
}
EntityField* Entity::getFieldByName(const std::string& name){
for (size_t i = 0; i < m_fields.size(); ++i){
EntityField* ret = m_fields[i];
if (ret && ret->getFieldName() == name){
return ret;
}
}
return NULL;
}
void Entity::clearField(){
for (size_t i = 0; i < m_fields.size(); ++i){
EntityField* ret = m_fields[i];
if (ret){
m_fields[i] = NULL;
delete ret;
}
}
m_fields.clear();
}
void EntityMgr::add(EntityPtr p){
m_allEntity[p->getType()][p->getUid()] = p;
if (p->getSession()){
m_session2entity[p->getSession()] = p;
}
}
bool EntityMgr::del(int ntype, userid_t id){
std::map<userid_t, EntityPtr>& allEntity = m_allEntity[ntype];
std::map<userid_t, EntityPtr>::iterator it = allEntity.find(id);
if (it != allEntity.end()){
EntityPtr p = it->second;
if (p->getSession()){
m_session2entity.erase(p->getSession());
}
allEntity.erase(it);
return true;
}
return false;
}
EntityPtr EntityMgr::get(int ntype, userid_t id){
std::map<userid_t, EntityPtr>& allEntity = m_allEntity[ntype];
std::map<userid_t, EntityPtr>::iterator it = allEntity.find(id);
if (it != allEntity.end()){
return it->second;
}
return NULL;
}
static userid_t Entity_getUid(EntityPtr p){
if (p){
return p->getUid();
}
return 0;
}
static size_t Entity_totalNum(){
return Entity::EntityPtr2Ref.size();
}
int64_t gInt = 0;
static bool initEntityEnvir(){
//!这里演示的是如何注册脚本接口
SCRIPT_UTIL.reg("Entity.getUid", Entity_getUid);
SCRIPT_UTIL.reg("Entity.totalNum", Entity_totalNum);
/*example code
printf("initEntityEnvir....\n");
ScriptArgObjPtr ret = SCRIPT_UTIL.callScript<ScriptArgObjPtr>("testScriptCall");
ret->dump();
int arg1 = 0;
string arg2 = "sss";
double arg3 = 3.14;
int ret = 0;
for (int i = 0; i < 1; ++i)
ret = SCRIPT_UTIL.callScript<int>("testScriptCall", arg1, arg2, arg3, arg1, arg2, arg3, arg1, arg2, arg3);
printf("initEntityEnvir....callScript:%d\n", ret);
*/
return true;
}
static bool cleanupEntityEnvir(){
/*example code
printf("cleanupEntityEnvir....\n");
SCRIPT_UTIL.callScript<void>("testScriptCall");
*/
return true;
}
WORKER_AT_SETUP(initEntityEnvir);
WORKER_AT_EXIT(cleanupEntityEnvir);