btl.cpp
12.7 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include "btl.h"
#include "user.h"
#include "gateway.h"
#include "btl_rank.h"
btl_mgr_t::btl_mgr_t() : el::lib_timer_base_t(g_timer)
{
this->user = NULL;
this->is_btl = false;
this->time_out_handle = NULL;
this->shoot = 0;
}
void btl_mgr_t::begin()
{
this->peer_pet_lv_vec.clear();
this->pet_lv_vec.clear();
this->win_vec.clear();
this->is_btl = true;
{
auto event_data = this->user->event_mgr.find_data(common_msg::DAILY_EVENT_PET_BTL_CNT);
this->user->event_mgr.update_event(user->uid(), common_msg::DAILY_EVENT_PET_BTL_CNT, 0, event_data+1, el::lib_time_t::tomorrow_start_time()-1);
}
{
auto events = this->user->event_mgr.find_events(common_msg::FOREVER_EVENT_PET_BTL);
FOREACH(events, it){
event_t& e = it->second;
PET_ID pet_id = (PET_ID)e.id;
share_msg::pet_t* pet = user->pet_mgr.find(pet_id);
PET_LV pet_lv = g_pet_cfg_mgr->gen_level(pet_id, pet->exp());
table_pet_t table_pet;
table_pet.pet_id = pet_id;
table_pet.pet_lv = pet_lv;
this->pet_lv_vec.push_back(table_pet);
}
}
{
std::vector<PET_ID> peer_pet_id_vec;
{
std::vector<PET_ID> pet_id_vec;
FOREACH(g_pet_cfg_mgr->pet_cfg_map, it){
pet_id_vec.push_back(it->first);
}
std::random_shuffle(pet_id_vec.begin(), pet_id_vec.end());
std::copy(pet_id_vec.begin(), pet_id_vec.begin()+3, peer_pet_id_vec.begin());
}
static int s_lv_min = 2;
static int s_lv_max = 2;
int peer_pet_idx = 0;
FOREACH(this->pet_lv_vec, it){
PET_ID pet_id = it->pet_id;
int pet_lv = it->pet_lv;
int lv_min = pet_lv - s_lv_min <= 0 ? 1 : pet_lv - s_lv_min;
int lv_max = g_pet_cfg_mgr->max_level() < (uint32_t)pet_lv + s_lv_max ? g_pet_cfg_mgr->max_level() : pet_lv + s_lv_max;
int new_lv = el::lib_random_t::random(lv_min, lv_max);
table_pet_t table_pet;
table_pet.pet_id = peer_pet_id_vec[peer_pet_idx];
table_pet.pet_lv = new_lv;
this->peer_pet_lv_vec.push_back(table_pet);
peer_pet_idx++;
}
}
//0:peer 1:user
this->shoot = el::lib_random_t::random(0,1);
if (0 == this->shoot){
this->time_out_handle = this->add_sec_event(btl_mgr_t::time_out, this, el_async::get_now_sec() + 5);
} else {
this->time_out_handle = this->add_sec_event(btl_mgr_t::time_out, this, el_async::get_now_sec() + 15);
}
{// 通知战前信息 [2018/9/26 Administrator]
gateway_msg::notify_btl_begin_msg_res out;
out.set_shoot(shoot);
FOREACH(this->peer_pet_lv_vec, it){
share_msg::pet_btl_t* peer_pet = out.add_peer_pet();
peer_pet->set_id(it->pet_id);
peer_pet->set_lv(it->pet_lv);
}
user->send_msg_res(&out);
}
std::random_shuffle(this->peer_pet_lv_vec.begin(), this->peer_pet_lv_vec.end());
std::random_shuffle(this->pet_lv_vec.begin(), this->pet_lv_vec.end());
}
void btl_mgr_t::end( bool win, uint32_t add_exp )
{
if (NULL != this->time_out_handle){
this->remove_sec_timer(this->time_out_handle);
}
this->peer_pet_lv_vec.clear();
this->is_btl = false;
{
auto btl_cnt = this->user->event_mgr.find_data(common_msg::DAILY_EVENT_PET_BTL_CNT);
uint32_t cd_idx = 0;
if (0 < btl_cnt){
cd_idx = btl_cnt - 1;
}
if (g_pet_cfg_mgr->cd_vec.size() <= cd_idx){
cd_idx = g_pet_cfg_mgr->cd_vec.size()-1;
}
uint32_t next_btl_time = g_pet_cfg_mgr->cd_vec[cd_idx] + el_async::get_now_sec();
this->user->event_mgr.update_event(user->uid(), common_msg::DAILY_EVENT_PET_BTL_TIME, 0, next_btl_time, el::lib_time_t::tomorrow_start_time()-1);
}
uint32_t win_cnt_all = 0;
if (win){
auto win_cnt = this->user->event_mgr.find_data(common_msg::FOREVER_EVENT_PET_BTL_WIN_CNT);
win_cnt_all = win_cnt + 1;
this->user->event_mgr.update_event(user->uid(), common_msg::FOREVER_EVENT_PET_BTL_WIN_CNT, 0, win_cnt_all, 0);
}
uint32_t exp_all = 0;
if (0 < add_exp){
auto exp = this->user->event_mgr.find_data(common_msg::FOREVER_EVENT_PET_BTL_EXP);
exp_all = exp + add_exp;
this->user->event_mgr.update_event(user->uid(), common_msg::FOREVER_EVENT_PET_BTL_EXP, 0, exp_all, 0);
{
uint32_t type = share_msg::E_BTL_RANK_TYPE_COMMON;
auto it = g_btl_rank_mgr->rank_map.find(type);
if (g_btl_rank_mgr->rank_map.end() != it){
std::set<btl_rank_user_t>& rank_user_set = it->second;
if (rank_user_set.size() < share_msg::E_BTL_RANK_MAX_CNT){
auto prank_user = g_btl_rank_mgr->find(type, this->user->uid());
share_msg::btl_rank_user_t bru;
if (NULL == prank_user){
btl_rank_user_t rank_user;
g_btl_rank_mgr->pack(rank_user, type, win_cnt_all, exp_all, this->user);
rank_user_set.insert(rank_user);
bru.CopyFrom(rank_user);
} else {
prank_user->set_exp(exp_all);
prank_user->set_win_cnt(win_cnt_all);
prank_user->set_tim(el_async::get_now_sec());
bru.CopyFrom(*prank_user);
}
g_btl_rank_mgr->db_update_rank(bru);
} else {
auto rank_user = g_btl_rank_mgr->find(type, this->user->uid());
share_msg::btl_rank_user_t bru;
if (NULL == rank_user){// 没找到
auto rank_user_last = g_btl_rank_mgr->find_last(type);
if (exp_all <= rank_user_last->exp()){
return;
}
g_btl_rank_mgr->db_delete_user_rank(type, rank_user_last->user_show().uid());
rank_user_set.erase(*rank_user_last);
btl_rank_user_t rank_user;
g_btl_rank_mgr->pack(rank_user, type, win_cnt_all, exp_all, this->user);
rank_user_set.insert(rank_user);
bru.CopyFrom(rank_user);
} else {// 找到了
rank_user->set_exp(exp_all);
rank_user->set_win_cnt(win_cnt_all);
rank_user->set_tim(el_async::get_now_sec());
bru.CopyFrom(*rank_user);
}
g_btl_rank_mgr->db_update_rank(bru);
}
} else {
std::set<btl_rank_user_t> rank_user_set;
btl_rank_user_t rank_user;
g_btl_rank_mgr->pack(rank_user, type, win_cnt_all, exp_all, this->user);
rank_user_set.insert(rank_user);
g_btl_rank_mgr->rank_map[type] = rank_user_set;
share_msg::btl_rank_user_t bru;
bru.CopyFrom(rank_user);
g_btl_rank_mgr->db_update_rank(bru);
}
}
}
}
int btl_mgr_t::time_out( void* owen, void* data )
{
btl_mgr_t* btl_mgr = (btl_mgr_t*)data;
btl_mgr->time_out_handle = NULL;
table_pet_t table_pet;
table_pet.shoot = btl_mgr->shoot;
if (0 == btl_mgr->shoot){
//电脑出牌 [2018/9/27 Administrator]
table_pet_t& tp = btl_mgr->peer_pet_lv_vec[btl_mgr->peer_pet_lv_vec.size()-1];
table_pet.pet_id = tp.pet_id;
table_pet.pet_lv = tp.pet_lv;
btl_mgr->peer_pet_lv_vec.pop_back();
btl_mgr->shoot = 1;
{
gateway_msg::btl_shoot_msg_res out;
out.set_shoot(0);
out.set_pet_id(table_pet.pet_id);
btl_mgr->user->send_msg_res(&out);
}
} else {
//用户出牌 [2018/9/27 Administrator]
table_pet_t& tp = btl_mgr->pet_lv_vec[btl_mgr->pet_lv_vec.size()-1];
table_pet.pet_id = tp.pet_id;
table_pet.pet_lv = tp.pet_lv;
btl_mgr->pet_lv_vec.pop_back();
btl_mgr->shoot = 0;
{
gateway_msg::btl_shoot_msg_res out;
out.set_shoot(1);
out.set_pet_id(table_pet.pet_id);
btl_mgr->user->send_msg_res(&out);
}
}
btl_mgr->table_pet_vec.push_back(table_pet);
btl_mgr->cal();
return 0;
}
btl_mgr_t::~btl_mgr_t()
{
if (NULL != this->time_out_handle){
this->remove_sec_timer(this->time_out_handle);
}
}
void btl_mgr_t::cal()
{
if (0 == this->table_pet_vec.size()%2){
uint32_t win = 0;
table_pet_t& tp1 = this->table_pet_vec[this->table_pet_vec.size()-1];
table_pet_t& tp2 = this->table_pet_vec[this->table_pet_vec.size()-2];
if (0 == tp1.shoot){
win = this->cal_ret(tp1, tp2);
} else {
win = this->cal_ret(tp2, tp1);
}
this->win_vec.push_back(win);
{
gateway_msg::notify_btl_ret_msg_res out;
out.set_win(win);
this->user->send_msg_res(&out);
}
}
if (this->peer_pet_lv_vec.empty() && this->pet_lv_vec.empty()){
uint32_t user_win_cnt = 0;
uint32_t user_lost_cnt = 0;
{
FOREACH(this->win_vec, it){
if (0 == *it){
user_lost_cnt++;
} else if (1 == *it){
user_win_cnt++;
}
}
}
uint32_t win = 0;
uint32_t score = 0;
if (user_win_cnt == user_lost_cnt){
win = 2;
score = 1;
} else if (user_lost_cnt < user_win_cnt){
win = 1;
score = 2;
}
if (3 == user_win_cnt){
win = 1;
score = 3;
}
{
gateway_msg::notify_btl_end_ret_msg_res out;
out.set_win(win);
out.set_score(score);
this->user->send_msg_res(&out);
}
this->end(user_lost_cnt < user_win_cnt, score);
}
}
uint32_t btl_mgr_t::cal_ret( table_pet_t tp_ai, table_pet_t tp_user )
{
pet_lv_data_t* pld_ai = NULL;
{
pet_cfg_t* pet_cfg = g_pet_cfg_mgr->find(tp_ai.pet_id);
auto it = pet_cfg->pet_lv_data_map.find(tp_ai.pet_lv);
pld_ai = &it->second;
}
pet_lv_data_t* pld_user = NULL;
{
pet_cfg_t* pet_cfg = g_pet_cfg_mgr->find(tp_user.pet_id);
auto it = pet_cfg->pet_lv_data_map.find(tp_user.pet_lv);
pld_user = &it->second;
}
bool die_ai = false;
bool die_user = false;
if (pld_user->def <= pld_ai->ack){
die_user = true;
}
if (pld_ai->def <= pld_user->ack){
die_ai = true;
}
if (die_ai == die_user){
return 2;
}
if (die_ai){
return 1;
}
return 0;
}
int gateway_t::on_btl_msg(el::lib_tcp_peer_info_t* peer_fd_info,
google::protobuf::Message* msg,
user_t* user)
{
if (user->btl_mgr.is_btl){
return 0;
}
std::map<EVENT_ID, event_t>& events = user->event_mgr.find_events(common_msg::FOREVER_EVENT_PET_BTL);
if (3 != events.size()){
return 0;
}
auto event_data = user->event_mgr.find_data(common_msg::DAILY_EVENT_PET_BTL_CNT);
if (g_pet_cfg_mgr->btl_cnt <= event_data){
return 0;
}
{
auto t = user->event_mgr.find_data(common_msg::DAILY_EVENT_PET_BTL_TIME);
if (el_async::get_now_sec() < t){
return 0;
}
}
user->btl_mgr.begin();
{
gateway_msg::btl_msg_res out;
user->send_msg_res(&out);
}
return 0;
}
int gateway_t::on_btl_shoot_msg(el::lib_tcp_peer_info_t* peer_fd_info,
google::protobuf::Message* msg,
user_t* user)
{
if (!user->btl_mgr.is_btl){
return 0;
}
if (1 != user->btl_mgr.shoot){
return 0;
}
if (user->btl_mgr.pet_lv_vec.empty()){
return 0;
}
auto in = (gateway_msg::btl_shoot_msg*)msg;
{
table_pet_t* tp = NULL;
{
FOREACH(user->btl_mgr.pet_lv_vec, it){
if ((*it).pet_id == in->pet_id()){
tp = &*it;
break;
}
}
if (NULL == tp){
return 0;
}
}
table_pet_t table_pet;
table_pet.shoot = user->btl_mgr.shoot;
table_pet.pet_id = tp->pet_id;
table_pet.pet_lv = tp->pet_lv;
user->btl_mgr.table_pet_vec.push_back(table_pet);
{
FOREACH(user->btl_mgr.pet_lv_vec, it){
if ((*it).pet_id == in->pet_id()){
user->btl_mgr.peer_pet_lv_vec.erase(it);
break;
}
}
}
}
if (NULL != user->btl_mgr.time_out_handle){
this->remove_sec_timer(user->btl_mgr.time_out_handle);
}
user->btl_mgr.shoot = 0;
user->btl_mgr.time_out_handle = this->add_sec_event(btl_mgr_t::time_out, &user->btl_mgr, el_async::get_now_sec() + 5);
{
gateway_msg::btl_shoot_msg_res out;
out.set_shoot(1);
out.set_pet_id(in->pet_id());
user->send_msg_res(&out);
}
user->btl_mgr.cal();
return 0;
}
int gateway_t::on_get_duanwei_lv_reward_msg(el::lib_tcp_peer_info_t* peer_fd_info,
google::protobuf::Message* msg,
user_t* user)
{
auto in = (gateway_msg::get_duanwei_lv_reward_msg*)msg;
{
auto e = user->event_mgr.find(common_msg::FOREVER_EVENT_DUANWEI_LV_REWARD, in->lv());
if (NULL != e){
return 0;
}
auto duanwei_lv = g_pet_cfg_mgr->get_duanwei_level(user->event_mgr.find_data(common_msg::FOREVER_EVENT_PET_BTL_EXP));
if (duanwei_lv < in->lv()){
return 0;
}
}
{
auto it = g_pet_cfg_mgr->duanwei_lv_reward_map.find(in->lv());
std::vector<share_msg::item_t>& items = it->second;
FOREACH(items, it_item){
share_msg::item_t& item = *it_item;
user->item_mgr.add(item.id(), item.cnt());
}
}
{
gateway_msg::get_duanwei_lv_reward_msg_res out;
user->send_msg_res(&out);
}
return 0;
}
int gateway_t::on_get_btl_rank_reward_msg(el::lib_tcp_peer_info_t* peer_fd_info,
google::protobuf::Message* msg,
user_t* user)
{
auto in = (gateway_msg::get_btl_rank_reward_msg*)msg;
{
auto e = user->event_mgr.find(common_msg::DAILY_EVENT_PET_BTL_RANK_REWARD);
if (NULL != e){
return 0;
}
auto btl_rank_user = g_btl_rank_mgr->find_yesterday(share_msg::E_BTL_RANK_TYPE_COMMON, user->uid());
if (NULL == btl_rank_user){
return 0;
}
auto it = g_pet_cfg_mgr->no_reward_map.find(btl_rank_user->no);
if (g_pet_cfg_mgr->no_reward_map.end() == it){
return 0;
}
{
// 给奖励 [2018/10/9 Administrator]
std::vector<share_msg::item_t>& items = it->second;
FOREACH(items, it_item){
share_msg::item_t& item = *it_item;
user->item_mgr.add(item.id(), item.cnt());
}
}
}
user->event_mgr.update_event(user->uid(), common_msg::DAILY_EVENT_PET_BTL_RANK_REWARD, 0, 0, el::lib_time_t::tomorrow_start_time() - 1);
{
gateway_msg::get_btl_rank_reward_msg_res out;
user->send_msg_res(&out);
}
return 0;
}