-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFunctor.cpp
More file actions
123 lines (101 loc) · 3.02 KB
/
Copy pathFunctor.cpp
File metadata and controls
123 lines (101 loc) · 3.02 KB
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
#include "Functor.h"
#include "LogComm.h"
#include "PushServer.h"
#include "UserStateProcessor.h"
#include "util/tc_common.h"
//线程停止标记
volatile bool g_bStop = false;
//事件检查调整
#define SLEEP_INTERVAL 60
//一天秒数
#define ONE_DAY_SEC 86400
//获取当天000秒数
time_t GetTodayZeroTime(const long time, const int offset)
{
time_t t = time_t(time);
struct tm pTm;
localtime_r(&t, &pTm);
pTm.tm_hour = 0;
pTm.tm_min = 0;
pTm.tm_sec = 0;
return mktime(&pTm) + offset;
}
Functor::Functor() : iLastUpdateOnlineTime(0), iLastUpdateStateTime(0)
{
}
Functor::~Functor()
{
}
//事件处理
void Functor::operator()(int t)
{
if (t == eUpdateOnlineNumber)
{
updateOnlineNumber();
}
else if (t == eUpdateUserPlayState)
{
updateUserPlayState();
}
}
//
void Functor::stop()
{
g_bStop = true;
}
//更新在线人数
void Functor::updateOnlineNumber()
{
__TRY__
while (!g_bStop)
{
int interval = g_app.getOuterFactoryPtr()->getOnlineInterval();
if (TNOW - iLastUpdateOnlineTime >= interval)
{
iLastUpdateOnlineTime = TNOW;
g_app.getOuterFactoryPtr()->setOnline();
int iOnlineNum = g_app.getOuterFactoryPtr()->getOnline();
LOG_INFO << "Update the number of online players, iOnlineNum: " << iOnlineNum << endl;
}
TC_Common::sleep(SLEEP_INTERVAL);
}
__CATCH__
}
//更新玩家状态
void Functor::updateUserPlayState()
{
__TRY__
if (iLastUpdateStateTime == 0)
{
iLastUpdateStateTime = GetTodayZeroTime(TNOW, ONE_DAY_SEC);
}
while (!g_bStop)
{
LOG_DEBUG << "------------------->LastUpdateTime:" << iLastUpdateStateTime << ", NowTime: " << TNOW << endl;
//零点重置
if (TNOW >= iLastUpdateStateTime)
{
iLastUpdateStateTime = GetTodayZeroTime(TNOW, ONE_DAY_SEC);
auto robots = g_app.getOuterFactoryPtr()->getRobotList();
if (robots.empty())
{
g_app.getOuterFactoryPtr()->readRobotListResp();
robots = g_app.getOuterFactoryPtr()->getRobotList();
LOG_ERROR << "Load online robot list, robotNum: " << robots.size() << endl;
}
std::map<long, long> uids;
UserStateSingleton::getInstance()->getOnlineUsers(uids, true);
LOG_ERROR << "Update online player status, iOnlineNum: " << uids.size() << endl;
for (auto iter = uids.begin(); iter != uids.end(); iter++)
{
long uid = (*iter).first;
g_app.getOuterFactoryPtr()->asyncUserStateNotify(uid);
LOG_ERROR << "Update player status, uid: " << uid << endl;
}
}
//冗余信息清除
UserStateSingleton::getInstance()->cleanOnlineUser(true);
TC_Common::sleep(SLEEP_INTERVAL);
}
__CATCH__
}