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
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package Player.PlayerActSingleRecharge
#
# @todo:µ¥±ÊÀÛ³ä»î¶¯
# @author hxp
# @date 2021-04-07
# @version 1.0
#
# ÏêϸÃèÊö: µ¥±ÊÀÛ³ä»î¶¯
#
#-------------------------------------------------------------------------------
#"""Version = 2021-04-07 17:30"""
#-------------------------------------------------------------------------------
 
import PyGameData
import ShareDefine
import PlayerControl
import IpyGameDataPY
import ItemControler
import ChPyNetSendPack
import IPY_GameWorld
import NetPackCommon
import GameWorld
import ChConfig
import CommFunc
 
def GetTemplateID(cfgID, dayIndex):
    if not cfgID or dayIndex == None:
        return 0
    ipyData = IpyGameDataPY.GetIpyGameData("ActSingleRecharge", cfgID)   
    if not ipyData:
        return 0
    templateIDList = ipyData.GetTemplateIDList()
    templateID = templateIDList[-1] if dayIndex >= len(templateIDList) else templateIDList[dayIndex]
    return templateID
 
def OnPlayerLogin(curPlayer):
    
    for actInfo in PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_SingleRecharge, {}).values():
        actNum = actInfo.get(ShareDefine.ActKey_ActNum, 0)
        isReset = __CheckPlayerSingleRechargeAction(curPlayer, actNum)
        if not isReset:
            # »î¶¯ÖÐͬ²½»î¶¯ÐÅÏ¢
            if actInfo.get(ShareDefine.ActKey_State):
                Sync_SingleRechargeActionInfo(curPlayer, actNum)
                Sync_SingleRechargePlayerInfo(curPlayer, actNum)
                
    return
 
def RefreshSingleRechargeActionInfo(actNum):
    ## ÊÕµ½GameServerͬ²½µÄ»î¶¯ÐÅÏ¢£¬Ë¢Ð»ÐÅÏ¢
    playerManager = GameWorld.GetPlayerManager()
    for index in xrange(playerManager.GetPlayerCount()):
        curPlayer = playerManager.GetPlayerByIndex(index)
        if curPlayer.GetID() == 0:
            continue
        __CheckPlayerSingleRechargeAction(curPlayer, actNum)
    return
 
def __CheckPlayerSingleRechargeAction(curPlayer, actNum):
    ## ¼ì²éÍæ¼Ò»î¶¯Êý¾ÝÐÅÏ¢
    
    playerID = curPlayer.GetPlayerID()
    actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_SingleRecharge, actNum)
    actID = actInfo.get(ShareDefine.ActKey_ID, 0)
    state = actInfo.get(ShareDefine.ActKey_State, 0)
    
    playerActID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_SingleRechargeID % actNum) # Íæ¼ÒÉíÉϵĻID
    playerTemplateID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_SingleRechargeTemplateID % actNum)
    
    templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))
    
    # »î¶¯ID ÏàͬµÄ»°²»´¦Àí
    if actID == playerActID:
        GameWorld.DebugLog("µ¥±ÊÀÛ³ä»î¶¯ID²»±ä£¬²»´¦Àí£¡actNum=%s,actID=%s" % (actNum, actID), playerID)
        if state and templateID and templateID != playerTemplateID:
            PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_SingleRechargeTemplateID % actNum, templateID)
            Sync_SingleRechargeActionInfo(curPlayer, actNum)
            Sync_SingleRechargePlayerInfo(curPlayer, actNum)
            GameWorld.DebugLog("    »î¶¯ÖиüÐÂÄ£°åID: templateID=%s" % templateID, playerID)
            
        return
    
    actWorldLV = actInfo.get(ShareDefine.ActKey_WorldLV, 0)
    playerWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_SingleRechargeWorldLV % actNum)
    
    GameWorld.DebugLog("µ¥±ÊÀÛ³äÖØÖÃ! actNum=%s,actID=%s,playerActID=%s,state=%s,templateID=%s,playerTemplateID=%s" 
                       % (actNum, actID, playerActID, state, templateID, playerTemplateID), playerID)
    
    # Î´ÁìÈ¡µÄ½±ÀøÓʼþ·¢·Å
    __SendSingleRechargeMail(curPlayer, playerTemplateID, playerWorldLV, actNum)
    
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_SingleRechargeID % actNum, actID)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_SingleRechargeTemplateID % actNum, templateID)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_SingleRechargeWorldLV % actNum, actWorldLV)
    if actID and templateID:
        ipyDataList = IpyGameDataPY.GetIpyGameDataList("ActSingleRechargeAward", templateID)
        if ipyDataList:
            for ipyData in ipyDataList:
                awardIndex = ipyData.GetAwardIndex()
                PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_SingleRechargeValue % (actNum, awardIndex), 0)
                
    Sync_SingleRechargeActionInfo(curPlayer, actNum)
    Sync_SingleRechargePlayerInfo(curPlayer, actNum)
    return True
 
def GetAwardCountInfo(curPlayer, actNum, awardIndex):
    valueInfo = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_SingleRechargeValue % (actNum, awardIndex))
    getCount = valueInfo / 1000
    canGetCount = valueInfo % 1000
    return getCount, canGetCount
def SetAwardCountInfo(curPlayer, actNum, awardIndex, getCount, canGetCount):
    valueInfo = getCount * 1000 + canGetCount
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_SingleRechargeValue % (actNum, awardIndex), valueInfo)
    return valueInfo
 
def __SendSingleRechargeMail(curPlayer, playerTemplateID, playerWorldLV, actNum):
    # Î´ÁìÈ¡µÄ½±ÀøÓʼþ·¢·Å
    
    if not playerTemplateID:
        return
    
    ipyDataList = IpyGameDataPY.GetIpyGameDataList("ActSingleRechargeAward", playerTemplateID)
    if not ipyDataList:
        return
    
    playerID = curPlayer.GetPlayerID()
    batchPlayerIDList, batchAddItemList, batchParamList = [], [], []
    
    for ipyData in ipyDataList:
        awardIndex = ipyData.GetAwardIndex()
        getCount, canGetCount = GetAwardCountInfo(curPlayer, actNum, awardIndex)
        if getCount >= canGetCount:
            #GameWorld.DebugLog("µ¥±ÊÀÛ³äÒѾ­È«²¿Áì½±£¡awardIndex=%s" % awardIndex)
            continue
        SetAwardCountInfo(curPlayer, actNum, awardIndex, canGetCount, canGetCount)
        
        unGetCount = canGetCount - getCount
        singleValue = ipyData.GetSingleRechargeValue()
        awardItemList = GameWorld.GetDictValueByRangeKey(ipyData.GetAwardItem(), playerWorldLV, [])
        for _ in range(unGetCount):
            batchPlayerIDList.append([playerID])
            batchAddItemList.append(awardItemList)
            batchParamList.append([singleValue])
            
    if batchPlayerIDList:
        PlayerControl.SendMailBatch("SingleRechargeMail%s" % actNum, batchPlayerIDList, batchAddItemList, batchParamList)
        
    return
 
def UpdSingleRechargeValue(curPlayer, curRechargeValue, coinType):
    if curRechargeValue <= 0:
        return
    
    for actInfo in PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_SingleRecharge, {}).values():
        actNum = actInfo.get(ShareDefine.ActKey_ActNum, 0)
        if not actInfo.get(ShareDefine.ActKey_State):
            GameWorld.DebugLog("µ¥±ÊÀÛ³ä»î¶¯µ±Ç°Î´¿ªÆô! actNum=%s" % actNum)
            continue
        
        cfgID = actInfo.get(ShareDefine.ActKey_CfgID, 0)
        if not cfgID:
            continue
        
        ipyData = IpyGameDataPY.GetIpyGameData("ActSingleRecharge", cfgID)
        if not ipyData:
            continue
        
        ctgTypeEffValue = ipyData.GetCTGTypeEffValue()
        if not ctgTypeEffValue & pow(2, coinType):
            GameWorld.DebugLog("µ¥±ÊÀÛ³ä³äÖµ»î¶¯£¬³äÖµÀàÐͶԸûÎÞЧ! actNum=%s,coinType=%s,ctgTypeEffValue=%s" 
                               % (actNum, coinType, ctgTypeEffValue), curPlayer.GetPlayerID())
            continue
        
        templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))
        if not templateID:
            GameWorld.ErrLog("µ¥±ÊÀÛ³ä³äÖµ»î¶¯Êý¾ÝÒì³£!cfgID=%s,templateID=%s" % (cfgID, templateID), curPlayer.GetPlayerID())
            continue
        
        awardIpyDataList = IpyGameDataPY.GetIpyGameDataList("ActSingleRechargeAward", templateID)
        if not awardIpyDataList:
            continue
        
        awardIndexList = []
        awardRuleType = ipyData.GetAwardRuleType()
        GameWorld.DebugLog("µ¥±ÊÀÛ³ä³äÖµ»î¶¯£¬actNum=%s,awardRuleType=%s,curRechargeValue=%s" % (actNum, awardRuleType, curRechargeValue))
        # ÏòϼæÈÝËùÓеµ´Î
        if awardRuleType == 0:
            pass
        # Æ¥Åä×î¸ßµµ´Î
        elif awardRuleType == 1:
            pass        
        # ¾«È·Æ¥Åä¶ÔÓ¦µµ´Î
        elif awardRuleType == 2:
            for awardIpyData in awardIpyDataList:
                awardIndex = awardIpyData.GetAwardIndex()
                singleValue = CommFunc.RMBToCoin(awardIpyData.GetSingleRechargeValue())
                if curRechargeValue != singleValue:
                    #GameWorld.DebugLog("    ²»µÈÓÚµµ´Î¶î¶È£¬²»´¦Àí£¡awardRuleType=%s,awardIndex=%s,singleValue=%s" 
                    #                   % (awardRuleType, awardIndex, singleValue))
                    continue
                awardCountMax = awardIpyData.GetAwardCountMax()
                getCount, canGetCount = GetAwardCountInfo(curPlayer, actNum, awardIndex)
                if canGetCount >= awardCountMax:
                    GameWorld.DebugLog("    ÒÑ´ï×î´ó¿ÉÁì½±´ÎÊý£¬²»´¦Àí£¡awardRuleType=%s,awardIndex=%s,awardCountMax=%s" % (awardRuleType, awardIndex, awardCountMax))
                    break
                
                awardIndexList.append(awardIndex)
                canGetCount += 1
                SetAwardCountInfo(curPlayer, actNum, awardIndex, getCount, canGetCount)
                GameWorld.DebugLog("    ÉèÖþ«È·µµ´Î¿ÉÁìÈ¡£¡awardRuleType=%s,awardIndex=%s,singleValue=%s,canGetCount=%s" 
                                   % (awardRuleType, awardIndex, singleValue, canGetCount))
                break
            
        else:
            continue
        
        if not awardIndexList:
            continue
        
        Sync_SingleRechargePlayerInfo(curPlayer, actNum, awardIndexList)
        
        # ÏßÏ»£¬¼ì²é·¢·Å½±Àø
        if ipyData.GetIsOfflineAct():
            playerWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_SingleRechargeWorldLV % actNum)
            __SendSingleRechargeMail(curPlayer, templateID, playerWorldLV, actNum)
            
    return
 
def OnGetSingleRechargeAward(curPlayer, awardIndex, actNum):
    '''ÁìÈ¡½±Àø
    @param awardIndex: ½±ÀøË÷Òý
    @param actNum: »î¶¯±àºÅ£¬Èç11 »ò  12 ´ú±í²»Í¬µÄ»î¶¯
    '''
    actNum = GameWorld.ToIntDef(actNum, 0)
    if actNum <= 0:
        GameWorld.DebugLog("ûÓÐÖ¸¶¨ÁìÈ¡µÄ»î¶¯±àºÅ! actNum=%s" % actNum)
        return
    
    playerID = curPlayer.GetPlayerID()
    actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_SingleRecharge, actNum)
    
    state = actInfo.get(ShareDefine.ActKey_State, 0)
    templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))
    if not state or not templateID:
        GameWorld.DebugLog("¸Ãµ¥±ÊÀÛ³ä»î¶¯·Ç»î¶¯ÖУ¬ÎÞ·¨Áì½±£¡actNum=%s,state=%s,templateID=%s" % (actNum, state, templateID), playerID)
        return
    
    ipyDataList = IpyGameDataPY.GetIpyGameDataList("ActSingleRechargeAward", templateID)
    if not ipyDataList:
        return
    
    awardIpyData = None
    for ipyData in ipyDataList:
        if ipyData.GetAwardIndex() == awardIndex:
            awardIpyData = ipyData
            break
        
    if not awardIpyData:
        GameWorld.DebugLog("ÕÒ²»µ½¸Ãµ¥±ÊÀÛ³ä»î¶¯µµÎ»Ë÷Òý½±Àø!actNum=%s,templateID=%s,awardIndex=%s" % (actNum, templateID, awardIndex), playerID)
        return
    
    getCount, canGetCount = GetAwardCountInfo(curPlayer, actNum, awardIndex)
    if getCount >= canGetCount:
        GameWorld.DebugLog("¸Ãµ¥±ÊÀÛ³ä»î¶¯½±ÀøÒÑ´ï×î´ó¿ÉÁì½±´ÎÊý! actNum=%s,awardIndex=%s,getCount(%s) >= canGetCount(%s)" % (actNum, awardIndex, getCount, canGetCount), playerID)
        return
    
    singleValue = awardIpyData.GetSingleRechargeValue()
    
    actWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_SingleRechargeWorldLV % actNum)
    awardItemList = GameWorld.GetDictValueByRangeKey(ipyData.GetAwardItem(), actWorldLV, [])
    
    if not ItemControler.CheckPackSpaceEnough(curPlayer, awardItemList):
        return
    
    getCount += 1
    SetAwardCountInfo(curPlayer, actNum, awardIndex, getCount, canGetCount)
    Sync_SingleRechargePlayerInfo(curPlayer, actNum, [awardIndex])
    
    GameWorld.DebugLog("    ÁìÈ¡³É¹¦! actNum=%s,templateID=%s,awardIndex=%s,singleValue=%s,getCount=%s,canGetCount=%s" 
                       % (actNum, templateID, awardIndex, singleValue, getCount, canGetCount))
    
    notifyKey = awardIpyData.GetNotifyKey()
    if notifyKey:
        PlayerControl.WorldNotify(0, notifyKey, [curPlayer.GetPlayerName(), str(awardIpyData.GetSingleRechargeValue())])
        
    for itemID, itemCount, isAuctionItem in awardItemList:
        ItemControler.GivePlayerItem(curPlayer, itemID, itemCount, isAuctionItem, [IPY_GameWorld.rptItem], event=["SingleRechargeAward", False, {}])
        
    return
 
def Sync_SingleRechargePlayerInfo(curPlayer, actNum, awardIndexList=None):
    ## Í¨ÖªÍæ¼ÒÊý¾ÝÐÅÏ¢
    
    actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_SingleRecharge, actNum)
    if not actInfo.get(ShareDefine.ActKey_State):
        return
    
    cfgID = actInfo.get(ShareDefine.ActKey_CfgID)
    if not cfgID:
        return
    
    ipyData = IpyGameDataPY.GetIpyGameData("ActSingleRecharge", cfgID)
    if not ipyData:
        return
    
    if ipyData.GetIsOfflineAct():
        #ÏßÏ»²»Ï·¢
        return
    
    if awardIndexList == None:
        awardIndexList = []
        templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))
        if not templateID:
            return
        ipyDataList = IpyGameDataPY.GetIpyGameDataList("ActSingleRechargeAward", templateID)
        if not ipyDataList:
            return
        for awardIpyData in ipyDataList:
            awardIndexList.append(awardIpyData.GetAwardIndex())
            
    if not awardIndexList:
        return
    
    playerActInfo = ChPyNetSendPack.tagMCActSingleRechargePlayerInfo()
    playerActInfo.ActNum = actNum
    playerActInfo.AwardRecordList = []
    for awardIndex in awardIndexList:
        getCount, canGetCount = GetAwardCountInfo(curPlayer, actNum, awardIndex)
        awardRecord = ChPyNetSendPack.tagMCActSingleRechargePlayerAward()
        awardRecord.Clear()
        awardRecord.AwardIndex = awardIndex
        awardRecord.CanGetCount = canGetCount
        awardRecord.GetCount = getCount
        playerActInfo.AwardRecordList.append(awardRecord)
    playerActInfo.RecordCount = len(playerActInfo.AwardRecordList)
    NetPackCommon.SendFakePack(curPlayer, playerActInfo)
    return
 
def Sync_SingleRechargeActionInfo(curPlayer, actNum):
    ## Í¨Öª»î¶¯ÐÅÏ¢
    
    actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_SingleRecharge, actNum)
    if not actInfo.get(ShareDefine.ActKey_State):
        return
    
    cfgID = actInfo.get(ShareDefine.ActKey_CfgID)
    if not cfgID:
        return
    
    ipyData = IpyGameDataPY.GetIpyGameData("ActSingleRecharge", cfgID)
    if not ipyData:
        return
    
    if ipyData.GetIsOfflineAct():
        #ÏßÏ»²»Ï·¢
        return
    
    templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))
    if not templateID:
        return
    
    actWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_SingleRechargeWorldLV % actNum)
    startDateStr, endDateStr = GameWorld.GetOperationActionDateStr(ipyData)
    
    clientPack = ChPyNetSendPack.tagMCActSingleRechargeInfo()
    clientPack.ActNum = actNum
    clientPack.StartDate = startDateStr
    clientPack.EndtDate = endDateStr
    clientPack.IsDayReset = ipyData.GetIsDayReset()
    clientPack.LimitLV = ipyData.GetLVLimit()
    clientPack.AwardRuleType = ipyData.GetAwardRuleType()
    clientPack.AwardInfo = []
    
    ipyDataList = IpyGameDataPY.GetIpyGameDataList("ActSingleRechargeAward", templateID)
    if ipyDataList:
        for awardIpyData in ipyDataList:
            awardInfo = ChPyNetSendPack.tagMCActSingleRechargeAward()
            awardInfo.AwardIndex = awardIpyData.GetAwardIndex()
            awardInfo.AwardCountMax = awardIpyData.GetAwardCountMax()
            awardInfo.SingleRechargeValue = CommFunc.RMBToCoin(awardIpyData.GetSingleRechargeValue())
            awardInfo.AwardItem = []
            awardItemList = GameWorld.GetDictValueByRangeKey(awardIpyData.GetAwardItem(), actWorldLV, [])
            for itemID, itemCount, isBind in awardItemList:
                awardItem = ChPyNetSendPack.tagMCActSingleRechargeAwardItem()
                awardItem.ItemID = itemID
                awardItem.ItemCount = itemCount
                awardItem.IsBind = isBind
                awardInfo.AwardItem.append(awardItem)
            awardInfo.AwardItemCount = len(awardInfo.AwardItem)
            
            clientPack.AwardInfo.append(awardInfo)
            
    clientPack.AwardCount = len(clientPack.AwardInfo)
    NetPackCommon.SendFakePack(curPlayer, clientPack)
    return