| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------------------  | 
| #  | 
| #-------------------------------------------------------------------------------------------  | 
| ##@package Operate_EquipSuitCompose.py  | 
| # @todo: Ì××°ºÏ³É  | 
| #  | 
| # @author: xdh  | 
| # @date 2017-08-15  | 
| # @version 1.0  | 
| #  | 
| ## ÏêϸÃèÊö  | 
| #  | 
| #  | 
| #---------------------------------------------------------------------  | 
| #"""Version = 2017-08-15 21:00"""  | 
| #------------------------------------------------------------------------------  | 
| import IPY_GameWorld  | 
| import GameWorld  | 
| import ItemCommon  | 
| import PlayerControl  | 
| import ShareDefine  | 
| import ChConfig  | 
| import ChEquip  | 
| import PlayerSuccess  | 
| import ItemControler  | 
| import IpyGameDataPY  | 
| import EventShell  | 
|   | 
|   | 
| #//A3 17 Ì××°ºÏ³É#tagCMSuitCompose  | 
| #struct tagCMSuitCompose  | 
| #{  | 
| #    tagHead Head;  | 
| #    BYTE    SuiteType;    // Ì××°ÀàÐÍ1ÆÕͨ 2Ç¿»¯  | 
| #    BYTE    EquipPlace;    // ×°±¸Î»Öà  | 
| #};  | 
|   | 
| ## Ì××°ºÏ³É  | 
| #  @param playerIndex, clientData, tick  | 
| #  @return None  | 
| def OnEquipSuitCompose(playerIndex, clientData, tick):  | 
|     curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(playerIndex)  | 
|     equipPlace = clientData.EquipPlace  | 
|     suiteType = clientData.SuiteType  | 
|     #È¡³öÖ÷×°±¸  | 
|     mainEquipPack = curPlayer.GetItemManager().GetPack(IPY_GameWorld.rptEquip)  | 
|       | 
|     mainEquip = mainEquipPack.GetAt(equipPlace)  | 
|     if not ItemCommon.CheckItemCanUse(mainEquip):  | 
|         GameWorld.DebugLog('    Ì××°ºÏ³É ¸ÃλÖÃû´©×°±¸£¡ equipPlace=%s'%equipPlace)  | 
|         return  | 
|       | 
|       | 
|     curSuiteLV = ChEquip.GetEquipPartSuiteLV(curPlayer, equipPlace, suiteType)  | 
|     tagSuitLV = curSuiteLV + 1  | 
|       | 
|     needColorStarDict = IpyGameDataPY.GetFuncEvalCfg('EquipSuitColorStar')  | 
|     if str(suiteType) not in needColorStarDict:  | 
|         return  | 
|     needColor, needQuality = needColorStarDict[str(suiteType)]  | 
|     if mainEquip.GetItemColor() < needColor or mainEquip.GetItemQuality() < needQuality:  | 
|         GameWorld.DebugLog(' ¸Ã×°±¸²»Âú×ãÆ·ÖÊ»òÐǼ¶ÒªÇ󠣡needColor=%s, needQuality=%s'%(needColor, needQuality))  | 
|         return  | 
|     #×°±¸½×¼¶²»ÄܵÍÓÚÌ××°µÈ¼¶  | 
|     equipClassLV = ItemCommon.GetItemClassLV(mainEquip)  | 
|     if equipClassLV < tagSuitLV:  | 
|         GameWorld.DebugLog('µ±Ç°×°±¸½×¼¶²»ÄܵÍÓÚÌ××°µÈ¼¶')  | 
|         return  | 
|     #²»ÄܸßÓڵͼ¶Ì××°ÀàÐ͵ĵȼ¶  | 
|     for stype in xrange(1, suiteType):  | 
|         if tagSuitLV > ChEquip.GetEquipPartSuiteLV(curPlayer, equipPlace, stype):  | 
|             GameWorld.DebugLog('²»ÄܸßÓڵͼ¶Ì××°ÀàÐ͵ĵȼ¶')  | 
|             return  | 
|       | 
|     stuffItemDict = __GetStuffItem(curPlayer, suiteType, equipPlace, tagSuitLV)  | 
|      | 
|     if not stuffItemDict:  | 
|         return  | 
|       | 
|     hasEnough, useItemList = __GetNeedItemIndexList(curPlayer, stuffItemDict)      | 
|     if not hasEnough:  | 
|         #²ÄÁϲ»×ã  | 
|         GameWorld.DebugLog('OnEquipSuitCompose stuff no enough stuffItemDict=%s' % stuffItemDict)  | 
|         return  | 
|     # É¾³ý²ÄÁÏ  | 
|     for costItemCount, costItemIndexList, bindCnt, unBindCnt in useItemList:  | 
|         ItemCommon.DelCostItemByBind(curPlayer, costItemIndexList, bindCnt, unBindCnt, costItemCount,   | 
|                                      ChConfig.ItemDel_EquipSuit, {"EquipPlace":equipPlace})  | 
|   | 
|     #¸üÐÂ×°±¸Ì××°Æ·ÖÊ  | 
|     ChEquip.SetEquipPartSuiteLV(curPlayer, equipPlace, suiteType, tagSuitLV)  | 
|       | 
|     #֪ͨµÈ¼¶  | 
|     ChEquip.NotifyEquipPartSuiteLV(curPlayer, equipPlace)  | 
|       | 
|     #ÏÈË¢×°±¸BUFF ÔÙ¼ÆËãÊôÐÔ  | 
|     ChEquip.RefreshPlayerEquipAttribute(curPlayer)  | 
|     PlayerControl.PlayerControl(curPlayer).RefreshPlayerAttrState()  | 
|       | 
|     __DoSuiteSuccess(curPlayer)  | 
|     GameWorld.DebugLog('    Ì××°Éý¼¶³É¹¦ equipPlace=%s,suiteType=%s, tagSuitLV=%s, useItemList=%s'%(equipPlace, suiteType, tagSuitLV, useItemList))  | 
|       | 
|     # ºÏ³É½á¹û֪ͨ  | 
|     curPlayer.Sync_MakeItemAnswer(ShareDefine.Def_mitSuiteCompound, ChConfig.Def_ComposeState_Sucess)  | 
|     return  | 
|   | 
|   | 
|   | 
| ## »ñµÃÐèÒªµÄÎïÆ·Ë÷Òý×Öµä  | 
| #  @param itemDict: ÐèÒªÎïÆ·ÊýÁ¿  | 
| #  @param curPack: ±³°ü  | 
| #  @return ÊÇ·ñ×ã¹»,[itemID, itemCnt, indexList, hasBind, lackCnt]  | 
| def __GetNeedItemIndexList(curPlayer, itemDict):  | 
|     itemIndexList = []  | 
|     isEnough = True  | 
|     # ÐèÇóÎïÆ·×Öµä{id,ÊýÁ¿}  | 
|     for itemID, itemCnt in itemDict.items():  | 
|         costItemIndexList, bindCnt, unBindCnt = ItemCommon.GetPackItemBindStateIndexInfo(curPlayer, itemID)  | 
|         if (bindCnt + unBindCnt) < itemCnt:  | 
|             isEnough = False  | 
|           | 
|         itemIndexList.append([itemCnt, costItemIndexList, bindCnt, unBindCnt])  | 
|       | 
|     return isEnough, itemIndexList  | 
|   | 
| def GetEquipCanDoMaxSuiteLV(curPlayer, curEquip, suiteType, groupType=-1):  | 
|     #»ñÈ¡¸Ã×°±¸¿É¶ÍÔìÖÁ×î¸ßÌ××°µÈ¼¶  | 
|     equipPlace = curEquip.GetEquipPlace()  | 
|     if groupType == -1:  | 
|         groupType = ChEquip.GetEquipPartSuiteGroupType(curPlayer, equipPlace)  | 
|           | 
|     equipClassLV = ItemCommon.GetItemClassLV(curEquip)  | 
|     needColorStarDict = IpyGameDataPY.GetFuncEvalCfg('EquipSuitColorStar')  | 
|     if str(suiteType) not in needColorStarDict:  | 
|         return 0  | 
|     needColor, needQuality = needColorStarDict[str(suiteType)]  | 
|     if curEquip.GetItemColor() < needColor or curEquip.GetItemQuality() < needQuality:  | 
|         return 0  | 
|     return equipClassLV  | 
|   | 
| #// A3 08 Ì××°²ð½â #tagCMSuiteDecompose  | 
| #  | 
| #struct    tagCMSuiteDecompose  | 
| #{  | 
| #    tagHead        Head;  | 
| #    BYTE    EquipPlace;  //²¿Î»Ë÷Òý  | 
| #    BYTE    SuiteType; //Ì××°ÀàÐÍ 0´ú±íÈ«²¿ÀàÐÍ  | 
| #};   | 
| def OnDecomposeSuite(index, clientData, tick):  | 
|     curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  | 
|     equipPlace = clientData.EquipPlace  | 
|     resetSuiteType = clientData.SuiteType  | 
|       | 
|     __DoDecomposeSuite(curPlayer, equipPlace, resetSuiteType, 0)  | 
|       | 
|     return  | 
|   | 
| def __DoDecomposeSuite(curPlayer, equipPlace, resetSuiteType, downByEquip=False):  | 
|     #Ö´ÐÐÌ××°½µ¼¶¡¢²ð½â  resetSuiteTypeÌ××°ÀàÐÍ 0´ú±íÈ«²¿ÀàÐÍ  downByEquip¸ù¾ÝÒÑ´©×°±¸×î¸ßÌ××°µÈ¼¶À´½µ¼¶  | 
|     giveItemDict = {}  | 
|     maxSuiteType = IpyGameDataPY.GetFuncCfg('EquipSuitColorStar', 2)  | 
|       | 
|       | 
|     equipPack = curPlayer.GetItemManager().GetPack(IPY_GameWorld.rptEquip)  | 
|     curEquip = equipPack.GetAt(equipPlace)  | 
|     hasEquip = ItemCommon.CheckItemCanUse(curEquip)  | 
|   | 
|     for suiteType in range(1, maxSuiteType+1):  | 
|         if resetSuiteType and resetSuiteType != suiteType:  | 
|             continue  | 
|         curSuiteLV = ChEquip.GetEquipPartSuiteLV(curPlayer, equipPlace, suiteType)  | 
|         toSuiteLV = 0  | 
|         if downByEquip and hasEquip:  | 
|             maxSuiteLV = GetEquipCanDoMaxSuiteLV(curPlayer, curEquip, suiteType)  | 
|             toSuiteLV = min(maxSuiteLV, curSuiteLV)  | 
|         if toSuiteLV == curSuiteLV:  | 
|             continue  | 
|         for suiteLV in range(toSuiteLV+1, curSuiteLV+1):  | 
|             stuffItemDict = __GetStuffItem(curPlayer, suiteType, equipPlace, suiteLV)  | 
|             if not stuffItemDict:  | 
|                 GameWorld.ErrLog('    ²ð½âÌ××°,ûÕÒµ½²ÄÁÏ£¡')  | 
|                 return  | 
|               | 
|             for itemID, itemCnt in stuffItemDict.items():  | 
|                 giveItemDict[itemID] = giveItemDict.get(itemID, 0) + itemCnt  | 
|           | 
|         ChEquip.SetEquipPartSuiteLV(curPlayer, equipPlace, suiteType, toSuiteLV)  | 
|       | 
|     if not giveItemDict:  | 
|         return  | 
|     GameWorld.DebugLog('    ²ð½âÌ××° equipPlace=%s,resetSuiteType=%s,giveItemDict=%s'%(equipPlace, resetSuiteType, giveItemDict))  | 
|       | 
|     needSpace = len(giveItemDict)  | 
|     emptySpace = ItemCommon.GetItemPackSpace(curPlayer, IPY_GameWorld.rptItem, needSpace)  | 
|     if emptySpace < needSpace:  | 
|         prizeItemList = [(itemID, itemCnt, 1) for itemID, itemCnt in giveItemDict.items()]  | 
|         PlayerControl.SendMailByKey('DefaultLackSpace', [curPlayer.GetPlayerID()], prizeItemList)  | 
|     else:  | 
|         for itemID, itemCnt in giveItemDict.items():  | 
|             ItemControler.GivePlayerItem(curPlayer, itemID, itemCnt, True, [IPY_GameWorld.rptItem], True,   | 
|                                          event=[ChConfig.ItemGive_EquipSuit, False, {"EquipPlace":equipPlace}])  | 
|       | 
|     #֪ͨµÈ¼¶  | 
|     ChEquip.NotifyEquipPartSuiteLV(curPlayer, equipPlace)  | 
|       | 
|     #ÏÈË¢×°±¸BUFF ÔÙ¼ÆËãÊôÐÔ  | 
|     ChEquip.RefreshPlayerEquipAttribute(curPlayer)  | 
|     PlayerControl.PlayerControl(curPlayer).RefreshPlayerAttrState()  | 
|       | 
|     __DoSuiteSuccess(curPlayer)  | 
|     return  | 
|   | 
| def CheckEquipSuitReduce(curPlayer, equipIndex):  | 
|     ##ÓÉÓÚ×°±¸¸ü»»£¬Ì××°½µ¼¶¼ì²é  | 
|     __DoDecomposeSuite(curPlayer, equipIndex, 0, True)  | 
|       | 
|       | 
|     return  | 
|   | 
|   | 
|   | 
| def __GetStuffItem(curPlayer, suiteType, equipPlace, tagSuitLV):  | 
|     job = curPlayer.GetJob() if equipPlace not in ChConfig.BaseEquipPlace_Relics else 0  | 
|     ipyData = IpyGameDataPY.GetIpyGameData('EquipSuitCompound', suiteType, equipPlace, tagSuitLV, job)  | 
|     if not ipyData:  | 
|         return {}  | 
|     costItemIDList = ipyData.GetCostItemID()  | 
|     costItemCntList = ipyData.GetCostItemCnt()  | 
|     stuffItemDict = {}  | 
|     for i, itemid in enumerate(costItemIDList):  | 
|         stuffItemDict[itemid] = stuffItemDict.get(itemid, 0) + costItemCntList[i]  | 
|     return stuffItemDict  | 
|   | 
|   | 
|   | 
| def __DoSuiteSuccess(curPlayer):  | 
|     PlayerSuccess.ResetSuccessByType(curPlayer, ShareDefine.SuccType_EquipSuit)  | 
|     equipSuitTypeDict = IpyGameDataPY.GetFuncEvalCfg('EquipSuitType')  | 
|     groupCnt = 0 #2½×Ç¿»¯·À¾ßÌ××°¼þÊý  | 
|     for groupType, equipIndexList in equipSuitTypeDict.items():  | 
|         for i in equipIndexList:  | 
|             suiteInfo = ChEquip.GetSuiteInfoByPlace(curPlayer, i)  | 
|             for suiteType, lv in suiteInfo.items():  | 
|                 if lv >= 2 and int(groupType) == 1 and suiteType == 2:  | 
|                     groupCnt +=1  | 
|                 for j in xrange(1,lv+1):  | 
|                     PlayerSuccess.DoAddSuccessProgress(curPlayer, ShareDefine.SuccType_EquipSuit, 1, [j, suiteType, int(groupType)])  | 
|     #GameWorld.DebugLog('2½×Ç¿»¯·À¾ßÌ××°¼þÊý=%s'%groupCnt)  | 
|     EventShell.EventRespons_SuitPlus(curPlayer, groupCnt)  | 
|     return |