OSDN Git Service

Linux環境でビルドが通らない問題を修正
[mmo/main.git] / server / Account.hpp
1 //\r
2 // Account.hpp\r
3 //\r
4 \r
5 #pragma once\r
6 \r
7 #include <string>\r
8 #include <map>\r
9 #include <list>\r
10 #include <unordered_map>\r
11 #include <stdint.h>\r
12 #include "../common/database/AccountProperty.hpp"\r
13 #include "../common/network/Utils.hpp"\r
14 #include "../common/Logger.hpp"\r
15 #include <boost/thread.hpp>\r
16 \r
17 typedef uint32_t UserID;\r
18 #undef GetUserName\r
19 \r
20 class Account {\r
21     public:\r
22         Account();\r
23         ~Account();\r
24 \r
25         void LoadInitializeData(UserID user_id, std::string data);\r
26 \r
27         uint32_t GetCurrentRevision();\r
28         std::string GetUserRevisionPatch(UserID user_id, uint32_t revision);\r
29 \r
30         UserID GetUserIdFromFingerPrint(const std::string&);\r
31         std::string GetPublicKey(UserID);\r
32         UserID RegisterPublicKey(const std::string&);\r
33 \r
34         void LogIn(UserID);\r
35         void LogOut(UserID);\r
36         void LogOutAll();\r
37 \r
38                 void Remove(UserID);\r
39 \r
40         std::string GetUserName(UserID) const;\r
41         void SetUserName(UserID, const std::string&);\r
42         std::string GetUserTrip(UserID) const;\r
43         void SetUserTrip(UserID, const std::string&);\r
44         std::string GetUserModelName(UserID) const;\r
45         void SetUserModelName(UserID, const std::string&);\r
46 \r
47         std::string GetUserIPAddress(UserID) const;\r
48         void SetUserIPAddress(UserID, const std::string&);\r
49         uint16_t GetUserUDPPort(UserID) const;\r
50         void SetUserUDPPort(UserID, uint16_t);\r
51         uint32_t GetUserRevision(UserID) const;\r
52                 \r
53         void SetUserChannel(UserID, unsigned char);\r
54         unsigned char GetUserChannel(UserID) const;\r
55 \r
56         void SetUserPosition(UserID, const PlayerPosition&);\r
57         PlayerPosition GetUserPosition(UserID) const;\r
58 \r
59         std::vector<UserID> GetIDList() const;\r
60 \r
61     private:\r
62         template <class T>\r
63         void Set(UserID user_id, AccountProperty property, T value, bool revision = true)\r
64         {\r
65                         if (user_id == 0) {\r
66                                 Logger::Error(_T("Invalid session id"));\r
67                                 return;\r
68                         }\r
69 \r
70             T old_value;\r
71             if (!Get(user_id, property, &old_value) || old_value != value) {\r
72                                 boost::unique_lock<boost::recursive_mutex> lock(mutex_);\r
73 \r
74                 if (user_map_.find(user_id) == user_map_.end()) {\r
75                     user_map_[user_id] = PropertyMap();\r
76                 }\r
77 \r
78                 user_map_[user_id][property].value = network::Utils::Serialize(value);\r
79 \r
80                 if (revision) {\r
81                     uint32_t new_revision = GetUserRevision(user_id) + 1;\r
82                     Logger::Debug("Userdata Update %d %d Revision: %d",\r
83                               user_id, property, new_revision);\r
84 \r
85                     user_map_[user_id][property].revision = new_revision;\r
86                     Set(user_id, REVISION, new_revision, false);\r
87                 }\r
88             }\r
89         }\r
90 \r
91         template <class T>\r
92         bool Get(UserID user_id, AccountProperty property, T* value) const\r
93         {\r
94             UserMap::const_iterator usermap_it;\r
95             if ((usermap_it = user_map_.find(user_id)) != user_map_.end()) {\r
96                 PropertyMap::const_iterator property_it;\r
97                 if ((property_it = usermap_it->second.find(property)) != usermap_it->second.end()) {\r
98                     network::Utils::Deserialize(property_it->second.value, value);\r
99                     return true;\r
100                 }\r
101             }\r
102             return false;\r
103         }\r
104 \r
105         struct PropertyValue {\r
106             PropertyValue() : revision(0) {}\r
107             uint32_t revision;\r
108             std::string value;\r
109         };\r
110 \r
111         typedef std::map<AccountProperty, PropertyValue> PropertyMap;\r
112         typedef std::map<UserID, PropertyMap> UserMap;\r
113         UserMap user_map_;\r
114 \r
115         typedef std::unordered_map<std::string, UserID> FingerprintMap;\r
116         FingerprintMap fingerprint_map_;\r
117 \r
118         typedef std::map<UserID, PlayerPosition> PositionMap;\r
119         PositionMap position_map_;\r
120 \r
121         uint32_t revision_;\r
122         UserID max_user_id_;\r
123 \r
124                 boost::recursive_mutex mutex_;\r
125 };\r