| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package Player.PlayerCoat  | 
| #  | 
| # @todo:Íæ¼ÒʱװÍâÌ×  | 
| # @author hxp  | 
| # @date 2015-6-15  | 
| # @version 1.1  | 
| #  | 
| # @change: "2015-08-10 10:00" ljd Ï·¢ÖÜÎ§Íæ¼Òʱװ´©´÷״̬  | 
| # ÏêϸÃèÊö: Íæ¼ÒʱװÍâÌ×  | 
| #  | 
| #---------------------------------------------------------------------  | 
| """Version = 2015-08-10 10:00"""  | 
| #---------------------------------------------------------------------  | 
|   | 
| import ReadChConfig  | 
| import NetPackCommon  | 
| import IPY_GameWorld  | 
| import PlayerControl  | 
| import ItemControler  | 
| import ChPyNetSendPack  | 
| import ShareDefine  | 
| import ItemCommon  | 
| import EventReport  | 
| import GameWorld  | 
| import ChConfig  | 
| import ChEquip  | 
|   | 
| import math  | 
|   | 
| ## µÇ¼  | 
| #  @param curPlayer  | 
| #  @return None  | 
| def OnLogin_Coat(curPlayer):  | 
|     Sync_ClothesCoatSkinInfo(curPlayer)  | 
|     return  | 
|   | 
|   | 
| ## ¼¤»îʱװÒ·þƤ·ô  | 
| #  @param curPlayer  | 
| #  @return None  | 
| def ActivateClothesCoatSkinItem(curPlayer, clothesCoatSkinID):  | 
|       | 
|     playerID = curPlayer.GetPlayerID()  | 
|       | 
|     clothesCoatSkinDict = ReadChConfig.GetEvalChConfig("ClothesCoatSkin")  | 
|     if clothesCoatSkinID not in clothesCoatSkinDict:  | 
|         GameWorld.ErrLog("¼¤»îʱװƤ·ôÒì³££¬ÎÞ¸ÃʱװƤ·ôÎïÆ·ÐÅÏ¢£¡ClothesCoatSkin.txt! ID=%s" % clothesCoatSkinID, playerID)  | 
|         return False  | 
|       | 
|     clotherSkinInfo = clothesCoatSkinDict[clothesCoatSkinID]  | 
|     if not clotherSkinInfo:  | 
|         return False  | 
|   | 
|     curSkinIndex = clotherSkinInfo[0]  | 
|     skinOpenState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinOpenState)  | 
|     curSkinState = skinOpenState & pow(2, curSkinIndex)  | 
|       | 
|     if curSkinState:  | 
| #        GameWorld.Log("¸ÃʱװƤ·ôÒѾ¼¤»î¹ý£¡clothesCoatSkinID=%s,index=%s,skinOpenState=%s"   | 
| #                      % (clothesCoatSkinID, curSkinIndex, skinOpenState), playerID)  | 
| #        PlayerControl.NotifyCode(curPlayer, "GeRen_hgg_850801")  | 
|         #µ±Ê¹ÓÃÒѼ¤»î¹ýµÄʱװ¼¤»îµÀ¾ßʱ£¬½«»ñµÃÒ»¶¨ÊýÁ¿µÄÎïÆ· ·µ»ØTure¿Û³ýÎïÆ·  | 
|         clothesCoatUpDict = ReadChConfig.GetEvalChConfig("ClothesCoatUp")  | 
|         if curSkinIndex not in clothesCoatUpDict:  | 
|             PlayerControl.NotifyCode(curPlayer, "GeRen_hgg_850801")  | 
|             return False  | 
|         packSpace = ItemCommon.GetItemPackSpace(curPlayer, IPY_GameWorld.rptItem, 1)  | 
|         if packSpace < 1:  | 
|             PlayerControl.NotifyCode(curPlayer, "GeRen_chenxin_998371")  | 
|             return False  | 
|         itemId, getItemCntList = clothesCoatUpDict[curSkinIndex]  | 
|         if not getItemCntList:  | 
|             return False  | 
|         getItemCnt = getItemCntList[0]  | 
|           | 
|         packIndexList = [IPY_GameWorld.rptItem]  | 
|         ItemControler.GivePlayerItem(curPlayer, itemId, getItemCnt, True, packIndexList, True, showSysInfo=True)  | 
|         return True  | 
|   | 
|     isOK = ItemControler.PutItemInTempSwap(curPlayer, clothesCoatSkinID)  | 
|     if not isOK:  | 
|         GameWorld.Log("ʱװ·ÅÈëÁÙʱ½»»»±³°üʧ°Ü£¡", playerID)  | 
|         return False  | 
|   | 
|     #===========================================================================  | 
|     # # »ñµÃ·ÅÈëµÄʱװ  | 
|     # clothesItem = ItemCommon.FindItemInPackByItemID(curPlayer, clothesCoatSkinID,  | 
|     #                                                ShareDefine.rptTempSwap)  | 
|     # if not clothesItem:  | 
|     #    return False  | 
|     #   | 
|     # tick = GameWorld.GetGameWorld().GetTick()  | 
|     # ChEquip.DoPlayerEquipItem(curPlayer, clothesItem, IPY_GameWorld.retClothesCoat, tick)  | 
|     #===========================================================================  | 
|       | 
|     # ¸üлû¯×´Ì¬  | 
|     updSkinOpenState = skinOpenState | pow(2, curSkinIndex)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ClothesSkinOpenState, updSkinOpenState)  | 
|     Sync_ClothesCoatSkinInfo(curPlayer, curSkinIndex)  | 
|     GameWorld.Log("ʱװ¼¤»î³É¹¦£¡clothesCoatSkinID=%s,index=%s,skinOpenState=%s,updSkinOpenState=%s"   | 
|                   % (clothesCoatSkinID, curSkinIndex, skinOpenState, updSkinOpenState), playerID)  | 
|   | 
|     # Ë¢ÊôÐÔ  | 
|     PlayerControl.PlayerControl(curPlayer).RefreshPlayerAttrState()  | 
|       | 
|     ClothesCoaSkinNotifyDict = ReadChConfig.GetEvalChConfig("ClothesCoaSkinNotify")  | 
|     if clothesCoatSkinID in ClothesCoaSkinNotifyDict:  | 
|         notifyMark = ClothesCoaSkinNotifyDict[clothesCoatSkinID]  | 
|         PlayerControl.WorldNotify(0, notifyMark, [curPlayer.GetPlayerName(), clothesCoatSkinID])  | 
|           | 
|     return True  | 
|   | 
|   | 
| #// A5 0B Íæ¼ÒʱװÉý¼¶ #tagCMCoatUp  | 
| #  | 
| #struct    tagCMCoatUp  | 
| #{  | 
| #    tagHead        Head;  | 
| #    BYTE        CoatIndex; // Ê±×°Ë÷Òý  | 
| #};  | 
| def OnPlayerCoatUp(index, clientData, tick):  | 
|     curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     coatIndex = clientData.CoatIndex  | 
|     clothesCoatUpDict = ReadChConfig.GetEvalChConfig("ClothesCoatUp")  | 
|     if coatIndex not in clothesCoatUpDict:  | 
|         GameWorld.DebugLog("clothesCoatUpDict.txt Î´ÅäÖøÃË÷Òý coatIndex=%s" % (coatIndex), playerID)  | 
|         return  | 
|       | 
|     skinOpenState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinOpenState)  | 
|     if not skinOpenState & pow(2, coatIndex):  | 
|         GameWorld.DebugLog("¸Ãʱװδ¼¤»î, ÎÞ·¨Éý¼¶! coatIndex=%s" % (coatIndex), playerID)  | 
|         return  | 
|     costItemID, needCntList = clothesCoatUpDict[coatIndex]  | 
|       | 
|     curSkinLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinLV % coatIndex)  | 
|   | 
|       | 
|     if curSkinLV >= len(needCntList) - 1:  | 
|         GameWorld.DebugLog("¸ÃʱװÒÑÂú¼¶£¬ÎÞ·¨Éý¼¶!coatIndex=%s,curSkinLV=%s" % (coatIndex, curSkinLV), playerID)  | 
|         return  | 
|     plusCost = needCntList[curSkinLV + 1]  | 
|     curBless = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinCurBless % coatIndex)  | 
|     lackCnt = plusCost - curBless  | 
|       | 
|     curPack = curPlayer.GetItemManager().GetPack(IPY_GameWorld.rptItem)  | 
|     hasEnough, itemIndexList = ItemCommon.GetItem_FromPack_ByID(costItemID, curPack, lackCnt)  | 
|     if not itemIndexList:  | 
|         GameWorld.DebugLog("OnPlayerCoatUp() Ê±×°Éý¼¶²ÄÁϲ»×ã, needCnt=%s" % (lackCnt))  | 
|         return  | 
|     if hasEnough:  | 
|         reduceCnt = lackCnt  | 
|         aftBless = 0 #Éý¼¶ºóµÄ¾ÑéÖµ  | 
|         aftlv = curSkinLV + 1  | 
|     else:  | 
|         reduceCnt = 0  | 
|         for itemIndex in itemIndexList:  | 
|             curItem = curPack.GetAt(itemIndex)  | 
|             itemCount = curItem.GetCount()  | 
|             reduceCnt += itemCount  | 
|               | 
|         aftBless = curBless + reduceCnt  | 
|         aftlv = curSkinLV  | 
|           | 
|     #¿Û³ýÎïÆ·  | 
|     ItemCommon.ReduceItem(curPlayer, curPack, itemIndexList, reduceCnt, True)  | 
|     if aftlv != curSkinLV:  | 
|         PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ClothesSkinLV % coatIndex, aftlv)  | 
|     if aftBless != curBless:  | 
|         PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_ClothesSkinCurBless % coatIndex, aftBless)  | 
|       | 
|     Sync_ClothesCoatSkinInfo(curPlayer, coatIndex)  | 
|     # Ë¢ÊôÐÔ  | 
|     PlayerControl.PlayerControl(curPlayer).RefreshPlayerAttrState()  | 
|       | 
|     clothesID = GetClothesIDByIndex(coatIndex)  | 
|     itemData = GameWorld.GetGameData().GetItemByTypeID(clothesID)  | 
|     coatName = clothesID if not itemData else itemData.GetName()  | 
|     EventReport.WriteEvent_coat_lv(curPlayer, coatName, curSkinLV, curBless, reduceCnt, aftlv, aftBless)  | 
|     GameWorld.DebugLog("ʱװÉý¼¶!coatIndex=%s,coatName=%s,curSkinLV=%s,curBless=%s,reduceCnt=%s,aftlv=%s,aftBless=%s"   | 
|                        % (coatIndex, coatName, curSkinLV, curBless, reduceCnt, aftlv, aftBless), playerID)  | 
|     return  | 
|   | 
|   | 
| ## ¼ÆËãʱװÊôÐÔ  | 
| #  @param curPlayer Íæ¼Ò  | 
| #  @param allAttrList ÊôÐÔÁÐ±í  | 
| #  @return None  | 
| def CalcClothesCoatSkinAttr(curPlayer, allAttrList):  | 
|     skinOpenState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinOpenState)  | 
|       | 
|     if not skinOpenState:  | 
|         return  | 
|       | 
|     clothesCoatSkinDict = ReadChConfig.GetEvalChConfig("ClothesCoatSkin")  | 
|     clothesCoatUpDict = ReadChConfig.GetEvalChConfig("ClothesCoatUp")  | 
|     for clothesInfo in clothesCoatSkinDict.values():  | 
|         curIndex, attrDict = clothesInfo  | 
|           | 
|         if not skinOpenState & pow(2, curIndex):  | 
|             continue  | 
|         skinLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinLV % curIndex)  | 
|         curExp = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinCurBless % curIndex)  | 
|         if curIndex not in clothesCoatUpDict:  | 
|             expPer = 0  | 
|         else:  | 
|             itemCntList = clothesCoatUpDict[curIndex][1]  | 
|             maxExp = itemCntList[skinLV + 1] if skinLV + 1 < len(itemCntList) else 0  | 
|             expPer = float(curExp) / maxExp if maxExp != 0 else 0  | 
|         for attrName, attrLVList in attrDict.items():  | 
|             attrValue = attrLVList[skinLV] if skinLV < len(attrLVList) else attrLVList[-1]  | 
|             nextValue = attrLVList[skinLV + 1] if skinLV + 1 < len(attrLVList) else attrLVList[-1]  | 
|             addValue = int(math.ceil((nextValue-attrValue) * expPer))  | 
|             PlayerControl.CalcAttrDict_Type(attrName, attrValue + addValue, allAttrList)  | 
|             GameWorld.DebugLog('    Ê±×°ÊôÐÔcurIndex=%s attrName=%s,expPer=%s,attrValue=%s,addValue=%s' % (curIndex, attrName, expPer, attrValue, addValue))  | 
|           | 
|     return  | 
|   | 
|   | 
| ##´©ÉÏ/ÍÑÏÂʱװ  | 
| # @param curPlayer Íæ¼ÒʵÀý  | 
| # @param srcBackpack Æðµã±³°ü  | 
| # @param desBackPack Ä¿±ê±³°ü  | 
| # @param srcIndex ÆðµãË÷Òý  | 
| # @param destIndex Ä¿±êË÷Òý  | 
| # @return   | 
| def SwitchCoat(curPlayer, srcBackpack, desBackPack, srcIndex, destIndex):  | 
|     if not ((desBackPack == IPY_GameWorld.rptEquip and srcBackpack == ShareDefine.rptTempSwap and destIndex == IPY_GameWorld.retClothesCoat) \  | 
|     or (srcBackpack == IPY_GameWorld.rptEquip and desBackPack == ShareDefine.rptTempSwap and srcIndex == IPY_GameWorld.retClothesCoat)):  | 
|         return False  | 
|       | 
|     #===============================================================================================  | 
|     # # ²»¿ÉÒÔ½»»»Ê±×°  | 
|     # if curPlayer.GetMapID() in [ChConfig.Def_FBMapID_RedFort]:  | 
|     #    PlayerControl.NotifyCode(curPlayer, "GeRen_liubo_422737")  | 
|     #    return True  | 
|     #===============================================================================================  | 
|       | 
|     playerID = curPlayer.GetPlayerID()  | 
|     clothesIndex = IPY_GameWorld.retClothesCoat  | 
|       | 
|     # ´©Ê±×°  | 
|     if desBackPack == IPY_GameWorld.rptEquip:  | 
|         # ´©µÄʱºòsrcIndex´ú±íʱװµÄË÷Òý  | 
|         skinOpenState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinOpenState)  | 
|         if not skinOpenState & pow(2, srcIndex):  | 
|             GameWorld.Log("ʱװδ¼¤»î£¬²»ÄÜ´©£¡state=%s,srcIndex=%s" % (skinOpenState, srcIndex), playerID)  | 
|             return True  | 
|           | 
|         findSwichClothesID = GetClothesIDByIndex(srcIndex)  | 
|               | 
|         if findSwichClothesID <= 0:  | 
|             GameWorld.Log("ÕÒ²»µ½Ê±×°ID£¬²»ÄÜ´©£¡srcIndex=%s" % (srcIndex), playerID)  | 
|             return True  | 
|           | 
|         # ÕÒµ½Ê±×°IDºó£¬srcIndexת»¯Îª½»»»ÎïÆ·µÄ¸ñ×ÓË÷Òý£¬Ä¬ÈÏ0  | 
|         srcIndex, destIndex = 0, clothesIndex  | 
|           | 
|         # ¸øÁÙʱ½»»»ÎïÆ·  | 
|         if not ItemControler.PutItemInTempSwap(curPlayer, findSwichClothesID):  | 
|             GameWorld.Log("ʱװID(%s)·ÅÈëÁÙʱ½»»»±³°üʧ°Ü!" % (findSwichClothesID), playerID)  | 
|             return True  | 
|           | 
|     # ÍÑʱװ  | 
|     else:  | 
|         ItemControler.ClearPack(curPlayer, ShareDefine.rptTempSwap)  | 
|         srcIndex, destIndex = clothesIndex, 0  | 
|       | 
|     isOK = ItemCommon.DoLogicSwitchItemEx(curPlayer, srcBackpack, desBackPack, srcIndex, destIndex)  | 
|     # Ï·¢ÖÜÎ§Íæ¼Òʱװ´©´÷״̬  | 
|     if isOK:  | 
|         if desBackPack == IPY_GameWorld.rptEquip:  | 
|             packIndex = IPY_GameWorld.rptEquip  | 
|             itemPlace = IPY_GameWorld.retClothesCoat  | 
|             itemPack = curPlayer.GetItemManager().GetPack(packIndex)  | 
|             curItem = itemPack.GetAt(itemPlace)  | 
|             itemId = curItem.GetItemTypeID()  | 
|             itemStarLV = 0#curItem.GetItemStarLV()  | 
|             itemHoleCnt = 0#curItem.GetCanPlaceStoneCount()  | 
|             itemStoneCnt = 0#curItem.GetUseStoneCount()  | 
|             itemUseData = curItem.GetUserData()  | 
|             curPlayer.ChangeEquip(itemId, itemPlace, itemStarLV, itemHoleCnt, itemStoneCnt, itemUseData)  | 
|         else:  | 
|             packIndex = ShareDefine.rptTempSwap  | 
|             itemPack = curPlayer.GetItemManager().GetPack(packIndex)  | 
|             curItem = itemPack.GetAt(0)  | 
|             if curItem:  | 
|                 curPlayer.Sync_UnEquipItem(curItem.GetItemTypeID(), IPY_GameWorld.retClothesCoat)  | 
|     GameWorld.DebugLog("SwitchCoat isOK=%s" % isOK)  | 
|     return True  | 
|   | 
| ## ¸ù¾ÝË÷Òý»ñȡװ±¸Ê±×°ID  | 
| def GetClothesIDByIndex(coatIndex):  | 
|     findSwichClothesID = 0  | 
|     clothesCoatSkinDict = ReadChConfig.GetEvalChConfig("ClothesCoatSkin")  | 
|     for clothesID, clothesInfo in clothesCoatSkinDict.items():  | 
|         index = clothesInfo[0]  | 
|         if index == coatIndex:  | 
|             findSwichClothesID = clothesID  | 
|             break  | 
|     return findSwichClothesID  | 
|   | 
| ## Í¨Öª¿Í»§¶Ëʱװ¿ªÆô״̬  | 
| #  @param curPlayer  | 
| #  @return None  | 
| def Sync_ClothesCoatSkinInfo(curPlayer, coatIndex= -1):  | 
|     stateData = ChPyNetSendPack.tagMCClothesCoatSkinState()  | 
|     stateData.Clear()  | 
|     stateData.SkinOpenState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinOpenState)  | 
|     stateData.CoatInfoList = []  | 
|   | 
|     if coatIndex == -1:  | 
|         clothesCoatSkinDict = ReadChConfig.GetEvalChConfig("ClothesCoatSkin")  | 
|         for index in range(len(clothesCoatSkinDict)):  | 
|             coatInfo = ChPyNetSendPack.tagMCClothesCoatLVInfo()  | 
|             coatInfo.Clear()  | 
|             coatInfo.CoatIndex = index  | 
|             coatInfo.CoatLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinLV % index)  | 
|             coatInfo.CoatExp = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinCurBless % index)  | 
|             stateData.CoatInfoList.append(coatInfo)  | 
|     else:  | 
|         coatInfo = ChPyNetSendPack.tagMCClothesCoatLVInfo()  | 
|         coatInfo.Clear()  | 
|         coatInfo.CoatIndex = coatIndex  | 
|         coatInfo.CoatLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinLV % coatIndex)  | 
|         coatInfo.CoatExp = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_ClothesSkinCurBless % coatIndex)  | 
|         stateData.CoatInfoList.append(coatInfo)  | 
|       | 
|     stateData.CoatNum = len(stateData.CoatInfoList)  | 
|     NetPackCommon.SendFakePack(curPlayer, stateData)  | 
|     return  | 
|   |