hxp
2025-05-28 a6fe9b060edf315f6abde7443e48db5dea439f47
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
#!/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 IpyGameDataPY
import NetPackCommon
import ChPyNetSendPack
import ItemControler
import PlayerControl
import PlayerFamily
import GameWorld
import DBDataMgr
 
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.GetUserData()) # ÎïÆ·Áбí
def SetFAItemList(gActionData, itemList): gActionData.SetUserData(itemList)
 
# ³ÉÔ±¿³¼Û¼Ç¼
#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 OnDay(family):
    OnZhenbaogeReset(family)        
    return
 
def OnPlayerLogin(curPlayer):
    Sync_ZhenbaogeInfo(curPlayer)
    return
 
def PlayerOnDay(curPlayer):
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_Player_Dict_FamilyZhenbaogeCut, 0)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_Player_Dict_FamilyZhenbaogeBuy, 0)
    Sync_ZhenbaogeInfo(curPlayer)
    return
 
def OnZhenbaogeReset(family):
    ## Õ䱦¸óÖØÖÃ
    
    familyID = family.GetID()
    
    actionMgr = DBDataMgr.GetFamilyActionMgr()
    actionMgr.DelFamilyAction(familyID, ActionType)
    familyAction = actionMgr.GetFamilyAction(familyID, ActionType)
    gActionData = familyAction.AddAction()
    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)
    
    PlayerFamily.SendFamilyAction(gActionData)
    #GameWorld.DebugLog("Õ䱦¸óÖØÖÃ! familyID=%s,itemList=%s" % (familyID, itemList), familyID)    
    return
 
def GetZhenbaogeActionData(familyID, playerID):
    ## »ñÈ¡Õ䱦¸óAction
    findActionData = None
    familyAction = DBDataMgr.GetFamilyActionMgr().GetFamilyAction(familyID, ActionType)
    for index in range(0, familyAction.Count()):
        actionData = familyAction.At(index)
        if GetFAPlayerID(actionData) == playerID:
            findActionData = actionData
            break
    return findActionData
 
def CalcCutPrice(curFamily, gActionData, playerID):
    ## ¼ÆË㿳¼Û¼Û¸ñ
    # @return: None-¿³¼ÛÏÞÖÆµÈ£»>0-¿³¼ÛÖµ
    
    if not curFamily:
        return
    
    familyID = curFamily.GetID()
    familyAction = DBDataMgr.GetFamilyActionMgr().GetFamilyAction(familyID, ActionType)
    actionCount = familyAction.Count() # ÆäÖÐ1ÌõÊǹ«¹²Êý¾Ý
    hadCutCount = actionCount - 1
    
    familyLV = curFamily.GetLV()
    fmLVIpyData = IpyGameDataPY.GetIpyGameData("Family", familyLV)
    if not fmLVIpyData:
        return
    maxMemberCnt = fmLVIpyData.GetMemberMax()
    if hadCutCount >= maxMemberCnt:
        GameWorld.ErrLog("ÒÑ´ïµ½ÏÉÃË¿³¼ÛÈË´ÎÉÏÏÞ! hadCutCount=%s >= %s,familyID=%s,familyLV=%s" 
                         % (hadCutCount, maxMemberCnt, familyID, familyLV), 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 = fmLVIpyData.GetZhenbaogeWeights()
    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 = DBDataMgr.GetFamilyActionMgr().GetFamilyAction(familyID, ActionType)
    actionData = familyAction.AddAction()
    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:
        PlayerFamily.SendFamilyAction([gActionData, actionData])
    return actionData
 
#// A6 16 Õ䱦¸ó²Ù×÷ #tagCMZhenbaogeOP
#
#struct    tagCMZhenbaogeOP
#{
#    tagHead     Head;
#    BYTE    OpType;    // ²Ù×÷£º0-¿³¼Û£»1-¹ºÂò
#};
def OnZhenbaogeOP(index, clientData, tick):
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)
    opType = clientData.OpType
    playerID = curPlayer.GetPlayerID()
    familyID = curPlayer.GetFamilyID()
    if not familyID:
        GameWorld.DebugLog("ûÓÐÏÉÃËÎÞ·¨²Ù×÷Õ䱦¸ó!", playerID)
        return
    curFamily = DBDataMgr.GetFamilyMgr().FindFamily(familyID)
    if not curFamily:
        return
    gActionData = GetZhenbaogeActionData(familyID, ActionGlobalID)
    if not gActionData:
        GameWorld.DebugLog("Õ䱦¸ó»¹Ã»ÓÐË¢ÐÂ! familyID=%s" % familyID, playerID)
        return
    
    # ¿³¼Û
    if opType == 0:
        leaveTimeEx = PlayerControl.GetLeaveFamilyTimeEx(curPlayer)
        cutState = curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_FamilyZhenbaogeCut)
        if cutState and leaveTimeEx:
            cutCDTimes = IpyGameDataPY.GetFuncCfg("Zhenbaoge", 3) * 60
            passTimes = int(time.time()) - leaveTimeEx
            if passTimes < cutCDTimes:
                GameWorld.DebugLog("½ñÈÕÒÑ¿³¼Û±ä¸üÏÉÃË¿³¼ÛCDÖÐ! passTimes=%s < %s" % (passTimes, cutCDTimes), playerID)
                return
            
        cutPrice = CalcCutPrice(curFamily, gActionData, playerID)
        if not cutPrice:
            return
        
        AddCutPrice(familyID, playerID, curPlayer.GetName(), cutPrice, gActionData)
        PlayerControl.NotifyCode(curPlayer, "ZhenbaogeCut", [cutPrice])
        if not curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_FamilyZhenbaogeCut):
            PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_Player_Dict_FamilyZhenbaogeCut, 1)
            Sync_ZhenbaogeInfo(curPlayer)
            
    # ¹ºÂò
    elif opType == 1:
        buyState = curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_FamilyZhenbaogeBuy)
        if buyState:
            GameWorld.DebugLog("Õ䱦¸ó½ñÈÕÒѹºÂò!", playerID)
            return
        moneyType = IpyGameDataPY.GetFuncCfg("Zhenbaoge", 1)
        playerMoneyValue = PlayerControl.GetMoney(curPlayer, moneyType)
        
        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)
        PlayerFamily.SendFamilyAction(actionData)
        
        if nowPrice > 0:
            if not PlayerControl.PayMoney(curPlayer, moneyType, nowPrice, "Zhenbaoge"):
                GameWorld.ErrLog("Õ䱦¸ó¹ºÂò»õ±Ò²»×ã! nowPrice=%s" % nowPrice, playerID)
                return
        elif nowPrice < 0:
            PlayerControl.GiveMoney(curPlayer, moneyType, -nowPrice, "Zhenbaoge")
        else: # 0²»´¦Àí
            pass
        
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_Player_Dict_FamilyZhenbaogeBuy, 1)
        Sync_ZhenbaogeInfo(curPlayer)
        
        if giveItemList:
            ItemControler.GivePlayerItemOrMail(curPlayer, giveItemList, event=["Zhenbaoge", False, {}], isNotifyAward=False)
        ItemControler.NotifyGiveAwardInfo(curPlayer, giveItemList, "Zhenbaoge", moneyInfo={moneyType:0 if nowPrice > 0 else -nowPrice})
                
    return
 
def Sync_ZhenbaogeInfo(curPlayer):
    clientPack = ChPyNetSendPack.tagMCFamilyZhenbaogeInfo()
    clientPack.CutState = curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_FamilyZhenbaogeCut)
    clientPack.BuyState = curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_FamilyZhenbaogeBuy)
    NetPackCommon.SendFakePack(curPlayer, clientPack)
    return