#!/usr/bin/python  
 | 
# -*- coding: GBK -*-  
 | 
#-------------------------------------------------------------------------------  
 | 
#  
 | 
#-------------------------------------------------------------------------------  
 | 
#  
 | 
##@package PlayerOnlinePrize  
 | 
#  
 | 
# @todo: ´´½Ç7ÌìÔÚÏß½±Àø  
 | 
# @author xdh  
 | 
# @date 2018-03-06 17:00  
 | 
# @version 1.0  
 | 
#  
 | 
#---------------------------------------------------------------------  
 | 
#"""Version = 2018-03-06 17:00"""  
 | 
#---------------------------------------------------------------------  
 | 
  
 | 
import ChPyNetSendPack  
 | 
import ChConfig  
 | 
import GameWorld  
 | 
import ItemControler  
 | 
import IPY_GameWorld  
 | 
import ItemCommon  
 | 
import NetPackCommon  
 | 
import PlayerControl  
 | 
import IpyGameDataPY  
 | 
import PlayerTJG  
 | 
  
 | 
import time  
 | 
  
 | 
'''  
 | 
ÌØÊâ˵Ã÷£º  
 | 
     ¸Ų̂Ö÷¸ÉÒªÐÞ¸ÄΪ´´½ÇÆßÈÕ½±Àø£¬µ«ÊÇÒòΪǰ¶ËÈȸüÎÊÌ⣬ǰ¶ËÎÞ·¨Ê¹Óþɱí¸ú¾É·â°ü A3 08 ÔÚÏß½±ÀøÐÅÏ¢ #tagMCOnlinePrize  
 | 
     ÎªÁ˱£³Öǰºó¶ËÂ߼ͳһ£¬ºó¶Ë²»ÔÙ¶¯¾É±í¸ú¾É·â°üA308£¬·ÀÖ¹Ð޸ĺóǰ¶Ë±¨´í  
 | 
     Ê¹ÓÃÐÂ±í¼°Ð·â°ü  B1 13 ÔÚÏß½±ÀøÐÅϢР#tagMCOnlinePrizeNew  
 | 
'''  
 | 
  
 | 
#//A5 06 Íæ¼ÒÁìÈ¡ÔÚÏß½±Àø #tagCMGetOnlinePrize  
 | 
#  
 | 
#struct   tagCMGetOnlinePrize  
 | 
#  
 | 
#{  
 | 
#    tagHead         Head;  
 | 
#    BYTE        Index;    // ½±ÀøË÷Òý,ÆäËû°´Ë³Ðò  
 | 
#    BYTE        IsDaily;    // ÊÇ·ñÁìȡÿÈÕÈÕ³£µÄ½±Àø£¬ÊÇ·¢1£¬Èç¹ûÁìÈ¡¶¨ÖÆ´´½ÇÌì½±ÀøµÄÔòĬÈÏ·¢0¼´¿É  
 | 
#};  
 | 
def GetOnlinePrize(index, clientPack, tick):  
 | 
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  
 | 
    creatRoleDay = GameWorld.GetCreateRoleDays(curPlayer)  
 | 
    prizeIndex = clientPack.Index  
 | 
    GameWorld.DebugLog("ÁìÈ¡ÔÚÏß½±Àø: creatRoleDay=%s,prizeIndex=%s" % (creatRoleDay, prizeIndex))  
 | 
    if prizeIndex <= 0:  
 | 
        GameWorld.DebugLog("prizeIndexÐèÒª´Ó1¿ªÊ¼£¬ÎÒÒ²²»Ï룬±£ÁôÔÂß¼°É£¡£¡£¡")  
 | 
        return  
 | 
    dayID = creatRoleDay  
 | 
    ipyData = IpyGameDataPY.GetIpyGameData('OnlineAwardNew', dayID)  
 | 
    if not ipyData:  
 | 
        return  
 | 
    #ÏȾÀÕýÒ»´Îʱ¼ä  
 | 
    onlineTime = GetOnlineTime(curPlayer)  
 | 
      
 | 
    #ÒÑÁì  
 | 
    awardInfo = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_OnlinePrizeNote % dayID)  
 | 
    awardMark = pow(2, prizeIndex - 1)  
 | 
    if awardInfo & awardMark:  
 | 
        #ÒÑÁìÈ¡  
 | 
        GameWorld.DebugLog("ÒÑÁìÈ¡½±Àø£¡dayID=%s,prizeIndex=%s" % (dayID, prizeIndex))  
 | 
        return  
 | 
    stageTimeList = ipyData.GetStageTime()  
 | 
      
 | 
    if prizeIndex - 1 >= len(stageTimeList):  
 | 
        GameWorld.DebugLog("²»´æÔÚ¸ÃÔÚÏß½±ÀøË÷Òý!")  
 | 
        return  
 | 
    needTime = stageTimeList[prizeIndex - 1]  
 | 
    if needTime * 60 > onlineTime:  
 | 
        GameWorld.DebugLog("ÔÚÏßʱ¼ä²»×㣡dayID=%s,prizeIndex=%s,needTime=%s" % (dayID, prizeIndex, needTime))  
 | 
        return  
 | 
      
 | 
    awardDict = ipyData.GetReward()  
 | 
    if str(prizeIndex) not in awardDict:  
 | 
        GameWorld.DebugLog("ûÓÐÅäÖøÃË÷Òý¶ÔÓ¦ÔÚÏß½±Àø!")  
 | 
        return  
 | 
    itemList = awardDict[str(prizeIndex)]  
 | 
      
 | 
    # ¼ì²é±³°ü  
 | 
    needSpace = len(itemList)  
 | 
    packSpace = ItemCommon.GetItemPackSpace(curPlayer, IPY_GameWorld.rptItem, needSpace)  
 | 
    if needSpace > packSpace:  
 | 
        PlayerControl.NotifyCode(curPlayer, "GeRen_chenxin_998371")  
 | 
        return  
 | 
          
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_OnlinePrizeNote % dayID, awardInfo | awardMark)  
 | 
      
 | 
    # ¸øÎïÆ·  
 | 
    for itemID, itemCount in itemList:  
 | 
        ItemControler.GivePlayerItem(curPlayer, itemID, itemCount, 0, [IPY_GameWorld.rptItem])  
 | 
          
 | 
    #·¢°ü  
 | 
    SendOnlinePrizeInfo(curPlayer)  
 | 
    return  
 | 
  
 | 
  
 | 
##²éѯÔÚÏß½±Àø  
 | 
# @param None  
 | 
# @return None  
 | 
def QueryOnlinePrize(index, clientPack, tick):  
 | 
#    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  
 | 
#    # ¼ÆËãʱ³¤  
 | 
#    CalcOnlineTime(curPlayer, tick, True)  
 | 
    return  
 | 
  
 | 
def GetOnlineTime(curPlayer):  
 | 
    onlineTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_DayOnlineTime)  
 | 
    markTick = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_OnlineStartTick)  
 | 
    if not markTick:  
 | 
        return onlineTime  
 | 
    curTime = int(time.time())  
 | 
    passTick = curTime - markTick  
 | 
    if passTick <= 0:  
 | 
        return onlineTime  
 | 
    return onlineTime + passTick  
 | 
  
 | 
##ÀëÏ߸üмǼÔÚÏßʱ³¤  
 | 
# @param notify ÊÇ·ñ֪ͨ¿Í»§¶Ë  
 | 
# @return None  
 | 
def CalcOnlineTime(curPlayer):  
 | 
    if PlayerTJG.GetIsTJG(curPlayer):  
 | 
        return  
 | 
    onlineTime = GetOnlineTime(curPlayer)  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_DayOnlineTime, onlineTime)  
 | 
    SetStartCalcOnlineTick(curPlayer, 0)  
 | 
    return  
 | 
  
 | 
      
 | 
##·¢Ëͽ±ÀøÐÅÏ¢°ü  
 | 
# @param None  
 | 
# @return None  
 | 
def SendOnlinePrizeInfo(curPlayer):  
 | 
    dataCount = IpyGameDataPY.IPY_Data().GetOnlineAwardNewCount()  
 | 
    if not dataCount:  
 | 
        return  
 | 
    creatRoleDay = GameWorld.GetCreateRoleDays(curPlayer)  
 | 
    sendPack = ChPyNetSendPack.tagMCOnlinePrizeNew()  
 | 
    sendPack.Clear()  
 | 
    sendPack.OnlineTime = GetOnlineTime(curPlayer) * 1000  
 | 
    sendPack.PrizeInfo = [curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_OnlinePrizeNote % creatRoleDay)] # ´´½Ç7ÈÕµÄÓëǰ¶ËÔ¼¶¨¹Ì¶¨Ö»·¢µ±ÌìµÄ  
 | 
#    for i in xrange(dataCount):  
 | 
#        dayID = i + 1#i+1£¬Ôö¼ÓÁËÿÈÕÖ§³Ö£¬´Ó0¿ªÊ¼£¬´ú±íÿÈÕ  
 | 
#        sendPack.PrizeInfo.append(curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_OnlinePrizeNote % dayID))  
 | 
    sendPack.Len = len(sendPack.PrizeInfo)  
 | 
    #֪ͨ¿Í»§¶Ëʱ¼ä  
 | 
    NetPackCommon.SendFakePack(curPlayer, sendPack)  
 | 
    return  
 | 
  
 | 
  
 | 
##ÉèÖÿªÊ¼¼ÆÊ±Ê±¼ä  
 | 
# @param None  
 | 
# @return None  
 | 
def SetStartCalcOnlineTick(curPlayer, newTime=-1):  
 | 
    if newTime == -1:  
 | 
        newTime=int(time.time())  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_OnlineStartTick, newTime)  
 | 
    return  
 | 
  
 | 
## µÇ¼  
 | 
#  @param curPlayer  
 | 
#  @return None  
 | 
def OnPlayerLogin(curPlayer):  
 | 
    SetStartCalcOnlineTick(curPlayer)  
 | 
    SendOnlinePrizeInfo(curPlayer)  
 | 
    return  
 | 
  
 | 
##ONDAYÖØÖÃ, Í¨Öª¿Í»§¶Ë  
 | 
# @param None  
 | 
# @return None  
 | 
def ResetPrizeInfoOnDay(curPlayer):  
 | 
    __OnlineMailPrize(curPlayer)  
 | 
      
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_DayOnlineTime, 0)  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_OnlinePrizeNote % 0, 0) # ÖØÖÃÿÈÕÔÚÏß½±Àø  
 | 
     
 | 
    if PlayerTJG.GetIsTJG(curPlayer):  
 | 
        return  
 | 
    SetStartCalcOnlineTick(curPlayer)  
 | 
      
 | 
    SendOnlinePrizeInfo(curPlayer)  
 | 
    return  
 | 
  
 | 
def __OnlineMailPrize(curPlayer):  
 | 
    #Óʼþ²¹·¢½±Àø  
 | 
    creatRoleDay = GameWorld.GetCreateRoleDays(curPlayer)  
 | 
    if creatRoleDay <= 1:  
 | 
        return  
 | 
    mailAwardDay = creatRoleDay - 1  
 | 
    ipyData = IpyGameDataPY.GetIpyGameDataNotLog('OnlineAwardNew', mailAwardDay)  
 | 
    if not ipyData:  
 | 
        ipyData = IpyGameDataPY.GetIpyGameDataNotLog('OnlineAwardNew', 0)  
 | 
        if not ipyData:  
 | 
            return  
 | 
        mailAwardDay = 0 # ×òÈշǶ¨ÖƵģ¬ÇÒÓÐÅäÖÃ0³£¹æµÄ£¬Ôò²¹·¢0µÄ  
 | 
          
 | 
    prizeInfo = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_OnlinePrizeNote % mailAwardDay)  
 | 
    onlineTime = GetOnlineTime(curPlayer)  
 | 
    needTimeList = ipyData.GetStageTime()  
 | 
    awardDict = ipyData.GetReward()  
 | 
    itemList = []  
 | 
    for i, needTime in enumerate(needTimeList):  
 | 
        if onlineTime >= needTime * 60 and not prizeInfo & pow(2, i):  
 | 
            for itemID, itemCnt in awardDict[str(i + 1)]:  
 | 
                itemList.append([itemID, itemCnt, 1])  
 | 
            prizeInfo|=pow(2, i)  
 | 
      
 | 
    if not itemList:  
 | 
        return  
 | 
    GameWorld.DebugLog('²¹·¢×òÈÕÔÚÏß½±Àø: mailAwardDay=%s,onlineTime=%s' % (mailAwardDay, onlineTime))  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_OnlinePrizeNote % mailAwardDay, prizeInfo)  
 | 
    PlayerControl.SendMailByKey('OnLineAward1', [curPlayer.GetID()], itemList)  
 | 
    return  
 | 
              
 | 
              
 | 
      
 |