#!/usr/bin/python  
 | 
# -*- coding: GBK -*-  
 | 
#-------------------------------------------------------------------------------  
 | 
#  
 | 
##@package Player.PlayerCostRebate  
 | 
#  
 | 
# @todo:Ïû·Ñ·µÀû»î¶¯  
 | 
# @author hxp  
 | 
# @date 2018-7-16  
 | 
# @version 1.0  
 | 
#  
 | 
# ÏêϸÃèÊö: Ïû·Ñ·µÀû»î¶¯  
 | 
#  
 | 
#-------------------------------------------------------------------------------  
 | 
#"""Version = 2018-7-16 12:00"""  
 | 
#-------------------------------------------------------------------------------  
 | 
  
 | 
import PyGameData  
 | 
import ShareDefine  
 | 
import PlayerControl  
 | 
import IpyGameDataPY  
 | 
import ItemControler  
 | 
import ChPyNetSendPack  
 | 
import DataRecordPack  
 | 
import IPY_GameWorld  
 | 
import NetPackCommon  
 | 
import GameWorld  
 | 
import ChConfig  
 | 
  
 | 
def GetTemplateID(cfgID, dayIndex):  
 | 
    if cfgID == None or dayIndex == None:  
 | 
        return 0  
 | 
    ipyData = IpyGameDataPY.GetIpyGameData("ActCostRebate", cfgID)  
 | 
    if not ipyData:  
 | 
        return 0  
 | 
    templateIDList = ipyData.GetTemplateIDList()  
 | 
    templateID = templateIDList[-1] if dayIndex >= len(templateIDList) else templateIDList[dayIndex]  
 | 
    return templateID  
 | 
  
 | 
def OnPlayerLogin(curPlayer):  
 | 
      
 | 
    for actInfo in PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_CostRebate, {}).values():  
 | 
        actNum = actInfo.get(ShareDefine.ActKey_ActNum, 0)  
 | 
        isReset = __CheckPlayerCostRebateAction(curPlayer, actNum)  
 | 
        if not isReset:  
 | 
            # »î¶¯ÖÐͬ²½»î¶¯ÐÅÏ¢  
 | 
            if actInfo.get(ShareDefine.ActKey_State):  
 | 
                Sync_CostRebateActionInfo(curPlayer, actNum)  
 | 
                Sync_CostRebateInfo(curPlayer, actNum)  
 | 
    return  
 | 
  
 | 
def RefreshCostRebateActionInfo(actNum):  
 | 
    ## ÊÕµ½GameServerͬ²½µÄ»î¶¯ÐÅÏ¢£¬Ë¢Ð»ÐÅÏ¢  
 | 
    playerManager = GameWorld.GetPlayerManager()  
 | 
    for index in xrange(playerManager.GetPlayerCount()):  
 | 
        curPlayer = playerManager.GetPlayerByIndex(index)  
 | 
        if curPlayer.GetID() == 0:  
 | 
            continue  
 | 
        __CheckPlayerCostRebateAction(curPlayer, actNum)  
 | 
    return  
 | 
  
 | 
def __CheckPlayerCostRebateAction(curPlayer, actNum):  
 | 
    ## ¼ì²éÍæ¼ÒÏû·Ñ·µÀû»î¶¯Êý¾ÝÐÅÏ¢  
 | 
      
 | 
    playerID = curPlayer.GetPlayerID()  
 | 
      
 | 
    actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_CostRebate, actNum)  
 | 
    actID = actInfo.get(ShareDefine.ActKey_ID, 0)  
 | 
    state = actInfo.get(ShareDefine.ActKey_State, 0)  
 | 
      
 | 
    playerActID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateID % actNum) # Íæ¼ÒÉíÉϵĻID  
 | 
      
 | 
    # »î¶¯ID ÏàͬµÄ»°²»´¦Àí  
 | 
    if actID == playerActID:  
 | 
        GameWorld.DebugLog("Ïû·Ñ·µÀû»î¶¯ID²»±ä£¬²»´¦Àí£¡actNum=%s,actID=%s" % (actNum, actID), curPlayer.GetPlayerID())  
 | 
        return  
 | 
    actWorldLV = actInfo.get(ShareDefine.ActKey_WorldLV, 0)  
 | 
    playerWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateWorldLV % actNum)  
 | 
    templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))  
 | 
    playerTemplateID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateTemplateID % actNum)  
 | 
      
 | 
    GameWorld.DebugLog("Ïû·Ñ·µÀûÖØÖÃ! actNum=%s,actID=%s,playerActID=%s,state=%s,templateID=%s,playerTemplateID=%s"   
 | 
                       % (actNum, actID, playerActID, state, templateID, playerTemplateID), playerID)  
 | 
      
 | 
    # Î´ÁìÈ¡µÄ½±ÀøÓʼþ·¢·Å  
 | 
    __SendCostRebateMail(curPlayer, playerTemplateID, playerWorldLV, actNum)  
 | 
      
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateID % actNum, actID)  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateTemplateID % actNum, templateID)  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateWorldLV % actNum, actWorldLV)  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateGold % actNum, 0)  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateAwardRecord % actNum, 0)  
 | 
      
 | 
    Sync_CostRebateActionInfo(curPlayer, actNum)  
 | 
    Sync_CostRebateInfo(curPlayer, actNum)  
 | 
    return True  
 | 
  
 | 
def __SendCostRebateMail(curPlayer, playerTemplateID, playerWorldLV, actNum):  
 | 
    # Î´ÁìÈ¡µÄ½±ÀøÓʼþ·¢·Å  
 | 
      
 | 
    if not playerTemplateID:  
 | 
        return  
 | 
      
 | 
    costGold = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateGold % actNum)  
 | 
    if not costGold:  
 | 
        return  
 | 
      
 | 
    ipyDataList = IpyGameDataPY.GetIpyGameDataList("CostRebateTemplate", playerTemplateID)  
 | 
    if not ipyDataList:  
 | 
        return  
 | 
    job = curPlayer.GetJob()  
 | 
    playerID = curPlayer.GetPlayerID()  
 | 
    batchPlayerIDList, batchAddItemList, batchParamList = [], [], []  
 | 
    awardRecord = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateAwardRecord % actNum)  
 | 
      
 | 
    for ipyData in ipyDataList:  
 | 
        awardIndex = ipyData.GetAwardIndex()  
 | 
        if awardRecord & pow(2, awardIndex):  
 | 
            continue  
 | 
          
 | 
        needCostGold = ipyData.GetNeedCostGold()  
 | 
        if costGold < needCostGold:  
 | 
            continue  
 | 
        awardRecord |= pow(2, awardIndex)   
 | 
          
 | 
        awardItemList = __GetItemList(ipyData.GetAwardItemList(), job, playerWorldLV)  
 | 
        batchPlayerIDList.append([playerID])  
 | 
        batchAddItemList.append(awardItemList)  
 | 
        batchParamList.append([needCostGold])  
 | 
          
 | 
    if batchPlayerIDList:  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateAwardRecord % actNum, awardRecord)  
 | 
        PlayerControl.SendMailBatch("ConsumptionRebateMail", batchPlayerIDList, batchAddItemList, batchParamList)  
 | 
          
 | 
    return  
 | 
  
 | 
  
 | 
def __GetItemList(itemDict, job, worldLV):  
 | 
    #{ÊÀ½çµÈ¼¶·¶Î§:[(ÎïÆ·ID,¸öÊý,ÊÇ·ñ°ó¶¨), ...]},ÈôÎïÆ·ID񻂿·ÖÖ°ÒµÔòÅä{ÊÀ½çµÈ¼¶·¶Î§:[({Ö°Òµ:ÎïÆ·ID,..},¸öÊý,ÊÇ·ñ°ó¶¨), ...]}  
 | 
    itemList = []  
 | 
    itemInfoList = GameWorld.GetDictValueByRangeKey(itemDict, worldLV, [])  
 | 
    for itemInfo in itemInfoList:  
 | 
        if type(itemInfo[0]) == dict:  
 | 
            itemID = itemInfo[0].get(job)  
 | 
            if not itemID:  
 | 
                GameWorld.ErrLog('ÀۼƳäÖµ½±ÀøÎ´ÅäÖøÃÖ°ÒµitemDict=%s,job=%s'%(itemDict, job))  
 | 
                continue  
 | 
        else:  
 | 
            itemID = itemInfo[0]  
 | 
        itemList.append([itemID, itemInfo[1], itemInfo[2]])  
 | 
    return itemList  
 | 
  
 | 
  
 | 
def AddCostRebateGold(curPlayer, costType, costGold, infoDict):  
 | 
    if costGold <= 0:  
 | 
        return  
 | 
    if costType in ChConfig.CostRebate_DisableType:  
 | 
        GameWorld.DebugLog("²»¼ÆÈëÏû·Ñ·µÀûµÄÏû·ÑÀàÐÍ!costType=%s" % costType, curPlayer.GetPlayerID())  
 | 
        return  
 | 
      
 | 
    for actInfo in PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_CostRebate, {}).values():  
 | 
        actNum = actInfo.get(ShareDefine.ActKey_ActNum, 0)  
 | 
          
 | 
        if not actInfo.get(ShareDefine.ActKey_State):  
 | 
            GameWorld.DebugLog("Ïû·Ñ·µÀû»î¶¯µ±Ç°Î´¿ªÆô!actNum=%s" % actNum)  
 | 
            continue  
 | 
          
 | 
        actID = actInfo.get(ShareDefine.ActKey_ID)  
 | 
        templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))  
 | 
        if not actID or not templateID:  
 | 
            GameWorld.ErrLog("Ïû·Ñ·µÀû»î¶¯Êý¾ÝÒì³£!actNum=%s,actID=%s,templateID=%s" % (actNum, actID, templateID), curPlayer.GetPlayerID())  
 | 
            continue  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateID % actNum, actID)  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateTemplateID % actNum, templateID)  
 | 
          
 | 
        curCostGold = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateGold % actNum)  
 | 
        updCostGold = curCostGold + costGold  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateGold % actNum, updCostGold)  
 | 
        Sync_CostRebateInfo(curPlayer, actNum)  
 | 
        GameWorld.DebugLog("Íæ¼ÒÏû·Ñ·µÀû»î¶¯: actNum=%s,actID=%s,templateID=%s,curCostGold=%s,costGold=%s,updCostGold=%s"   
 | 
                           % (actNum, actID, templateID, curCostGold, costGold, updCostGold), curPlayer.GetPlayerID())  
 | 
    return  
 | 
  
 | 
def OnGetCostRebateAward(curPlayer, awardIndex, actNum):  
 | 
    ## ÁìÈ¡Ïû·Ñ·µÀû½±Àø  
 | 
      
 | 
    actNum = GameWorld.ToIntDef(actNum, 0)  
 | 
    if actNum <= 0:  
 | 
        GameWorld.DebugLog("ûÓиû±àºÅ! actNum=%s" % actNum)  
 | 
        return  
 | 
      
 | 
    playerID = curPlayer.GetPlayerID()  
 | 
      
 | 
    actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_CostRebate, actNum)  
 | 
    costRebateID = actInfo.get(ShareDefine.ActKey_ID, 0)  
 | 
    state = actInfo.get(ShareDefine.ActKey_State, 0)  
 | 
    templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))  
 | 
    if not state or not templateID:  
 | 
        GameWorld.DebugLog("ûÓÐÏû·Ñ·µÀû»î¶¯£¬ÎÞ·¨Áì½±£¡actNum=%s,state=%s,templateID=%s" % (actNum, state, templateID), playerID)  
 | 
        return  
 | 
      
 | 
    playerCostRebateID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateID % actNum) # Íæ¼ÒÉíÉϵĻID  
 | 
    if costRebateID != playerCostRebateID:  
 | 
        return  
 | 
      
 | 
    awardRecord = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateAwardRecord % actNum)  
 | 
    if awardRecord & pow(2, awardIndex):  
 | 
        GameWorld.DebugLog("ÒѾÁìÈ¡¹ý¸ÃÏû·Ñ·µÀû»î¶¯½±Àø!actNum=%s,awardIndex=%s,awardRecord=%s" % (actNum, awardIndex, awardRecord), playerID)  
 | 
        return  
 | 
      
 | 
    ipyDataList = IpyGameDataPY.GetIpyGameDataList("CostRebateTemplate", templateID)  
 | 
    if not ipyDataList:  
 | 
        return  
 | 
      
 | 
    awardIpyData = None  
 | 
    for ipyData in ipyDataList:  
 | 
        if ipyData.GetAwardIndex() == awardIndex:  
 | 
            awardIpyData = ipyData  
 | 
            break  
 | 
          
 | 
    if not awardIpyData:  
 | 
        GameWorld.DebugLog("ÕÒ²»µ½¸Ã·µÀû»î¶¯µµÎ»Ë÷Òý½±Àø!templateID=%s,awardIndex=%s" % (templateID, awardIndex), playerID)  
 | 
        return  
 | 
      
 | 
    needCostGold = awardIpyData.GetNeedCostGold()  
 | 
    actWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateWorldLV % actNum)  
 | 
    awardItemList = __GetItemList(awardIpyData.GetAwardItemList(), curPlayer.GetJob(), actWorldLV)  
 | 
      
 | 
    curCostGold = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateGold % actNum)  
 | 
    if curCostGold < needCostGold:  
 | 
        GameWorld.DebugLog("ËùÐèÏû·ÑÏÉÓñÊý²»×㣬ÎÞ·¨ÁìÈ¡!templateID=%s,awardIndex=%s,needCostGold=%s,curCostGold=%s"   
 | 
                           % (templateID, awardIndex, needCostGold, curCostGold), playerID)  
 | 
        return  
 | 
      
 | 
    if not ItemControler.CheckPackSpaceEnough(curPlayer, awardItemList):  
 | 
        return  
 | 
      
 | 
    awardRecord |= pow(2, awardIndex)  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_CostRebateAwardRecord % actNum, awardRecord)  
 | 
    Sync_CostRebateInfo(curPlayer, actNum)  
 | 
      
 | 
    notifyKey = awardIpyData.GetNotifyKey()  
 | 
    if notifyKey:  
 | 
        PlayerControl.WorldNotify(0, notifyKey, [curPlayer.GetPlayerName(), needCostGold])  
 | 
          
 | 
    for itemID, itemCount, _ in awardItemList:  
 | 
        ItemControler.GivePlayerItem(curPlayer, itemID, itemCount, 0, [IPY_GameWorld.rptItem])  
 | 
          
 | 
    addDataDict = {"TemplateID":templateID, "NeedCostGold":needCostGold, "AwardIndex":awardIndex,  
 | 
                   "ItemList":str(awardItemList), "ActNum":actNum}  
 | 
    DataRecordPack.DR_FuncGiveItem(curPlayer, "CostRebateAward", addDataDict)  
 | 
    return  
 | 
  
 | 
def Sync_CostRebateInfo(curPlayer, actNum):  
 | 
    ## Í¨ÖªÏû·Ñ·µÀûÍæ¼ÒÊý¾ÝÐÅÏ¢  
 | 
    playerActInfo = ChPyNetSendPack.tagMCCostRebatePlayerInfo()  
 | 
    playerActInfo.ActNum = actNum  
 | 
    playerActInfo.CostGoldTotal = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateGold % actNum)  
 | 
    playerActInfo.AwardRecord = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateAwardRecord % actNum)  
 | 
    NetPackCommon.SendFakePack(curPlayer, playerActInfo)  
 | 
    return  
 | 
  
 | 
def Sync_CostRebateActionInfo(curPlayer, actNum):  
 | 
    ## Í¨ÖªÏû·Ñ·µÀû»î¶¯ÐÅÏ¢  
 | 
      
 | 
    actCostRebateInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_CostRebate, actNum)  
 | 
    if not actCostRebateInfo:  
 | 
        return  
 | 
      
 | 
    if not actCostRebateInfo.get(ShareDefine.ActKey_State):  
 | 
        return  
 | 
      
 | 
    cfgID = actCostRebateInfo.get(ShareDefine.ActKey_CfgID)  
 | 
    ipyData = IpyGameDataPY.GetIpyGameData("ActCostRebate", cfgID)  
 | 
    if not ipyData:  
 | 
        return  
 | 
      
 | 
    templateIDList = ipyData.GetTemplateIDList()  
 | 
    if not templateIDList:  
 | 
        return  
 | 
    job = curPlayer.GetJob()  
 | 
    actWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_CostRebateWorldLV % actNum)  
 | 
    startDateStr, endDateStr = GameWorld.GetOperationActionDateStr(ipyData)  
 | 
    startDateSync = actCostRebateInfo.get(ShareDefine.ActKey_StartDateSync, startDateStr)  
 | 
    actInfo = ChPyNetSendPack.tagMCCostRebateInfo()  
 | 
    actInfo.ActNum = actNum  
 | 
    actInfo.StartDate = startDateSync  
 | 
    actInfo.EndtDate = endDateStr  
 | 
    actInfo.LimitLV = ipyData.GetLVLimit()  
 | 
    actInfo.IsDayReset = ipyData.GetIsDayReset()  
 | 
    actInfo.AwardDayInfo = []  
 | 
    for templateID in templateIDList:  
 | 
        ipyDataList = IpyGameDataPY.GetIpyGameDataList("CostRebateTemplate", templateID)  
 | 
        if not ipyDataList:  
 | 
            continue  
 | 
          
 | 
        dayInfo = ChPyNetSendPack.tagMCCostRebateAwardDay()  
 | 
        dayInfo.AwardInfo = []  
 | 
        for ipyData in ipyDataList:  
 | 
            awardInfo = ChPyNetSendPack.tagMCCostRebateAward()  
 | 
            awardInfo.AwardIndex = ipyData.GetAwardIndex()  
 | 
            awardInfo.NeedGold = ipyData.GetNeedCostGold()  
 | 
            awardInfo.AwardItem = []  
 | 
            for itemID, itemCount, isBind in __GetItemList(ipyData.GetAwardItemList(), job, actWorldLV):  
 | 
                awardItem = ChPyNetSendPack.tagMCCostRebateAwardItem()  
 | 
                awardItem.ItemID = itemID  
 | 
                awardItem.ItemCount = itemCount  
 | 
                awardItem.IsBind = isBind  
 | 
                awardInfo.AwardItem.append(awardItem)  
 | 
            awardInfo.AwardItemCount = len(awardInfo.AwardItem)  
 | 
              
 | 
            dayInfo.AwardInfo.append(awardInfo)  
 | 
              
 | 
        dayInfo.AwardCount = len(dayInfo.AwardInfo)  
 | 
        actInfo.AwardDayInfo.append(dayInfo)  
 | 
          
 | 
    actInfo.AwardDays = len(actInfo.AwardDayInfo)  
 | 
    NetPackCommon.SendFakePack(curPlayer, actInfo)  
 | 
    return  
 | 
  
 | 
  
 |