-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_room.cpp
292 lines (237 loc) · 8.39 KB
/
base_room.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "stdafx.h"
#include "base_room.h"
#include "robot_utils.h"
BaseRoom::BaseRoom() {}
BaseRoom::BaseRoom(const int& roomid) {
if (RobotUtils::IsValidRoomID(roomid)) {
assert(roomid);
return;
}
set_room_id(roomid);
}
BaseRoom::~BaseRoom() {}
RoomOptional BaseRoom::GetRoomType() {
if (IS_BIT_SET(options_, kClassicsRoom)) {
return kClassicsRoom;
}
if (IS_BIT_SET(options_, kPrivateRoom)) {
return kPrivateRoom;
}
if (IS_BIT_SET(options_, kMatchRoom)) {
return kMatchRoom;
}
return kUnkonwRoom;
}
int BaseRoom::PlayerEnterGame(const UserPtr &user) {
CHECK_USER(user);
const auto tableno = user->get_table_no();
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. userid [%d], tableno [%d]", user->get_user_id(), tableno);
ASSERT_FALSE_RETURN;
}
if (kCommSucc != table->BindPlayer(user)) {
LOG_WARN("BindPlayer faild. userid [%d], tableno [%d]", user->get_user_id(), tableno);
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::BindPlayer(const UserPtr& user) const {
CHECK_USER(user);
//V524 It is odd that the body of 'BindPlayer' function is fully equivalent to the body of 'PlayerEnterGame' function.base_room.cpp 43
const auto tableno = user->get_table_no();
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. userid [%d], tableno [%d]", user->get_user_id(), tableno);
ASSERT_FALSE_RETURN;
}
if (kCommSucc != table->BindPlayer(user)) {
LOG_WARN("BindPlayer faild. userid [%d], tableno [%d]", user->get_user_id(), tableno);
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::UserLeaveGame(const UserID& userid, const TableNO& tableno) {
CHECK_USERID(userid);
CHECK_TABLENO(tableno);
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. userid [%d], tableno [%d]", userid, tableno);
ASSERT_FALSE_RETURN;
}
if (!IsValidTable(table->get_table_no())) {
LOG_WARN("Get table[%d] faild", tableno);
ASSERT_FALSE_RETURN;
}
if (kCommSucc != table->UnbindUser(userid)) {
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::UnbindUser(const UserID& userid, const TableNO& tableno) const {
CHECK_USERID(userid);
CHECK_TABLENO(tableno);
//V524 It is odd that the body of 'UnbindUser' function is fully equivalent to the body of 'UserLeaveGame' function.base_room.cpp 91
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. userid [%d], tableno [%d]", userid, tableno);
ASSERT_FALSE_RETURN;
}
if (!IsValidTable(table->get_table_no())) {
LOG_WARN("Get table[%d] faild", tableno);
ASSERT_FALSE_RETURN;
}
if (kCommSucc != table->UnbindUser(userid)) {
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::UserGiveUp(const UserID& userid, const TableNO& tableno) {
CHECK_USERID(userid);
CHECK_TABLENO(tableno);
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. userid [%d], tableno [%d]", userid, tableno);
ASSERT_FALSE_RETURN;
}
if (!IsValidTable(table->get_table_no())) {
LOG_WARN("Get table[%d] faild", tableno);
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::Looker2Player(const UserPtr& user) {
CHECK_USER(user);
const auto userid = user->get_user_id();
const auto tableno = user->get_table_no();
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. userid [%d] tableno [%d]", userid, tableno);
ASSERT_FALSE_RETURN;
}
if (!IsValidTable(tableno)) {
LOG_WARN("IsValidTable faild. userid [%d] tableno [%d]", userid, tableno);
ASSERT_FALSE_RETURN;
}
if (!table->IsTableUser(userid)) {
LOG_WARN("IsTableUser faild. userid [%d] tableno [%d]", userid, tableno);
ASSERT_FALSE_RETURN;
}
const auto ret = table->BindPlayer(user);
if (kCommSucc != ret) {
LOG_WARN("user[%d] BindPlayer faild. ret [%d]", user->get_user_id(), ret);
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::Player2Looker(const UserPtr& user) {
CHECK_USER(user);
const auto tableno = user->get_table_no();
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. userid [%d], tableno [%d]", user->get_user_id(), tableno);
ASSERT_FALSE_RETURN;
}
if (!IsValidTable(table->get_table_no())) {
LOG_WARN("Get table[%d] faild", user->get_table_no());
ASSERT_FALSE_RETURN;
}
if (!table->IsTablePlayer(user->get_user_id())) {
LOG_WARN("user[%d] is not in table[%d]", user->get_user_id(), user->get_table_no());
ASSERT_FALSE_RETURN;
}
if (IS_BIT_SET(table->get_table_status(), kTablePlaying)) {
(void) UserGiveUp(user->get_user_id(), user->get_table_no());
}
if (kCommSucc != table->UnbindPlayer(user->get_user_id())) {
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::SwitchTable(const UserPtr& user, const TableNO& tableno) {
const auto userid = user->get_user_id();
if (kCommSucc != UnbindUser(userid, tableno)) {
ASSERT_FALSE_RETURN;
}
if (kCommSucc != BindPlayer(user)) {
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::StartGame(const TableNO& tableno) {
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. tableno [%d]", tableno);
ASSERT_FALSE_RETURN;
}
if (!IsValidTable(table->get_table_no())) {
LOG_WARN("Get table [%d] faild", tableno);
ASSERT_FALSE_RETURN;
}
if (kCommSucc != table->StartGame()) {
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
int BaseRoom::LookerEnterGame(const UserPtr &user) {
CHECK_USER(user);
const auto target_tableno = user->get_table_no();
if (!IsValidTable(target_tableno)) {
LOG_WARN("Get table[%d] faild", target_tableno);
ASSERT_FALSE_RETURN;
}
const auto tableno = user->get_table_no();
TablePtr table;
if (kCommSucc != GetTable(tableno, table)) {
LOG_WARN("GetTable faild. userid [%d], tableno [%d]", user->get_user_id(), tableno);
ASSERT_FALSE_RETURN;
}
if (kCommSucc != table->BindLooker(user)) {
ASSERT_FALSE_RETURN;
}
return kCommSucc;
}
bool BaseRoom::IsValidTable(const TableNO& tableno) const {
if (tableno <= 0 || tableno > get_max_table_cout()) {
return false;
}
return true;
}
bool BaseRoom::IsValidDeposit(const INT64& deposit) const {
if (deposit >= get_min_deposit() && deposit < get_max_deposit()) {
return true;
}
return false;
}
int BaseRoom::AddTable(const TableNO& tableno, const TablePtr& table) {
CHECK_TABLENO(tableno);
CHECK_TABLE(table);
tables_[tableno - 1] = table;
return kCommSucc;
}
int BaseRoom::GetTable(const TableNO& tableno, TablePtr& table) const {
CHECK_TABLENO(tableno);
if (!IsValidTable(tableno)) {
LOG_WARN("GetTable faild. valid tableno[%d]", tableno);
ASSERT_FALSE_RETURN;
}
// 桌号从1开始,下标从0开始
table = tables_.at(tableno - 1);
return kCommSucc;
}
int BaseRoom::SnapShotObjectStatus() {
#ifdef _DEBUG
LOG_INFO("roomid [%d] options [%d] configs [%d] min_playercount_per_table [%d] chaircount_per_table [%d] manages [%d] max_table_cout [%d] min_deposit [%I64d] max_deposit [%I64d]",
room_id_, options_, configs_, min_playercount_per_table_, chaircount_per_table_, manages_, max_table_cout_, min_deposit_, max_deposit_);
LOG_INFO("tables_ size [%d]", max_table_cout_);
/*for (auto i = 0; i < max_table_cout_; i = i + 200) {
if (tables_[i])
tables_[i]->SnapShotObjectStatus();
}*/
/*if (tables_[0])
tables_[0]->SnapShotObjectStatus();
if (tables_[1])
tables_[1]->SnapShotObjectStatus();*/
#endif
return kCommSucc;
}