-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathusers.h
78 lines (67 loc) · 1.44 KB
/
users.h
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
#pragma once
#include "../system/singleton.h"
#include <boost/noncopyable.hpp>
#include "../protocol/define.h"
#include "../system/define.h"
#include <queue>
class Users : private boost::noncopyable
{
public:
typedef network::Serial Serial;
bool Add(UserID uid, Serial serial)
{
if (id_serial_map_.size() >= kMaxUsersCount)
{
return false;
}
id_serial_map_[uid] = serial;
serial_id_map_[serial] = uid;
return true;
}
void Remove(Serial serial)
{
auto iter = serial_id_map_.find(serial);
if (iter != serial_id_map_.end())
{
UserID uid = iter->second;
serial_id_map_.erase(iter);
id_serial_map_.erase(uid);
}
}
Serial UserID2Serial(UserID uid)
{
auto iter = id_serial_map_.find(uid);
if (iter != id_serial_map_.end())
{
return iter->second;
}
return network::kErrorSerial;
}
bool Has(Serial serial)
{
return serial_id_map_.find(serial) != serial_id_map_.end();
}
UserID Serial2UserID(Serial serial)
{
return serial_id_map_[serial];
}
template<typename Func>
void ForEachSerial(Func f)
{
for (const auto& si: serial_id_map_)
{
f(si.first);
}
}
private:
static const size_t kMaxUsersCount = 14096;
private:
std::map<UserID, Serial> id_serial_map_;
std::map<Serial, UserID> serial_id_map_;
std::map<Serial, std::queue<network::Flag>> world_flags_;
std::map<Serial, std::queue<network::Flag>> interact_flags_;
};
inline Users& GetUsers()
{
return singleton_default<Users>::instance();
}