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
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package Player.PlayerChatBox
#
# @todo:ÁÄÌìÆøÅݱíÇé
# @author hxp
# @date 2024-09-13
# @version 1.0
#
# ÏêϸÃèÊö: ÁÄÌìÆøÅݱíÇé
#
#-------------------------------------------------------------------------------
#"""Version = 2024-09-13 16:30"""
#-------------------------------------------------------------------------------
 
import GameWorld
import ShareDefine
import PlayerControl
import IPY_GameWorld
import ChPyNetSendPack
import IpyGameDataPY
import NetPackCommon
import ItemCommon
import ChConfig
 
import time
 
def OnPlayerLogin(curPlayer):
    SyncChatBoxInfo(curPlayer)
    SyncEmojiPackInfo(curPlayer)
    return
 
def OnMinute(curPlayer):
    curTime = int(time.time())
    delBoxList = []
    ipyDataMgr = IpyGameDataPY.IPY_Data()
    for index in range(ipyDataMgr.GetChatBubbleBoxCount()):
        ipyData = ipyDataMgr.GetChatBubbleBoxByIndex(index)
        boxID = ipyData.GetBoxID()
        if ipyData.GetUnlockDefault():
            # Ä¬Èϼ¤»îµÄ²»´¦Àí
            continue
        if not GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ChatBubbleBoxState, boxID):
            # Î´¼¤»îµÄ²»´¦Àí
            continue
        endTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ChatBoxEndTime % boxID)
        if not endTime or endTime > curTime:
            # ÓÀ¾Ã»òδ¹ýÆÚ
            continue
        if DelChatBox(curPlayer, boxID, False, "ChatBoxTimeout"):
            delBoxList.append(boxID)
            
    if delBoxList:
        RefreshBoxAttr(curPlayer)
        
    # ±íÇé°ü
    for index in range(ipyDataMgr.GetEmojiPackCount()):
        ipyData = ipyDataMgr.GetEmojiPackByIndex(index)
        packID = ipyData.GetEmojiPackID()
        if ipyData.GetUnlockDefault():
            # Ä¬Èϼ¤»îµÄ²»´¦Àí
            continue
        if not GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_EmojiPackState, packID):
            # Î´¼¤»îµÄ²»´¦Àí
            continue
        endTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_EmojiPackEndTime % packID)
        if not endTime or endTime > curTime:
            # ÓÀ¾Ã»òδ¹ýÆÚ
            continue
        DelEmojiPack(curPlayer, packID, "EmojiPackTimeout")
        
    return
 
def AddChatBox(curPlayer, boxID, setExpireTimes=None):
    if boxID <= 0:
        return
    playerID = curPlayer.GetPlayerID()
    ipyData = IpyGameDataPY.GetIpyGameData("ChatBubbleBox", boxID)
    if not ipyData:
        return
    if ipyData.GetUnlockDefault():
        GameWorld.DebugLog("ÆøÅÝ¿òĬÈϽâËøµÄ²»ÓÃÌí¼Ó: boxID=%s" % (boxID), playerID)
        return
    ipyExpireSeconds = ipyData.GetExpireMinutes() * 60
    
    curTime = int(time.time())
    state = GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ChatBubbleBoxState, boxID)
    endTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ChatBoxEndTime % boxID)
    GameWorld.Log("Ìí¼ÓÆøÅÝ¿ò: boxID=%s,setExpireTimes=%s,state=%s,endTime=%s,ipyExpireSeconds=%s,curTime=%s" 
                  % (boxID, setExpireTimes, state, endTime, ipyExpireSeconds, curTime), playerID)
    updEndTime = endTime
    
    # Ö¸¶¨Ê±³¤µÄ£¬ÈçGMÖ¸¶¨
    if setExpireTimes >= 0:
        updEndTime = curTime + setExpireTimes
        GameWorld.Log("    Ö¸¶¨ÆøÅÝ¿òʱ³¤: boxID=%s,updEndTime=%s" % (boxID, updEndTime), playerID)
        
    # ÓÀ¾Ã
    elif ipyExpireSeconds == 0:
        updEndTime = 0
        GameWorld.Log("    Ìí¼ÓÓÀ¾ÃÆøÅÝ¿ò: boxID=%s,updEndTime=%s" % (boxID, updEndTime), playerID)
        
    else:
        # Î´¹ýÆÚ
        if endTime > curTime:
            updEndTime = endTime + ipyExpireSeconds
            GameWorld.Log("    ÀÛ¼ÓÆøÅÝ¿òʱ³¤: boxID=%s,updEndTime=%s" % (boxID, updEndTime), playerID)
        else:
            updEndTime = curTime + ipyExpireSeconds
            GameWorld.Log("    ÖØ줻îÆøÅÝ¿ò: boxID=%s,updEndTime=%s" % (boxID, updEndTime), playerID)
            
    GameWorld.SetDictValueByBit(curPlayer, ChConfig.Def_PDict_ChatBubbleBoxState, boxID, 1)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ChatBoxEndTime % boxID, updEndTime)
    RefreshBoxAttr(curPlayer)
    SyncChatBoxInfo(curPlayer, [boxID])
    return True
 
def DelChatBox(curPlayer, boxID, isRefreshAttr=True, notifyMail=""):
    playerID = curPlayer.GetPlayerID()
    ipyData = IpyGameDataPY.GetIpyGameData("ChatBubbleBox", boxID)
    if not ipyData:
        return
    if ipyData.GetUnlockDefault():
        return
    GameWorld.Log("ɾ³ýÍæ¼ÒÁÄÌìÆøÅÝ¿ò: boxID=%s,notifyMail=%s" % (boxID, notifyMail), playerID)
    GameWorld.SetDictValueByBit(curPlayer, ChConfig.Def_PDict_ChatBubbleBoxState, boxID, 0)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ChatBoxEndTime % boxID, 0)
    # ÐǼ¶²»ÖØÖã¬ÖØ줻îºóÔÙ´ÎÉúЧ
    
    if PlayerControl.GetChatBubbleBox(curPlayer) == boxID:
        canUseID = GetCanUseBoxID(curPlayer)
        PlayerControl.SetChatBubbleBox(curPlayer, canUseID)
        GameWorld.DebugLog("Íæ¼ÒÁÄÌìÆøÅÝ¿ò±»É¾³ý£¬ÖØÐÂÉèÖÿÉÓÃID=%s" % canUseID, playerID)
        
    if isRefreshAttr:
        RefreshBoxAttr(curPlayer)
    SyncChatBoxInfo(curPlayer, [boxID])
    if notifyMail:
        PlayerControl.SendMailByKey(notifyMail, [playerID], [], [boxID])
    return True
 
def GetCanUseBoxID(curPlayer):
    ## »ñÈ¡Ò»¸ö¿ÉÓõÄÆøÅÝ¿ò
    ipyDataMgr = IpyGameDataPY.IPY_Data()
    for index in range(ipyDataMgr.GetChatBubbleBoxCount())[::-1]:
        ipyData = ipyDataMgr.GetChatBubbleBoxByIndex(index)
        boxID = ipyData.GetBoxID()
        if IsBoxCanUse(curPlayer, boxID, ipyData):
            return boxID
    return 0
 
def IsBoxCanUse(curPlayer, boxID, ipyData=None):
    ## ÆøÅÝ¿òÊÇ·ñ¿ÉÓÃ
    state = GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ChatBubbleBoxState, boxID)
    if state:
        return True
    
    if not ipyData:
        ipyData = IpyGameDataPY.GetIpyGameData("ChatBubbleBox", boxID)
    if ipyData:
        if ipyData.GetUnlockDefault():
            return True
        
        needLV = ipyData.GetNeedLV()
        if needLV and curPlayer.GetLV() < needLV:
            #GameWorld.DebugLog("    µÈ¼¶²»×㣬ÎÞ·¨Ê¹ÓøÃÆøÅÝ¿ò£¡needLV=%s" % needLV, curPlayer.GetPlayerID())
            return False
        
        needVIPLVGift = ipyData.GetNeedVIPLVGift() # ¼æÈÝÒѹºÂòVIPÀñ°üµÄÀϺÅÓÃ
        if needVIPLVGift:
            record = curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_VipAwardRecord)
            hasBuy = record & pow(2, needVIPLVGift)
            if not hasBuy:
                #GameWorld.DebugLog("    Î´¹ºÂòVIPÀñ°ü£¬ÎÞ·¨Ê¹ÓøÃÆøÅÝ¿ò£¡needVIPLVGift=%s" % needVIPLVGift, curPlayer.GetPlayerID())
                return False
            
        if needLV or needVIPLVGift:
            return True
        
    return False
 
def RefreshBoxAttr(curPlayer):
    CalcBoxAttr(curPlayer)
    PlayerControl.PlayerControl(curPlayer).RefreshPlayerAttrState()
    return
 
def CalcBoxAttr(curPlayer):
    
    fightPowerEx = 0
    allAttrList = [{} for i in range(4)]
    
    ipyDataMgr = IpyGameDataPY.IPY_Data()
    for index in range(ipyDataMgr.GetChatBubbleBoxCount()):
        ipyData = ipyDataMgr.GetChatBubbleBoxByIndex(index)
        boxID = ipyData.GetBoxID()
        if not IsBoxCanUse(curPlayer, boxID, ipyData):
            continue
        
        fightPowerEx += ipyData.GetLightFightPower()
        # ¼¤»îÊôÐÔ
        lightAttrType = ipyData.GetLightAttrType()
        lightAttrValue = ipyData.GetLightAttrValue()
        for i, attrID in enumerate(lightAttrType):
            attrValue = lightAttrValue[i]
            PlayerControl.CalcAttrDict_Type(attrID, attrValue, allAttrList)
        
        # ÐǼ¶ÊôÐÔ
        boxStar = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ChatBoxStar % boxID)
        starIpyData = IpyGameDataPY.GetIpyGameDataNotLog("ChatBubbleBoxStar", boxID, boxStar)
        if starIpyData:
            starAttrType = starIpyData.GetStarAttrType()
            starAttrValue = starIpyData.GetStarAttrValue()
            for i, attrID in enumerate(starAttrType):
                attrValue = starAttrValue[i]
                PlayerControl.CalcAttrDict_Type(attrID, attrValue, allAttrList)
                
    # ±£´æ¼ÆËãÖµ
    PlayerControl.SetCalcAttrListValue(curPlayer, ChConfig.Def_CalcAttrFunc_ChatBox, allAttrList)
    curPlayer.SetDict(ChConfig.Def_PlayerKey_MFPEx % ShareDefine.Def_MFPType_ChatBox, fightPowerEx)
    return
 
#// A2 30 ÉèÖÃÁÄÌìÆøÅÝ¿ò #tagCMSetChatBubbleBox
#
#struct    tagCMSetChatBubbleBox
#{
#    tagHead         Head;
#    BYTE        BubbleBoxType;    //ÆøÅÝ¿òÀàÐÍ
#};
def OnSetChatBubbleBox(index, clientData, tick):
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)
    useBoxID = clientData.BubbleBoxType
    playerID = curPlayer.GetPlayerID()
    if not IsBoxCanUse(curPlayer, useBoxID):
        GameWorld.DebugLog("ÁÄÌìÆøÅÝ¿ò²»¿ÉÓã¬ÎÞ·¨ÉèÖÃ! useBoxID=%s" % useBoxID, playerID)    
        return
    GameWorld.DebugLog("ÉèÖÃʹÓõÄÁÄÌìÆøÅÝ¿òID: useBoxID=%s" % useBoxID, playerID)    
    PlayerControl.SetChatBubbleBox(curPlayer, useBoxID)
    return
 
#// A2 36 ÁÄÌìÆøÅÝ¿òÉýÐÇ #tagCMChatBubbleBoxStarUP
#
#struct    tagCMChatBubbleBoxStarUP
#{
#    tagHead         Head;
#    WORD        BoxID;    //ÆøÅÝID
#};
def OnChatBubbleBoxStarUP(index, clientData, tick):
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)
    playerID = curPlayer.GetPlayerID()
    boxID = clientData.BoxID
    
    if not IsBoxCanUse(curPlayer, boxID):
        GameWorld.DebugLog("ÁÄÌìÆøÅÝ¿ò²»¿ÉÓÃ! boxID=%s" % (boxID), playerID)
        return
    
    curStar = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ChatBoxStar % boxID)        
    nextStar = curStar + 1
    nextIpyData = IpyGameDataPY.GetIpyGameData("ChatBubbleBoxStar", boxID, nextStar)
    if not nextIpyData:
        GameWorld.DebugLog("²»´æÔÚ¸ÃÆøÅÝ¿òÐǼ¶! boxID=%s,nextStar=%s" % (boxID, nextStar), playerID)
        return
    needItemList = nextIpyData.GetStarUpNeedItemList()
    if not needItemList:
        return
    itemPack = curPlayer.GetItemManager().GetPack(IPY_GameWorld.rptItem)
    lackItemDict, delInfoDict = ItemCommon.GetCostItemIndexList(needItemList, itemPack)
    if lackItemDict:
        GameWorld.DebugLog("ÆøÅÝ¿òÉýÐÇËùÐèÎïÆ·²»×ã! boxID=%s,nextStar=%s,needItemList=%s,lackItemDict=%s" 
                           % (boxID, nextStar, needItemList, lackItemDict), playerID)
        return
    ItemCommon.DelCostItem(curPlayer, itemPack, delInfoDict, "ChatBoxStarUP")
    
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ChatBoxStar % boxID, nextStar)
    
    GameWorld.DebugLog("ÆøÅÝ¿òÉýÐÇ! boxID=%s,curStar=%s,nextStar=%s" % (boxID, curStar, nextStar), playerID)
    
    RefreshBoxAttr(curPlayer)
    SyncChatBoxInfo(curPlayer, [boxID])
    return
 
def SyncChatBoxInfo(curPlayer, boxIDList=None):
    if boxIDList == None:
        syncIDList = []
        ipyDataMgr = IpyGameDataPY.IPY_Data()
        for index in range(ipyDataMgr.GetChatBubbleBoxCount()):
            ipyData = ipyDataMgr.GetChatBubbleBoxByIndex(index)
            syncIDList.append(ipyData.GetBoxID())
    else:
        syncIDList = boxIDList
        
    boxList = []
    for boxID in syncIDList:
        state = GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ChatBubbleBoxState, boxID)
        if not state and boxIDList == None:
            continue
        box = ChPyNetSendPack.tagMCChatBubbleBox()
        box.BoxID = boxID
        box.State = state
        box.EndTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ChatBoxEndTime % boxID)
        box.Star = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ChatBoxStar % boxID)
        boxList.append(box)
        
    if not boxList:
        return
    
    clientPack = ChPyNetSendPack.tagMCChatBubbleBoxState()
    clientPack.BoxList = boxList
    clientPack.Count = len(clientPack.BoxList)
    NetPackCommon.SendFakePack(curPlayer, clientPack)
    return
 
##-------------------------------------------------------------------------------------------------
 
def AddEmojiPack(curPlayer, packID, setExpireTimes=None):
    if packID <= 0:
        return
    playerID = curPlayer.GetPlayerID()
    ipyData = IpyGameDataPY.GetIpyGameData("EmojiPack", packID)
    if not ipyData:
        return
    if ipyData.GetUnlockDefault():
        GameWorld.DebugLog("±íÇé°üĬÈϽâËøµÄ²»ÓÃÌí¼Ó: packID=%s" % (packID), playerID)
        return
    ipyExpireSeconds = ipyData.GetExpireMinutes() * 60
    
    curTime = int(time.time())
    state = GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_EmojiPackState, packID)
    endTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_EmojiPackEndTime % packID)
    GameWorld.Log("Ìí¼Ó±íÇé°ü: packID=%s,setExpireTimes=%s,state=%s,endTime=%s,ipyExpireSeconds=%s,curTime=%s" 
                  % (packID, setExpireTimes, state, endTime, ipyExpireSeconds, curTime), playerID)
    updEndTime = endTime
    
    # Ö¸¶¨Ê±³¤µÄ£¬ÈçGMÖ¸¶¨
    if setExpireTimes >= 0:
        updEndTime = curTime + setExpireTimes
        GameWorld.Log("    Ö¸¶¨±íÇé°üʱ³¤: packID=%s,updEndTime=%s" % (packID, updEndTime), playerID)
        
    # ÓÀ¾Ã
    elif ipyExpireSeconds == 0:
        updEndTime = 0
        GameWorld.Log("    Ìí¼ÓÓÀ¾Ã±íÇé°ü: packID=%s,updEndTime=%s" % (packID, updEndTime), playerID)
        
    else:
        # Î´¹ýÆÚ
        if endTime > curTime:
            updEndTime = endTime + ipyExpireSeconds
            GameWorld.Log("    ÀÛ¼Ó±íÇé°üʱ³¤: packID=%s,updEndTime=%s" % (packID, updEndTime), playerID)
        else:
            updEndTime = curTime + ipyExpireSeconds
            GameWorld.Log("    ÖØ줻î±íÇé°ü: packID=%s,updEndTime=%s" % (packID, updEndTime), playerID)
            
    GameWorld.SetDictValueByBit(curPlayer, ChConfig.Def_PDict_EmojiPackState, packID, 1)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_EmojiPackEndTime % packID, updEndTime)
    
    SyncEmojiPackInfo(curPlayer, [packID])
    return True
 
def DelEmojiPack(curPlayer, packID, notifyMail=""):
    playerID = curPlayer.GetPlayerID()
    ipyData = IpyGameDataPY.GetIpyGameData("EmojiPack", packID)
    if not ipyData:
        return
    if ipyData.GetUnlockDefault():
        return
    GameWorld.Log("ɾ³ýÍæ¼ÒÁÄÌì±íÇé°ü: packID=%s,notifyMail=%s" % (packID, notifyMail), playerID)
    GameWorld.SetDictValueByBit(curPlayer, ChConfig.Def_PDict_EmojiPackState, packID, 0)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_EmojiPackEndTime % packID, 0)
    
    SyncEmojiPackInfo(curPlayer, [packID])
    if notifyMail:
        PlayerControl.SendMailByKey(notifyMail, [playerID], [], [packID])
    return True
 
def SyncEmojiPackInfo(curPlayer, packIDList=None):
    if packIDList == None:
        syncIDList = []
        ipyDataMgr = IpyGameDataPY.IPY_Data()
        for index in range(ipyDataMgr.GetEmojiPackCount()):
            ipyData = ipyDataMgr.GetEmojiPackByIndex(index)
            syncIDList.append(ipyData.GetEmojiPackID())
    else:
        syncIDList = packIDList
        
    packList = []
    for packID in syncIDList:
        state = GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_EmojiPackState, packID)
        if not state and packIDList == None:
            continue
        pack = ChPyNetSendPack.tagMCEmojiPack()
        pack.PackID = packID
        pack.State = state
        pack.EndTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_EmojiPackEndTime % packID)
        packList.append(pack)
        
    if not packList:
        return
    
    clientPack = ChPyNetSendPack.tagMCEmojiPackInfo()
    clientPack.EmojiPackList = packList
    clientPack.Count = len(clientPack.EmojiPackList)
    NetPackCommon.SendFakePack(curPlayer, clientPack)
    return