#!/usr/bin/python  
 | 
# -*- coding: GBK -*-  
 | 
#-------------------------------------------------------------------------------  
 | 
#  
 | 
##@package Player.PlayerFlashSale  
 | 
#  
 | 
# @todo:ÏÞʱÇÀ¹º»î¶¯  
 | 
# @author xdh  
 | 
# @date 2018-7-17  
 | 
# @version 1.0  
 | 
#  
 | 
# ÏêϸÃèÊö: ÏÞʱÇÀ¹º»î¶¯  
 | 
#  
 | 
#-------------------------------------------------------------------------------  
 | 
#"""Version = 2018-7-17 12:00"""  
 | 
#-------------------------------------------------------------------------------  
 | 
  
 | 
import PyGameData  
 | 
import ShareDefine  
 | 
import PlayerControl  
 | 
import IpyGameDataPY  
 | 
import FunctionNPCCommon  
 | 
import ChPyNetSendPack  
 | 
import NetPackCommon  
 | 
import GameWorld  
 | 
import ChConfig  
 | 
  
 | 
import datetime  
 | 
  
 | 
  
 | 
def GetShopTypeList(cfgID, dayIndex, state, woldLV):  
 | 
    if cfgID == 0 or state == 0:  
 | 
        return []  
 | 
    ipyData = IpyGameDataPY.GetIpyGameData("ActFlashSale", cfgID)  
 | 
    if not ipyData:  
 | 
        return []  
 | 
    shopTypeList = GameWorld.GetDictValueByRangeKey(ipyData.GetShopTypeList(), woldLV, [])  
 | 
    if not shopTypeList:  
 | 
        return []  
 | 
    todayShopTypeList = shopTypeList[-1] if dayIndex >= len(shopTypeList) else shopTypeList[dayIndex]  
 | 
    return [todayShopTypeList[state - 1] if state - 1 < len(todayShopTypeList) else todayShopTypeList[-1]]  
 | 
  
 | 
  
 | 
def OnPlayerLogin(curPlayer):  
 | 
    __CheckPlayerflashSaleAction(curPlayer)  
 | 
    return  
 | 
  
 | 
  
 | 
def RefreshflashSaleActionInfo():  
 | 
    ## ÊÕµ½GameServerͬ²½µÄ»î¶¯ÐÅÏ¢£¬Ë¢Ð»ÐÅÏ¢  
 | 
    playerManager = GameWorld.GetPlayerManager()  
 | 
    for index in xrange(playerManager.GetPlayerCount()):  
 | 
        curPlayer = playerManager.GetPlayerByIndex(index)  
 | 
        if curPlayer.GetID() == 0:  
 | 
            continue  
 | 
        __CheckPlayerflashSaleAction(curPlayer)  
 | 
    return  
 | 
  
 | 
  
 | 
def __CheckPlayerflashSaleAction(curPlayer):  
 | 
    ## ¼ì²éÍæ¼ÒÏÞʱÇÀ¹º»î¶¯Êý¾ÝÐÅÏ¢  
 | 
      
 | 
    playerID = curPlayer.GetPlayerID()  
 | 
      
 | 
    actInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_FlashSale, {})  
 | 
    actID = actInfo.get(ShareDefine.ActKey_ID, 0)  
 | 
    state = actInfo.get(ShareDefine.ActKey_State, 0)  
 | 
    cfgID = actInfo.get(ShareDefine.ActKey_CfgID, 0)  
 | 
    dayIndex = actInfo.get(ShareDefine.ActKey_DayIndex, 0)  
 | 
    woldLV = actInfo.get(ShareDefine.ActKey_WorldLV, 0)  
 | 
    playerActID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_FlashSaleID, 0, ChConfig.Def_PDictType_FlashSale)  # Íæ¼ÒÉíÉϵĻID  
 | 
    playerActState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_FlashSaleState, 0, ChConfig.Def_PDictType_FlashSale)  # Íæ¼ÒÉíÉϵĻState  
 | 
    playerMailState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_FlashSaleMailState, 0, ChConfig.Def_PDictType_FlashSale)  # Íæ¼ÒÉíÉϵĻÌáÐÑÓʼþ״̬  
 | 
    isReset = False  
 | 
    if actID != playerActID or (state != playerActState):  
 | 
        isReset = True  
 | 
        if cfgID:  
 | 
            flashSaleIpyData = IpyGameDataPY.GetIpyGameData("ActFlashSale", cfgID)  
 | 
            openServerDay = GameWorld.GetGameWorld().GetGameWorldDictByKey(ShareDefine.Def_Notify_WorldKey_ServerDay) + 1  
 | 
            startDate = GameWorld.GetOperationActionDateStr(flashSaleIpyData.GetStartDate(), openServerDay)  
 | 
            startTimeNum = GameWorld.ChangeTimeStrToNum(startDate, timeFormat=ChConfig.TYPE_Time_Format_Day)  
 | 
        else:  
 | 
            startTimeNum = 0  
 | 
        if playerMailState != startTimeNum:  
 | 
            GameWorld.DebugLog('    ÏÞʱÇÀ¹º»î¶¯ÖØÖÃ!')  
 | 
            curPlayer.ClearNomalDict(ChConfig.Def_PDictType_FlashSale)  
 | 
          
 | 
        shopTypeList = GetShopTypeList(cfgID, dayIndex, state, woldLV)  
 | 
        if shopTypeList:  
 | 
            FunctionNPCCommon.ResetShopItemBuyCountByShopType(curPlayer, shopTypeList)  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_FlashSaleID, actID, ChConfig.Def_PDictType_FlashSale)  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_FlashSaleState, state, ChConfig.Def_PDictType_FlashSale)  
 | 
        #Sync_flashSaleActionInfo(curPlayer)  
 | 
        GameWorld.DebugLog("ÏÞʱÇÀ¹ºµ¥³¡ÖØÖÃ! actID=%s,playerActID=%s,dayIndex=%s,state=%s,playerActState=%s,shopTypeList=%s,playerMailState=%s" % (actID, playerActID, dayIndex, state, playerActState, shopTypeList,playerMailState), playerID)  
 | 
    else:  
 | 
        GameWorld.DebugLog("ÏÞʱÇÀ¹º»î¶¯ID²»±ä£¬²»´¦Àí£¡", playerID)  
 | 
          
 | 
    if cfgID:  
 | 
        Sync_flashSaleActionInfo(curPlayer)  
 | 
        Sync_FlashSaleAppointmentInfo(curPlayer)  
 | 
    return isReset  
 | 
  
 | 
  
 | 
def ProcessFlashSaleMail(curPlayer, tick):  
 | 
    if not GameWorld.SetPlayerTickTime(curPlayer, ChConfig.TYPE_Player_Tick_FlashSale, tick):  
 | 
        return  
 | 
    playerMailState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_FlashSaleMailState, 0, ChConfig.Def_PDictType_FlashSale)  # Íæ¼ÒÉíÉϵĻÌáÐÑÓʼþ״̬  
 | 
    if playerMailState:  
 | 
        return  
 | 
    actInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_FlashSale, {})  
 | 
    if not actInfo:  
 | 
        return  
 | 
    cfgID = actInfo.get(ShareDefine.ActKey_CfgID, 0)  
 | 
    if not cfgID:  
 | 
        return  
 | 
    flashSaleIpyData = IpyGameDataPY.GetIpyGameData("ActFlashSale", cfgID)  
 | 
    if not flashSaleIpyData:  
 | 
        return  
 | 
    mailKey = flashSaleIpyData.GetMailKey()  
 | 
    LVLimit = flashSaleIpyData.GetLVLimit()  
 | 
    if not mailKey or curPlayer.GetLV() < LVLimit:  
 | 
        return  
 | 
    openServerDay = GameWorld.GetGameWorld().GetGameWorldDictByKey(ShareDefine.Def_Notify_WorldKey_ServerDay) + 1  
 | 
    startDate = GameWorld.GetOperationActionDateStr(flashSaleIpyData.GetStartDate(), openServerDay)  
 | 
    endDate = GameWorld.GetOperationActionDateStr(flashSaleIpyData.GetEndDate(), openServerDay)  
 | 
    startTimeList = flashSaleIpyData.GetStartTimeList()  
 | 
    endTimeList = flashSaleIpyData.GetEndTimeList()  
 | 
    if not startTimeList or not endTimeList:  
 | 
        return  
 | 
    advanceMinutes = flashSaleIpyData.GetAdvanceMinutes()  
 | 
    startTime = datetime.datetime.strptime("%s %s:00" % (startDate, startTimeList[0]), ChConfig.TYPE_Time_Format) + datetime.timedelta(minutes=-advanceMinutes)  
 | 
    endTime = datetime.datetime.strptime("%s %s:00" % (endDate, endTimeList[-1]), ChConfig.TYPE_Time_Format)  
 | 
    curDateTime = GameWorld.GetCurrentTime()  
 | 
    if startTime <= curDateTime <= endTime:  
 | 
        paramList = [startDate.replace('-','/'), endDate.replace('-','/'), GameWorld.GbkToCode('¡¢').join(startTimeList)]  
 | 
        PlayerControl.SendMailByKey(mailKey, [curPlayer.GetID()], flashSaleIpyData.GetMailItemPrize(), paramList)  
 | 
        startTimeNum = GameWorld.ChangeTimeStrToNum(startDate, timeFormat=ChConfig.TYPE_Time_Format_Day)  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_FlashSaleMailState, startTimeNum, ChConfig.Def_PDictType_FlashSale)  
 | 
        GameWorld.DebugLog("    ·¢ËÍÐÂÏÞʱÇÀ¹ºÓʼþÌáÐÑ!", curPlayer.GetID())  
 | 
#    else:  
 | 
#        GameWorld.DebugLog("    ·¢ËÍÐÂÏÞʱÇÀ¹ºÓʼþÌáÐÑ  Ê±¼äûµ½!startTime=%s,endTime=%s,curDateTime=%s"%(startTime,endTime,curDateTime), curPlayer.GetID())  
 | 
    return  
 | 
  
 | 
  
 | 
def Sync_flashSaleActionInfo(curPlayer):  
 | 
    ## Í¨ÖªÏÞʱÇÀ¹º»î¶¯ÐÅÏ¢  
 | 
    actInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_FlashSale, {})  
 | 
    if not actInfo:  
 | 
        return  
 | 
      
 | 
    #ÐèÒªÌáǰ֪ͨ£¬ËùÒÔÈ¥µô´ËÏÞÖÆ  
 | 
    #if not actInfo.get(ShareDefine.ActKey_State):  
 | 
    #    return  
 | 
      
 | 
    cfgID = actInfo.get(ShareDefine.ActKey_CfgID, 0)  
 | 
    if not cfgID:  
 | 
        return  
 | 
  
 | 
    flashSaleIpyData = IpyGameDataPY.GetIpyGameData("ActFlashSale", cfgID)  
 | 
    if not flashSaleIpyData:  
 | 
        return  
 | 
    woldLV = actInfo.get(ShareDefine.ActKey_WorldLV, 0)  
 | 
    shopTypeList = GameWorld.GetDictValueByRangeKey(flashSaleIpyData.GetShopTypeList(), woldLV, [])  
 | 
    startTimeList = flashSaleIpyData.GetStartTimeList()  
 | 
    endTimeList = flashSaleIpyData.GetEndTimeList()  
 | 
    if len(startTimeList) != len(endTimeList):  
 | 
        GameWorld.ErrLog("ÏÞʱÇÀ¹º¿ª¹ØÊ±¼äʱ·ÖÅäÖôíÎó!cfgID=%s" % cfgID)  
 | 
        return  
 | 
    job = curPlayer.GetJob()  
 | 
    openServerDay = GameWorld.GetGameWorld().GetGameWorldDictByKey(ShareDefine.Def_Notify_WorldKey_ServerDay) + 1  
 | 
    packInfo = ChPyNetSendPack.tagMCFlashSaleInfo()  
 | 
    packInfo.StartDate = GameWorld.GetOperationActionDateStr(flashSaleIpyData.GetStartDate(), openServerDay)  
 | 
    packInfo.EndtDate = GameWorld.GetOperationActionDateStr(flashSaleIpyData.GetEndDate(), openServerDay)  
 | 
    packInfo.AdvanceMinutes = flashSaleIpyData.GetAdvanceMinutes()  
 | 
    packInfo.ActivityTime = []  
 | 
    for i, startTime in enumerate(startTimeList):  
 | 
        timeInfo = ChPyNetSendPack.tagMCFlashSaleTime()  
 | 
        timeInfo.StartTime = startTime  
 | 
        timeInfo.EndtTime = endTimeList[i]  
 | 
        packInfo.ActivityTime.append(timeInfo)  
 | 
    packInfo.ActivityTimeCount = len(packInfo.ActivityTime)  
 | 
    packInfo.IsDayReset = flashSaleIpyData.GetIsDayReset()  
 | 
    packInfo.LimitLV = flashSaleIpyData.GetLVLimit()  
 | 
    packInfo.ShopInfo = []  
 | 
    for dayIndex, shopList in enumerate(shopTypeList):  
 | 
        for timeIndex, shopType in enumerate(shopList):   
 | 
            shopItemIpyDataList = IpyGameDataPY.GetIpyGameDataByCondition("Store", {"ShopType":shopType}, True, True)  
 | 
            if not shopItemIpyDataList:  
 | 
                continue  
 | 
              
 | 
            shop = ChPyNetSendPack.tagMCFlashSaleShop()  
 | 
            shop.DayIndex = dayIndex  
 | 
            shop.TimeIndex = timeIndex  
 | 
            shop.GiftbagInfo = []  
 | 
              
 | 
            for itemIpyData in shopItemIpyDataList:  
 | 
                giftBag = ChPyNetSendPack.tagMCFlashSaleGiftbag()  
 | 
                giftBag.GiftID = itemIpyData.GetID()  
 | 
                giftBag.BuyCountLimit = 0 if not itemIpyData.GetLimitCnt() else itemIpyData.GetLimitCnt()[0]  
 | 
                giftBag.ServerBuyCountLimit = itemIpyData.GetServerLimitCnt()  
 | 
                giftBag.MoneyType = itemIpyData.GetMoneyType()  
 | 
                giftBag.MoneyNumber = itemIpyData.GetMoneyNum()  
 | 
                giftBag.MoneyOriginal = itemIpyData.GetMoneyOriginal()  
 | 
                itemID = itemIpyData.GetItemID()  
 | 
                jobItemList = itemIpyData.GetJobItem()  
 | 
                jobItemID = FunctionNPCCommon.GetShopJobItem(job, itemID, jobItemList)  
 | 
                giftBag.ItemID = jobItemID  
 | 
                giftBag.ItemCount = itemIpyData.GetItemCnt()  
 | 
                giftBag.IsBind = itemIpyData.GetIsBind()  
 | 
                shop.GiftbagInfo.append(giftBag)  
 | 
                  
 | 
            shop.GiftbagCount = len(shop.GiftbagInfo)  
 | 
            packInfo.ShopInfo.append(shop)  
 | 
          
 | 
    packInfo.ShopCount = len(packInfo.ShopInfo)  
 | 
    NetPackCommon.SendFakePack(curPlayer, packInfo)  
 | 
    return  
 | 
  
 | 
  
 | 
#// AA 05 ÏÞʱÇÀ¹ºÔ¤Ô¼ #tagCMFlashSaleAppointment  
 | 
#  
 | 
#struct     tagCMFlashSaleAppointment  
 | 
#{  
 | 
#    tagHead        Head;  
 | 
#    DWORD        GoodsID;         // ÇÀ¹ºÉÌÆ·±êʶ  
 | 
#    BYTE        State;         // 1-Ô¤Ô¼ 0-È¡Ïû  
 | 
#};  
 | 
def OnFlashSaleAppointment(index, packData, tick):  
 | 
    curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  
 | 
    goodsID = packData.GoodsID  
 | 
      
 | 
    actInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_FlashSale, {})  
 | 
    if not actInfo:  
 | 
        return  
 | 
    cfgID = actInfo.get(ShareDefine.ActKey_CfgID, 0)  
 | 
    if not cfgID:  
 | 
        return  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_FlashSaleYY % goodsID, packData.State, ChConfig.Def_PDictType_FlashSale)  
 | 
    Sync_FlashSaleAppointmentInfo(curPlayer, goodsID)  
 | 
    return  
 | 
  
 | 
  
 | 
def Sync_FlashSaleAppointmentInfo(curPlayer, goodsID=0):  
 | 
    ##֪ͨÏÞʱÇÀ¹ºÔ¤Ô¼Çé¿ö  
 | 
    actInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_FlashSale, {})  
 | 
    if not actInfo:  
 | 
        return  
 | 
    cfgID = actInfo.get(ShareDefine.ActKey_CfgID, 0)  
 | 
    if not cfgID:  
 | 
        return  
 | 
    flashSaleIpyData = IpyGameDataPY.GetIpyGameData("ActFlashSale", cfgID)  
 | 
    if not flashSaleIpyData:  
 | 
        return  
 | 
    woldLV = actInfo.get(ShareDefine.ActKey_WorldLV, 0)  
 | 
      
 | 
    packInfo = ChPyNetSendPack.tagMCFlashSaleAppointmentInfo()  
 | 
    packInfo.GoodsList = []  
 | 
    if goodsID == 0:  
 | 
        packInfo.IsAll = 1  
 | 
        shopTypeList = GameWorld.GetDictValueByRangeKey(flashSaleIpyData.GetShopTypeList(), woldLV, [])  
 | 
        for dayIndex, shopList in enumerate(shopTypeList):  
 | 
            for timeIndex, shopType in enumerate(shopList):  
 | 
                shopItemIpyDataList = IpyGameDataPY.GetIpyGameDataByCondition("Store", {"ShopType":shopType}, True, True)  
 | 
                if not shopItemIpyDataList:  
 | 
                    continue  
 | 
                for i in xrange(len(shopItemIpyDataList)):  
 | 
                    goodsMark = dayIndex * 10000 + timeIndex * 100 + i  #ÉÌÆ·±êʶ  
 | 
                    isAppointment = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_FlashSaleYY % goodsMark, 0, ChConfig.Def_PDictType_FlashSale)  
 | 
                    if isAppointment:  
 | 
                        appoinetmentPack = ChPyNetSendPack.tagMCFlashSaleAppointmentState()  
 | 
                        appoinetmentPack.GoodsMark = goodsMark  
 | 
                        appoinetmentPack.State = 1  
 | 
                        packInfo.GoodsList.append(appoinetmentPack)  
 | 
    else:  
 | 
        packInfo.IsAll = 0  
 | 
        isAppointment = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_FlashSaleYY % goodsID, 0, ChConfig.Def_PDictType_FlashSale)  
 | 
        appoinetmentPack = ChPyNetSendPack.tagMCFlashSaleAppointmentState()  
 | 
        appoinetmentPack.GoodsMark = goodsID  
 | 
        appoinetmentPack.State = isAppointment  
 | 
        packInfo.GoodsList.append(appoinetmentPack)  
 | 
    packInfo.GoodsCount = len(packInfo.GoodsList)  
 | 
    NetPackCommon.SendFakePack(curPlayer, packInfo)  
 | 
    return  
 |