#!/usr/bin/python  
 | 
# -*- coding: GBK -*-  
 | 
#-------------------------------------------------------------------------------  
 | 
#  
 | 
##@package Player.PlayerGoldInvest  
 | 
#  
 | 
# @todo:Ͷ×Ê  
 | 
# @author hxp  
 | 
# @date 2020-09-10  
 | 
# @version 1.0  
 | 
#  
 | 
# ÏêϸÃèÊö: Í¶×Ê  
 | 
#  
 | 
#-------------------------------------------------------------------------------  
 | 
#"""Version = 2020-09-10 11:00"""  
 | 
#-------------------------------------------------------------------------------  
 | 
  
 | 
import GameWorld  
 | 
import IpyGameDataPY  
 | 
import NetPackCommon  
 | 
import DataRecordPack  
 | 
import ChPyNetSendPack  
 | 
import PlayerFamilyRedPacket  
 | 
import ItemControler  
 | 
import PlayerControl  
 | 
import IPY_GameWorld  
 | 
import ChConfig  
 | 
  
 | 
import time  
 | 
  
 | 
def __TransferPlayerInvestDBKey(curPlayer):  
 | 
    ## ×ª»»Íæ¼Ò´æ´¢×ֵ䣬»»keyÃû  
 | 
      
 | 
    # ¾É°ækey  
 | 
    # Í¶×ÊÀí²Æ Def_PDictType_GoldInvest  
 | 
    Def_PDict_GoldInvest_Time = "Invest_Time_%s"  # Í¶×ÊʱµÄʱ¼ä£¬²ÎÊýΪͶ×ÊÀàÐÍ  
 | 
    #Def_PDict_GoldInvest_Gold = "Invest_Gold_%s"  # Í¶×ʵĶî¶È£¬²ÎÊýΪͶ×ÊÀàÐÍ                                             ±ê¼ÇÊÇ·ñÒÑͶ×Ê£¬¿ÉÓÃͶ×Êʱ¼ä¼´¿É£¬²»´¦Àí  
 | 
    #Def_PDict_GoldInvest_AwardData = "Invest_AwardData_%s"  # Í¶×ÊʱµÄÊý¾Ý£¬²ÎÊýΪͶ×ÊÀàÐÍ                                 Ä¬ÈÏ1£¬ÎÞÓ㬿ɲ»¹Ü  
 | 
    Def_PDict_GoldInvest_GotRewardValue = "Invest_GotRewardValue_%s_%s"  # µÈ¼¶»Ø±¨ÒÑÁìÈ¡ÊýÖµ£¬²ÎÊýΪͶ×ÊÀàÐÍ Ë÷Òý  
 | 
      
 | 
    playerID = curPlayer.GetPlayerID()  
 | 
    for investType in [ChConfig.InvestType_NewMonth, ChConfig.InvestType_Month1]:  
 | 
        investTimeKeyOld = Def_PDict_GoldInvest_Time % investType  
 | 
        investTime = curPlayer.NomalDictGetProperty(investTimeKeyOld, 0, ChConfig.Def_PDictType_GoldInvest)  
 | 
        if not investTime:  
 | 
            continue  
 | 
          
 | 
        investTimeKeyNew = ChConfig.Def_PDict_InvestTime % investType  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, investTimeKeyNew, investTime)  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, investTimeKeyOld, 0, ChConfig.Def_PDictType_GoldInvest)  
 | 
        GameWorld.Log("ת»»Í¶×Êkey: investType=%s,investTimeKeyNew=%s,investTime=%s,investTimeKeyOld=%s"   
 | 
                      % (investType, investTimeKeyNew, investTime, investTimeKeyOld), playerID)  
 | 
          
 | 
        maxDay = __GetInvestMaxDays(investType)  
 | 
        for i in xrange(1, maxDay + 1):  
 | 
            rewardKeyOld = Def_PDict_GoldInvest_GotRewardValue % (investType, i)  
 | 
            reward = curPlayer.NomalDictGetProperty(rewardKeyOld, 0, ChConfig.Def_PDictType_GoldInvest)  
 | 
            if not reward:  
 | 
                continue  
 | 
              
 | 
            GameWorld.SetDictValueByBit(curPlayer, ChConfig.Def_PDict_InvestReward, i, 1, True, [investType])  
 | 
            PlayerControl.NomalDictSetProperty(curPlayer, rewardKeyOld, 0, ChConfig.Def_PDictType_GoldInvest)  
 | 
            GameWorld.Log("    ×ª»»Í¶×ÊÒÑÁìÈ¡¼Ç¼: i=%s,rewardKeyOld=%s" % (i, rewardKeyOld), playerID)  
 | 
              
 | 
    return  
 | 
  
 | 
## µÇ¼  
 | 
def OnLogin(curPlayer):  
 | 
    __TransferPlayerInvestDBKey(curPlayer)  
 | 
    for investType in ChConfig.InvestTypeList:  
 | 
        if not curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_InvestTime % investType):  
 | 
            continue  
 | 
        Sync_InvestInfo(curPlayer, investType)  
 | 
    return  
 | 
  
 | 
## ¹ýÌì  
 | 
def OnDay(curPlayer):  
 | 
    for investType in ChConfig.InvestTypeList:  
 | 
        if not curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_InvestTime % investType):  
 | 
            continue  
 | 
          
 | 
        # µÇ¼¿¨  
 | 
        if investType == ChConfig.InvestType_Login:  
 | 
            progressValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_InvestProgress % (investType, 0))  
 | 
            maxDays = __GetInvestMaxDays(investType)  
 | 
            if maxDays and progressValue < maxDays:  
 | 
                progressValue += 1  
 | 
                PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_InvestProgress % (investType, 0), progressValue)  
 | 
                GameWorld.DebugLog("¸üеǼͶ×Ê¿ÉÁì½±ÌìÊý½ø¶È: %s" % progressValue)  
 | 
                  
 | 
        Sync_InvestInfo(curPlayer, investType)  
 | 
    return  
 | 
  
 | 
def __GetInvestPassDays(curPlayer, investType):  
 | 
    ## »ñȡͶ×ÊÒѹýÌìÊý£¬Í¶×ʵ±ÌìΪµÚÒ»Ì죬¼´´Ó 1 ¿ªÊ¼£» 0´ú±íδͶ×Ê  
 | 
    investTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_InvestTime % investType)  
 | 
    if not investTime:  
 | 
        return 0  
 | 
    curTime = int(time.time())  
 | 
    return max(0, GameWorld.GetDiff_Day(curTime, investTime) + 1)  
 | 
  
 | 
def __GetInvestMaxDays(investType):  
 | 
    ## »ñȡͶ×Ê×î´óÌìÊý£¬0ΪÓÀ¾Ã  
 | 
    investMaxDayDict = IpyGameDataPY.GetFuncEvalCfg("InvestMaxDay", 1, {})  
 | 
    return investMaxDayDict.get(str(investType), 0)  
 | 
  
 | 
def GetInvestState(curPlayer, investType):  
 | 
    ## »ñȡͶ×Ê¿¨×´Ì¬ 0-δͶ×Ê£»1-ÒÑͶ×Ê£»  
 | 
    investTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_InvestTime % investType)  
 | 
    if not investTime:  
 | 
        return 0  
 | 
    return 1  
 | 
  
 | 
#// A5 40 Í¶×ÊÀí²Æ #tagCMGoldInvest  
 | 
#  
 | 
#struct    tagCMGoldInvest  
 | 
#{  
 | 
#    tagHead        Head;  
 | 
#    BYTE        InvestType; // Í¶×ÊÀàÐÍ  
 | 
#    DWORD        InvestGold; // Í¶×ʶî¶È  
 | 
#};  
 | 
def OnGoldInvest(index, clientData, tick):  
 | 
    ## ÏûºÄÏÉÓñͶ×ʵģ¬ÔÝ·ÏÆú  
 | 
    return  
 | 
  
 | 
## ³äÖµÖ±¹ºÍ¶×Ê  
 | 
def InvestByCTG(curPlayer, ctgID):  
 | 
    ctgInvestDict = IpyGameDataPY.GetFuncEvalCfg("InvestCost", 3, {})  
 | 
    for investType, ctgIDList in ctgInvestDict.items():  
 | 
        if ctgID in ctgIDList:  
 | 
            __DoLogicInvest(curPlayer, int(investType))  
 | 
            break  
 | 
    return  
 | 
  
 | 
## Ö´ÐÐͶ×ÊÂß¼  
 | 
def __DoLogicInvest(curPlayer, investType):  
 | 
    if GetInvestState(curPlayer, investType) == 1:  
 | 
        GameWorld.DebugLog("ÒÑͶ×Ê£¬ÎÞ·¨Öظ´Í¶×Ê! investType=%s" % investType)  
 | 
        return  
 | 
      
 | 
    # ¿ÉͶ×Ê£¬¸üÐÂÖØÖÃͶ×ÊÏà¹ØÊý¾Ý  
 | 
    curTime = int(time.time())  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_InvestTime % investType, curTime)  
 | 
    for keyNum in range(ChConfig.Def_PDict_InvestKeyCount):  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_InvestReward % (investType, keyNum), 0)  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_InvestProgress % (investType, keyNum), 0)  
 | 
      
 | 
    # µÇ¼¿¨£¬Í¶×ʵ±Ìì¿ÉÁì½±£¬Íê³ÉÉèÖÃΪ1  
 | 
    if investType == ChConfig.InvestType_Login:  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_InvestProgress % (investType, 0), 1)  
 | 
          
 | 
    # Í¶×ʺì°ü  
 | 
    redPacketID = IpyGameDataPY.GetFuncEvalCfg("InvestRedPackAward", 1, {}).get(investType, 0)  
 | 
    if redPacketID:  
 | 
        PlayerFamilyRedPacket.CreatRedPacketByID(curPlayer, redPacketID)  
 | 
          
 | 
    # ¹ã²¥  
 | 
    PlayerControl.WorldNotify(0, "BuyInvest_%s" % investType, [curPlayer.GetName(), __GetTotalGetGold(investType)])  
 | 
      
 | 
    GameWorld.DebugLog("Ͷ×ÊÀí²Æ³É¹¦: investType=%s" % (investType))  
 | 
    Sync_InvestInfo(curPlayer, investType)  
 | 
    return  
 | 
  
 | 
def __GetTotalGetGold(investType):  
 | 
    ## »ñȡͶ×ÊÔ¤¼Æ×ÜÊÕÒæÏÉÓñ¡¢Áéʯ  
 | 
    totalGold = 0  
 | 
    ipyDataMgr = IpyGameDataPY.IPY_Data()  
 | 
    for index in xrange(ipyDataMgr.GetInvestCount()):  
 | 
        ipyData = ipyDataMgr.GetInvestByIndex(index)  
 | 
        if ipyData.GetType() != investType:  
 | 
            continue  
 | 
        rewardDict = ipyData.GetReward()  
 | 
        for rewardInfo in rewardDict.values():  
 | 
            for itemID, itemCount, _ in rewardInfo:  
 | 
                if itemID in [20, 30]: # µ¥Î»1µÄÏÉÓñ¡¢Áéʯ  
 | 
                    totalGold += itemCount  
 | 
    return totalGold  
 | 
  
 | 
def OnKillBoss(curPlayer, npcID):  
 | 
    ## ²ÎÓë»÷ɱboss  
 | 
    if GetInvestState(curPlayer, ChConfig.InvestType_Boss) != 1:  
 | 
        #GameWorld.DebugLog("bossͶ×ÊδͶ×Ê!")  
 | 
        return  
 | 
    key = "BossInvestNPCIDDict"  
 | 
    BossInvestNPCIDDict = IpyGameDataPY.GetConfigEx(key)  
 | 
    if not BossInvestNPCIDDict:  
 | 
        BossInvestNPCIDDict = {}  
 | 
        ipyDataMgr = IpyGameDataPY.IPY_Data()  
 | 
        for index in xrange(ipyDataMgr.GetInvestCount()):  
 | 
            ipyData = ipyDataMgr.GetInvestByIndex(index)  
 | 
            if ipyData.GetType() != ChConfig.InvestType_Boss:  
 | 
                continue  
 | 
            if not ipyData.GetNeedNPCID():  
 | 
                continue  
 | 
            BossInvestNPCIDDict[ipyData.GetNeedNPCID()] = ipyData.GetID()  
 | 
        IpyGameDataPY.SetConfigEx(key, BossInvestNPCIDDict)  
 | 
        GameWorld.Log("»º´æbossͶ×ʶÔÓ¦¹ØÏµ: %s" % BossInvestNPCIDDict)  
 | 
          
 | 
    if npcID not in BossInvestNPCIDDict:  
 | 
        #GameWorld.DebugLog("    ¸ÃbossûÓÐͶ×ʽ±Àø!")  
 | 
        return  
 | 
    investID = BossInvestNPCIDDict[npcID]  
 | 
    investType, rewardIndex = investID / 100, investID % 100  
 | 
    if GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_InvestProgress, rewardIndex, True, [investType]):  
 | 
        #GameWorld.DebugLog("ÒÑÉèÖû÷ɱ¹ý¸ÃͶ×Êboss: npcID=%s,rewardIndex=%s,progressValue=%s" % (npcID, rewardIndex, progressValue))  
 | 
        return  
 | 
    progressValue, updProgressValue = GameWorld.SetDictValueByBit(curPlayer, ChConfig.Def_PDict_InvestProgress, rewardIndex, 1, True, [investType])  
 | 
    Sync_InvestInfo(curPlayer, investType)  
 | 
    GameWorld.DebugLog("ÉèÖÃɱ¹ýͶ×Êboss: npcID=%s,rewardIndex=%s,progressValue=%s,updProgressValue=%s"   
 | 
                       % (npcID, rewardIndex, progressValue, updProgressValue))  
 | 
    return  
 | 
  
 | 
#// A5 41 ÁìȡͶ×ÊÀí²Æ»Ø±¨ #tagCMGetInvestReward  
 | 
#  
 | 
#struct    tagCMGetInvestReward  
 | 
#{  
 | 
#    tagHead        Head;  
 | 
#    BYTE        InvestType; // Í¶×ÊÀàÐÍ  
 | 
#    BYTE        RewardIndex; // »Ø±¨Ë÷Òý  
 | 
#};  
 | 
def OnGetInvestReward(index, clientData, tick):  
 | 
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  
 | 
    investType = clientData.InvestType  
 | 
    rewardIndex = clientData.RewardIndex  
 | 
    GameWorld.DebugLog("ÁìȡͶ×ÊÀí²Æ»Ø±¨£ºinvestType=%s,rewardIndex=%s" % (investType, rewardIndex))  
 | 
      
 | 
    if investType not in ChConfig.InvestTypeList:  
 | 
        #GameWorld.DebugLog("²»´æÔÚ¸ÃͶ×ÊÀàÐÍ!")  
 | 
        return  
 | 
      
 | 
    if GetInvestState(curPlayer, investType) != 1:  
 | 
        GameWorld.DebugLog("    Î´Í¶×Ê»òÒѹýÆÚ£¬ÎÞ·¨Áì½±!")  
 | 
        return  
 | 
      
 | 
    ipyData = IpyGameDataPY.GetIpyGameData("Invest", investType * 100 + rewardIndex)  
 | 
    if not ipyData:  
 | 
        return  
 | 
      
 | 
    needLV = ipyData.GetNeedLV()  
 | 
    if needLV and curPlayer.GetLV() < needLV:  
 | 
        GameWorld.DebugLog("    µÈ¼¶²»×㣬ÎÞ·¨Áì½±! needLV=%s" % needLV)  
 | 
        return  
 | 
      
 | 
    if GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_InvestReward, rewardIndex, True, [investType]):  
 | 
        GameWorld.DebugLog("    ÒÑÁìÈ¡¹ý¸ÃË÷Òý½±Àø! rewardIndex=%s" % rewardIndex)  
 | 
        return  
 | 
      
 | 
    needDay = ipyData.GetNeedDay()  
 | 
    rewardInfo = ipyData.GetReward()  
 | 
      
 | 
    indexProgressState = GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_InvestProgress, rewardIndex, True, [investType])  
 | 
      
 | 
    # Ô¿¨  
 | 
    if investType in [ChConfig.InvestType_NewMonth, ChConfig.InvestType_Month1]:  
 | 
        passDays = __GetInvestPassDays(curPlayer, investType) # °´Í¶×Êʱ¼ä¼ÆËãÌì  
 | 
        if needDay > passDays:  
 | 
            GameWorld.DebugLog("    Í¶×ÊÌìÊý²»×㣬ÎÞ·¨ÁìÈ¡! needDay=%s > passDays=%s" % (ipyData.GetNeedDay(), passDays))  
 | 
            return  
 | 
          
 | 
    # µÇ¼  
 | 
    elif investType == ChConfig.InvestType_Login:  
 | 
        loginDays = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_InvestProgress % (investType, 0)) # °´µÇ¼Ìì¼ÆËãÌ죬ÓеǼ²ÅËã  
 | 
        if needDay > loginDays:  
 | 
            GameWorld.DebugLog("    µÇ¼Ͷ×ÊÌìÊý²»×㣬ÎÞ·¨ÁìÈ¡! needDay=%s > loginDays=%s" % (needDay, loginDays))  
 | 
            return  
 | 
          
 | 
    # µÈ¼¶  
 | 
    elif investType == ChConfig.InvestType_LV:  
 | 
        # ¹«¹²µÈ¼¶Ìõ¼þÒÑÅÐ¶Ï  
 | 
        pass  
 | 
          
 | 
    # Boss  
 | 
    elif investType == ChConfig.InvestType_Boss:  
 | 
        if not indexProgressState:  
 | 
            GameWorld.DebugLog("    BossͶ×ÊÎÞ²ÎÓë»÷ɱ¸Ãboss£¬ÎÞ·¨ÁìÈ¡! rewardIndex=%s" % (rewardIndex))  
 | 
            return  
 | 
          
 | 
    else:  
 | 
        return  
 | 
      
 | 
    if not ipyData:  
 | 
        return  
 | 
          
 | 
    rewardKey = "1" # Ä¬ÈÏ1  
 | 
    rewardItemList = rewardInfo.get(str(rewardKey), [])  
 | 
    if not rewardItemList:  
 | 
        return  
 | 
      
 | 
    if not ItemControler.CheckPackSpaceEnough(curPlayer, rewardItemList):  
 | 
        return  
 | 
      
 | 
    rewardValue, updRewardValue = GameWorld.SetDictValueByBit(curPlayer, ChConfig.Def_PDict_InvestReward, rewardIndex, 1, True, [investType])  
 | 
    Sync_InvestInfo(curPlayer, investType)  
 | 
    GameWorld.DebugLog("    rewardValue=%s,updRewardValue=%s,rewardItemList=%s" % (rewardValue, updRewardValue, rewardItemList))  
 | 
      
 | 
    for itemID, itemCount, isAuctionItem in rewardItemList:  
 | 
        ItemControler.GivePlayerItem(curPlayer, itemID, itemCount, isAuctionItem, [IPY_GameWorld.rptItem],  
 | 
                                     event=["Invest", False, {}])  
 | 
          
 | 
    # ¼Ç¼Áìȡʼþ  
 | 
    DataRecordPack.DR_GetGoldInvestReward(curPlayer, investType, rewardIndex, rewardItemList)  
 | 
      
 | 
    # ÁìÍêÁË£¬ÖØÖÿÉÖØ¸´¹ºÂòµÄ·ÇÓÀ¾Ã¿¨  
 | 
    maxDays = __GetInvestMaxDays(investType)  
 | 
    canRepetBuyTypeList = IpyGameDataPY.GetFuncEvalCfg("InvestMaxDay", 2)  
 | 
    if maxDays > 1 and investType in canRepetBuyTypeList:  
 | 
        isAllDayGet = True  
 | 
        for i in range(maxDays, -1, -1):  
 | 
            if not IpyGameDataPY.GetIpyGameDataNotLog("Invest", investType * 100 + i):  
 | 
                continue  
 | 
            if not GameWorld.GetDictValueByBit(curPlayer, ChConfig.Def_PDict_InvestReward, i, True, [investType]):  
 | 
                isAllDayGet = False  
 | 
                #GameWorld.DebugLog("    »¹ÓÐδÁìÈ¡: i=%s,isAllDayGet=%s" % (i, isAllDayGet))  
 | 
                break  
 | 
              
 | 
        if isAllDayGet:  
 | 
            PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_InvestTime % investType, 0)  
 | 
            for keyNum in range(ChConfig.Def_PDict_InvestKeyCount):  
 | 
                PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_InvestReward % (investType, keyNum), 0)  
 | 
                PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_InvestProgress % (investType, keyNum), 0)  
 | 
            Sync_InvestInfo(curPlayer, investType)  
 | 
            GameWorld.DebugLog("    ÁìÍê½±ÀøÁË£¬ÖØÖÃͶ×Ê! investType=%s" % investType)  
 | 
              
 | 
    return  
 | 
  
 | 
## Í¬²½Í¶×ÊÀí²ÆÐÅÏ¢  
 | 
def Sync_InvestInfo(curPlayer, investType):  
 | 
    investInfoPack = ChPyNetSendPack.tagMCInvestInfo()  
 | 
    investInfoPack.InvestType = investType  
 | 
    investInfoPack.CurDay = __GetInvestPassDays(curPlayer, investType)  
 | 
    for keyNum in range(ChConfig.Def_PDict_InvestKeyCount):  
 | 
        investInfoPack.RewardValue.append(curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_InvestReward % (investType, keyNum)))  
 | 
        investInfoPack.ProgressValue.append(curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_InvestProgress % (investType, keyNum)))  
 | 
    investInfoPack.ValueCount = len(investInfoPack.RewardValue)  
 | 
    NetPackCommon.SendFakePack(curPlayer, investInfoPack)  
 | 
    return  
 | 
  
 | 
  
 |