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
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package Player.PlayerZhanling
#
# @todo:Õ½Áî
# @author hxp
# @date 2023-12-05
# @version 1.0
#
# ÏêϸÃèÊö: Õ½Áî
#
#-------------------------------------------------------------------------------
#"""Version = 2023-12-05 15:30"""
#-------------------------------------------------------------------------------
 
import GameWorld
import ShareDefine
import NetPackCommon
import IpyGameDataPY
import PlayerControl
import ChPyNetSendPack
import PlayerGatherTheSoul
import PlayerActHorsePetTrain
import PlayerActGubao
import ItemControler
import IPY_GameWorld
import PlayerGubao
import ChConfig
import time
 
# Õ½ÁîÀàÐÍ
ZhanlingTypeList = (
ZhanlingType_LV, # µÈ¼¶ 1
ZhanlingType_Realm, # ¾³½ç 2
ZhanlingType_SkyTower, # ÌìÐÇËþ 3
ZhanlingType_GubaoStar, # ¹Å±¦×ÜÐÇÊý 4
ZhanlingType_Login, # µÇ¼ 5
ZhanlingType_GatherTheSoulLV, # ¾Û»ê 6
ZhanlingType_HorsePetTrain, # Æï³èÑø³É 7
ZhanlingType_GubaoTrain, # ¹Å±¦Ñø³É 8
ZhanlingType_Xianyuan, # ÏÉÔµ 9
ZhanlingType_Huanjingge, # »Ã¾³¸ó 10
) = range(1, 1 + 10)
 
def OnPlayerLogin(curPlayer):
    for zhanlingType in ZhanlingTypeList:
        if zhanlingType == ZhanlingType_Login:
            value1 = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingValue1 % zhanlingType)
            if not value1:
                firstLoginTime = int(time.time())
                PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingValue1 % zhanlingType, firstLoginTime)
                GameWorld.DebugLog("ÉèÖõǼսÁîÊ״εǼʱ¼ä: %s" % firstLoginTime, curPlayer.GetPlayerID())
        SyncZhanlingInfo(curPlayer, zhanlingType)
    return
 
def OnActiviteByCTGID(curPlayer, ctgID):
    zhanlingCTGIDDict = IpyGameDataPY.GetFuncEvalCfg("Zhanling", 1)
    for zhanlingTypeStr, ctgIDList in zhanlingCTGIDDict.items():
        if ctgID not in ctgIDList:
            continue
        zhanlingType = int(zhanlingTypeStr)
        state = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingState)
        if state&pow(2, zhanlingType):
            break
        
        updState = state|pow(2, zhanlingType)
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingState, updState)
        SyncZhanlingInfo(curPlayer, zhanlingType)
        GameWorld.Log("¼¤»îÕ½ÁîÆÕͨ: zhanlingType=%s,updState=%s" % (zhanlingType, updState), curPlayer.GetPlayerID())
        break
    
    # ¸ß¼¶Õ½Áî
    zhanlingCTGIDDictH = IpyGameDataPY.GetFuncEvalCfg("Zhanling", 3)
    for zhanlingTypeStr, ctgIDList in zhanlingCTGIDDictH.items():
        if ctgID not in ctgIDList:
            continue
        zhanlingType = int(zhanlingTypeStr)
        state = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingStateH)
        if state&pow(2, zhanlingType):
            break
        
        updState = state|pow(2, zhanlingType)
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingStateH, updState)
        SyncZhanlingInfo(curPlayer, zhanlingType)
        GameWorld.Log("¼¤»îÕ½Áî¸ß¼¶: zhanlingType=%s,updState=%s" % (zhanlingType, updState), curPlayer.GetPlayerID())
        break
    return
 
def ResetZhanling(curPlayer, zhanlingType, backValue=None):
    ''' ÖØÖÃÕ½Áî
    @param zhanlingType: Õ½ÁîÀàÐÍ 
    @param backValue: ÖØÖÃÕ½ÁîʱµÄÕ½Áî½±ÀøÖµ£¬ÓÃÓÚ·µ»¹Î´ÁìÈ¡½±Àø
    '''
    state = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingState)
    stateH = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingStateH)
    updState = GameWorld.SetBitValue(state, zhanlingType, 0)
    updStateH = GameWorld.SetBitValue(stateH, zhanlingType, 0)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingState, updState)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingStateH, updStateH)
    if zhanlingType == ZhanlingType_Xianyuan:
        backValue = PlayerControl.GetMoney(curPlayer, ShareDefine.TYPE_Price_XianyuanScore)
        PlayerControl.SetMoney(curPlayer, ShareDefine.TYPE_Price_XianyuanScore, 0)
    GameWorld.Log("ÖØÖÃÕ½Áî: zhanlingType=%s,backValue=%s,state=(%s to %s) stateH=(%s to %s)" 
                  % (zhanlingType, backValue, state, updState, stateH, updStateH), curPlayer.GetPlayerID())
    
    if backValue:
        __giveUngetAward(curPlayer, zhanlingType, backValue, state&pow(2, zhanlingType), stateH&pow(2, zhanlingType))
        
    for keyNum in range(10):
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingReward % (zhanlingType, keyNum), 0)
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingRewardH % (zhanlingType, keyNum), 0)
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingRewardFree % (zhanlingType, keyNum), 0)
    SyncZhanlingInfo(curPlayer, zhanlingType)
    return
 
def __giveUngetAward(curPlayer, zhanlingType, backValue, activeState, activeStateH):
    ipyDataList = IpyGameDataPY.GetIpyGameDataByCondition("Zhanling", {"ZhanlingType":zhanlingType}, True)
    if not ipyDataList:
        return
    if not backValue:
        return
    playerID = curPlayer.GetPlayerID()
    GameWorld.DebugLog("´¦ÀíÕ½ÁîÖØÖò¹·¢½±Àø: zhanlingType=%s,backValue=%s,activeState=%s,activeStateH=%s" 
                       % (zhanlingType, backValue, activeState, activeStateH), playerID)
    ungetItemDict = {}
    for ipyData in ipyDataList:
        needValue = ipyData.GetNeedValue()
        rewardIndex = ipyData.GetRewardIndex()
        if backValue < needValue:
            break
        
        if not GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ZhanlingRewardFree, rewardIndex, True, [zhanlingType]):
            rewardItemList = ipyData.GetFreeRewardItemList()
            for itemID, itemCount, _ in rewardItemList:
                ungetItemDict[itemID] = ungetItemDict.get(itemID, 0) + itemCount
            GameWorld.DebugLog("    Õ½Áî²¹·¢Ãâ·Ñ½±Àø: rewardIndex=%s,needValue=%s,%s, %s" % (rewardIndex, needValue, rewardItemList, ungetItemDict), playerID)
                
        if activeState and not GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ZhanlingReward, rewardIndex, True, [zhanlingType]):
            rewardItemList = ipyData.GetZLRewardItemList()
            for itemID, itemCount, _ in rewardItemList:
                ungetItemDict[itemID] = ungetItemDict.get(itemID, 0) + itemCount
            GameWorld.DebugLog("    Õ½Áî²¹·¢ÆÕͨ½±Àø: rewardIndex=%s,needValue=%s,%s, %s" % (rewardIndex, needValue, rewardItemList, ungetItemDict), playerID)
                
        if activeStateH and not GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ZhanlingRewardH, rewardIndex, True, [zhanlingType]):
            rewardItemList = ipyData.GetZLRewardItemListH()
            for itemID, itemCount, _ in rewardItemList:
                ungetItemDict[itemID] = ungetItemDict.get(itemID, 0) + itemCount
            GameWorld.DebugLog("    Õ½Áî²¹·¢¸ß¼¶½±Àø: rewardIndex=%s,needValue=%s,%s, %s" % (rewardIndex, needValue, rewardItemList, ungetItemDict), playerID)
            
    if not ungetItemDict:
        return
    paramList = [zhanlingType]
    itemList = []
    for itemID, itemCount in ungetItemDict.items():
        itemList.append([itemID, itemCount, 0])
    GameWorld.DebugLog("    Õ½Áî²¹·¢½±Àø»ã×Ü: %s, %s" % (itemList, ungetItemDict), playerID)
    
    PlayerControl.SendMailByKey("ZhanlingRewardUnget", [playerID], itemList, paramList)
    return
 
def AddZhanlingValue(curPlayer, zhanlingType, addValue=1):
    if zhanlingType not in [ZhanlingType_Huanjingge]:
        return
    curValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingValue1 % zhanlingType)
    updValue = min(ChConfig.Def_UpperLimit_DWord, curValue + addValue)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingValue1 % zhanlingType, updValue)
    GameWorld.DebugLog("Ôö¼ÓÕ½Áî½ø¶È: zhanlingType=%s,addValue=%s,curValue=%s,updValue=%s" 
                       % (zhanlingType, addValue, curValue, updValue), curPlayer.GetPlayerID())
    SyncZhanlingInfo(curPlayer, zhanlingType, [])
    return updValue
 
def SetZhanlingValue(curPlayer, zhanlingType, value1):
    if zhanlingType not in [ZhanlingType_Huanjingge]:
        return
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ZhanlingValue1 % zhanlingType, value1)
    GameWorld.DebugLog("ÉèÖÃÕ½Áî½ø¶È: zhanlingType=%s,value1=%s" % (zhanlingType, value1), curPlayer.GetPlayerID())
    SyncZhanlingInfo(curPlayer, zhanlingType, [])
    return value1
 
def GetZhanlingReward(curPlayer, zhanlingType, rewardID):
    ## ÁìȡսÁî½±Àø
    rewardID = GameWorld.ToIntDef(rewardID)
    needValue, rewardType = rewardID/10, rewardID%10 # rewardType£º 0-Ãâ·Ñ£»1-ÆÕͨ£»2-¸ß¼¶
    playerID = curPlayer.GetPlayerID()
    
    ipyData = IpyGameDataPY.GetIpyGameData("Zhanling", zhanlingType, needValue)
    if not ipyData:
        return
    
    curValue = 0
    if zhanlingType == ZhanlingType_LV:
        curValue = curPlayer.GetLV()
    elif zhanlingType == ZhanlingType_Realm:
        curValue = curPlayer.GetOfficialRank()
    elif zhanlingType == ZhanlingType_SkyTower:
        curValue = curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_SkyTowerFloor)
    elif zhanlingType == ZhanlingType_GubaoStar:
        _, curValue = PlayerGubao.GetGubaoTotalLVStar(curPlayer)
    elif zhanlingType == ZhanlingType_Login:
        firstLoginTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingValue1 % zhanlingType)
        if not firstLoginTime:
            return
        curValue = GameWorld.GetDiff_Day(int(time.time()), firstLoginTime) + 1      
    elif zhanlingType == ZhanlingType_GatherTheSoulLV:
        curValue = PlayerGatherTheSoul.GetGatherTheSoulTotalLV(curPlayer)
    elif zhanlingType == ZhanlingType_HorsePetTrain:
        curValue = PlayerActHorsePetTrain.GetActHorsePetTrainScore(curPlayer)
    elif zhanlingType == ZhanlingType_GubaoTrain:
        curValue = PlayerActGubao.GetActGubaoTrainScore(curPlayer)
    elif zhanlingType == ZhanlingType_Xianyuan:
        curValue = PlayerControl.GetMoney(curPlayer, ShareDefine.TYPE_Price_XianyuanScore)
    elif zhanlingType == ZhanlingType_Huanjingge:
        curValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingValue1 % zhanlingType)
    else:
        return
        
    if curValue < needValue:
        GameWorld.DebugLog("Õ½ÁîËùÐèÖµ²»×㣬ÎÞ·¨Áì½±: zhanlingType=%s,curValue=%s < %s" % (zhanlingType, curValue, needValue), playerID)
        return
    
    rewardIndex = ipyData.GetRewardIndex()
    itemList = ipyData.GetFreeRewardItemList()
    rewardKey = ChConfig.Def_PDict_ZhanlingRewardFree
    if rewardType == 1:
        itemList = ipyData.GetZLRewardItemList()
        rewardKey = ChConfig.Def_PDict_ZhanlingReward
        state = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingState)
        if not state&pow(2, zhanlingType):
            GameWorld.DebugLog("ÆÕͨսÁîδ¼¤»î£¬ÎÞ·¨ÁìȡսÁî½±Àø: zhanlingType=%s,state=%s" % (zhanlingType, state), playerID)
            return
    elif rewardType == 2:
        itemList = ipyData.GetZLRewardItemListH()
        rewardKey = ChConfig.Def_PDict_ZhanlingRewardH
        state = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingStateH)
        if not state&pow(2, zhanlingType):
            GameWorld.DebugLog("¸ß¼¶Õ½Áîδ¼¤»î£¬ÎÞ·¨ÁìȡսÁî½±Àø: zhanlingType=%s,state=%s" % (zhanlingType, state), playerID)
            return
        
    if not itemList:
        GameWorld.DebugLog("ûÓиÃÕ½Áî½±Àø! zhanlingType=%s,needValue=%s,rewardType=%s" % (zhanlingType, needValue, rewardType), playerID)
        return
    
    if GameWorld.GetDictValueByBit(curPlayer, rewardKey, rewardIndex, True, [zhanlingType]):
        GameWorld.DebugLog("ÒѾ­ÁìÈ¡¹ý¸ÃÕ½Áî½±Àø! zhanlingType=%s,needValue=%s,rewardType=%s" % (zhanlingType, needValue, rewardType), playerID)
        return
    
    # ¼ì²é±³°ü
    if not ItemControler.CheckPackSpaceEnough(curPlayer, itemList):
        return
    
    # ¸üÐÂÒÑÁìÈ¡³É¹¦±ê¼Ç
    GameWorld.SetDictValueByBit(curPlayer, rewardKey, rewardIndex, 1, True, [zhanlingType])
    SyncZhanlingInfo(curPlayer, zhanlingType, [ipyData])
    GameWorld.DebugLog("ÁìȡսÁî½±Àø: zhanlingType=%s,needValue=%s,rewardType=%s,itemList=%s" % (zhanlingType, needValue, rewardType, itemList), playerID)
    
    # ¸øÎïÆ·
    for itemID, itemCount, isAuctionItem in itemList:
        ItemControler.GivePlayerItem(curPlayer, itemID, itemCount, isAuctionItem, [IPY_GameWorld.rptItem])
    ItemControler.NotifyGiveAwardInfo(curPlayer, itemList, "Zhanling")
    
    return
 
def SyncZhanlingInfo(curPlayer, zhanlingType, ipyDataList=None):
    
    if ipyDataList == None:
        ipyDataList = IpyGameDataPY.GetIpyGameDataByCondition("Zhanling", {"ZhanlingType":zhanlingType}, True)
         
    rewardList = []
    for ipyData in ipyDataList:
        rewardIndex = ipyData.GetRewardIndex()
        reward = ChPyNetSendPack.tagMCZhanling()
        reward.Clear()
        reward.NeedValue = ipyData.GetNeedValue()
        reward.FreeRewardState = 1 if GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ZhanlingRewardFree, rewardIndex, True, [zhanlingType]) else 0
        reward.ZLRewardState = 1 if GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ZhanlingReward, rewardIndex, True, [zhanlingType]) else 0
        reward.ZLRewardStateH = 1 if GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_ZhanlingRewardH, rewardIndex, True, [zhanlingType]) else 0
        rewardList.append(reward)
        
    clientPack = ChPyNetSendPack.tagMCZhanlingInfo()
    clientPack.Clear()
    clientPack.ZhanlingType = zhanlingType
    clientPack.IsActivite = 1 if curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingState)&pow(2, zhanlingType) else 0
    clientPack.IsActiviteH = 1 if curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingStateH)&pow(2, zhanlingType) else 0
    clientPack.Value1 = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ZhanlingValue1 % zhanlingType)
    clientPack.RewardList = rewardList
    clientPack.RewardCount = len(clientPack.RewardList)
    NetPackCommon.SendFakePack(curPlayer, clientPack)
    return