| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package PlayerFamilyZhenbaoge  | 
| #  | 
| # @todo:Õ䱦¸ó  | 
| # @author hxp  | 
| # @date 2024-10-29  | 
| # @version 1.0  | 
| #  | 
| # ÏêϸÃèÊö: Õ䱦¸ó  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #"""Version = 2024-10-29 17:30"""  | 
| #-------------------------------------------------------------------------------  | 
|   | 
| import ChConfig  | 
| import ShareDefine  | 
| import PlayerFamilyAction  | 
| import IpyGameDataPY  | 
| import PlayerFamily  | 
| import GameWorld  | 
|   | 
| import random  | 
| import time  | 
|   | 
| ActionType = ShareDefine.Def_ActionType_Zhenbaoge  | 
| ActionGlobalID = 1 # È«¾Ö¼Ç¼ID  | 
|   | 
| # È«¾Ö¼Ç¼  | 
| #Time    ±¾ÂÖ¿ªÊ¼Ê±¼ä´Á  | 
| #Value1    ActionGlobalID  | 
| def GetFAPrice(gActionData): return gActionData.GetValue2()  | 
| def SetFAPrice(gActionData, price): return gActionData.SetValue2(price)  | 
| def GetFAIsNegative(gActionData): return gActionData.GetValue3() # ÊÇ·ñ¸ºÖµ  | 
| def SetFAIsNegative(gActionData, isNegative): return gActionData.SetValue3(isNegative)  | 
| def GetFAPriceFinal(gActionData):  | 
|     ## »ñÈ¡×îÖÕ¼Û¸ñ  | 
|     price = GetFAPrice(gActionData)  | 
|     isNegative = GetFAIsNegative(gActionData)  | 
|     return price if not isNegative else -price  | 
| def SetFAPriceFinal(gActionData, totalPrice):  | 
|     SetFAPrice(gActionData, abs(totalPrice))  | 
|     SetFAIsNegative(gActionData, 1 if totalPrice < 0 else 0)  | 
|     return  | 
| def GetFAItemList(gActionData): return eval(gActionData.GetUseData()) # ÎïÆ·ÁÐ±í  | 
| def SetFAItemList(gActionData, itemList):  | 
|     dataStr = str(itemList)  | 
|     gActionData.SetUseData(dataStr, len(dataStr))  | 
|     return  | 
|   | 
| # ³ÉÔ±¿³¼Û¼Ç¼  | 
| #Time    ¿³¼Ûʱ¼ä´Á  | 
| #Name    Íæ¼ÒÃû  | 
| def GetFAPlayerID(actionData): return actionData.GetValue1()  | 
| def SetFAPlayerID(actionData, playerID): return actionData.SetValue1(playerID)  | 
| def GetFACutPrice(actionData): return actionData.GetValue2() # Íæ¼Ò¿³Á˶àÉÙ  | 
| def SetFACutPrice(actionData, cutPrice): return actionData.SetValue2(cutPrice)  | 
| def GetFABuyState(actionData): return actionData.GetValue3() # Íæ¼ÒÊÇ·ñÒѹºÂò  | 
| def SetFABuyState(actionData, buyState): return actionData.SetValue3(buyState)  | 
|   | 
| def OnDayEx(family):  | 
|     OnZhenbaogeReset(family)          | 
|     return  | 
|   | 
| def OnZhenbaogeReset(family):  | 
|     ## Õ䱦¸óÖØÖà  | 
|       | 
|     familyID = family.GetID()  | 
|     curTime = int(time.time())  | 
|       | 
|     GameWorld.GetFamilyActionManager().DelFamilyAction(familyID, ActionType)  | 
|       | 
|     familyAction = GameWorld.GetFamilyActionManager().GetFamilyAction(familyID, ActionType)  | 
|     gActionData = familyAction.AddAction()  | 
|     gActionData.SetTime(curTime)  | 
|     gActionData.SetFamilyId(familyID)  | 
|     gActionData.SetActionType(ActionType)  | 
|     SetFAPlayerID(gActionData, ActionGlobalID)  | 
|       | 
|     initPrice = IpyGameDataPY.GetFuncCfg("Zhenbaoge", 2)  | 
|       | 
|     randItemList = []  | 
|     ipyDataMgr = IpyGameDataPY.IPY_Data()  | 
|     for index in range(ipyDataMgr.GetFamilyZhenbaogeItemCount()):  | 
|         ipyData = ipyDataMgr.GetFamilyZhenbaogeItemByIndex(index)  | 
|         randItemList.append([ipyData.GetItemWeight(), ipyData.GetItemList()])  | 
|           | 
|     itemList = GameWorld.GetResultByWeightList(randItemList, [])  | 
|       | 
|     SetFAPriceFinal(gActionData, initPrice)  | 
|     SetFAItemList(gActionData, itemList)  | 
|       | 
|     PlayerFamilyAction.SendFamilyAction(gActionData)  | 
|     GameWorld.DebugLog("Õ䱦¸óÖØÖÃ! familyID=%s,itemList=%s" % (familyID, itemList), familyID)      | 
|     return  | 
|   | 
| def GetZhenbaogeActionData(familyID, playerID):  | 
|     ## »ñÈ¡Action  | 
|     findActionData = None  | 
|     familyAction = GameWorld.GetFamilyActionManager().GetFamilyAction(familyID, ActionType)  | 
|     for index in range(0, familyAction.Count()):  | 
|         actionData = familyAction.At(index)  | 
|         if GetFAPlayerID(actionData) == playerID:  | 
|             findActionData = actionData  | 
|             break  | 
|     return findActionData  | 
|   | 
| def MapServer_ZhenbaogeOP(curPlayer, msgList):  | 
|     mapID = curPlayer.GetRealMapID()  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     GameWorld.DebugLog("MapServer_ZhenbaogeOP mapID=%s,msgList=%s" % (mapID, msgList), playerID)  | 
|     if not msgList:  | 
|         return  | 
|       | 
|     curFamily = curPlayer.GetFamily()  | 
|     if not curFamily:  | 
|         return  | 
|       | 
|     familyID = curFamily.GetID()  | 
|     gActionData = GetZhenbaogeActionData(familyID, ActionGlobalID)  | 
|     if not gActionData:  | 
|         GameWorld.DebugLog("Õ䱦¸ó»¹Ã»ÓÐË¢ÐÂ! familyID=%s" % familyID, playerID)  | 
|         return  | 
|       | 
|     msgType, dataMsg = msgList  | 
|     ret = None  | 
|       | 
|     if msgType == "Cut":  | 
|         cutPrice = CalcCutPrice(curFamily, gActionData, playerID)  | 
|         if cutPrice:  | 
|             AddCutPrice(familyID, playerID, curPlayer.GetName(), cutPrice, gActionData)  | 
|         ret = [cutPrice]  | 
|           | 
|     elif msgType == "Buy":  | 
|         buyRet = __DoZhenbaogeBuy(familyID, playerID, gActionData, dataMsg)  | 
|         if buyRet:  | 
|             ret = [True] + list(buyRet)  | 
|         else:  | 
|             ret = [False]  | 
|               | 
|     if ret == None:  | 
|         return  | 
|     return msgList + (ret if isinstance(ret, list) else [ret])  | 
|   | 
| def CalcCutPrice(curFamily, gActionData, playerID):  | 
|     ## ¼ÆË㿳¼Û¼Û¸ñ  | 
|     # @return: None-¿³¼ÛÏÞÖÆµÈ£»>0-¿³¼ÛÖµ  | 
|       | 
|     familyID = curFamily.GetID()  | 
|       | 
|     familyAction = GameWorld.GetFamilyActionManager().GetFamilyAction(familyID, ActionType)  | 
|     actionCount = familyAction.Count() # ÆäÖÐ1ÌõÊǹ«¹²Êý¾Ý  | 
|     hadCutCount = actionCount - 1  | 
|     maxMemberCnt = PlayerFamily.GetFamilySetting(curFamily, ChConfig.Def_FamilySetting_MaxMemberCnt)  | 
|     if hadCutCount >= maxMemberCnt:  | 
|         GameWorld.ErrLog("ÒÑ´ïµ½ÏÉÃË¿³¼ÛÈË´ÎÉÏÏÞ! hadCutCount=%s >= %s,familyID=%s,familyLV=%s"   | 
|                          % (hadCutCount, maxMemberCnt, familyID, curFamily.GetLV()), playerID)  | 
|         return  | 
|       | 
|     actionData = GetZhenbaogeActionData(familyID, playerID)  | 
|     if actionData and GetFACutPrice(actionData):  | 
|         GameWorld.DebugLog("ÒѾÔÚ¸ÃÃËÕ䱦¸ó¿³¼Û¹ýÁË! familyID=%s" % familyID, playerID)  | 
|         return  | 
|       | 
|     cutNum = hadCutCount + 1  | 
|     cutIpyData = IpyGameDataPY.GetIpyGameData("FamilyZhenbaogeCut", cutNum)  | 
|     if not cutIpyData:  | 
|         return  | 
|     cutWeight = cutIpyData.GetCutWeight()  | 
|     minRatio = cutIpyData.GetMinRatio()  | 
|     randRatio = cutIpyData.GetRandRatio()  | 
|     totalWeight = PlayerFamily.GetFamilySetting(curFamily, ChConfig.Def_FamilySetting_ZhenbaogeWeights)  | 
|     rand = random.random()  #Ëæ»úÖµ 0~1  | 
|       | 
|     initPrice = IpyGameDataPY.GetFuncCfg("Zhenbaoge", 2)  | 
|       | 
|     cutPrice = max(1, eval(IpyGameDataPY.GetFuncCompileCfg("ZhenbaogeCut", 2)))  | 
|       | 
|     nowPrice = GetFAPriceFinal(gActionData)  | 
|     updPrice = nowPrice - cutPrice  | 
|       | 
|     lowestPrice = IpyGameDataPY.GetFuncCfg("ZhenbaogeCut", 1) # ×îµÍ¼Û¸ñ±£»¤  | 
|     if updPrice < lowestPrice:  | 
|         if nowPrice > lowestPrice:  | 
|             updPrice = int(lowestPrice - random.randint(0, 2)) # Ö®Ç°»¹Î´µ½´ï×îµÍ¼Û£¬¹Ì¶¨×îµÍ¼Û+Ëæ»ú0~2  | 
|             cutPrice = nowPrice - updPrice  | 
|         else:  | 
|             cutPrice = random.randint(1, 2) # µÍÓÚ×îµÍ¼Û¸ñºó¿³¼ÛÖ»ÄÜËæ»ú1»ò2  | 
|             updPrice = nowPrice - cutPrice  | 
|         GameWorld.DebugLog("Õ䱦¸ó¿³¼Û¼ÆËãµØ°å¼Û±£»¤: cutPrice=%s,nowPrice=%s,updPrice=%s" % (cutPrice, nowPrice, updPrice), playerID)  | 
|     else:  | 
|         GameWorld.DebugLog("Õ䱦¸ó¿³¼Û¼ÆËã: cutPrice=%s,nowPrice=%s,updPrice=%s,cutWeight=%s/%s,minRatio=%s,randRatio=%s,rand=%s,initPrice=%s"   | 
|                    % (cutPrice, nowPrice, updPrice, cutWeight, totalWeight, minRatio, randRatio, rand, initPrice), playerID)  | 
|           | 
|     return max(1, cutPrice)  | 
|   | 
| def AddCutPrice(familyID, playerID, playerName, cutPrice, gActionData, isNotify=True):  | 
|     ## Ìí¼Ó¿³¼Û¼Ç¼  | 
|     familyAction = GameWorld.GetFamilyActionManager().GetFamilyAction(familyID, ActionType)  | 
|     actionData = familyAction.AddAction()  | 
|     actionData.SetFamilyId(familyID)  | 
|     actionData.SetActionType(ActionType)  | 
|     actionData.SetTime(int(time.time()))  | 
|     actionData.SetName(playerName)  | 
|     SetFAPlayerID(actionData, playerID)  | 
|     SetFACutPrice(actionData, cutPrice)  | 
|       | 
|     # ¸üÐÂ×îÖÕ¼Û¸ñ  | 
|     nowPrice = GetFAPriceFinal(gActionData)  | 
|     updPrice = nowPrice - cutPrice      | 
|     SetFAPriceFinal(gActionData, updPrice)  | 
|     GameWorld.DebugLog("Õ䱦¸ó¿³¼Û¸üÐÂ: playerID=%s,cutPrice=%s,nowPrice=%s,updPrice=%s,familyID=%s"   | 
|                        % (playerID, cutPrice, nowPrice, updPrice, familyID), playerID)  | 
|     if isNotify:  | 
|         PlayerFamilyAction.SendFamilyAction([gActionData, actionData])  | 
|     return actionData  | 
|   | 
| def __DoZhenbaogeBuy(familyID, playerID, gActionData, dataMsg):  | 
|     playerMoneyValue = dataMsg[0]  | 
|     actionData = GetZhenbaogeActionData(familyID, playerID)  | 
|     if not actionData:  | 
|         GameWorld.DebugLog("Õ䱦¸óδ¿³¼Û£¬ÎÞ·¨¹ºÂò! familyID=%s" % familyID, playerID)  | 
|         return  | 
|     if GetFABuyState(actionData):  | 
|         GameWorld.DebugLog("Õ䱦¸óÒѾ¹ºÂò¹ýÁË! familyID=%s" % familyID, playerID)  | 
|         return  | 
|     nowPrice = GetFAPriceFinal(gActionData)  | 
|     if nowPrice > 0 and playerMoneyValue < nowPrice:  | 
|         GameWorld.DebugLog("Íæ¼Òµ±Ç°»õ±Ò²»×㣬ÎÞ·¨¹ºÂòÕ䱦¸óÎïÆ·! playerMoneyValue=%s < %s" % (playerMoneyValue, nowPrice), playerID)  | 
|         return  | 
|       | 
|     giveItemList = GetFAItemList(gActionData)  | 
|     GameWorld.DebugLog("Õ䱦¸ó¹ºÂò! familyID=%s,nowPrice=%s,giveItemList=%s" % (familyID, nowPrice, giveItemList), playerID)  | 
|       | 
|     # ÉèÖÃÒÑÂò  | 
|     SetFABuyState(actionData, 1)  | 
|     PlayerFamilyAction.SendFamilyAction(actionData)  | 
|     return nowPrice, giveItemList  | 
|   |