hxp
2019-03-05 79a23de6692781dfe4b18fdb57bc4b3e69f34014
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
#!/usr/bin/python
# -*- coding: GBK -*-
#
#
##@package PlayerCompensation.py
#
# @todo:Óʼþ¹ÜÀí£¨²¹³¥£©
#        1. È«·þÓʼþÒ»¸öGUIDÒ»·âÓʼþ£¬ÓÐʱ¼äÏÞÖÆ£¬½ÓÊÕÓʼþµÄ³ÖÐøÊ±¼äºÍÑÓ³Ùɾ³ýʱ¼äµã(¿É½ÓÊÕºóµÄXÌìɾ³ýÓʼþ)
#           ±íʾÈç¹ûÈ«·þÓʼþÒѾ­Í¨Öª¹ýij¸öÍæ¼Ò£¬ÄÇô¶ÔÓÚ¸ÃÍæ¼Ò£¬¼´Ê¹Óʼþ¹ýÆÚÍæ¼ÒûÓÐÖ÷¶¯É¾³ýÓʼþ£¬Ò²Ó¦¸Ã֪ͨ¸ÃÍæ¼Ò
#        2. ¸öÈËÓʼþÒ»¸öGUID¿ÉÒÔÊǸöÈË»òÕß¶àÈËÓʼþ£¬µ«ÊǶàÈËÓʼþ²»Ö§³ÖÅäÖÿÉÁìÈ¡µÄ³ÖÐøÊ±¼ä£¬ÈôÒªÖ§³ÖÐèÐÞ¸ÄÁìÈ¡¼Ç¼Âß¼­
#        3. ¸öÈËÓʼþ°´ÀàÐÍ·Ö²»Í¬µÄ¿É±£´æÊýÁ¿£¬Íæ¼Ò²»¸ÐÖª
#        4. ¹ØÓÚÓʼþ·¢¼þÈË ±êÌ⠺͠ÕýÎÄ£¬Èç¹ûÊÇÄ£°å»òÕß×éºÏÐÅÏ¢À࣬¶¼Ó¦¸ÃÓÉǰ¶Ë×éÖ¯Íê³É£¬ºó¶Ëֻȡģ°å±êʶ
#        5. ¿ÉÁìÈ¡¼´ÒÑ֪ͨ¹ýÍæ¼Ò¸Ã·âÓʼþ£¬ÐèÒªÔÚÓʼþ·¢ÉúʱºÍÉÏÏßʱÉèÖÃ״̬
#
# @author alee
# @date 2017-08-11 20:00
# @version 1.1
# @note:
# @change: "2018-08-10 12:00" hxp È«·þÓʼþÔö¼ÓÉý¼¶ºóÊÇ·ñ¿ÉÁìȡѡÏîÉèÖã¬Ôö¼ÓÓʼþ²éѯ¡¢Í¨¹ýÉóºË¡¢É¾³ý¹¦ÄÜ£»
#------------------------------------------------------------------------------ 
#"""Version = 2018-08-10 12:00"""
#------------------------------------------------------------------------------ 
import GameWorld
import ChConfig
import ShareDefine
import IPY_GameServer
import ChGameToMapPyPack
import NetPackCommon
import CommFunc
import ChPyNetSendPack
import DataRecordPack
import ReadChConfig
import PlayerDBOper
import EventReport
import IpyGameDataPY
import PlayerControl
import datetime
import uuid
import math
import json
#Áìȡ״̬  ¸öλÊý±êʶ £¨¿ÉÁìÈ¡¼´ÒÑ֪ͨ¹ýÍæ¼Ò¸Ã·âÓʼþ£¬ÐèÒªÔÚÓʼþ·¢ÉúʱºÍÉÏÏßʱÉèÖÃ״̬
#0 Î´Í¨Öª£¬1 ²»¿ÉÁìÈ¡£¬ 2 ¿ÉÁìÈ¡£¬ 3 ÒÑÁìÈ¡
(
Unknown_State,
Disable_State,
Enable_State,
Yet_State
) = range(4)
 
CheckState_OK = 0 # ÒÑÉóºË
CheckState_No = 1 # Î´ÉóºË
 
LimitLVType_Not = 0 # Éý¼¶Âú×ãÁìÈ¡µÈ¼¶ºó²»¿ÉÁì
LimitLVType_Can = 1 # Éý¼¶Âú×ãÁìÈ¡µÈ¼¶ºó¿ÉÁì
 
#Áìȡ״̬  Ê®Î»Êý±êʶ
(
Read_State_No,  # Óʼþδ¶Á
Read_State_Yes  # ÓʼþÒѶÁ
) = range(2)
 
#Áìȡ״̬
(
Entire_CompensationType,    #È«·þ
Personal_CompensationType,  #¸öÈË
Unknow_CompensationType     #δ֪
) = range(3)
 
Def_RequestState = "CompensationRequestState"
 
## ¸ù¾ÝÎïÆ·ÐÅÏ¢×ֵ䣬Éú³É²¹³¥ÎïÆ·ÊµÀý£¬ÓÃÓÚGM¹¤¾ßÌí¼Ó²¹³¥
#  @param curItemDict 
#  @return IpyCompensationItem
def MakeCompensationItem(curItemDict):
    curItemData = IPY_GameServer.IpyCompensationItem()
    #curItemData.GUID = curItemDict['GUID']
    curItemData.ItemID = curItemDict['ItemID']
    curItemData.Count = curItemDict['Count']
    curItemData.IsBind = curItemDict.get('IsBind',0)
    curItemData.UserData = curItemDict.get('UserData', '')
    return curItemData
 
#  ´Ë´¦»õ±ÒplayerIDList·¢·Åͳһ£¬Èç¸ù¾ÝÍæ¼Ò²»Í¬¶ø±ä£¬ÔòÓ¦ÐèÐÞ¸Ä
## ¹¦ÄÜ·¢·ÅÎïÆ·²¹³¥/½±ÀøÓʼþ
#  @param addItemList [(itemID, itemCnt, isBind), {»òÎïÆ·ÐÅÏ¢×Öµä}, ...]
#  @return GUID
def SendPersonalItemMailEx(title, content, getDays, playerIDList, addItemList, gold = 0, goldPaper = 0, silver = 0, 
                           detail="", moneySource=ChConfig.Def_GiveMoney_Mail):
    limitTime = str(GameWorld.GetDatetimeByDiffDays(getDays))
    limitTime = limitTime.split(".")[0]
    return SendPersonalItemMail(title, content, limitTime, playerIDList, addItemList, gold, goldPaper, silver, detail, moneySource)
    
def SendPersonalItemMailBatch(batchMailInfoList):
    ## ÅúÁ¿·¢ËÍÓʼþ
    mailTypeKey, batchPlayerIDList, batchAddItemList, batchParamList, batchGold, batchGoldPaper, batchSilver, batchDetail, moneySource = batchMailInfoList
    
    lenPlayerID = len(batchPlayerIDList)
    lenItem = len(batchAddItemList)
    lenParam = len(batchParamList)
    lenGold = len(batchGold)
    lenGoldPaper = len(batchGoldPaper)
    lenSilver = len(batchSilver)
    lenDetail = len(batchDetail)
    
    title = ""
    getDays = 30
    limitTime = str(GameWorld.GetDatetimeByDiffDays(getDays))
    limitTime = limitTime.split(".")[0]
    
    for i, playerIDList in enumerate(batchPlayerIDList):
        addItemList = batchAddItemList[i] if lenItem == lenPlayerID else []
        paramList = batchParamList[i] if lenParam == lenPlayerID else []
        gold = batchGold[i] if lenGold == lenPlayerID else 0
        goldPaper = batchGoldPaper[i] if lenGoldPaper == lenPlayerID else 0
        silver = batchSilver[i] if lenSilver == lenPlayerID else 0
        detail = batchDetail[i] if lenDetail == lenPlayerID else ""
        content = "<MailTemplate>%s</MailTemplate>%s" % (mailTypeKey, str(paramList))
        SendPersonalItemMail(title, content, limitTime, playerIDList, addItemList, gold, goldPaper, silver, detail=detail, moneySource=moneySource)
        
    return
 
def SendMailByKey(mailTypeKey, playerIDList, addItemList, paramList=[], gold=0, goldPaper=0, silver=0, 
                  detail="", moneySource=ChConfig.Def_GiveMoney_Mail):
    if not mailTypeKey:
        mailTypeKey = ShareDefine.DefaultLackSpaceMailType
    GameWorld.DebugLog("SendMailByKey %s, playerIDList=%s, addItemList=%s, paramList=%s, gold=%s, goldPaper=%s, silver=%s, moneySource=%s" 
                       % (mailTypeKey, playerIDList, addItemList, paramList, gold, goldPaper, silver, moneySource))
    title = ""
    content = "<MailTemplate>%s</MailTemplate>%s" % (mailTypeKey, json.dumps(paramList, ensure_ascii=False))
    return SendPersonalItemMailEx(title, content, 30, playerIDList, addItemList, gold, goldPaper, silver, detail, moneySource)
 
#  ´Ë´¦»õ±ÒplayerIDList·¢·Åͳһ£¬Èç¸ù¾ÝÍæ¼Ò²»Í¬¶ø±ä£¬ÔòÓ¦ÐèÐÞ¸Ä
## ¹¦ÄÜ·¢·ÅÎïÆ·²¹³¥/½±ÀøÓʼþ
#  @param addItemList [(itemID, itemCnt, isBind), {»òÎïÆ·ÐÅÏ¢×Öµä}, ...]
#  @return GUID
#  @remarks addItemListÖ§³Öappend×Öµä
def SendPersonalItemMail(title, content, limitTime, playerIDList, addItemList, gold = 0, goldPaper = 0, silver = 0, 
                         detail="", moneySource=ChConfig.Def_GiveMoney_Mail):
    if not playerIDList:
        return ""
    
    #if not addItemList:
    #    return ""
 
    curServerTime = GameWorld.GetCurrentDataTimeStr()
    # µ±Ç°Ê±¼äÒѾ­³¬¹ýÁìȡʱ¼ä
    if not curServerTime or curServerTime >= limitTime:
        GameWorld.DebugLog("¹¦ÄÜ·¢·ÅÎïÆ·²¹³¥/½±ÀøÓʼþ£¬Áìȡʱ¼äÒѳ¬Ê±£¬Ä¬Èϲ»Ìí¼Ó£¡LimitTime=%s" % limitTime)
        return ""
    
    addItemDictList = []
    for itemInfo in addItemList:
        if isinstance(itemInfo, dict):
            addItemDictList.append(itemInfo)
            continue
        
        
        if len(itemInfo) == 3:
            itemID, itemCnt, isBind = itemInfo
        else:
            continue
            
        addItemDict = {}
        addItemDict['ItemID'] = itemID
        addItemDict['Count'] = itemCnt
        addItemDict['IsBind'] = isBind
        addItemDictList.append(addItemDict)
        
    perMailItemCnt = IpyGameDataPY.GetFuncCfg("MailMaxItemCnt")
    mailCnt = max(1, int(math.ceil(len(addItemDictList)/float(perMailItemCnt)))) # Ò»·âÓʼþ×î¶à5¸öÎïÆ·
    for i in xrange(mailCnt):
        startIndex = i*perMailItemCnt
        GUID = str(uuid.uuid1())
        AddPersonalItem(GUID, addItemDictList[startIndex:startIndex + perMailItemCnt], playerIDList, 
                                           limitTime, "%s<$_$>%s<$_$>%s" % (ChConfig.Def_Mail_SenderSys, title, content),
                                           gold, goldPaper, silver, detail, moneySource)
    return GUID
 
## ·¢ËÍ´¿ÎÄ×Ö¸öÈ˲¹³¥
#  @param limitTime ¿ÉÒÔ´«¿Õ
#  @return None
def SendPersonalAsTextMail(PlayerID, title, content, limitTime):
    if GameWorld.IsCrossServer():
        return
    GUID = str(uuid.uuid1())
    PyAddPersonalCompensation(GUID, PlayerID, GameWorld.GetCurrentDataTimeStr(), limitTime, 
                              "%s<$_$>%s<$_$>%s" % (ChConfig.Def_Mail_SenderSys,title, content))
    return
 
def GetEntireCompensationInfo(checkState, limitLVType, limitLV):
    return checkState * 1000000 + limitLVType * 100000 + limitLV
def ParseEntireCompensationInfo(mailInfo):
    limitLV = mailInfo % 100000
    checkState = mailInfo / 1000000
    limitLVType = (mailInfo - checkState * 1000000) / 100000
    return checkState, limitLVType, limitLV
 
def QueryCompensationInfo(fromDate, toDate, guid, searchTitle, searchContent, searchState=None, maxCount=10):
    '''²¹³¥Óʼþ²éѯ
    '''
    
    compensationMgr = GameWorld.GetCompensationMgr()
    retList = []
    if guid:
        infoDict = __GetQueryCompensationInfoDict(compensationMgr, compensationMgr.FindEntireCompensation(guid), "", "", searchTitle, searchContent, searchState)
        if infoDict:
            retList.append(infoDict)
        return retList
    
    if fromDate:
        fromDate = "%s 00:00:00" % fromDate
    if toDate:
        toDate = "%s 23:59:59" % toDate
        
    entireCnt = compensationMgr.GetEntireCompensationCount()
    for i in xrange(entireCnt):
        compensation = compensationMgr.AtEntireCompensation(i)
                    
        infoDict = __GetQueryCompensationInfoDict(compensationMgr, compensation, fromDate, toDate, searchTitle, searchContent, searchState)
        if not infoDict:
            continue
        
        retList.append(infoDict)
        if len(retList) >= maxCount:
            break
        
    return retList
 
def __GetQueryCompensationInfoDict(compensationMgr, compensation, fromDate, toDate, searchTitle, searchContent, searchState):
    if not compensation or not compensation.GUID:
        return
    
    if fromDate and compensation.CreateTime < fromDate:
        return
    
    if toDate and compensation.CreateTime > toDate:
        return
    
    contentList = compensation.Text.split("<$_$>")
    if len(contentList) != 3:
        return
    sender, title, content = contentList
    
    if searchTitle and searchTitle not in title:
        return
    
    if searchContent and searchContent not in content:
        return
    
    checkState, limitLVType, limitLV = ParseEntireCompensationInfo(compensation.PlayerLV)
    if searchState != None and searchState != checkState:
        return
    
    GUID = compensation.GUID
    itemList = []
    curGUIDItemCount = compensationMgr.FindItemCount(GUID)
    for i in xrange(curGUIDItemCount):
        curItem = GameWorld.GetCompensationMgr().FindItemAt(GUID, i)
        itemID = curItem.ItemID
        if not itemID:
            continue
        itemList.append([itemID, curItem.Count, curItem.IsBind, curItem.UserData])
        
    compensationDict = {"GUID":GUID, "CheckState":checkState, "LimitLVType":limitLVType, "LimitLV":limitLV,
                        "Gold":compensation.Gold, "GoldPaper":compensation.GoldPaper, "Silver":compensation.Silver,
                        "PlayerJob":compensation.PlayerJob, "Sender":sender, "Title":title, "Content":content,
                        "CreateTime":compensation.CreateTime, "LimitTime":compensation.LimitTime, "ItemList":itemList}
    return compensationDict
 
def EntireCompensationOper(operGUIDList, operation):
    '''È«·þÓʼþ²Ù×÷¹ÜÀí
    @param operGUIDList: ´ý²Ù×÷µÄÓʼþGUIDÁбí
    @param operation: 1-ͨ¹ý£»2-ɾ³ý
    '''
    
    okGUIDList = []
    successGUIDList = []
    compensationMgr = GameWorld.GetCompensationMgr()
    for GUID in operGUIDList:
        compensation = compensationMgr.FindEntireCompensation(GUID)
        if not compensation or compensation.GUID != GUID:
            continue
        
        # É¾³ý
        if operation == 2:
            compensationMgr.DeleteEntireCompensation(GUID)
            compensationMgr.DeleteCompensationItem(GUID)
            compensationMgr.DeleteRecByGUID(GUID)
            GameWorld.Log("ɾ³ýÈ«·þÓʼþ: GUID=%s" % GUID)
            DataRecordPack.DR_EntireCompensation(GUID, "Delete")
            
        # Í¨¹ý
        elif operation == 1:
            mailInfo = compensation.PlayerLV
            checkState, limitLVType, limitLV = ParseEntireCompensationInfo(mailInfo)
            updMailInfo = GetEntireCompensationInfo(CheckState_OK, limitLVType, limitLV)
            compensationMgr.SetEntireCompensationPlayerLV(GUID, updMailInfo)
            GameWorld.Log("È«·þÓʼþÉóºËͨ¹ý: GUID=%s,mailInfo=%s,checkState=%s,updMailInfo=%s" 
                          % (GUID, mailInfo, checkState, updMailInfo))
            if checkState != CheckState_OK:
                okGUIDList.append(GUID)
                DataRecordPack.DR_EntireCompensation(GUID, "CheckOK")
        else:
            continue
        successGUIDList.append(GUID)
        
    playerManager = GameWorld.GetPlayerManager()
    for i in xrange(playerManager.GetActivePlayerCount()):
        player = playerManager.GetActivePlayerAt(i)
        if player == None or not player.GetInitOK() or PlayerControl.GetIsTJG(player):
            continue
        #¹ã²¥ÔÚÏßÍæ¼Ò
        notifyList = []
        for GUID in okGUIDList:
            compensationType, curEntireRequire = CheckRequestCompensation(player, GUID, False)
            if compensationType == Unknow_CompensationType:
                continue
            Text, gold, goldPaper, silver = curEntireRequire.Text, curEntireRequire.Gold, curEntireRequire.GoldPaper, curEntireRequire.Silver
            createTime = curEntireRequire.CreateTime
            notifyList.append((GUID, Text, createTime, gold, goldPaper, silver))
            
        #GameWorld.DebugLog("¹ã²¥ÔÚÏßÍæ¼ÒÈ«·þÓʼþ: %s" % str(notifyList), player.GetPlayerID())
        if notifyList:
            SyncQueryCompensationResult(player, notifyList)
            
    return successGUIDList
 
## Ìí¼ÓÈ«·þ²¹³¥
#  @param addItemDictList, LimitTime, mailInfo, PlayerJob,  Text 
#  @return None
def AddEntireItem(GUID, addItemDictList, LimitTime, mailInfo, PlayerJob, Text,
                  gold = 0, goldPaper = 0, silver = 0, detail="", serverID=0):
    '''
    @param mailInfo: GetEntireCompensationInfo µÄ·µ»ØÖµ, Ä¿Ç°ÔÝ´æ×ֶΠPlayerLV ÖÐ
    '''
    if GameWorld.IsCrossServer():
        return
    #GameWorld.DebugLog("Compensation### AddEntireItem GUID:%s ItemDict:\n%s "%(GUID, addItemDictList))
    
    #Ìí¼Ó²¹³¥°üµÄËùÓÐÎïÆ·
    for addItemDict in addItemDictList:
        curItemData = MakeCompensationItem(addItemDict)
        GameWorld.GetCompensationMgr().AddCompensationItem(GUID, curItemData)
        
    createTime = GameWorld.GetCurrentDataTimeStr()
    
    #Ìí¼ÓÈ«·þÁìÈ¡²¹³¥Ìõ¼þ
    GameWorld.GetCompensationMgr().AddEntireCompensationItem(GUID, createTime, LimitTime, mailInfo, 
                                                             PlayerJob, Text, gold, goldPaper, silver, serverID)
    checkState, limitLVType, limitLV = ParseEntireCompensationInfo(mailInfo)
    addDict = {"LimitTime":LimitTime, "LimitLV":limitLV, "LimitLVType":limitLVType, "CheckState":checkState, "PlayerJob":PlayerJob, "Text":Text, 
               "Gold":gold, "GoldPaper":goldPaper, "Silver":silver, "ItemList":addItemDictList, "Detail":detail}
    #Ìí¼ÓÁ÷Ïò
    eventName = "Add" if checkState == CheckState_OK else "AddToCheck"
    DataRecordPack.DR_EntireCompensation(GUID, eventName, addDict)
    __WriteSendMailOKEvent(GUID, addItemDictList, LimitTime, Text, None, mailInfo, PlayerJob)
    
    testAccIDList = ReadChConfig.GetEvalChConfig("TestAccID")
    playerManager = GameWorld.GetPlayerManager()
    for i in xrange(playerManager.GetActivePlayerCount()):
        player = playerManager.GetActivePlayerAt(i)
        if player == None or not player.GetInitOK() or PlayerControl.GetIsTJG(player):
            continue
        if checkState == CheckState_No:
            playerAccID = GameWorld.GetPlatformAccID(player.GetAccID())
            if playerAccID not in testAccIDList:
                continue
                
        #¹ã²¥ÔÚÏßÍæ¼Ò
        compensationType, curEntireRequire = CheckRequestCompensation(player, GUID, False)
        if compensationType == Unknow_CompensationType:
            continue
        Text, gold, goldPaper, silver = curEntireRequire.Text, curEntireRequire.Gold, curEntireRequire.GoldPaper, curEntireRequire.Silver
        #GameWorld.Log("#¹ã²¥ÔÚÏßÍæ¼Ò%s"%player.GetPlayerID())
        SyncQueryCompensationResult(player, [(GUID, Text, createTime, gold, goldPaper, silver)])
    return
 
# ¼ì²éÓʼþÊÇ·ñÒÑ´ïµ½±£´æÉÏÏÞ£¬È糬¹ýÔòÏÈɾ³ý¾ÉÓʼþ
# @param LimitTime ²ÎÊý¿É´«¿Õ£¬¸öÈËÓʼþ²»ÏÞÖÆÁìÈ¡ÆÚÏÞ£¬Ö»ÏÞÖÆ±£´æÊýÁ¿£¬ 
def PyAddPersonalCompensation(GUID, PlayerID, CreateTime, LimitTime, Text, gold = 0, goldPaper = 0, silver = 0, moneySource=ChConfig.Def_GiveMoney_Mail):
    curPlayer = GameWorld.GetPlayerManager().FindPlayerByID(PlayerID)
    totalCount = GameWorld.GetCompensationMgr().GetPersonalCompensationCount(PlayerID)
    #´ýÖ§³Öpy¶Á±í
    maxMailCount = IpyGameDataPY.GetFuncCfg("MailDefaultCount")
    delCount = totalCount - maxMailCount + 1
    GameWorld.DebugLog("ÐÂÔö¸öÈËÓʼþ: totalCount=%s,maxMailCount=%s" % (totalCount, maxMailCount), PlayerID)
    if delCount > 0:
        GameWorld.Log("¸öÈËÓʼþ´ïµ½ÉÏÏÞ£¬ÐèҪɾ³ý£¡delCount=%s" % (delCount), PlayerID)
        #ÏÈÈ¡µÃҪɾ³ýµÄGUID
        delGUIDs = []
        for i in xrange(delCount):
            curIpyPersonalData = GameWorld.GetCompensationMgr().PersonalCompensationAt(PlayerID, i)
            delGUIDs.append(curIpyPersonalData.GUID)
            
        for guid in delGUIDs:
            ClearPersonalCompensation(PlayerID, guid)
            GameWorld.Log("    DeletePersonalCompensation GUID = %s" % guid, PlayerID)
            
            if curPlayer:
                NotifyCompensationResult(curPlayer, guid, 1)
            #δ²¹Á÷Ïò
            
    #´Ë´¦Ã»ÓÐÏ·¢Í¨Öª
    mailType = moneySource - ChConfig.Def_GiveMoney_Unknown # typeÀàÐÍΪbyte£¬´æÖµÊ±ÐèÒª´¦ÀíÏÂ
    GameWorld.GetCompensationMgr().AddPersonalCompensation(GUID, PlayerID, CreateTime,
                                                           LimitTime, Text, mailType, gold, goldPaper, silver)
    return
 
 
#  ´Ë´¦»õ±ÒplayerIDList·¢·Åͳһ£¬Èç¸ù¾ÝÍæ¼Ò²»Í¬¶ø±ä£¬ÔòÓ¦ÐèÐÞ¸Ä
## Ìí¼Ó¸öÈ˲¹³¥
#  @param addItemDict, PlayerIDList, LimitTime, Text 
#  @return None
def AddPersonalItem(GUID, addItemDictList, PlayerIDList, LimitTime, Text, gold = 0, goldPaper = 0, silver = 0, detail="", moneySource=ChConfig.Def_GiveMoney_Mail):
    if GameWorld.IsCrossServer():
        return
    GameWorld.DebugLog("Compensation### AddPersonalItem GUID:%s ItemDict:\n%s "%(GUID, addItemDictList))
    
    #Ìí¼Ó²¹³¥°üµÄËùÓÐÎïÆ·
    for addItemDict in addItemDictList:
        curItemData = MakeCompensationItem(addItemDict)
        GameWorld.GetCompensationMgr().AddCompensationItem(GUID, curItemData)
        
    #offlinePlayerIDList = []
    onlinePlayerInfo = {}
    #ÅúÁ¿Ìí¼ÓÍæ¼Ò¸öÈ˲¹³¥ÁìÈ¡±í
    createTime = GameWorld.GetCurrentDataTimeStr()
    for PlayerID in PlayerIDList:
        PyAddPersonalCompensation(GUID, PlayerID, createTime, LimitTime, Text, gold, goldPaper, silver, moneySource)
        # Õë¶Ô¸öÈ˲¹³¥£¬Èç¹ûÊÇÔÚÏßÍæ¼Ò£¬ÔòÁ¢¼´ÍÆË͸ø¿Í»§¶Ë
        curPlayer = GameWorld.GetPlayerManager().FindPlayerByID(PlayerID)
        if curPlayer:
            SyncQueryCompensationResult(curPlayer, [(GUID, Text, createTime, gold, goldPaper, silver)])
            onlinePlayerInfo[curPlayer.GetAccID()] = [curPlayer.GetName(), curPlayer.GetLV()]
    #===========================================================================
    #    else:
    #        offlinePlayerIDList.append(PlayerID)
    #        
    # if offlinePlayerIDList:
    #    PlayerDBOper.FindDBOper(PlayerDBOper.Table_DBPlayer, {"PlayerID":{"$in":offlinePlayerIDList}},
    #                            {"PlayerID":1, "AccID":1, "PlayerName":1, "LV":1, "_id":0},
    #                            FindDBResult_SendMail, [GUID, addItemDictList, createTime, Text])
    # if onlinePlayerInfo:
    #    __WriteSendMailOKEvent(GUID, addItemDictList, createTime, Text, onlinePlayerInfo)
    #===========================================================================
    
    #Ìí¼ÓÁ÷Ïò
    addDict = {"LimitTime":LimitTime, "Text":Text, "Gold":gold, "GoldPaper":goldPaper, "Silver":silver, 
               "ItemListLen":len(addItemDictList), "Detail":detail, "MoneySource":moneySource}
    DataRecordPack.DR_AddPersonalCompensation(PlayerIDList, GUID, addItemDictList, addDict)
    return
 
def FindDBResult_SendMail(resultSetList, extendValueList):
    if not resultSetList:
        return
    
    playerInfoDict = {}
    GUID, addItemDictList, LimitTime, Text = extendValueList
    GameWorld.Log("ÓʼþÀëÏßÍæ¼ÒDBÐÅÏ¢: GUID=%s, %s" % (GUID, str(resultSetList)))
    for resultDict in resultSetList:
        playerInfoDict[resultDict["AccID"]] = [resultDict["PlayerName"], resultDict["LV"]]
        
    __WriteSendMailOKEvent(GUID, addItemDictList, LimitTime, Text, playerInfoDict)
    return
 
## Í¨ÖªµØÍ¼Óʼþ·¢ËÍOK
def __WriteSendMailOKEvent(GUID, addItemDictList, LimitTime, Text, playerInfoDict={}, playerLV=0, playerJob=0):
#===============================================================================
#    eventName = "SendMail"
#    #%s<$_$>%s
#    sender, title, content = Text.split("<$_$>")
#    #<MailTemplate>Ä£°å±àºÅ</MailTemplate>[%s,%s]
#    if "</MailTemplate>" in content:
#        mailContentList = content.split("</MailTemplate>")
#        mailTemplateNO = GameWorld.ToIntDef(mailContentList[0][14:], None)
#        EventMailInfo = ReadChConfig.GetEvalChConfig("EventMailInfo")
#        if mailTemplateNO not in EventMailInfo:
#            return
#        title, content = EventMailInfo[mailTemplateNO]
#        
#        mailParamStr = mailContentList[1]
#        mailParamStr = mailParamStr.replace(" ", "")
#        if mailParamStr.startswith("[") and mailParamStr.endswith("]") and len(mailParamStr) > 2:
#            content = content % tuple(eval(mailParamStr))
#            
#    itemInfo = "ÎïÆ·:"
#    for addItemDict in addItemDictList:
#        itemName = "%s" % addItemDict['ItemID']
#        if addItemDict.get('IsBind',0):
#            itemName += "(°ó)"
#        itemInfo += " %s*%s" % (itemName, addItemDict['Count'])
#        
#    # È«·þ
#    if not playerInfoDict:
#        comments = "%s: %s ÓÐЧÆÚ:%s, ÏÞÖÆµÈ¼¶:%s,Ö°Òµ:%s; %s; %s" % (title, GUID, LimitTime, playerLV, playerJob, itemInfo, content)
#        customEvent = EventReport.custom_events()
#        customEvent.account_id = "ServerAccount"
#        customEvent.event_name = eventName
#        customEvent.session_id = "ServerSession"
#        customEvent.account_name = customEvent.account_id
#        customEvent.comments = CommFunc.GetStrCutoff(comments, 255)
#        EventReport.WriteEvent(customEvent)
#    else:
#        comments = "%s: %s ÓÐЧÆÚ:%s, %s; %s" % (title, GUID, LimitTime, itemInfo, content)
#        comments = CommFunc.GetStrCutoff(comments, 255)
#        for accID, playerInfo in playerInfoDict.items():
#            playerName, playerLV = playerInfo
#            customEvent = EventReport.custom_events()
#            customEvent.SetEventAgentInfo(GameWorld.GetPlayerPlatform(accID))
#            customEvent.account_id = GameWorld.GetPlatformAccID(accID)
#            customEvent.event_name = eventName
#            customEvent.session_id = "GameServerPlayerSession"
#            
#            customEvent.account_name = customEvent.account_id
#            customEvent.chr_name = playerName
#            customEvent.comments = comments
#            EventReport.WriteEvent(customEvent)
#===============================================================================
    return
 
 
##A9 A1 ²éÑ¯Íæ¼Ò²¹³¥#tagCGQueryCompensation
#  @param index, clientData, tick 
#  @return None
def OnQueryPlayerCompensation(index, clientData, tick):
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)
    NotifyPlayerCompensation(curPlayer)
 
 
# ÌáÈ¡½ÓÊÕÓʼþÏ·¢
def NotifyPlayerCompensation(curPlayer):
    notifyList = SeekPlayerCompensation(curPlayer)
    SyncQueryCompensationResult(curPlayer, notifyList)
    return
 
# É¾³ý¸öÈËÓʼþ±í ×´Ì¬±í£¬Í¬Ò»¸öGUID ¿ÉÒÔ¶àÈËÁìÈ¡£¬²»ÄÜͬʱɾ³ýÎïÆ·
def ClearPersonalCompensation(curPlayerID, curGUID):
    GameWorld.GetCompensationMgr().DeletePersonalCompensation(curPlayerID, curGUID)
    GameWorld.GetCompensationMgr().DeletePlayerCompensationRec(curPlayerID, curGUID)
    
    #´æÔÚ¶àÈËÓʼþµÄÇé¿ö£¬¹Êɾ³ýÎïÆ·Ðè¼ì²éÊÇ·ñ×îºóÒ»¸öÁìÈ¡Õ߲ſÉɾ³ý
    if GameWorld.GetCompensationMgr().GetPersonalCountByGUID(curGUID) == 0:
        GameWorld.GetCompensationMgr().DeleteCompensationItem(curGUID)
    return
 
# ÉèÖÃÁìȡ״̬
def SetPrizeState(curPlayerID, GUID, prizeState, readState):
    state = readState*10 + prizeState
    GameWorld.GetCompensationMgr().AddPlayerRec(curPlayerID, GUID, state)
 
##²éÕÒÍæ¼Ò¿ÉÁìÈ¡µÄ²¹³¥Áбí
#  @param curPlayer 
#  @return None
def SeekPlayerCompensation(curPlayer):
    curPlayerID = curPlayer.GetID()
    curTime = datetime.datetime.today()
 
    allList = [] #²¹³¥ÎïÆ·±í [(GUID,TEXT),(GUID,TEXT),...]
    #¸öÈ˲¹³¥
    curPersonalCount = GameWorld.GetCompensationMgr().GetPersonalCompensationCount(curPlayerID)
    for i in xrange(curPersonalCount):
        curIpyPersonalData = GameWorld.GetCompensationMgr().PersonalCompensationAt(curPlayerID, i)
 
        #¿ÉÒÔÓõĽ±Àø
        allList.append((curIpyPersonalData.GUID, curIpyPersonalData.Text, curIpyPersonalData.CreateTime,
                        curIpyPersonalData.Gold, curIpyPersonalData.GoldPaper, curIpyPersonalData.Silver))
    
    #È«·þ²¹³¥    
    curEntireCount = GameWorld.GetCompensationMgr().GetEntireCompensationCount()
    curJob = curPlayer.GetJob()
    curLV = curPlayer.GetLV()
    isTestAccID = GameWorld.GetPlatformAccID(curPlayer.GetAccID()) in ReadChConfig.GetEvalChConfig("TestAccID")
    for i in xrange(curEntireCount - 1, -1, -1):
        curRequire = GameWorld.GetCompensationMgr().AtEntireCompensation(i)
        
        checkState, limitLVType, limitLV = ParseEntireCompensationInfo(curRequire.PlayerLV)
        if checkState == CheckState_No:
            if not isTestAccID:
                #δÉóºËµÄÓʼþ²»´¦Àí
                continue
            
        #»ñÈ¡¼Ç¼
        states = GameWorld.GetCompensationMgr().FindPlayerRecState(curPlayerID, curRequire.GUID)
        readState = states/10    # ¿É¶Á       
        curState = states%10    # Áìȡ״̬
        if curState in (Disable_State, Yet_State):
            #²»¿ÉÁì »ò ÒÑÁì
            
            continue
        
        limitTime = datetime.datetime.strptime(curRequire.LimitTime, ChConfig.TYPE_Time_Format)
        if limitTime < curTime and curState == Unknown_State:
            #δ֪ͨ¹ýµÄÓʼþ³¬Ê±£¬²Å¿ÉÉèÖóɲ»¿ÉÁìÈ¡
            SetPrizeState(curPlayerID, curRequire.GUID, Disable_State, readState)
            continue
        
        if limitLV > curLV:
            #µÈ¼¶²»×ã
            if limitLVType == LimitLVType_Not:
                SetPrizeState(curPlayerID, curRequire.GUID, Disable_State, readState)
            continue
        if curRequire.PlayerJob not in (-1, 127):
            curJob = curPlayer.GetJob()
            #ÓÐÖ°ÒµÏÞÖÆ
            if (curRequire.PlayerJob&pow(2,curJob)) == 0:
                SetPrizeState(curPlayerID, curRequire.GUID, Disable_State, readState)
                continue
            
        #¿ÉÒÔÓõĽ±Àø
        if Enable_State != curState:
            SetPrizeState(curPlayerID, curRequire.GUID, Enable_State, readState)
        allList.append((curRequire.GUID, curRequire.Text, curRequire.CreateTime,
                        curRequire.Gold, curRequire.GoldPaper, curRequire.Silver))
 
    return allList
 
##·¢ËÍ¿ÉÁìÈ¡½±Àø²éѯ½á¹û
#  @param curPlayer, notifyList 
#  @return None
def SyncQueryCompensationResult(curPlayer, notifyList):
    sendPack = ChPyNetSendPack.tagGCQueryCompensationResult()
    sendPack.Clear()
    for GUID, Text, CreateTime, gold, goldPaper, silver in notifyList:
        curGUIDItemCount = GameWorld.GetCompensationMgr().FindItemCount(GUID)
        subPack = ChPyNetSendPack.tagGCCompensationPack()
        subPack.GUID = GUID
        subPack.CreateTime = CreateTime
        subPack.Text = Text
        subPack.TextLen = len(Text)
        subPack.Gold = gold
        subPack.GoldPaper = goldPaper
        subPack.Silver = silver 
        subPack.IsRead = GameWorld.GetCompensationMgr().FindPlayerRecState(curPlayer.GetPlayerID(), GUID)/10
        for index in xrange(curGUIDItemCount):
            curItem = GameWorld.GetCompensationMgr().FindItemAt(GUID, index)
            subPackItem = ChPyNetSendPack.tagGCCompensationItem()
            subPackItem.ItemID = curItem.ItemID
            subPackItem.Count = curItem.Count
            subPackItem.IsBind = curItem.IsBind
            subPackItem.UserData = curItem.UserData
            subPackItem.UserDataLen = len(curItem.UserData)
            subPack.Items.append(subPackItem)
        subPack.Count = len(subPack.Items)
        sendPack.PackList.append(subPack)
    sendPack.Count = len(sendPack.PackList)
    NetPackCommon.SendFakePack(curPlayer, sendPack)
    return
 
##03 03 Íæ¼ÒÇëÇóÁìÈ¡²¹³¥#tagMGRequestCompensation
#  @param routeIndex, mapID, curPackData, tick
#  @return None
def OnMGRequestCompensation(routeIndex, mapID, curPackData, tick):
    global g_PlayerRequestStateDict
    GameWorld.DebugLog("Compensation### OnMGRequestCompensation in")
    myPlayerID = curPackData.PlayerID    
    curPlayer = GameWorld.GetPlayerManager().FindPlayerByID(myPlayerID)
    if not curPlayer:
        return
    if curPlayer.GetDictByKey(Def_RequestState):#УÑéËø
        GameWorld.DebugLog("Compensation### already request")
        return
    
    curPlayer.SetDict(Def_RequestState, 1)#¼ÓËø
    
    GUID = curPackData.GUID
    GameWorld.DebugLog("Compensation### OnMGRequestCompensation myPlayerID %s GUID %s"%(myPlayerID, GUID))
    
    compensationType, curEntireRequire = CheckRequestCompensation(curPlayer, GUID) 
    if compensationType == Unknow_CompensationType:
        #Áìȡʧ°Ü
        curPlayer.SetDict(Def_RequestState, 0)#½âËø
        GameWorld.DebugLog("Compensation### OnMGRequestCompensation no found")
        return
    Text, gold, goldPaper, silver, moneySource = curEntireRequire.Text, curEntireRequire.Gold, curEntireRequire.GoldPaper, curEntireRequire.Silver, curEntireRequire.Type
    #УÑé±³°üÊ£Óà¿Õ¼äÊÇ·ñ×ã¹»
    #===========================================================================
    # curCUIDItemCount = GameWorld.GetCompensationMgr().FindItemCount(GUID)
    # if curPackData.PackSpaceCount < curCUIDItemCount:
    #    curPlayer.SetDict(Def_RequestState, 0)#½âËø
    #    GameWorld.DebugLog("Compensation### OnMGRequestCompensation no enough space")
    #    return
    #===========================================================================
    #·¢Ë͵½MapServer¸øÓè½±Àø
    SendGMRequestCompensationResult(routeIndex, mapID, curPlayer, GUID, compensationType, Text, gold, goldPaper, silver, moneySource)
    GameWorld.DebugLog("Compensation### OnMGRequestCompensation out")
    
##ÇëÇóÁìÈ¡ÎïÆ·
#  @param curPlayer, GUID 
#  @return CompensationType ²¹³¥ÀàÐÍ
def CheckRequestCompensation(curPlayer, GUID, isPersonnal = True):
    curPlayerID = curPlayer.GetID()
    
    if isPersonnal:
        #ÔÚ¸öÈ˲¹³¥ÖÐ
        curPersonalCompensation = GameWorld.GetCompensationMgr().FindPersonalCompensation(curPlayerID, GUID)
        if curPersonalCompensation.PlayerID == curPlayerID:
            #·µ»Ø²¹³¥ÀàÐÍ
            return Personal_CompensationType, curPersonalCompensation
        
    #---------
    #ÔÚÈ«·þ²¹³¥ÖÐ
    states = GameWorld.GetCompensationMgr().FindPlayerRecState(curPlayerID, GUID)
    readState = states/10    # ¿É¶Á       
    curState = states%10    # Áìȡ״̬
    
    if curState in (Disable_State, Yet_State):
        #²»¿ÉÁì »ò ÒÑÁì
        return Unknow_CompensationType, None
    curEntireRequire = GameWorld.GetCompensationMgr().FindEntireCompensation(GUID)
    if curEntireRequire.GUID != GUID:
        #²»ÔÙÈ«·þ²¹³¥ÖУ¬É¾³ý¼Ç¼
        GameWorld.GetCompensationMgr().DeletePlayerCompensationRec(curPlayerID, GUID)
        return Unknow_CompensationType, None
    
    checkState, limitLVType, limitLV = ParseEntireCompensationInfo(curEntireRequire.PlayerLV)
    if checkState == CheckState_No:
        isTestAccID = GameWorld.GetPlatformAccID(curPlayer.GetAccID()) in ReadChConfig.GetEvalChConfig("TestAccID")
        if not isTestAccID:
            #δÉóºËµÄÓʼþ²»´¦Àí
            return Unknow_CompensationType, None
        
    #δ֪ͨ¹ý£¬ÇÒÒѳ¬Ê±
    limitTime = datetime.datetime.strptime(curEntireRequire.LimitTime, ChConfig.TYPE_Time_Format)
    curTime = datetime.datetime.today()
    if limitTime < curTime and Unknown_State == curState:
        #³¬Ê±£¬ÉèÖóɲ»¿ÉÁìÈ¡
        SetPrizeState(curPlayerID, GUID, Disable_State, readState)
        return Unknow_CompensationType, None
 
    if Enable_State == curState:
        #¿ÉÁìÈ¡ÇÒδ³¬Ê±£¬·µ»ØÎïÆ·ÐÅÏ¢
        return Entire_CompensationType, curEntireRequire
    
    curLV = curPlayer.GetLV()
    if limitLV > curLV:
        #µÈ¼¶²»×ã
        if limitLVType == LimitLVType_Not:
            SetPrizeState(curPlayerID, GUID, Disable_State, readState)
        return Unknow_CompensationType, None
    curJob = curPlayer.GetJob()
    if curEntireRequire.PlayerJob not in (-1, 127):
        #ÓÐÖ°ÒµÏÞÖÆ
        if (curEntireRequire.PlayerJob&pow(2,curJob)) == 0:
            SetPrizeState(curPlayerID, GUID, Disable_State, readState)
            return Unknow_CompensationType, None
    
    if curEntireRequire.ServerID and curEntireRequire.ServerID != GameWorld.GetPlayerServerID(curPlayer):
        # Ö¸¶¨·þÎñÆ÷Óʼþ
        SetPrizeState(curPlayerID, GUID, Disable_State, readState)
        return Unknow_CompensationType, None
     
    #¿ÉÒÔÓõĽ±Àø
    SetPrizeState(curPlayerID, GUID, Enable_State, readState)
    #·µ»ØÎïÆ·ÐÅÏ¢   
    return Entire_CompensationType, curEntireRequire
 
##·¢ËÍ·â°ü 03 02 Íæ¼ÒÁìÈ¡²¹³¥½á¹û#tagGMRequestCompensationResult
#  @param routeIndex, mapID, curPlayer, curItem
#  @return None
def SendGMRequestCompensationResult(routeIndex, mapID, curPlayer, GUID, compensationType, Text,
                                    gold, goldPaper, silver, moneySource):
    sendPack = ChGameToMapPyPack.tagGMRequestCompensationResult() 
    sendPack.PlayerID = curPlayer.GetID()
    sendPack.CompensationType = compensationType
    sendPack.GUID = GUID
    sendPack.Text = Text
    sendPack.TextLen = len(Text)
    sendPack.Gold = gold
    sendPack.GoldPaper = goldPaper
    sendPack.Silver = silver
    sendPack.MoneySource = moneySource
    curGUIDItemCount = GameWorld.GetCompensationMgr().FindItemCount(GUID)
    
    for i in xrange(curGUIDItemCount):
        curItem = GameWorld.GetCompensationMgr().FindItemAt(GUID, i)
        subPackItem = ChGameToMapPyPack.tagGMCompensationItem()
        subPackItem.ItemID = curItem.ItemID
        subPackItem.Count = curItem.Count
        subPackItem.IsBind = curItem.IsBind
        subPackItem.UserData = curItem.UserData
        subPackItem.UserDataLen = len(curItem.UserData)
        sendPack.Items.append(subPackItem)
    sendPack.Count = len(sendPack.Items)
    NetPackCommon.SendPyPackToMapServer(routeIndex, mapID, sendPack) 
    return
 
 
##03 04 Íæ¼ÒÁìÈ¡²¹³¥ÎïÆ··¢·Å½á¹û#tagMGGiveCompensationResult
#  @param routeIndex, mapID, curPackData, tick
#  @return None
def OnMGGiveCompensationResult(routeIndex, mapID, curPackData, tick):
    
    myPlayerID = curPackData.PlayerID
    curPlayer = GameWorld.GetPlayerManager().FindPlayerByID(myPlayerID)
    if not curPlayer:
        return
    GUID = curPackData.GUID
    CompensationType = curPackData.CompensationType
    curPlayer.SetDict(Def_RequestState, 0)#½âËø
    if curPackData.Result:
        GiveCompensationSuccess(curPlayer, GUID, CompensationType)
    return
 
 
 
##Íæ¼ÒÁìÈ¡²¹³¥ÎïÆ··¢·Å³É¹¦
#  @param curPlayer, GUID
#  @return None
def GiveCompensationSuccess(curPlayer, GUID, CompensationType):
    curPlayerID = curPlayer.GetID()
    NotifyCompensationResult(curPlayer, GUID, 1)
    
    #Á÷Ïò¼Ç¼
    DataRecordPack.DR_GiveCompensationSuccess(curPlayerID, GUID)
    #È«·þ½±ÀøÁìÈ¡¼Ç¼±ä¸üΪÒÑÁìÈ¡
    if CompensationType == Entire_CompensationType:
        GameWorld.GetCompensationMgr().AddPlayerRec(curPlayerID, GUID, 10 + Yet_State)
        return
    #¸öÈ˽±ÀøÁìÈ¡ÌõĿִÐÐɾ³ý
    if CompensationType == Personal_CompensationType:
        ClearPersonalCompensation(curPlayerID, GUID)
        return
    
    return
 
##ÇåÀí³¬Ê±È«·þ²¹³¥, È«·þÓʼþ·ÖÁ½²¿´¦Àí£º1. Óʼþ½ÓÊÕµÄÓÐЧʱ¼ä£¬ºǫ́ÉèÖã¬2. Óʼþɾ³ýʱ¼äΪ½ÓÊÕÓÐЧʱ¼äÔÙÑÓ³¤30Ìì
#  @param None
#  @return None
def ClearUpEntireCompensation():
    #УÑé¹ýÆÚ²¹³¥
    curTime = datetime.datetime.today()
    needClearGUIDList = []
    entireCnt = GameWorld.GetCompensationMgr().GetEntireCompensationCount()
    for i in xrange(entireCnt):
        curEntireRequire = GameWorld.GetCompensationMgr().AtEntireCompensation(i)
        # ³¬¹ý½ÓÊÕÓʼþ30ÌìÔòÍêȫɾ³ý´ËÓʼþ
        limitTime = datetime.datetime.strptime(curEntireRequire.LimitTime, ChConfig.TYPE_Time_Format) + datetime.timedelta(days = 30)
        if limitTime < curTime:
            needClearGUIDList.append(curEntireRequire.GUID)
            
    #ɾ³ý¹ýÆÚ²¹³¥ÐÅÏ¢£¬ Ã»ÓÐÖ÷¶¯Í¨ÖªÔÚÏßÍæ¼Ò
    for GUID in needClearGUIDList:
        #ɾ³ýÁìÈ¡Ìõ¼þ
        GameWorld.GetCompensationMgr().DeleteEntireCompensation(GUID)
        #ɾ³ý¹ýÆÚÎïÆ·
        GameWorld.GetCompensationMgr().DeleteCompensationItem(GUID)
        #Çå³ýÁìȡ״̬¼Ç¼¾Í
        GameWorld.GetCompensationMgr().DeleteRecByGUID(GUID)
    return
 
 
## ÇåÀí³¬Ê±²¹³¥, ¸öÈËÓʼþÔÚ³¬¹ýÉÏÏÞºó²Å»á×Ô¶¯É¾³ý
#  @param None
#  @return None
def ClearUpTimeOutCompensation():
    
    ClearUpEntireCompensation()
    return
 
 
#===============================================================================
# //A9 A6 ÉèÖÃÓʼþ£¨²¹³¥£©ÒѶÁ״̬ #tagCGReadCompensation
# struct    tagCGReadCompensation
# {
#    tagHead        Head;
#    char        GUID[40];
#    BYTE        IsRead;        //ÉèÖÃÓʼþ£¨²¹³¥£©ÊÇ·ñÒѶÁ 1¶ÁÈ¡
# };
#===============================================================================
def OnReadCompensation(index, clientData, tick):
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)
    
    GUID = clientData.GUID
    curPlayerID = curPlayer.GetID()
    
    #ÔÚ¸öÈ˲¹³¥ÖÐ
    curPersonalCompensation = GameWorld.GetCompensationMgr().FindPersonalCompensation(curPlayerID, GUID)
    if curPersonalCompensation.PlayerID != curPlayerID:
        curEntireRequire = GameWorld.GetCompensationMgr().FindEntireCompensation(GUID)
        if curEntireRequire.GUID != GUID:
            #GameWorld.DebugLog("ÕÒ²»µ½ÓʼþGUID:%s"%GUID)
            # ÕÒ²»µ½Óʼþ
            return
    curState = GameWorld.GetCompensationMgr().FindPlayerRecState(curPlayerID, GUID)
    if curState/10 >= Read_State_Yes:
        # ÒѶÁ
        #GameWorld.DebugLog("ÒѶÁGUID:%s"%GUID)
        return
    curState = Read_State_Yes*10 + curState%10
    GameWorld.GetCompensationMgr().AddPlayerRec(curPlayerID, GUID, curState)
    #GameWorld.DebugLog("ÒѶÁcurState:%s"%curState)
    return
 
#===============================================================================
# //A9 A7 É¾³ýÓʼþ£¨²¹³¥£© #tagCGDelCompensation
# struct    tagCGDelCompensation
# {
#    tagHead        Head;
#    char        GUID[40];
# };
#===============================================================================
def OnDelCompensation(index, clientData, tick):
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)
    GUID = clientData.GUID
    curPlayerID = curPlayer.GetID()
    
    #ÔÚ¸öÈ˲¹³¥ÖÐ
    curPersonalCompensation = GameWorld.GetCompensationMgr().FindPersonalCompensation(curPlayerID, GUID)
    if curPersonalCompensation.PlayerID == curPlayerID:
        ClearPersonalCompensation(curPlayerID, GUID)
        NotifyCompensationResult(curPlayer, GUID, 1)
        #GameWorld.DebugLog("¸öÈ˲¹³¥ÖÐOnDelCompensation:%s"%GUID)
        return
    
    curEntireRequire = GameWorld.GetCompensationMgr().FindEntireCompensation(GUID)
    if curEntireRequire.GUID != GUID:
        # ÕÒ²»µ½Óʼþ
        NotifyCompensationResult(curPlayer, GUID, 0)
        #GameWorld.DebugLog("ÕÒ²»µ½ÓʼþOnDelCompensation:%s"%GUID)
        return
    
    #È«·þÓʼþ
    curEntireRequire = GameWorld.GetCompensationMgr().FindEntireCompensation(GUID)
    if curEntireRequire.GUID == GUID:
        SetPrizeState(curPlayerID, GUID, Disable_State, GameWorld.GetCompensationMgr().FindPlayerRecState(curPlayerID, GUID)/10)
        NotifyCompensationResult(curPlayer, GUID, 1)
        #GameWorld.DebugLog("È«·þÓʼþOnDelCompensation:%s"%GUID)
        return
 
    NotifyCompensationResult(curPlayer, GUID, 0)
        
# Óʼþɾ³ýÇé¿ö   
def NotifyCompensationResult(curPlayer, GUID, result):    
    sendMCPack = ChPyNetSendPack.tagGCDelCompensationResult()
    sendMCPack.GUID = GUID
    sendMCPack.Result = result
    NetPackCommon.SendFakePack(curPlayer, sendMCPack)