hxp
2025-06-04 d196d101b54ca95a1343399841d6b4e1117143b7
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
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package Player.PlayerMail
#
# @todo:Óʼþ
# @author hxp
# @date 2025-05-14
# @version 1.0
#
# ÏêϸÃèÊö: Óʼþ
#
#-------------------------------------------------------------------------------
#"""Version = 2025-05-14 12:00"""
#-------------------------------------------------------------------------------
 
import GameWorld
import ItemCommon
import PlayerControl
import IPY_GameWorld
import DataRecordPack
import ChPyNetSendPack
import IpyGameDataPY
import ItemControler
import NetPackCommon
import ShareDefine
import DBDataMgr
import ChConfig
 
import json
import time
 
def OnPlayerLogin(curPlayer):
    Sync_PlayerServerMail(curPlayer)
    Sync_PersonalMail(curPlayer)
    return
 
def OnDayEx():
    curTime = int(time.time())
    DelTimeoutServerMail(curTime)
    DelTimeoutPersonalMail(curTime)
    return
 
def DelTimeoutServerMail(curTime):
    ## ÇåÀí³¬Ê±È«·þÓʼþ, È«·þÓʼþ·ÖÁ½²¿´¦Àí£º1. Óʼþ½ÓÊÕµÄÓÐЧʱ¼ä£¬ºǫ́ÉèÖã¬2. Óʼþɾ³ýʱ¼äΪ½ÓÊÕÓÐЧʱ¼äÔÙÑÓ³¤30Ìì
    mailMgr = DBDataMgr.GetMailMgr()
    serverMailGuids = mailMgr.GetServerMailGuids()
    for guid in serverMailGuids:
        mailObj = mailMgr.GetServerMail(guid)
        if not mailObj:
            continue
        createTime = GameWorld.ChangeTimeStrToNum(mailObj.GetCreateTime())
        limitTime = createTime + (mailObj.GetLimitDays() + 7) * 3600 * 24
        if curTime < limitTime:
            #GameWorld.DebugLog("È«·þÓʼþδ³¬Ê±£¬²»É¾³ý! %s,createTime=%s,limitTime=%s" % (guid, mailObj.GetCreateTime(), GameWorld.ChangeTimeNumToStr(limitTime)))
            continue
        
        DelServerMail(guid, "Timeout")
        
    return
 
def DelServerMail(guid, delEvent=""):
    GameWorld.Log("ɾ³ýÈ«·þÓʼþ: %s" % (guid))
    mailMgr = DBDataMgr.GetMailMgr()
    playerStateDict = mailMgr.DelServerMail(guid)
    DataRecordPack.DR_ServerMail(guid, "Delete" + delEvent)
    if not playerStateDict:
        return
    playerMgr = GameWorld.GetPlayerManager()
    clientPack = __packMailStateChange({guid:ShareDefine.MailState_Del})
    for playerID, mailState in playerStateDict.items():
        if mailState >= ShareDefine.MailState_Del:
            continue
        curPlayer = playerMgr.FindPlayerByID(playerID)
        if curPlayer:
            NetPackCommon.SendFakePack(curPlayer, clientPack)
    return
 
def DelTimeoutPersonalMail(curTime):
    ## ÇåÀí³¬Ê±¸öÈËÓʼþ
    mailMgr = DBDataMgr.GetMailMgr()
    personalMailDict = mailMgr.GetAllPersonalMailDict()
    for playerID, mailDict in personalMailDict.items():
        notifyGUIDState = {}
        for guid, mailObj in mailDict.items():
            createTime = GameWorld.ChangeTimeStrToNum(mailObj.GetCreateTime())
            limitTime = createTime + (mailObj.GetLimitDays() + 0) * 3600 * 24
            if curTime < limitTime:
                #GameWorld.DebugLog("¸öÈËÓʼþδ³¬Ê±£¬²»É¾³ý! %s,createTime=%s,limitTime=%s" % (guid, mailObj.GetCreateTime(), GameWorld.ChangeTimeNumToStr(limitTime)), playerID)
                continue
            
            mailState = mailObj.GetMailState()
            mailItemCount = mailMgr.GetMailItemCount(guid)
            if mailItemCount and mailState != ShareDefine.MailState_Got:
                #GameWorld.DebugLog("¸öÈËÓʼþÓÐÎïÆ·Î´ÁìÈ¡²»É¾³ý! %s" % guid, playerID)
                continue
            
            mailMgr.DelPersonalMail(playerID, guid)
            DataRecordPack.DR_MailDel(playerID, guid, "Timeout")
            notifyGUIDState[guid] = ShareDefine.MailState_Del
            
        if not notifyGUIDState:
            continue
        
        curPlayer = GameWorld.GetPlayerManager().FindPlayerByID(playerID)
        if curPlayer:
            Sync_PlayerMailState(curPlayer, notifyGUIDState)
    return
 
def SendMailByKey(mailTypeKey, playerID, itemList, paramList=[], limitDays=7):
    ## ·¢Ë͸öÈËÓʼþħ°å
    if not mailTypeKey:
        mailTypeKey = ShareDefine.DefaultLackSpaceMailType
    title = "<T>%s</T>" % mailTypeKey
    text = "%s" % json.dumps(paramList, ensure_ascii=False)
    SendMail(playerID, title, text, itemList, limitDays)
    return
 
def SendMail(playerID, title, text, itemList=None, limitDays=7, mailType=0):
    ## ·¢Ë͸öÈËÓʼþ
    if itemList == None:
        itemList = []
        
    mailMgr = DBDataMgr.GetMailMgr()
    mailMax = IpyGameDataPY.GetFuncCfg("PersonalMail", 1)
    mailCnt = mailMgr.GetPersonalMailCount(playerID)
    if mailCnt >= mailMax:
        mailList = mailMgr.GetPersonalMails(playerID)
        mailList.sort(key=lambda m: (-m.GetMailState(), m.GetCreateTime())) # °´Óʼþ״̬µ¹Ðò¡¢´´½¨Ê±¼äÉýÐòÅÅ
        oneDelCnt = max(1, mailMax / 10) # Ò»´ÎÐÔɾ³ý×î´óÊýÁ¿µÄ1/10·âÓʼþ
        isStop = False
        delCnt = 0
        notifyGUIDState = {}
        while delCnt < oneDelCnt and mailList and not isStop:
            mailObj = mailList.pop(0) # ¸ÃÁбíÊÇcopy£¬¿ÉÖ±½Ópop
            if not mailObj:
                continue
            guid = mailObj.GetGUID()
            mailState = mailObj.GetMailState()
            # ÒѾ­É¾µ½Î´¶Á»òδÁìÈ¡µÄÓʼþ£¬ÔòÇ¿ÖÆÍ£Ö¹£¬²»ÔÙɾ³ý
            if mailState == ShareDefine.MailState_UnRead or (mailState == ShareDefine.MailState_Read and mailMgr.GetMailItemCount(guid)):
                isStop = True
                if delCnt > 0:
                    # ÒѾ­ÓÐɾ¹ýÁ˱¾·â¿É²»É¾
                    break
                
            #ÖÁÉÙҪɾ1·â
            delCnt += 1
            GameWorld.DebugLog("ϵͳ×Ô¶¯É¾³ýÓʼþ: playerID=%s,guid=%s,mailState=%s,isStop=%s" % (playerID, guid, mailState, isStop))
            mailMgr.DelPersonalMail(playerID, guid)
            DataRecordPack.DR_MailDel(playerID, guid, "MaxCountLimiit")
            notifyGUIDState[guid] = ShareDefine.MailState_Del
            
        if notifyGUIDState:
            curPlayer = GameWorld.GetPlayerManager().FindPlayerByID(playerID)
            if curPlayer:
                Sync_PlayerMailState(curPlayer, notifyGUIDState)
                
    mailObj = mailMgr.AddPersonalMail(playerID, title, text, itemList, limitDays, mailType)
    CreateTime = mailObj.GetCreateTime()
    GUID = mailObj.GetGUID()
    
    #Ìí¼ÓÁ÷Ïò
    addDict = {"CreateTime":CreateTime, "Title":title, "Text":text, "LimitDays":limitDays, 
               "ItemList":itemList, "ItemListLen":len(itemList)}
    DataRecordPack.DR_MailSend(playerID, GUID, addDict)
    
    curPlayer = GameWorld.GetPlayerManager().FindPlayerByID(playerID)
    if curPlayer:
        Sync_PersonalMail(curPlayer, [mailObj.GetGUID()])
    return
 
def SendSeverMail(guid, title, text, itemList=None, limitDays=7, mailType=0, limitLV=0, limitLVType=0, checkState=0):
    '''·¢ËÍÈ«·þÓʼþ
    @param guid: ¿É´«Èë¿Õ£¬Ôòϵͳ×Ô¶¯Éú³Éguid
    @param limitLV: ÏÞÖÆ¿ÉÁìµÄ×îµÍµÈ¼¶
    @param limitLVType: µÈ¼¶´ïµ½ºóÊÇ·ñ¿ÉÁ죬ĬÈϲ»¿É
    @param checkState: ÊÇ·ñÐèÒªÉóºË£¬Ä¬Èϲ»ÐèÒª
    @return: None - ·¢ËÍʧ°Ü£» mailObj - ³É¹¦·¢Ë͵ÄÓʼþʵÀý
    '''
    mailMgr = DBDataMgr.GetMailMgr()
    mailObj = mailMgr.AddServerMail(guid, title, text, itemList, limitDays, mailType)
    if not mailObj:
        return mailObj
    GUID = mailObj.GetGUID()
    mailObj.SetLimitLV(limitLV)
    mailObj.SetLimitLVType(limitLVType)
    mailObj.SetCheckState(checkState)
    eventName = "Add" if not checkState else "AddToCheck"
    addDict = {"LimitDays":limitDays, "LimitLV":limitLV, "LimitLVType":limitLVType, "CheckState":checkState, 
               "title":title, "Text":text, "ItemList":itemList}
    DataRecordPack.DR_ServerMail(GUID, eventName, addDict)
    if not checkState:
        Sync_ServerMail(mailObj.GetGUID())
    return mailObj
 
def CheckServerMailResult(guid, isOK):
    ## ÉóºËÈ«·þÓʼþ½á¹û
    # @param isOK: ÊÇ·ñͨ¹ýÉóºË
    mailMgr = DBDataMgr.GetMailMgr()
    mailObj = mailMgr.GetServerMail(guid)
    if not mailObj:
        return
    if not isOK:
        # ÉóºË²»Í¨¹ýµÄÖ±½Óɾ³ý
        DelServerMail(guid, "GMDel")
        return
    GameWorld.Log("È«·þÓʼþÉóºËͨ¹ý: %s" % guid)
    mailObj.SetCheckState(0) # ÉèÖÃΪ0£¬²»ÐèÒªÉóºËÁË£¬¼´Í¨¹ý
    DataRecordPack.DR_ServerMail(guid, "CheckOK")
    Sync_ServerMail(guid)
    return
 
#// A5 37 ÇëÇóÓʼþ²Ù×÷ #tagCMRequestMail
#
#struct    tagCMRequestMail
#{
#    tagHead        Head;
#    char        GUID[36];    //ÓʼþGUID£¬¿É´«¿Õ£¬Ä¬ÈÏÕë¶Ô¸öÈËÓʼþµÄÅúÁ¿´¦Àí£¬ÈçÅúÁ¿ÁìÈ¡¡¢ÅúÁ¿É¾³ýÒÑÁìÓʼþµÈ£»È«·þÓʼþÔÝʱÏÞÖÆÖ»Äܵ¥·âÓʼþ´¦Àí
#    BYTE        ReqType;        //0-ÉèÖÃÒѶÁ£¬1-ÁìÈ¡Óʼþ£¬2-ɾ³ýÓʼþ
#};
def OnRequestMail(index, curPackData, tick):
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)
    guid = curPackData.GUID
    reqType = curPackData.ReqType
    
    if reqType == 2:
        doMailDel(curPlayer, guid)
    elif reqType == 1:
        doMailGetAward(curPlayer, guid)
    else:
        doMailRead(curPlayer, guid)
        
    return
 
def doMailRead(curPlayer, guid):
    ## Ö´ÐÐÓʼþÒѶÁ
    if not guid:
        return
    playerID = curPlayer.GetPlayerID()
    mailMgr = DBDataMgr.GetMailMgr()
    mailState = mailMgr.GetPlayerMailState(guid, playerID)
    if mailState == ShareDefine.MailState_UnRead:
        mailState = mailMgr.SetPlayerMailState(guid, playerID, ShareDefine.MailState_Read)
    Sync_PlayerMailState(curPlayer, {guid:mailState})
    return
 
def doMailGetAward(curPlayer, guid):
    ## Ö´ÐÐÓʼþÁìÈ¡£¬Ö§³ÖÅúÁ¿
    
    playerID = curPlayer.GetPlayerID()
    mailMgr = DBDataMgr.GetMailMgr()
    
    # ÅúÁ¿´¦Àí£¬½öÕë¶Ô¸öÈËÓʼþ
    if not guid:
        guidList = mailMgr.GetPersonalMailGuids(playerID)
    else:
        guidList = [guid]
        
    statItemList = []
    statItemDict = {} # Í³¼ÆÀÛ¼Æ
    isPackSpaceEnough = True
    notifyGUIDState = {}
    for guid in guidList:
        mailState = mailMgr.GetPlayerMailState(guid, playerID)
        if mailState == ShareDefine.MailState_Unknown or mailState >= ShareDefine.MailState_Got:
            GameWorld.DebugLog("Óʼþ״̬ÒÑÁìÈ¡»ò²»ÄÜÁìÈ¡! mailState=%s,%s" % (mailState, guid), playerID)
            continue
        
        mailItemCount = mailMgr.GetMailItemCount(guid)
        if not mailItemCount:
            GameWorld.DebugLog("ÓʼþûÓÐÎïÆ·¿ÉÁìÈ¡! %s" % (guid), playerID)
            continue
        
        needPackSpaceDict = {}
        for index in range(mailItemCount):
            mailItem = mailMgr.GetMailItemAt(guid, index)
            itemID = mailItem.GetItemID()
            itemCount = mailItem.GetCount()
            isBind = mailItem.GetIsBind()
            userData = mailItem.GetUserData()
            
            curItemData = GameWorld.GetGameData().GetItemByTypeID(itemID)
            if not curItemData:
                continue
            
            packType = ChConfig.GetItemPackType(curItemData)
            if userData:
                statItemList.append([itemID, itemCount, isBind, userData])
                needSpace = 1
            else:
                key = (itemID, isBind)
                if key not in statItemDict:
                    statItemDict[key] = 0
                    statItemList.append(key)
                statItemDict[key] = statItemDict[key] + itemCount
                
                needSpace = ItemControler.GetItemNeedPackCount(packType, curItemData, itemCount, isBind)
            needPackSpaceDict[packType] = needPackSpaceDict.get(packType, 0) + needSpace
            
        GameWorld.DebugLog("    guid=%s,needPackSpaceDict=%s" % (guid, needPackSpaceDict))
        
        for packType, needSpace in needPackSpaceDict.items():
            if needSpace > ItemCommon.GetItemPackSpace(curPlayer, packType, needSpace):
                PlayerControl.NotifyCode(curPlayer, "GeRen_chenxin_676165", [packType])
                isPackSpaceEnough = False
                break
            
        if not isPackSpaceEnough:
            break
        
        notifyGUIDState[guid] = mailMgr.SetPlayerMailState(guid, playerID, ShareDefine.MailState_Got)
        
        for index in range(mailItemCount):
            mailItem = mailMgr.GetMailItemAt(guid, index)
            itemID = mailItem.GetItemID()
            itemCount = mailItem.GetCount()
            isBind = mailItem.GetIsBind()
            userData = mailItem.GetUserData()
            #setAttrDict = {} if not userData else eval(userData) Ö®ºóÀ©Õ¹ÓÐÖ¸¶¨ÊôÐԵģ¬¿É²Î¿¼¿³Ê÷°æ±¾
            if not ItemControler.GivePlayerItem(curPlayer, itemID, itemCount, isBind, [IPY_GameWorld.rptItem], 
                                                event=[ChConfig.ItemGive_Mail, False, {"MailGUID":guid}]):
                continue
            
        DataRecordPack.DR_MailGiveSuccess(playerID, guid)
        
    giveItemInfo = []
    for statItem in statItemList:
        if len(statItem) == 2:
            itemID, isBind = statItem
            itemCount = statItemDict.get(statItem, 0)
            giveItemInfo.append([itemID, itemCount, isBind])
        else:
            giveItemInfo.append(statItem)
    if giveItemInfo:
        ItemControler.NotifyGiveAwardInfo(curPlayer, giveItemInfo, "Mail")
        
    Sync_PlayerMailState(curPlayer, notifyGUIDState)
    return
 
def doMailDel(curPlayer, guid, isGM=False, playerID=0):
    ## Ö´ÐÐÓʼþɾ³ý
    # @param isGM: ÊÇ·ñGMɾ³ý£¬ÎÞÊÓÎïÆ·ÊÇ·ñÒÑÁìÈ¡£¬Ç¿ÖÆÉ¾³ý
    
    if curPlayer:
        playerID = curPlayer.GetPlayerID()
    mailMgr = DBDataMgr.GetMailMgr()
    
    # ÅúÁ¿´¦Àí£¬½öÕë¶Ô¸öÈËÓʼþ
    if not guid:
        guidList = mailMgr.GetPersonalMailGuids(playerID)
    else:
        guidList = [guid]
        
    notifyGUIDState = {}
    for guid in guidList:
        mailState = mailMgr.GetPlayerMailState(guid, playerID)
        if mailState >= ShareDefine.MailState_Del:
            continue
        if not isGM:
            mailItemCount = mailMgr.GetMailItemCount(guid)
            if mailItemCount and mailState != ShareDefine.MailState_Got:
                GameWorld.DebugLog("ÓÐÎïÆ·ÓʼþδÁìÈ¡²»ÄÜɾ³ý! %s" % guid, playerID)
                continue
            if not mailItemCount and mailState != ShareDefine.MailState_Read:
                GameWorld.DebugLog("ÎÞÎïÆ·Óʼþδ¶Á²»ÄÜɾ³ý! %s" % guid, playerID)
                continue
            
        notifyGUIDState[guid] = mailMgr.SetPlayerMailState(guid, playerID, ShareDefine.MailState_Del)
        if isGM:
            DataRecordPack.DR_MailDel(playerID, guid, "GMDel")
        # ÕâÀïÍæ¼ÒÖ÷¶¯É¾³ýµÄ¿É²»¼Ç¼Á÷Ïò£¬ÒòΪδ¶ÁδÁìÈ¡²»ÔÊÐíÖ÷¶¯É¾³ý£¬ÒÑÁìÈ¡µÄÒÑÓÐÁìÈ¡¼Ç¼£¬ËùÒԿɲ»¼Ç¼
        
    if curPlayer:
        Sync_PlayerMailState(curPlayer, notifyGUIDState)
    return
 
def Sync_ServerMail(guid):
    ## Í¨ÖªÈ«·þÓʼþ
    mailMgr = DBDataMgr.GetMailMgr()
    serverMail = mailMgr.GetServerMail(guid)
    if not serverMail:
        return
    packMail = __packMailObj(serverMail)
    
    clientPack = ChPyNetSendPack.tagMCMailList()
    clientPack.IsServerMail = 1
    clientPack.MailList = [packMail]
    clientPack.Count = len(clientPack.MailList)
    
    playerManager = GameWorld.GetPlayerManager()
    for i in range(playerManager.OnlineCount()):
        curPlayer = playerManager.OnlineAt(i)
        if not GameWorld.IsNormalPlayer(curPlayer):
            continue
        
        mailState = CheckPlayerServerMailState(curPlayer, serverMail)
        if mailState == ShareDefine.MailState_Unknown or mailState >= ShareDefine.MailState_Del:
            continue
        
        packMail.MailState = mailState
        NetPackCommon.SendFakePack(curPlayer, clientPack)
        
    return
 
def Sync_PlayerServerMail(curPlayer):
    ## Í¨ÖªÍæ¼ÒÏà¹ØµÄÈ«·þÓʼþ
    mailMgr = DBDataMgr.GetMailMgr()
    guidList = mailMgr.GetServerMailGuids()
    if not guidList:
        return
    
    mailList = []
    for guid in guidList:
        mailObj = mailMgr.GetServerMail(guid)
        if not mailObj:
            continue
        
        mailState = CheckPlayerServerMailState(curPlayer, mailObj)
        if mailState == ShareDefine.MailState_Unknown or mailState >= ShareDefine.MailState_Del:
            continue
        
        mail = __packMailObj(mailObj)
        mail.MailState = mailState
        mailList.append(mail)
        
    if not mailList:
        return
    
    clientPack = ChPyNetSendPack.tagMCMailList()
    clientPack.IsServerMail = 1
    clientPack.MailList = mailList
    clientPack.Count = len(clientPack.MailList)
    NetPackCommon.SendFakePack(curPlayer, clientPack)
    return
 
def CheckPlayerServerMailState(curPlayer, serverMail):
    ## ¼ì²é´¦ÀíÍæ¼ÒÈ«·þÓʼþ״̬
    # @return: Íæ¼Ò¸ÃÈ«·þÓʼþµÄ״̬
    
    if not serverMail or serverMail.GetCheckState():
        # ÐèÒªÉóºËµÄ£¬Ö±½Ó·µ»Ø
        return ShareDefine.MailState_Unknown
    
    playerID = curPlayer.GetPlayerID()
    guid = serverMail.GetGUID()
    mailMgr = DBDataMgr.GetMailMgr()
    mailState = mailMgr.GetPlayerMailState(guid, playerID)
    if mailState != ShareDefine.MailState_Unknown:
        return mailState
    
    mailState = ShareDefine.MailState_Unknown
    limitLV = serverMail.GetLimitLV()
    limitLVType = serverMail.GetLimitLVType()
    playerLV = curPlayer.GetLV()
    if playerLV < limitLV:
        if not limitLVType:
            return mailMgr.SetPlayerMailState(guid, playerID, ShareDefine.MailState_Limit)
        return ShareDefine.MailState_Unknown
    
    return mailMgr.SetPlayerMailState(guid, playerID, ShareDefine.MailState_UnRead)
 
def Sync_PersonalMail(curPlayer, guidList=None):
    ## Í¨ÖªÍæ¼Ò¸öÈËÓʼþ
    
    playerID = curPlayer.GetPlayerID()
    mailMgr = DBDataMgr.GetMailMgr()
    if not guidList:
        guidList = mailMgr.GetPersonalMailGuids(playerID)
        
    if not guidList:
        return
    
    clientPack = ChPyNetSendPack.tagMCMailList()
    clientPack.MailList = []
    for guid in guidList:
        mailObj = mailMgr.GetPersonalMail(playerID, guid)
        if not mailObj:
            continue
        mail = __packMailObj(mailObj)
        mail.MailState = mailObj.GetMailState()
        clientPack.MailList.append(mail)
    clientPack.Count = len(clientPack.MailList)
    NetPackCommon.SendFakePack(curPlayer, clientPack)
    return
 
def __packMailObj(mailObj):
    ## ´ò°ü֪ͨµÄÓʼþ
    guid = mailObj.GetGUID()
    mail = ChPyNetSendPack.tagMCMail()
    mail.GUID = guid
    mail.Type = mailObj.GetType()
    mail.CreateTime = mailObj.GetCreateTime()
    mail.LimitDays = mailObj.GetLimitDays()
    mail.Title = mailObj.GetTitle()
    mail.TitleLen = len(mail.Title)
    mail.Text = mailObj.GetText()
    mail.TextLen = len(mail.Text)
    
    mailMgr = DBDataMgr.GetMailMgr()
    mailItemCount = mailMgr.GetMailItemCount(guid)
    for index in range(mailItemCount):
        mailItem = mailMgr.GetMailItemAt(guid, index)
        if not mailItem:
            continue
        item = ChPyNetSendPack.tagMCMailItem()
        item.ItemID = mailItem.GetItemID()
        item.Count = mailItem.GetCount()
        item.IsBind = mailItem.GetIsBind()
        item.UserData = mailItem.GetUserData()
        item.UserDataLen = len(item.UserData)
        mail.Items.append(item)
    mail.Count = len(mail.Items)
    return mail
 
def __packMailStateChange(notifyGUIDState):
    clientPack = ChPyNetSendPack.tagMCMailStateChange()
    clientPack.MailList = []
    for guid, mailState in notifyGUIDState.items():
        mail = ChPyNetSendPack.tagMCMailState()
        mail.GUID = guid
        mail.MailState = mailState
        clientPack.MailList.append(mail)
    clientPack.Count = len(clientPack.MailList)
    return clientPack
 
def Sync_PlayerMailState(curPlayer, notifyGUIDState):
    ## Í¨ÖªÓʼþ״̬±ä¸ü
    if not notifyGUIDState:
        return
    NetPackCommon.SendFakePack(curPlayer, __packMailStateChange(notifyGUIDState))
    return