| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package Player.PlayerMergeKing  | 
| #  | 
| # @todo:¿ç·þÍõÕßÕù°Ô  | 
| # @author hxp  | 
| # @date 2015-11-18  | 
| # @version 1.0  | 
| #  | 
| # ÏêϸÃèÊö: ¿ç·þÍõÕßÕù°Ô  | 
| #  | 
| #---------------------------------------------------------------------  | 
| """Version = 2015-11-18 18:30"""  | 
| #---------------------------------------------------------------------  | 
|   | 
| import ShareDefine  | 
| import ReadChConfig  | 
| import PlayerControl  | 
| import ChPyNetSendPack  | 
| import DataRecordPack  | 
| import NetPackCommon  | 
| import IPY_GameWorld  | 
| import ItemControler  | 
| import ItemCommon  | 
| import GameWorld  | 
| import ChConfig  | 
|   | 
|   | 
| (  | 
| MergeKing_GSQType_Support,  # Ñº×¢  | 
| MergeKing_GSQType_Bribe, # »ß¸  | 
| MergeKing_GSQType_SupportAward, # ÁìÈ¡»ý·Ö½±Àø  | 
| ) = range(3)  | 
|   | 
| ## ¿ç·þÍõÕßÕù°Ô»î¶¯ID±ä¸ü  | 
| def OnMergeKingActionChange(key, value):  | 
|     # ¿ç·þ·þÎñÆ÷²»´¦Àí  | 
|     if GameWorld.IsMergeServer():  | 
|         return  | 
|       | 
|     gameWorld = GameWorld.GetGameWorld()  | 
|     mapKeyValue = gameWorld.GetGameWorldDictByKey(key)  | 
|     if mapKeyValue == value:  | 
|         return  | 
|       | 
|     GameWorld.Log("¿ç·þÍõÕßÕù°Ô»î¶¯ID±ä¸ükey=%s,value=%s,mapKeyValue=%s" % (key, value, mapKeyValue))  | 
|       | 
|     for i in range(0, GameWorld.GetPlayerManager().GetPlayerCount()):  | 
|         curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(i)  | 
|           | 
|         if curPlayer == None or not curPlayer.GetPlayerID():  | 
|             continue  | 
|       | 
|         #¼ì²éÖØÖÃÍæ¼ÒÐÅÏ¢  | 
|         __CheckResetPlayerMergeKingData(curPlayer, key, value)  | 
|     return  | 
|   | 
| ## Çå³ýÍæ¼ÒÐÅÏ¢  | 
| #  @param curPlayer: Íæ¼Ò  | 
| #  @param key: Ìػݻkey£¬Ä¬ÈÏÈ«²¿¼ì²é£¬Ò»°ãµÇ¼ʱȫ²¿¼ì²é  | 
| #  @return: None  | 
| def __CheckResetPlayerMergeKingData(curPlayer, key, value, isSync=True):  | 
|     playerID = curPlayer.GetPlayerID()  | 
|       | 
|     # Íæ¼ÒÉíÉÏÊý¾ÝµÄ»î¶¯id  | 
|     playerActionIDKey = ChConfig.Def_PDict_MergeKing_ID % key  | 
|     playerActionID = __GetMergeKingPDictValue(curPlayer, playerActionIDKey)  | 
|     if playerActionID == value:  | 
|         GameWorld.DebugLog("¿ç·þÍõÕßÕù°Ô»î¶¯IDÏàͬ£¬²»´¦Àíkey=%s,actionID=%s" % (key, value), playerID)  | 
|         return  | 
|     GameWorld.Log("¿ç·þÍõÕßÕù°Ô»î¶¯ID²»Í¬£¬Êý¾ÝÖØÖÃkey=%s,actionID=%s,playerActionID=%s"   | 
|                   % (key, value, playerActionID), playerID)  | 
|     __SetMergeKingPDictValue(curPlayer, playerActionIDKey, value)  | 
|   | 
|     # Õù°ÔÈü¿ªÊ¼Ê±ÖØÖà  | 
|     if key == ShareDefine.Def_Notify_WorldKey_Merge_KingStartID:  | 
|         __SetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_SupportAward, 0)  | 
|           | 
|     # Õù°ÔÈü½áÊøÊ±ÖØÖà  | 
|     elif key == ShareDefine.Def_Notify_WorldKey_Merge_KingOverID:  | 
|         __SetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_ServerHonourAward, 0)  | 
|         __SetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_RankAward, 0)  | 
|           | 
|     if isSync:  | 
|         Sync_MergeKingAward(curPlayer)  | 
|               | 
|     return  | 
|   | 
| def MergeKingOnDay(curPlayer):  | 
|     __SetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_Worship, 0)  | 
|     Sync_MergeKingAward(curPlayer)  | 
|     return  | 
|   | 
| ## OnLogin´¦Àí  | 
| def MergePKOnLogin(curPlayer):  | 
|     gameWorld = GameWorld.GetGameWorld()  | 
|     for idKey in [ShareDefine.Def_Notify_WorldKey_Merge_KingStartID, ShareDefine.Def_Notify_WorldKey_Merge_KingOverID]:  | 
|         __CheckResetPlayerMergeKingData(curPlayer, idKey, gameWorld.GetGameWorldDictByKey(idKey), False)  | 
|           | 
|     Sync_MergeKingAward(curPlayer)  | 
|     return  | 
|   | 
| def __GetMergeKingPDictValue(curPlayer, key, defaultVlaue=0):  | 
|     return curPlayer.NomalDictGetProperty(key, defaultVlaue, ChConfig.Def_PDictType_MergeKing)  | 
|   | 
| def __SetMergeKingPDictValue(curPlayer, key, value):  | 
|     value = min(value, ChConfig.Def_UpperLimit_DWord)  | 
|     return PlayerControl.NomalDictSetProperty(curPlayer, key, value, ChConfig.Def_PDictType_MergeKing)  | 
|   | 
| #//C1 08 Íæ¼ÒÖ´ÐÐĤ°Ý #tagPlayerMergeWorship  | 
| #  | 
| #struct    tagPlayerMergeWorship  | 
| #{  | 
| #    tagHead        Head;  | 
| #};  | 
| def Client_MergeKingWorship(index, clientData, tick):  | 
|     curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     GameWorld.Log("Íæ¼Ò½øÐÐĤ°Ý!", playerID)  | 
|   | 
|     state = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_Worship)  | 
|       | 
|     if state:  | 
|         GameWorld.Log("    ½ñÈÕÒѾĤ°Ý¹ý!", playerID)  | 
|         return  | 
|       | 
|     worshipInfo = ReadChConfig.GetEvalChConfig("MergeKing_WorshipAward")  | 
|     baseGongXun, multipleRateList = worshipInfo  | 
|     multiple = GameWorld.GetResultByRandomList(multipleRateList)  | 
|     giveGongXun = baseGongXun * multiple  | 
|       | 
|     if multiple > 1:  | 
|         PlayerControl.NotifyCode(curPlayer, "GeRen_liubo_412986", [multiple, giveGongXun])  | 
|     else:  | 
|         PlayerControl.NotifyCode(curPlayer, "GeRen_liubo_155554", [giveGongXun])  | 
|       | 
|     __SetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_Worship, 1)  | 
|       | 
|     #PlayerControl.GiveMoney(curPlayer, ShareDefine.TYPE_Price_GongXun, giveGongXun, "Worship")  | 
|       | 
|     GameWorld.Log("    Ä¤°Ý½±ÀøOK!multiple=%s,giveGongXun=%s" % (multiple, giveGongXun), playerID)  | 
|     Sync_MergeKingAward(curPlayer)  | 
|     return  | 
|   | 
| #// C1 11 ¿ç·þÍõÕßÕù°ÔѺע #tagCMMergeKingSupport  | 
| #  | 
| #struct    tagCMMergeKingSupport  | 
| #{  | 
| #    tagHead        Head;  | 
| #    BYTE        SupportType;    // 0-Ѻע; 1-»ß¸  | 
| #    BYTE        GroupType;    // ×éÀàÐÍ£»Èç32Ç¿¡¢16Ç¿µÈ  | 
| #    BYTE        BattleIndex;    // ¶ÔÕ½×éË÷Òý£¬Ã¿ÖÖ×éÀàÐ͵ÄË÷Òý´Ó1¿ªÊ¼  | 
| #    BYTE        MoneyType;    // Ñº×¢µÄ»õ±ÒÀàÐÍ  | 
| #    BYTE        AccIDLen;  | 
| #    char        TagAccID[AccIDLen];    // Ö§³ÖµÄÄ¿±êÕ˺Š | 
| #};  | 
| def Client_MergeKingSupport(index, clientData, tick):  | 
|     curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  | 
|     playerID = curPlayer.GetPlayerID()  | 
|       | 
|     supportType = clientData.SupportType  | 
|     groupType = clientData.GroupType  | 
|     battleIndex = clientData.BattleIndex  | 
|     moneyType = clientData.MoneyType  | 
|     tagAccID = clientData.TagAccID  | 
|       | 
|     GameWorld.Log("¿ç·þÍõÕßÕù°ÔѺע SupportType=%s,GroupType=%s,BattleIndex=%s,MoneyType=%s,TagAccID=%s"   | 
|                   % (supportType, groupType, battleIndex, moneyType, tagAccID), playerID)  | 
|       | 
|     if supportType not in [0, 1]:  | 
|         return  | 
|       | 
|     supportDict = ReadChConfig.GetEvalChConfig("MergeKing_Support")  | 
|     groupSupportDict = supportDict.get(groupType)  | 
|       | 
|     if not groupSupportDict:  | 
|         GameWorld.ErrLog("    Õù°Ô¶ÔÕ½×éδÅäÖÃѺעÐÅÏ¢!groupType=%s" % groupType)  | 
|         return  | 
|       | 
|     supportInfoList = groupSupportDict.get(moneyType)  | 
|     if not supportInfoList:  | 
|         GameWorld.ErrLog("    Õù°Ô¶ÔÕ½×éδÅäÖûõ±ÒÀàÐͶÔÓ¦µÄѺעÐÅÏ¢!groupType=%s,moneyType=%s" % (groupType, moneyType))  | 
|         return  | 
|       | 
|     supportMoney, bribeMoney, score, unSupportScore = supportInfoList  | 
|       | 
|     # Ñº×¢  | 
|     if supportType == 0:  | 
|         costMoney = supportMoney  | 
|         queryType = MergeKing_GSQType_Support  | 
|           | 
|     # »ß¸Ѻע»ý·Ö  | 
|     elif supportType == 1:  | 
|         costMoney = bribeMoney  | 
|         queryType = MergeKing_GSQType_Bribe  | 
|           | 
|     if costMoney <= 0:  | 
|         GameWorld.ErrLog("    ²»Ö§³Ö¸Ã»õ±ÒѺע/»ß¸!moneyType=%s,costMoney=%s" % (moneyType, costMoney))  | 
|         return  | 
|       | 
|     if not PlayerControl.HaveMoney(curPlayer, moneyType, costMoney):  | 
|         GameWorld.Log("    »õ±Ò²»×ãѺע/»ß¸!moneyType=%s,costMoney=%s" % (moneyType, costMoney))  | 
|         return  | 
|   | 
|     queryList = [queryType, groupType, battleIndex, moneyType, costMoney, tagAccID, score, unSupportScore]  | 
|     MergeKing_GameServerQuery(curPlayer, queryList)  | 
|     return  | 
|   | 
| def MergeKing_GameServerQuery(curPlayer, queryList):  | 
|     queryListStr = str(queryList)  | 
|     GameWorld.GetPlayerManager().GameServer_QueryPlayerResult(curPlayer.GetID(), 0, 0, 'MergeKing',   | 
|                                                               queryListStr, len(queryListStr))  | 
|     return  | 
|   | 
| def MergeKing_GameServerQueryResult(curPlayer, resultInfo):  | 
|     resultType = resultInfo[0]  | 
|     resultParamList = resultInfo[1:]  | 
|       | 
|     if resultType == MergeKing_GSQType_Support:  | 
|         return __DoLogic_GSQResult_Support(curPlayer, resultParamList)  | 
|           | 
|     elif resultType == MergeKing_GSQType_Bribe:  | 
|         return __DoLogic_GSQResult_Bribe(curPlayer, resultParamList)  | 
|           | 
|     elif resultType == MergeKing_GSQType_SupportAward:  | 
|         return __DoLogic_GSQResult_SupportAward(curPlayer, resultParamList)  | 
|           | 
|     return  | 
|   | 
| def __DoLogic_GSQResult_Support(curPlayer, resultParamList):  | 
|     moneyType, costMoney = resultParamList  | 
|     if not PlayerControl.PayMoney(curPlayer, moneyType, costMoney, ChConfig.Def_Cost_MergeKingSupport):  | 
|         return False  | 
|     return  | 
|   | 
| def __DoLogic_GSQResult_Bribe(curPlayer, resultParamList):  | 
|     moneyType, costMoney = resultParamList  | 
|     if not PlayerControl.PayMoney(curPlayer, moneyType, costMoney, ChConfig.Def_Cost_MergeKingBribe):  | 
|         return False  | 
|     return  | 
|   | 
| ## Áìȡȫ·þÈÙÒ«½±Àø  | 
| def GetMergeKingAward_ServerHonour(curPlayer):  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     GameWorld.Log("Íæ¼ÒÁìȡȫ·þÈÙÒ«½±Àø!", playerID)  | 
|       | 
|     awardState = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_ServerHonourAward)  | 
|       | 
|     if awardState:  | 
|         GameWorld.Log("    ÒÑÁìÈ¡¹ýÈ«·þÈÙÒ«½±Àø!", playerID)  | 
|         return  | 
|       | 
|     honourAwardDict = ReadChConfig.GetEvalChConfig("MergeKing_ServerHonourAward")  | 
|     serverHonour = GameWorld.GetGameWorld().GetGameWorldDictByKey(ShareDefine.Def_Notify_WorldKey_Merge_KingServerHonour)  | 
|     awardInfo = honourAwardDict.get(serverHonour, [])  | 
|     if not awardInfo:  | 
|         GameWorld.Log("    Ã»ÓиÃÈ«·þÈÙÒ«½±Àø!serverHonour=%s" % serverHonour, playerID)  | 
|         return  | 
|       | 
|     needSpace, awardItemList = awardInfo  | 
|   | 
|     # ¼ì²é±³°ü  | 
|     packSpace = ItemCommon.GetItemPackSpace(curPlayer, IPY_GameWorld.rptItem)  | 
|     if needSpace > packSpace:  | 
|         PlayerControl.NotifyCode(curPlayer, "GeRen_chenxin_998371")  | 
|         return  | 
|       | 
|     __SetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_ServerHonourAward, 1)  | 
|   | 
|     __GivePlayerAwardItem(curPlayer, awardItemList, "MergeKingServerHonour", {"serverHonour":serverHonour})  | 
|       | 
|     GameWorld.Log("    Áìȡȫ·þÈÙÒ«½±ÀøOK!serverHonour=%s" % (serverHonour), playerID)  | 
|     Sync_MergeKingAward(curPlayer)  | 
|     return  | 
|   | 
| ## ÁìÈ¡½ú¼¶ÅÅÃû½±Àø  | 
| def GetMergeKingAward_Rank(curPlayer):  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     GameWorld.Log("Íæ¼ÒÁìÈ¡½ú¼¶ÅÅÃû½±Àø!", playerID)  | 
|       | 
|     rankAwardState = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_RankAward)  | 
|       | 
|     if rankAwardState:  | 
|         GameWorld.Log("    ÒÑÁìÈ¡¹ý½ú¼¶ÅÅÃû½±Àø!", playerID)  | 
|         return  | 
|       | 
|     rankAwardDict = ReadChConfig.GetEvalChConfig("MergeKing_RankAward")  | 
|     playerRank = PlayerControl.GetMergeWarRank(curPlayer)  | 
|     awardInfo = rankAwardDict.get(playerRank, [])  | 
|     if not awardInfo:  | 
|         GameWorld.Log("    Ã»Óиýú¼¶ÅÅÃû½±Àø!playerRank=%s" % playerRank, playerID)  | 
|         return  | 
|       | 
|     needSpace, awardItemList = awardInfo  | 
|   | 
|     # ¼ì²é±³°ü  | 
|     packSpace = ItemCommon.GetItemPackSpace(curPlayer, IPY_GameWorld.rptItem)  | 
|     if needSpace > packSpace:  | 
|         PlayerControl.NotifyCode(curPlayer, "GeRen_chenxin_998371")  | 
|         return  | 
|       | 
|     __SetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_RankAward, 1)  | 
|   | 
|     __GivePlayerAwardItem(curPlayer, awardItemList, "MergeKingRank", {"playerRank":playerRank})  | 
|       | 
|     GameWorld.Log("    ÁìÈ¡½ú¼¶ÅÅÃû½±ÀøOK!playerRank=%s" % (playerRank), playerID)  | 
|     Sync_MergeKingAward(curPlayer)  | 
|     return  | 
|   | 
| ## ÁìÈ¡¾º²Â»ý·Ö½±Àø  | 
| def GetMergeKingAward_Support(curPlayer, index, tick):  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     GameWorld.Log("Íæ¼ÒÁìÈ¡¾º²Â»ý·Ö½±Àø£º index=%s" % index, playerID)  | 
|       | 
|     state = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_SupportAward)  | 
|     if state & pow(2, index):  | 
|         GameWorld.Log("    ÒÑÁìÈ¡¹ý¸Ã½±Àøindex=%s,state=%s" % (index, state), playerID)  | 
|         return  | 
|   | 
|     if not GameWorld.CheckPlayerTick(curPlayer, ChConfig.TYPE_Player_Tick_MergeKingSupport, tick):  | 
|         GameWorld.Log("    ÁìÈ¡¾º²Â»ý·Ö½±Àø Áì½±ÇëÇóCDÖУ¡", playerID)  | 
|         return  | 
|       | 
|     queryList = [MergeKing_GSQType_SupportAward, index]  | 
|     MergeKing_GameServerQuery(curPlayer, queryList)  | 
|     return  | 
|   | 
| def __DoLogic_GSQResult_SupportAward(curPlayer, resultParamList):  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     index, totalScore = resultParamList  | 
|       | 
|     supportAwardList = ReadChConfig.GetEvalChConfig("MergeKing_SupportAward")  | 
|       | 
|     if index < 0 or index > len(supportAwardList) - 1:  | 
|         GameWorld.Log("    ¾º²Â»ý·Ö½±ÀøË÷ÒýÔ½½ç index=%s" % (index), playerID)  | 
|         return  | 
|       | 
|     awardInfo = supportAwardList[index]  | 
|     needScore, needSpace, awardItemList = awardInfo  | 
|   | 
|     # ¼ì²é±³°ü  | 
|     packSpace = ItemCommon.GetItemPackSpace(curPlayer, IPY_GameWorld.rptItem)  | 
|     if needSpace > packSpace:  | 
|         PlayerControl.NotifyCode(curPlayer, "GeRen_chenxin_998371")  | 
|         return  | 
|       | 
|     if totalScore < needScore:  | 
|         GameWorld.Log("    ¾º²Â»ý·ÖÁì½±»ý·Ö²»×ã index=%s,needScore=%s,totalScore=%s"   | 
|                       % (index, needScore, totalScore), playerID)  | 
|         return  | 
|       | 
|     state = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_SupportAward)  | 
|     if state & pow(2, index):  | 
|         GameWorld.Log("    ¾º²Â»ý·ÖÁì½±ÒÑÁìÈ¡¹ý¸Ã½±Àøindex=%s,state=%s" % (index, state), playerID)  | 
|         return  | 
|       | 
|     newState = state | pow(2, index)  | 
|     __SetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_SupportAward, newState)  | 
|   | 
|     __GivePlayerAwardItem(curPlayer, awardItemList, "MergeKingSupport", {"index":index})  | 
|       | 
|     curPlayer.SetTickByType(ChConfig.TYPE_Player_Tick_MergeKingSupport, 0)  | 
|     GameWorld.Log("    ¾º²Â»ý·ÖÁì½±OK!index=%s,state=%s,newState=%s"   | 
|                   % (index, state, newState), playerID)  | 
|     Sync_MergeKingAward(curPlayer)  | 
|     return  | 
|   | 
| def __GivePlayerAwardItem(curPlayer, awardItemList, drName, drDataDict={}):  | 
|       | 
|     for itemID, itemCnt, isBind in awardItemList:  | 
|           | 
|         if itemCnt <= 0 or itemID <= 0:  | 
|             continue  | 
|           | 
|         packList = [IPY_GameWorld.rptItem, IPY_GameWorld.rptAnyWhere]  | 
|         ItemControler.GivePlayerItem(curPlayer, itemID, itemCnt, isBind, packList, True,   | 
|                                      event=[drName, True, drDataDict])  | 
|           | 
|     return  | 
|   | 
| ## Í¬²½¿ç·þÍõÕßÕù°Ô½±ÀøÏà¹Ø  | 
| def Sync_MergeKingAward(curPlayer):  | 
|     awardPack = ChPyNetSendPack.tagMCMergeKingAwardInfo()  | 
|     awardPack.Clear()  | 
|     awardPack.ServerHonour = GameWorld.GetGameWorld().GetGameWorldDictByKey(ShareDefine.Def_Notify_WorldKey_Merge_KingServerHonour)  | 
|     awardPack.ServerHonourAward = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_ServerHonourAward)  | 
|     awardPack.RankAward = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_RankAward)  | 
|     awardPack.SupportAward = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_SupportAward)  | 
|     awardPack.WorshipState = __GetMergeKingPDictValue(curPlayer, ChConfig.Def_PDict_MergeKing_Worship)  | 
|     NetPackCommon.SendFakePack(curPlayer, awardPack)  | 
|     return  | 
|       | 
|      |