Repository navigation
Expand file tree
/
Copy pathshop.cpp
More file actions
717 lines (626 loc) · 20.4 KB
/
Copy pathshop.cpp
File metadata and controls
717 lines (626 loc) · 20.4 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
* =====================================================================================
*
* Filename: shop.cpp
*
* Description:
*
* Version: 1.0
* Created: 2012年02月15日 20时40分48秒
* Revision: none
* Compiler: gcc
*
* Author: luis, luis@taomee.com
* Company: TaoMee.Inc, ShangHai
*
* =====================================================================================
*/
#include <stdio.h>
#include <arpa/inet.h>
#include <map>
#include <string>
#include <functional>
#include <string.h>
#include <errno.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include "shop.h"
#include "utility.h"
using namespace std;
c_shop g_shop;
const int c_shop::m_max_flash_time;
const int c_shop::m_flash_interval;
const int c_shop::shop_rarity_num;
c_shop::c_shop(): m_inited(0), m_send_buffer_len(0), m_p_send_header(NULL), m_errno(0), m_userid(0), m_msg_type(0)
{
}
c_shop::~c_shop()
{
uninit();
}
int c_shop::init()
{
if (parse_xml_item() != 0)
{
ERROR_LOG("parse xml item");
return -1;
}
//解析商店的物品列表
if (parse_xml_shop() != 0)
{
ERROR_LOG("parse xml shop");
return -1;
}
//解析商店各种稀有度物品的数量
if (parse_xml_shop_num() != 0)
{
ERROR_LOG("parse xml shop num");
return -1;
}
//先初始化所有商店物品
for (map<uint32_t, int>::iterator iter = m_flash_time_map.begin(); iter != m_flash_time_map.end(); ++iter)
{
vector<uint32_t> time_vec;
for (int i = 1; i <= 100; ++i)
{
update_shop(i, iter->first);
time_vec.push_back(0);
}
m_shop_update_time_map.insert(pair<uint32_t, vector<uint32_t> >(iter->first, time_vec));
}
m_p_send_header = (svr_msg_header_t*)m_send_buffer;
m_inited = 1;
return 0;
}
int c_shop::uninit()
{
if(!m_inited)
{
return -1;
}
m_inited = 0;
return 0;
}
int c_shop::parse_xml_shop_num()
{
const char *shop_num_file = config_get_strval("shop_num_file");
if (NULL == shop_num_file)
{
ERROR_LOG("xml load shop num file failed.");
return -1;
}
xmlDocPtr doc = xmlParseFile(shop_num_file);
if (NULL == doc)
{
ERROR_LOG("parse xml file failed(%s).", strerror(errno));
return -1;
}
xmlNodePtr cur_node = xmlDocGetRootElement(doc);
if (cur_node == NULL)
{
ERROR_LOG("Get root node of xml file failed(%s).", strerror(errno));
xmlFreeDoc(doc);
return -1;
}
if (xmlStrcmp(cur_node->name, BAD_CAST "num"))
{
ERROR_LOG("Get global_config failed(%s).", strerror(errno));
xmlFreeDoc(doc);
return -1;
}
cur_node = cur_node->xmlChildrenNode;
cur_node = cur_node->next;
xmlNodePtr child_node;
while(cur_node != NULL)
{
if (!xmlStrcmp(cur_node->name, BAD_CAST "cat"))
{
xmlChar *attr = NULL;
shop_key_t key = {0};
if (xmlHasProp(cur_node, (const xmlChar *)"id"))
{
attr = xmlGetProp(cur_node, (const xmlChar *)"id");
key.id = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop num failed, no id.");
xmlFreeDoc(doc);
return -1;
}
child_node = cur_node->xmlChildrenNode;
child_node = child_node->next;
while (child_node != NULL)
{
if (xmlHasProp(child_node, (const xmlChar *)"id"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"id");
key.type = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop num failed, no id.");
xmlFreeDoc(doc);
return -1;
}
shop_num_t num = {0};
if (xmlHasProp(child_node, (const xmlChar *)"cur_min"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"cur_min");
num.cur_level_min = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop num failed, no cur_min.");
xmlFreeDoc(doc);
return -1;
}
if (xmlHasProp(child_node, (const xmlChar *)"cur_max"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"cur_max");
num.cur_level_max = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop num failed, no cur_max.");
xmlFreeDoc(doc);
return -1;
}
if (xmlHasProp(child_node, (const xmlChar *)"next_min"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"next_min");
num.next_level_min = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop num failed, no next_min.");
xmlFreeDoc(doc);
return -1;
}
if (xmlHasProp(child_node, (const xmlChar *)"next_max"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"next_max");
num.next_level_max = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop num failed, no next_max.");
xmlFreeDoc(doc);
return -1;
}
m_shop_num_map.insert(pair<shop_key_t, shop_num_t>(key, num));
child_node = child_node->next->next;
}
}
cur_node = cur_node->next->next;
}
xmlFreeDoc(doc);
return 0;
}
int c_shop::parse_xml_shop()
{
const char *shop_file = config_get_strval("shop_file");
if (NULL == shop_file)
{
ERROR_LOG("xml load shop file failed.");
return -1;
}
xmlDocPtr doc = xmlParseFile(shop_file);
if (NULL == doc)
{
ERROR_LOG("parse xml file failed(%s).", strerror(errno));
return -1;
}
xmlNodePtr cur_node = xmlDocGetRootElement(doc);
if (cur_node == NULL)
{
ERROR_LOG("Get root node of xml file failed(%s).", strerror(errno));
xmlFreeDoc(doc);
return -1;
}
if (xmlStrcmp(cur_node->name, BAD_CAST "Shops"))
{
ERROR_LOG("Get global_config failed(%s).", strerror(errno));
xmlFreeDoc(doc);
return -1;
}
cur_node = cur_node->xmlChildrenNode;
cur_node = cur_node->next;
xmlNodePtr child_node;
while(cur_node != NULL)
{
if (!xmlStrcmp(cur_node->name, BAD_CAST "Shop"))
{
xmlChar *attr = NULL;
shop_key_t key = {0};
if (xmlHasProp(cur_node, (const xmlChar *)"ID"))
{
attr = xmlGetProp(cur_node, (const xmlChar *)"ID");
key.id = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop failed, no ID.");
xmlFreeDoc(doc);
return -1;
}
if (xmlHasProp(cur_node, (const xmlChar *)"Type"))
{
attr = xmlGetProp(cur_node, (const xmlChar *)"Type");
key.type = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop failed, no Type.");
xmlFreeDoc(doc);
return -1;
}
int flash_time = 0;
if (xmlHasProp(cur_node, (const xmlChar *)"flash_time"))
{
attr = xmlGetProp(cur_node, (const xmlChar *)"flash_time");
flash_time = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop failed, no flash_time.");
xmlFreeDoc(doc);
return -1;
}
m_shop_type_map.insert(pair<uint32_t, int>(key.id, key.type));
m_flash_time_map.insert(pair<uint32_t, int>(key.id, flash_time));
vector<uint32_t> item_vec;
child_node = cur_node->xmlChildrenNode;
child_node = child_node->next;
while (child_node != NULL)
{
if (!xmlStrcmp(child_node->name, BAD_CAST "Item"))
{
uint32_t item_id = 0;
if (xmlHasProp(child_node, (const xmlChar *)"ID"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"ID");
item_id = atoi((char *)attr);
}
else
{
ERROR_LOG("parse shop failed, no item:id.");
xmlFreeDoc(doc);
return -1;
}
item_vec.push_back(item_id);
}
child_node = child_node->next->next;
}
m_shop_item_map.insert(pair<uint32_t, vector<uint32_t> >(key.id, item_vec));
}
cur_node = cur_node->next->next;
}
xmlFreeDoc(doc);
return 0;
}
void c_shop::create_shop()
{
time_t timep;
time(&timep);
struct tm *p=localtime(&timep); /*取得当地时间*/
if (!p)
{
ERROR_LOG("localtime failed");
return;
}
int seconds = (p->tm_min * 60 + p->tm_sec) % m_flash_interval;
for (map<uint32_t, int>::iterator iter = m_flash_time_map.begin(); iter != m_flash_time_map.end(); ++iter)
{
if (seconds < iter->second || seconds > iter->second + m_max_flash_time)
{
continue;
}
//有商店需要刷新
int level = (seconds - iter->second) / (m_max_flash_time / 100) + 1;
map<uint32_t, std::vector<uint32_t> >::iterator time_iter = m_shop_update_time_map.find(iter->first);
vector<uint32_t> &time_vec = time_iter->second;
int last_time = time_vec[level - 1];
if (timep - last_time >= m_flash_interval)
{
update_shop(level, iter->first);
vector<uint32_t> &time_vec = time_iter->second;
time_vec[level - 1] = timep;
}
}
}
void c_shop::update_shop(int mon_level, uint32_t shop_id)
{
INFO_LOG("update shop:%u, level:%u", shop_id, mon_level);
shop_key_t shop = {shop_id, mon_level};
m_cur_shop_vec_map.erase(shop);
map<uint32_t, int>::iterator shop_type_iter = m_shop_type_map.find(shop_id);
if (shop_type_iter == m_shop_type_map.end())
{
ERROR_LOG("cann't find shop:%u type", shop_id);
return;
}
int shop_type = shop_type_iter->second;
//获得每个稀有度的物品的数量
int num[shop_rarity_num] = {0};
int high_level_num[shop_rarity_num] = {0};
for (int i = 0; i != shop_rarity_num; ++i)
{
shop_key_t key = {shop_type, i};
map<shop_key_t, shop_num_t, c_shop_cmp>::iterator num_iter = m_shop_num_map.find(key);
if (num_iter == m_shop_num_map.end())
{
ERROR_LOG("can't find shop num in g_shop_num_map, type:%u, rarity:%u", key.id, key.type);
return;
}
const shop_num_t &shop_num = num_iter->second;
if (shop_num.cur_level_min == shop_num.cur_level_max)
{
num[i] = shop_num.cur_level_min;
}
else
{
num[i] = uniform_rand(shop_num.cur_level_min, shop_num.cur_level_max);
}
if (shop_num.next_level_min == shop_num.next_level_max)
{
high_level_num[i] = shop_num.next_level_min;
}
else
{
high_level_num[i] = uniform_rand(shop_num.next_level_min, shop_num.next_level_max);
}
}
num[0] += mon_level;
//随机更新商店物品的位置
map<uint32_t, vector<uint32_t> >::iterator shop_iter = m_shop_item_map.find(shop_id);
if (shop_iter == m_shop_item_map.end())
{
ERROR_LOG("cann't find shop:%u items", shop_id);
return;
}
vector<uint32_t> &shop_vec = shop_iter->second;
int item_num = shop_vec.size();
for (int i = item_num - 1; i != 0; --i)
{
int index = uniform_rand(0, i);
uint32_t temp = shop_vec[index];
shop_vec[index] = shop_vec[i];
shop_vec[i] = temp;
}
int count[shop_rarity_num] = {0};
int high_level_count[shop_rarity_num] = {0};
vector<uint32_t> item_vector;
for (vector<uint32_t>::iterator item_iter = shop_vec.begin(); item_iter != shop_vec.end(); ++item_iter)
{
uint32_t item_id = *item_iter;
map<uint32_t, item_t>::iterator item_info_iter = m_item_map.find(item_id);
if (item_info_iter == m_item_map.end())
{
ERROR_LOG("item:%u is not in g_item_map.", item_id);
return;
}
int rarity = item_info_iter->second.rarity;
if (item_info_iter->second.level <= mon_level)
{
if (count[rarity] < num[rarity])
{
item_vector.push_back(item_id);
++count[rarity];
}
}
else if (item_info_iter->second.level <= mon_level + 3)
{
if (high_level_count[rarity] < high_level_num[rarity])
{
item_vector.push_back(item_id);
++high_level_count[rarity];
}
}
else
{
//高三级以上的物品,不显示
}
}
INFO_LOG("create %u items", item_vector.size());
m_cur_shop_vec_map.insert(pair<shop_key_t, vector<uint32_t> >(shop, item_vector));
}
int c_shop::parse_xml_item()
{
const char *items_file = config_get_strval("items_file");
if (NULL == items_file)
{
ERROR_LOG("xml load items file failed.");
return -1;
}
xmlDocPtr doc = xmlParseFile(items_file);
if (NULL == doc)
{
ERROR_LOG("parse xml file failed(%s).", strerror(errno));
return -1;
}
xmlNodePtr cur_node = xmlDocGetRootElement(doc);
if (cur_node == NULL)
{
ERROR_LOG("Get root node of xml file failed(%s).", strerror(errno));
xmlFreeDoc(doc);
return -1;
}
if (xmlStrcmp(cur_node->name, BAD_CAST "items"))
{
ERROR_LOG("Get global_config failed(%s).", strerror(errno));
xmlFreeDoc(doc);
return -1;
}
cur_node = cur_node->xmlChildrenNode;
cur_node = cur_node->next;
xmlNodePtr child_node;
while(cur_node != NULL)
{
if (!xmlStrcmp(cur_node->name, BAD_CAST "Cat"))
{
xmlChar *attr = NULL;
uint32_t id = 0;
if (xmlHasProp(cur_node, (const xmlChar *)"ID"))
{
attr = xmlGetProp(cur_node, (const xmlChar *)"ID");
id = atoi((char *)attr);
}
else
{
ERROR_LOG("parse item failed, no category:id.");
xmlFreeDoc(doc);
return -1;
}
DEBUG_LOG("cat id:%u", id);
child_node = cur_node->xmlChildrenNode;
child_node = child_node->next;
while (child_node != NULL)
{
if (xmlStrcmp(child_node->name, BAD_CAST "Item"))
{
child_node = child_node->next->next;
continue;
}
uint32_t item_id = 0;
if (xmlHasProp(child_node, (const xmlChar *)"ID"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"ID");
item_id = atoi((char *)attr);
}
else
{
ERROR_LOG("parse item failed, no item:id.");
xmlFreeDoc(doc);
return -1;
}
map<uint32_t, item_t>::iterator iter = m_item_map.find(item_id);
if (iter != m_item_map.end())
{
ERROR_LOG("item id:%u is exist", item_id);
return -1;
}
item_t item = {0};
if (xmlHasProp(child_node, (const xmlChar *)"Price"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"Price");
item.price = atoi((char *)attr);
}
else
{
item.price = 0;
}
if (xmlHasProp(child_node, (const xmlChar *)"level"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"level");
item.level = atoi((char *)attr);
}
else
{
item.level = 0;
}
if (xmlHasProp(child_node, (const xmlChar *)"happy"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"happy");
item.happy = atoi((char *)attr);
}
else
{
item.happy = 0;
}
if (xmlHasProp(child_node, (const xmlChar *)"health"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"health");
item.health = atoi((char *)attr);
}
else
{
item.health = 0;
}
if (xmlHasProp(child_node, (const xmlChar *)"rarity"))
{
attr = xmlGetProp(child_node, (const xmlChar *)"rarity");
item.rarity = atoi((char *)attr);
}
else
{
item.rarity = 0;
}
if (item.rarity >= shop_rarity_num)
{
ERROR_LOG("item:%u rarity too large", item_id);
return -1;
}
item.category = id;
m_item_map.insert(pair<uint32_t, item_t>(item_id, item));
child_node = child_node->next->next;
}
}
cur_node = cur_node->next->next;
}
xmlFreeDoc(doc);
return 0;
}
int c_shop::get_shop_item(svr_msg_header_t *p_recv_header, int body_len, fdsession_t *fdsess)
{
m_errno = ERR_NO_ERR;
m_userid = p_recv_header->user_id;
m_msg_type = p_recv_header->msg_type;
m_send_buffer_len = sizeof(svr_msg_header_t);
shop_key_t *p_shop = (shop_key_t *)p_recv_header->body;
KINFO_LOG(p_recv_header->user_id, "get shop:%u, level:%u", p_shop->id, p_shop->type);
uint16_t remain_time = m_max_flash_time;
map<uint32_t, int>::iterator flash_iter = m_flash_time_map.find(p_shop->id); //保存每个店的刷新时间
if (flash_iter != m_flash_time_map.end())
{
remain_time = time(NULL) - flash_iter->second;
}
pack(&remain_time, sizeof(remain_time));
uint16_t count = 0;
map<shop_key_t, vector<uint32_t> >::iterator iter = m_cur_shop_vec_map.find(*p_shop);
if (iter == m_cur_shop_vec_map.end())
{
pack(&count, sizeof(count));
}
else
{
vector<uint32_t> &item_vec = iter->second;
count = item_vec.size();
pack(&count, sizeof(count));
KINFO_LOG(p_recv_header->user_id, "get %u items", count);
for (vector<uint32_t>::iterator item_iter = item_vec.begin(); item_iter != item_vec.end(); ++item_iter)
{
uint32_t item_id = *item_iter;
pack(&item_id, sizeof(item_id));
// KINFO_LOG(p_recv_header->user_id, "get item:%u", item_id);
}
}
return send_to_client(fdsess, p_recv_header->seq_id, m_msg_type);
}
int c_shop::pack(const void *val, uint32_t val_len)
{
if(m_send_buffer_len + val_len > MAX_SEND_PKG_SIZE)
{
return ERR_SNDBUF_OVERFLOW;
}
memcpy(m_send_buffer + m_send_buffer_len, val, val_len);
m_send_buffer_len += val_len;
return 0;
}
int c_shop::send_to_client(fdsession_t *fdsess, uint32_t seq_id, uint16_t msg_type)
{
if(m_errno != ERR_NO_ERR)
{
m_send_buffer_len = sizeof(svr_msg_header_t);
}
m_p_send_header->len = m_send_buffer_len;
m_p_send_header->seq_id = seq_id;
m_p_send_header->msg_type = msg_type;
m_p_send_header->result = m_errno;
m_p_send_header->user_id = m_userid;
if(0 != send_pkg_to_client(fdsess, m_send_buffer, m_send_buffer_len))
{
ERROR_LOG("send msg_type:%u to user:%u client failed.", msg_type, m_userid);
}
return 0;
}