hxp
2019-05-28 cce3ffb8fc917a82db16b7c82afc7d82485c55bd
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
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package UseItem.Item_Chests
#
# @todo:±¦Ïä
# @author hxp
# @date 2018-02-05
# @version 1.0
#
# ÏêϸÃèÊö: Ipy±¦Ïä±íÎïÆ·
#
#-------------------------------------------------------------------------------
#"""Version = 2018-02-05 15:00"""
#-------------------------------------------------------------------------------
 
#µ¼Èë
import GameWorld
import ItemCommon
import PlayerControl
import IpyGameDataPY
import IPY_GameWorld
import ItemControler
import PlayerRune
import ChConfig
import ChItem
 
import math
#---------------------------------------------------------------------
 
def BatchUseItem(curPlayer, curRoleItem, tick, useCnt, exData):
    ##ÅúÁ¿Ê¹ÓÃÎïÆ·
    
    chestsItemID = curRoleItem.GetItemTypeID()
    chestsIpyData = IpyGameDataPY.GetIpyGameDataNotLog("Chests", chestsItemID)
    if not chestsIpyData:
        return
    
    showType = chestsIpyData.GetShowType() # ¹æ¶¨ÓпªÏä±íÏÖµÄĬÈÏÖ»ÄÜ¿ªÆô1¸ö
    if showType:
        useCnt = 1
        
    isBind = int(chestsIpyData.GetIsBind()) # ½±ÀøÎïÆ·ÊÇ·ñ°ó¶¨
    costItemID = chestsIpyData.GetCostItemID()
    costItemCountTotal = chestsIpyData.GetCostItemCount() * useCnt
    costGoldTotal = chestsIpyData.GetCostGold() * useCnt
    auctionItemCanSell = chestsIpyData.GetAucionItemCanSell()
    
    if costGoldTotal and not PlayerControl.HaveMoney(curPlayer, IPY_GameWorld.TYPE_Price_Gold_Money, costGoldTotal):
        return
    
    if costItemID:
        costItemIndexList, bindCnt, unBindCnt = ItemCommon.GetPackItemBindStateIndexInfo(curPlayer, costItemID)
        if bindCnt + unBindCnt < costItemCountTotal:
            GameWorld.DebugLog("ËùÐèÏûºÄµÀ¾ß²»×㣬ÎÞ·¨´ò¿ª±¦Ïä!chestsItemID=%s,costItemID=%s,costItemCountTotal=%s,bindCnt=%s,unBindCnt=%s" 
                               % (chestsItemID, costItemID, costItemCountTotal, bindCnt, unBindCnt))
            return
        
    GameWorld.DebugLog("UseChests: chestsItemID=%s,useCnt=%s,isBind=%s,costItemID=%s*%s,costGoldTotal=%s" 
                       % (chestsItemID, useCnt, isBind, costItemID, costItemCountTotal, costGoldTotal))
    # ²ú³öÔ¤´¦Àí
    awardInfo = GetChestsAwardInfo(curPlayer, chestsItemID, useCnt, exData)
    if not awardInfo:
        return
    needSpaceDict, jobAwardItemList, moneyType, moneyCount, notifyItemList, updOpenCount = awardInfo
    GameWorld.DebugLog("    needSpaceDict=%s,jobAwardItemList=%s,moneyType=%s,moneyCount=%s,notifyItemList=%s,updOpenCount=%s,auctionItemCanSell=%s" 
                       % (needSpaceDict, jobAwardItemList, moneyType, moneyCount, notifyItemList, updOpenCount, auctionItemCanSell))
    
    for packType, needSpace in needSpaceDict.items():
        packSpace = ItemCommon.GetItemPackSpace(curPlayer, packType, needSpace)
        if packSpace < needSpace:
            #PlayerControl.NotifyCode(curPlayer, "GeRen_chenxin_676165", [packType])
            PlayerControl.NotifyCode(curPlayer, "OpenBoxItem", [packType, chestsItemID, needSpace - packSpace])
            return
        
    # ¿Û³ýÏûºÄ
    if costItemID:
        ItemCommon.DelCostItemByBind(curPlayer, costItemIndexList, bindCnt, unBindCnt, costItemCountTotal, "Chests")
    if costGoldTotal:
        infoDict = {ChConfig.Def_Cost_Reason_SonKey:chestsItemID}
        PlayerControl.PayMoney(curPlayer, IPY_GameWorld.TYPE_Price_Gold_Money, costGoldTotal, ChConfig.Def_Cost_UseItem, infoDict)
        
    # ¸üпªÆô´ÎÊý
    if updOpenCount:
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ChestsOpenCount % chestsItemID, updOpenCount)
        GameWorld.DebugLog("    ¸üб¦Ï俪Æô´ÎÊý: %s" % updOpenCount)
        
    saveDataDict = {"AwardItem":jobAwardItemList}
    ItemCommon.DelItem(curPlayer, curRoleItem, useCnt, True, ChConfig.ItemDel_Chests, saveDataDict)
    
    # ¸ø½±Àø
    syncItemList = []
    for itemID, itemCount in jobAwardItemList:
        isAuctionItem = 1 if auctionItemCanSell and IpyGameDataPY.GetIpyGameDataNotLog("AuctionItem", itemID) else 0
        curItem = ItemControler.GetOutPutItemObj(itemID, itemCount, isAuctionItem, curPlayer=curPlayer)
        if not curItem:
            GameWorld.ErrLog("±¦Ïä´´½¨½±ÀøÎïÆ·Òì³£!chestsItemID=%s,useCnt=%s,itemID=%s,itemCount=%s,isBind=%s" 
                             % (chestsItemID, useCnt, itemID, itemCount, isBind), curPlayer.GetPlayerID())
            continue
        
        if showType:
            syncItemList.append(ItemCommon.GetJsonItem(curItem)) # ±ØÐëÔÚ¸øÎïÆ·Ö®Ç°ÏÈget
            
        userData = curItem.GetUserData()
        isOK = ItemControler.DoLogic_PutItemInPack(curPlayer, curItem, event=[ChConfig.ItemGive_Chests, False, {"UseItemID":chestsItemID}])
        if isOK and itemID in notifyItemList:
            PlayerControl.WorldNotify(0, "ChooseMessage", [curPlayer.GetPlayerName(), chestsItemID, itemID, isBind, itemCount, userData])
        
    # »õ±Ò
    if moneyType:
        addDataDict = {ChConfig.Def_Give_Reason_SonKey:chestsItemID}
        if not PlayerControl.GiveMoney(curPlayer, moneyType, moneyCount, ChConfig.Def_GiveMoney_UseItem, addDataDict, False):
            moneyType, moneyCount = 0, 0
            
    # Í¨Öª
    if syncItemList:
        ChItem.SendUseItemGetItemResult(curPlayer, chestsItemID, useCnt, syncItemList, moneyType, moneyCount)
    return True, useCnt
 
 
def GetChestsAwardInfo(curPlayer, chestsItemID, useCount, exData=0):
    '''»ñÈ¡±¦Ï俪Æô½±Àø
    @return: None - »ñÈ¡±¦Ïä½±ÀøÊ§°Ü
    @return: needSpaceDict, jobAwardItemList [[itemID, itemCount], ...]
    '''
    
    awardIpyData = __GetChestsAwardIpyDataByLV(curPlayer, chestsItemID)
    if not awardIpyData:
        return
    
    awardItemDict = {}
    job = curPlayer.GetJob()
    jobItemList = awardIpyData.GetJobItemList()
    # Ñ¡ÔñÎïÆ·
    if awardIpyData.GetSelectItemDict():
        if not exData:
            GameWorld.ErrLog("Ñ¡ÔñµÄÎïÆ·´íÎó, ÐèÒªÖ¸¶¨Ò»¸öÑ¡ÔñÎïÆ·ID!chestsItemID=%s,selectItemID=%s" % (chestsItemID, exData))
            return
        selectItemID = exData
        
        isSelectOK = False
        for cfgItemID, itemCount in awardIpyData.GetSelectItemDict().items():
            # Ö§³Ö°´Ö°ÒµÑ¡Ôñ£¬Ç°¶ËÎÞÂÛ·¢Ö°Òµ¶ÔÓ¦ÎïÆ·ID»òÕßÅäÖõÄÎïÆ·ID¾ù¿É»ñµÃ
            jobItemID = __GetChestsJobItem(chestsItemID, job, cfgItemID, jobItemList)
            if not jobItemID:
                return
            if selectItemID == cfgItemID or selectItemID == jobItemID:
                __AddAwardItem(awardItemDict, jobItemID, itemCount * useCount)
                isSelectOK = True
                break
            
        if not isSelectOK:
            GameWorld.ErrLog("Ñ¡ÔñµÄÎïÆ·´íÎó, ¸Ã±¦ÏäûÓиÃÎïÆ·!chestsItemID=%s,selectItemID=%s" % (chestsItemID, selectItemID))
            return
        
    # ¹Ì¶¨²ú³öÎïÆ·
    if awardIpyData.GetFixedItemDict():
        for itemID, itemCount in awardIpyData.GetFixedItemDict().items():
            __AddAwardItem(awardItemDict, itemID, itemCount * useCount)
            
    # Ëæ»úÎïÆ·¿â1
    if awardIpyData.GetRandItemList1() and awardIpyData.GetRandTimeList1():
        if not __AddChestsRandAwardItem(curPlayer, chestsItemID, useCount, awardItemDict, awardIpyData.GetRandItemList1(), awardIpyData.GetRandTimeList1()):
            return
        
    # ¸ù¾Ý±¦Ï俪Æô´ÎÊýÌØÊâ²ú³ö
    updOpenCount = 0
    randItemList2DoCount = useCount
    randItemByUseCountDict = awardIpyData.GetRandItemByUseCount()
    if randItemByUseCountDict:
        maxOpenCount = max(randItemByUseCountDict)
        hisOpenCount = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ChestsOpenCount % chestsItemID)
        if hisOpenCount < maxOpenCount:
            updOpenCount = min(hisOpenCount + useCount, maxOpenCount)
            for specUseCount, randItemList in randItemByUseCountDict.items():
                if not (hisOpenCount < specUseCount <= updOpenCount):
                    GameWorld.DebugLog("    ²»Âú×ãÌØÊâ´ÎÊý²ú³ö: hisOpenCount=%s,specUseCount=%s,updOpenCount=%s" 
                                       % (hisOpenCount, specUseCount, updOpenCount))
                    continue
                randItemList2DoCount -= 1
                randItemInfo = GameWorld.GetResultByRandomList(randItemList)
                GameWorld.DebugLog("    ¸ù¾Ý¿ªÆô´ÎÊýÌØÊâ²ú³ö: specUseCount=%s, %s, randItemList2DoCount=%s" 
                                   % (specUseCount, randItemInfo, randItemList2DoCount))
                if not randItemInfo:
                    continue
                if len(randItemInfo) != 2:
                    GameWorld.ErrLog("±¦Ï俪Æô´ÎÊýÌØÊâ²ú³öËæ»ú¿âÅäÖôíÎó!chestsItemID=%s" % chestsItemID)
                    return
                itemID, itemCount = randItemInfo
                if not itemID:
                    continue
                __AddAwardItem(awardItemDict, itemID, itemCount)
            
    if randItemList2DoCount != useCount:
        GameWorld.DebugLog("    Ëæ»úÎïÆ·¿â2Ö´ÐдÎÊý: %s" % (randItemList2DoCount))
        
    # Ëæ»úÎïÆ·¿â2
    if awardIpyData.GetRandItemList2() and awardIpyData.GetRandTimeList2() and randItemList2DoCount:
        if not __AddChestsRandAwardItem(curPlayer, chestsItemID, randItemList2DoCount, awardItemDict, awardIpyData.GetRandItemList2(), awardIpyData.GetRandTimeList2()):
            return
    
    # ²ú³öÌØÊâÎïÆ·¹ã²¥
    needNotifyItemList = awardIpyData.GetNeedNotifyItemList()
    
    needSpaceDict = {} # {packType:needSpace, ...}
    notifyItemList = []
    jobAwardItemList = []
    for itemID, itemCount in awardItemDict.items():
        if not itemCount:
            continue
        jobItemID = __GetChestsJobItem(chestsItemID, job, itemID, jobItemList)
        if not jobItemID:
            return
        
        itemData = GameWorld.GetGameData().GetItemByTypeID(jobItemID)
        if not itemData:
            GameWorld.ErrLog("±¦Ïä½±ÀøÎïÆ·²»´æÔÚ! chestsItemID=%s,itemID=%s,jobItemID=%s" % (chestsItemID, itemID, jobItemID))
            return
        packType = ChConfig.GetItemPackType(itemData.GetType())
        needSpace = int(math.ceil(itemCount / float(itemData.GetPackCount())))
        needSpaceDict[packType] = needSpaceDict.get(packType, 0) + needSpace
        
        # ×°±¸²ð¿ª¸ø£¬ÐèҪͬ²½Ã¿¼þ×°±¸µÄÊôÐÔ
        if ItemCommon.GetIsEquip(itemData):
            jobAwardItemList.extend([[jobItemID, 1]] * itemCount)
        else:
            jobAwardItemList.append([jobItemID, itemCount])
        if itemID in needNotifyItemList or jobItemID in needNotifyItemList:
            notifyItemList.append(jobItemID)
            
    return needSpaceDict, jobAwardItemList, awardIpyData.GetMoneyType(), awardIpyData.GetMoneyCount() * useCount, notifyItemList, updOpenCount
 
def __GetMaxRandTime(randTimeList):
    if len(randTimeList) == 1 and type(randTimeList[0]) == int:
        return randTimeList[0]
    return randTimeList[-1][1]
 
def __AddChestsRandAwardItem(curPlayer, chestsItemID, useCount, awardItemDict, randItemList, randTimeList):
    
    # ¹ýÂËδ½âËøµÄÎïÆ·
    resetRandItemList = [] # ÖØÉè¿É²ú³öµÄÎïÆ·±ýͼ¸ÅÂÊÁбí
    for i, rateItemInfo in enumerate(randItemList):
        if type(rateItemInfo) not in [list, tuple] or len(rateItemInfo) != 2:
            GameWorld.ErrLog("±¦ÏäËæ»úÎïÆ·¿â±ýͼ¸ñʽ´íÎó!chestsItemID=%s,randItemList=%s" % (chestsItemID, randItemList))
            return False
        rate, itemInfo = rateItemInfo
        if itemInfo and itemInfo[0]:
            itemID = itemInfo[0] # ÓÐÅäÖÃÎïÆ·ID£¬ÐèÅжϸÃÎïÆ·IDÊÇ·ñºÏ·¨¿É¿ª³öµÈ£¬Ö§³ÖÅäÖÿÕÎïÆ·ID
            itemData = GameWorld.GetGameData().GetItemByTypeID(itemID)
            if not itemData:
                return False
            
            # ·ûÓ¡ÅжÏÊÇ·ñÒѾ­½âËø
            if itemData.GetType() == ChConfig.Def_ItemType_Rune:
                if not PlayerRune.GetIsOpenByRuneID(curPlayer, itemID):
                    GameWorld.DebugLog("´Ë·ûӡδ½âËøÎÞ·¨¿ª³ö! chestsItemID=%s,itemID=%s" % (chestsItemID, itemID))
                    continue
            
        curRate = rate if i == 0 else (rate - randItemList[i - 1][0])
        preRate = 0 if not resetRandItemList else resetRandItemList[-1][0]
        resetRandItemList.append([preRate + curRate, itemInfo]) # ÖØÉè±ýͼ¸ÅÂÊÁбí
    
    for _ in xrange(useCount):
        if len(randTimeList) == 1 and type(randTimeList[0]) == int:
            randTimes = randTimeList[0]
        else:
            randTimes = GameWorld.GetResultByRandomList(randTimeList)
            
        if randTimes == None:
            GameWorld.ErrLog("±¦ÏäËæ»ú´ÎÊýÅäÖôíÎó!chestsItemID=%s" % chestsItemID)
            return False
        
        if not randTimes:
            continue
        
        for _ in xrange(randTimes):
            randItemInfo = GameWorld.GetResultByRandomList(resetRandItemList)
            if not randItemInfo:
                #GameWorld.DebugLog("±¦ÏäËæ»ú¿âËæ»ú³öÎïÆ·0£¬randItemInfo=%s" % randItemInfo)
                continue
            if len(randItemInfo) != 2:
                GameWorld.ErrLog("±¦ÏäËæ»ú¿âÅäÖôíÎó!chestsItemID=%s" % chestsItemID)
                return False
            
            itemID, itemCount = randItemInfo
            if not itemID:
                #GameWorld.DebugLog("±¦ÏäËæ»ú¿âËæ»ú³öÎïÆ·itemID=0£¬randItemInfo=%s" % randItemInfo)
                continue
            __AddAwardItem(awardItemDict, itemID, itemCount)
            
    return True
 
def __GetChestsJobItem(chestsItemID, job, itemID, jobItemList):
    ## »ñÈ¡±¦ÏäÎïÆ·½±Àø¶ÔÓ¦µÄÖ°ÒµÎïÆ·£¬ Ö°Òµ´Ó1¿ªÊ¼
    for jobItemIDList in jobItemList:
        if type(jobItemIDList) not in [list, tuple]:
            GameWorld.ErrLog("±¦ÏäÖ°ÒµÎïÆ·×é¸ñʽ´íÎó!chestsItemID=%s,jobItemList=%s" % (chestsItemID, jobItemList))
            return 0
        if itemID in jobItemIDList:
            if job <= 0 or job > len(jobItemIDList):
                GameWorld.ErrLog("±¦ÏäÖ°ÒµÎïÆ·ÅäÖôíÎó,ûÓиÃÖ°Òµ¶ÔÓ¦ÎïÆ·ID!chestsItemID=%s,job=%s,itemID=%s" % (chestsItemID, job, itemID))
                return 0
            return jobItemIDList[job - 1]
    return itemID
 
def __AddAwardItem(awardItemDict, itemID, itemCount):
    awardItemDict[itemID] = awardItemDict.get(itemID, 0) + itemCount
    return
 
def __GetChestsAwardIpyDataByLV(curPlayer, chestsItemID):
    ## »ñÈ¡±¦Ï俪Æô½±Àø
    awardIpyDataList = IpyGameDataPY.GetIpyGameDataList("ChestsAward", chestsItemID)
    if not awardIpyDataList:
        return
    
    if len(awardIpyDataList) == 1:
        return awardIpyDataList[0]
    
    # ¶àÌõ²ú³ö¼Ç¼µÄ£¬°´µÈ¼¶À´
    lvIpyDataList = []
    for ipyData in awardIpyDataList:
        lvIpyDataList.append([ipyData.GetAwardLV(), ipyData])
    lvIpyDataList.sort() # ÉýÐòÅÅÐò
    
    curLV = curPlayer.GetLV()
    minLV = lvIpyDataList[0][0]
    if curLV < minLV:
        GameWorld.ErrLog("µ±Ç°µÈ¼¶ÎÞ·¨¿ªÆô¸Ã±¦Ïä!curLV=%s,minLV=%s,chestsItemID=%s" % (curLV, minLV, chestsItemID), curPlayer.GetPlayerID())
        return
    
    for i, lvIpyInfo in enumerate(lvIpyDataList[1:], 1):
        awardLV, ipyData = lvIpyInfo
        if curLV < awardLV:
            awardLV, ipyData = lvIpyDataList[i - 1]
            return ipyData
    return awardIpyDataList[-1]