-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetting_manager.cpp
203 lines (163 loc) · 5.99 KB
/
setting_manager.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "stdafx.h"
#include "setting_manager.h"
#include "main.h"
#include "robot_utils.h"
int SettingManager::Init() {
LOG_INFO_FUNC("\t[START]");
if (kCommSucc != InitSettingWithLock()) {
LOG_ERROR("InitSetting() failed");
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int SettingManager::Term() const {
LOG_INFO_FUNC("[EXIT]");
return kCommSucc;
}
int SettingManager::IsRoomSettingExist(const RoomID& roomid, bool& exist) {
if (room_setting_map_.find(roomid) != room_setting_map_.end()) {
exist = true;
}
return kCommSucc;
}
int SettingManager::InitSettingWithLock() {
const auto filename = g_curExePath + _T("robot.setting");
Json::Value root;
Json::Reader reader;
std::ifstream ifile; //@zhuhangmin 20190226 RAII OBJ, NO NEED TO CLOSE
ifile.open(filename, std::ifstream::in);
if (!reader.parse(ifile, root, false)) ASSERT_FALSE_RETURN;
if (!root.isMember("game_id")) ASSERT_FALSE_RETURN;
if (!root["rooms"].isArray()) ASSERT_FALSE_RETURN;
if (!root["robots"].isArray()) ASSERT_FALSE_RETURN;
game_id_ = root["game_id"].asInt();
auto rooms = root["rooms"];
int size = rooms.size();
for (auto n = 0; n < size; ++n) {
const auto roomid = rooms[n]["roomid"].asInt();
const auto wait_time = rooms[n]["wait_time"].asInt();
const auto count_per_table = rooms[n]["count_per_table"].asInt();
room_setting_map_[roomid] = RoomSetiing{roomid, wait_time, count_per_table};
}
auto robots = root["robots"];
size = robots.size();
for (auto i = 0; i < size; ++i) {
auto robot = robots[i];
auto userid = static_cast<UserID>(robot["userid"].asInt());
const auto password = robot["password"].asString();
const auto nickname = robot["nickname"].asString();
const auto headurl = robot["headurl"].asString();
RobotSetting unit;
unit.userid = userid;
unit.password = password;
unit.nickname = nickname;
unit.headurl = headurl;
robot_setting_map_[userid] = unit;
}
// optional setting
if (root.isMember("main_interval"))
main_interval_ = root["main_interval"].asInt();
if (root.isMember("deposit_interval"))
deposit_interval_ = root["deposit_interval"].asInt();
if (root.isMember("deposit_active_id"))
deposit_active_id_ = root["deposit_active_id"].asString();
if (root.isMember("deposit_gain_amount"))
gain_amount_ = root["deposit_gain_amount"].asInt();
if (root.isMember("deposit_back_amount"))
back_amount_ = root["deposit_back_amount"].asInt();
if (root.isMember("deposit_gain_url"))
deposit_gain_url_ = root["deposit_gain_url"].asString();
if (root.isMember("deposit_back_url"))
deposit_back_url_ = root["deposit_back_url"].asString();
// check
if (InvalidGameID == game_id_) {
LOG_ERROR("game_id_ = [%d]", InvalidGameID);
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
std::string& SettingManager::GetDepositActiveID() {
return deposit_active_id_;
}
RobotSettingMap SettingManager::GetRobotSettingMap() const {
return robot_setting_map_;
}
RoomSettingMap SettingManager::GetRoomSettingMap() const {
return room_setting_map_;
}
RobotSetting SettingManager::GetRobotSetting(const UserID& userid) const {
const auto iter = robot_setting_map_.find(userid);
if (iter != robot_setting_map_.end()) {
return iter->second;
}
return RobotSetting();
}
int SettingManager::GetMainsInterval() const {
return main_interval_;
}
int SettingManager::GetDepositInterval() const {
return deposit_interval_;
}
int64_t SettingManager::GetGainAmount() const {
return gain_amount_;
}
int64_t SettingManager::GetBackAmount() const {
return back_amount_;
}
GameID SettingManager::GetGameID() const {
return game_id_;
}
std::string& SettingManager::GetDepositGainUrl() {
return deposit_gain_url_;
}
std::string& SettingManager::GetDepositBackUrl() {
return deposit_back_url_;
}
int SettingManager::GetRandomUserID(UserID& random_userid) const {
// Ëæ»úÑ¡È¡userid
int64_t random_pos = 0;
if (kCommSucc != RobotUtils::GenRandInRange(0, robot_setting_map_.size() - 1, random_pos)) {
ASSERT_FALSE_RETURN;
}
const auto random_it = std::next(std::begin(robot_setting_map_), random_pos);
random_userid = random_it->first;
return kCommSucc;
}
int SettingManager::IsRobotSettingExist(const UserID& userid, bool& exist) {
const auto iter = robot_setting_map_.find(userid);
if (iter != robot_setting_map_.end()) {
exist = true;
return kCommSucc;
}
return kCommFaild;
}
int SettingManager::SnapShotObjectStatus() const {
#ifdef _DEBUG
LOG_INFO("game_id_ [%d]", game_id_);
LOG_INFO("main_interval_ [%d]", main_interval_);
LOG_INFO("deposit_interval_ [%d]", deposit_interval_);
LOG_INFO("gain_amount_ [%I64d]", gain_amount_);
LOG_INFO("back_amount_ [%I64d]", back_amount_);
LOG_INFO("deposit_active_id_ [%s]", deposit_active_id_.c_str());
LOG_INFO("robot_setting_map_ size [%d]", robot_setting_map_.size());
std::string userid_str;
for (auto& kv : robot_setting_map_) {
const auto userid = kv.first;
userid_str += std::to_string(userid);
userid_str += ", ";
}
LOG_INFO("room_setting_map_ size [%d]", room_setting_map_.size());
for (auto& kv : room_setting_map_) {
const auto roomid = kv.first;
const auto setting = kv.second;
LOG_INFO("roomid [%d] wait_time [%d] count_per_table [%d]", roomid, setting.wait_time, setting.count_per_table);
}
#endif
return kCommSucc;
}
int SettingManager::BriefInfo() const {
#ifdef _DEBUG
LOG_INFO("robot_setting_map_ size [%d]", robot_setting_map_.size());
#endif
return kCommSucc;
}