| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package Player.PlayerActLogin  | 
| #  | 
| # @todo:µÇ¼½±Àø  | 
| # @author xdh  | 
| # @date 2018-07-12 16:50  | 
| # @version 1.0  | 
| #  | 
| #  | 
| # ÏêϸÃèÊö: µÇ¼½±Àø  | 
| #  | 
| #---------------------------------------------------------------------  | 
| """Version = 2018-07-12 16:50"""  | 
| #---------------------------------------------------------------------  | 
|   | 
| import IPY_GameWorld  | 
| import GameWorld  | 
| import ChConfig  | 
| import IpyGameDataPY  | 
| import PlayerControl  | 
| import ItemControler  | 
| import ChPyNetSendPack  | 
| import NetPackCommon  | 
| import ShareDefine  | 
| import PyGameData  | 
| import ItemCommon  | 
|   | 
| import json  | 
|   | 
| def OnLogin(curPlayer):  | 
|     isReset = __CheckPlayerLoginAwardAction(curPlayer)  | 
|     if not isReset:  | 
|         actCostRebateInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_LoginAward, {})  | 
|         # »î¶¯ÖÐͬ²½»î¶¯ÐÅÏ¢  | 
|         SyncLoginAwardInfo(curPlayer)  | 
|         if actCostRebateInfo.get(ShareDefine.ActKey_State):  | 
|             SyncLoginAwardPlayerInfo(curPlayer)  | 
|     return  | 
|   | 
|   | 
| def RefreshOperationAction_LoginAward():  | 
|     playerManager = GameWorld.GetPlayerManager()  | 
|     for i in xrange(playerManager.GetPlayerCount()):  | 
|         curPlayer = playerManager.GetPlayerByIndex(i)  | 
|         if not GameWorld.IsNormalPlayer(curPlayer):  | 
|             continue  | 
|         __CheckPlayerLoginAwardAction(curPlayer)  | 
|     return  | 
|   | 
|   | 
| def __CheckPlayerLoginAwardAction(curPlayer):  | 
|     ## ¼ì²éÍæ¼ÒµÇ¼½±Àø»î¶¯Êý¾ÝÐÅÏ¢  | 
|     playerID = curPlayer.GetPlayerID()  | 
|       | 
|     actLoginAwardInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_LoginAward, {})  | 
|     #loginAwardID = actLoginAwardInfo.get(ShareDefine.ActKey_ID, 0)  | 
|     state = actLoginAwardInfo.get(ShareDefine.ActKey_State, 0)  | 
|     cfgID = actLoginAwardInfo.get(ShareDefine.ActKey_CfgID, 0)  | 
|     if state:  | 
|         #ÿÌì¶¼Òª´¥·¢  | 
|         AddLoginAwardActionCnt(curPlayer, ChConfig.Def_LoginAct_Login, 1, False, False)  | 
|         SyncLoginAwardPlayerInfo(curPlayer)  | 
|   | 
|     playerLoginAwardID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardID, 0, ChConfig.Def_PDictType_LoginAward)  # Íæ¼ÒÉíÉϵĻID  | 
|     # »î¶¯ID ÏàͬµÄ»°²»´¦Àí  | 
|     if cfgID == playerLoginAwardID:  | 
|         #GameWorld.DebugLog("µÇ¼½±Àø»î¶¯ID²»±ä£¬²»´¦Àí£¡", curPlayer.GetPlayerID())  | 
|         return  | 
|     actWorldLVList = actLoginAwardInfo.get(ShareDefine.ActKey_WorldLVList, [])  | 
|     # Î´ÁìÈ¡µÄ½±ÀøÓʼþ·¢·Å  | 
|     if playerLoginAwardID:  | 
|         __SendLoginAwardMail(curPlayer, playerLoginAwardID)  | 
|       | 
|     #if not cfgID:  | 
|     GameWorld.DebugLog('    µÇ¼½±Àø»î¶¯ÖØÖÃ!')  | 
|     curPlayer.ClearNomalDict(ChConfig.Def_PDictType_LoginAward)  | 
|       | 
|     GameWorld.DebugLog("µÇ¼½±Àø±ä¸ü! state=%s,cfgID=%s,playerLoginAwardID=%s£¬actWorldLVList=%s" % (state, cfgID, playerLoginAwardID, actWorldLVList), playerID)  | 
|       | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_LoginAwardID, cfgID, ChConfig.Def_PDictType_LoginAward)  | 
|     for i, actWorldLV in enumerate(actWorldLVList):  | 
|         PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_LoginAwardWorldLV % i, actWorldLV, ChConfig.Def_PDictType_LoginAward)  | 
|           | 
|     SyncLoginAwardInfo(curPlayer)  | 
|     return True  | 
|   | 
|   | 
| def __SendLoginAwardMail(curPlayer, cfgID):  | 
|     # Î´ÁìÈ¡µÄ½±ÀøÓʼþ·¢·Å  | 
|     actIpyData = IpyGameDataPY.GetIpyGameData("ActLoginAward", cfgID)  | 
|     if not actIpyData:  | 
|         return  | 
|     templateIDList = actIpyData.GetTemplateID()  | 
|     if not templateIDList:  | 
|         return  | 
|       | 
|     totalItemDict = {}  | 
|     for day, tidList in enumerate(templateIDList):  | 
|         playerWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardWorldLV % day, 0, ChConfig.Def_PDictType_LoginAward)  | 
|   | 
|         for tid in tidList:  | 
|             ipyData = IpyGameDataPY.GetIpyGameData('LoginAward', tid)  | 
|             if not ipyData:  | 
|                 continue  | 
|             singleTimes = ipyData.GetSingleTimes()  | 
|             curTimes = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardCurTimes % (day, tid), 0, ChConfig.Def_PDictType_LoginAward)  | 
|             gotTimes = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardGotTimes % (day, tid), 0, ChConfig.Def_PDictType_LoginAward)  | 
|             canGotCnt = (curTimes - gotTimes) / singleTimes  | 
|             if not canGotCnt:  | 
|                 continue  | 
|             itemDict = __GetAwardItem(curPlayer, ipyData, playerWorldLV, canGotCnt)  | 
|             GameWorld.AddDictValue(totalItemDict, itemDict)  | 
|       | 
|     if not totalItemDict:  | 
|         return  | 
|     totalItemList = [[itemID, itemCnt, 1] for itemID, itemCnt in totalItemDict.items()]  | 
|     PlayerControl.SendMailByKey('LoginAward_1', [curPlayer.GetID()], totalItemList)  | 
|     return  | 
|   | 
|   | 
| def AddLoginAwardActionCnt(curPlayer, actionID, addCnt=1, isAdd=True, isSync=True):  | 
|     '''Ôö¼ÓµÇ¼½±ÀøÏà¹Ø»î¶¯Íê³É´ÎÊý'''  | 
|     #ÅжϻÊÇ·ñ¿ªÆô  | 
|     if not addCnt:  | 
|         return  | 
|     actLoginAwardInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_LoginAward, {})  | 
|     state = actLoginAwardInfo.get(ShareDefine.ActKey_State, 0)  | 
|     cfgID = actLoginAwardInfo.get(ShareDefine.ActKey_CfgID, 0)  | 
|       | 
|     if not state or not cfgID:  | 
|         return  | 
|     actIpyData = IpyGameDataPY.GetIpyGameData("ActLoginAward", cfgID)  | 
|     if not actIpyData:  | 
|         return  | 
|     templateIDList = actIpyData.GetTemplateID()  | 
|     dayIndex = actLoginAwardInfo.get(ShareDefine.ActKey_DayIndex, 0)  | 
|     if dayIndex >= len(templateIDList):  | 
|         return  | 
|     dayTidList = templateIDList[dayIndex]  | 
|     findTid = 0  | 
|     for tid in dayTidList:  | 
|         if tid / 100 == actionID:  | 
|             findTid = tid  | 
|             break  | 
|     if not findTid:  | 
|         return  | 
|     ipyData = IpyGameDataPY.GetIpyGameData('LoginAward', findTid)  | 
|     if not ipyData:  | 
|         return  | 
|     totalTimes = ipyData.GetTotalTimes()  | 
|     curTimes = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardCurTimes % (dayIndex, findTid), 0, ChConfig.Def_PDictType_LoginAward)  | 
|     if isAdd:  | 
|         addCnt = addCnt if totalTimes == 0 else min(totalTimes - curTimes, addCnt)  | 
|         if addCnt <= 0:  | 
|             return  | 
|         updTimes = curTimes + addCnt  | 
|     else:  | 
|         updTimes = min(addCnt, totalTimes)  | 
|     realAddCnt = updTimes - curTimes  | 
|     if realAddCnt <= 0:  | 
|         return  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_LoginAwardCurTimes % (dayIndex, findTid), updTimes, ChConfig.Def_PDictType_LoginAward)  | 
|     GameWorld.DebugLog('   Ôö¼ÓµÇ¼½±ÀøÏà¹Ø»î¶¯Íê³É´ÎÊý dayIndex=%s, findTid=%s, realAddCnt=%s' % (dayIndex, findTid, realAddCnt))  | 
|     if isSync:  | 
|         SyncLoginAwardPlayerInfo(curPlayer, dayIndex, findTid)  | 
|     return True  | 
|   | 
|   | 
| def GetLoginAwardActionAward(curPlayer, day, templateID):  | 
|     '''ÁìÈ¡µÇ¼½±Àø»î¶¯½±Àø'''  | 
|     templateID = GameWorld.ToIntDef(templateID, 0)  | 
|     ipyData = IpyGameDataPY.GetIpyGameData('LoginAward', templateID)  | 
|     if not ipyData:  | 
|         return  | 
|     singleTimes = ipyData.GetSingleTimes()  | 
|     curTimes = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardCurTimes % (day, templateID), 0, ChConfig.Def_PDictType_LoginAward)  | 
|     gotTimes = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardGotTimes % (day, templateID), 0, ChConfig.Def_PDictType_LoginAward)  | 
|     if curTimes - gotTimes < singleTimes:  | 
|         return  | 
|     playerWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardWorldLV % day, 0, ChConfig.Def_PDictType_LoginAward)  | 
|     #¸ø½±Àø  | 
|     awardDict = __GetAwardItem(curPlayer, ipyData, playerWorldLV)  | 
|     # ¼ì²é±³°ü  | 
|     needSpace = len(awardDict)  | 
|     packSpace = ItemCommon.GetItemPackSpace(curPlayer, IPY_GameWorld.rptItem, needSpace)  | 
|     if needSpace > packSpace:  | 
|         PlayerControl.NotifyCode(curPlayer, "GeRen_chenxin_998371")  | 
|         return  | 
|     #¸üдÎÊý Ã¿´ÎÁì½±Ö»ÁìÒ»´Î  | 
|     newGotTimes = gotTimes + singleTimes  | 
|       | 
|     #GameWorld.Log('  actionID=%s,curTimes=%s,gotTimes=%s,singleTimes=%s,newGotTimes=%s'%(actionID, curTimes, gotTimes,singleTimes, newGotTimes))  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_LoginAwardGotTimes % (day, templateID), newGotTimes, ChConfig.Def_PDictType_LoginAward)  | 
|     for itemID, itemCnt in awardDict.items():  | 
|         ItemControler.GivePlayerItem(curPlayer, itemID, itemCnt, 0, [IPY_GameWorld.rptItem])  | 
|     SyncLoginAwardPlayerInfo(curPlayer, day, templateID)  | 
|     return  | 
|   | 
|   | 
| def __GetAwardItem(curPlayer, ipyData, worldLV, times=1):  | 
|     awardItemDict = {}  | 
|     awardDict = eval(ipyData.GetReward())  | 
|     wlvList = [int(lv) for lv in awardDict]  | 
|     wlvList.sort()  | 
|     findWlv = wlvList[-1]  | 
|     for wlv in wlvList:  | 
|         if worldLV <= wlv:  | 
|             findWlv = wlv  | 
|             break  | 
|     itemList = awardDict[str(findWlv)]  | 
|     for itemID, itemCnt, isbind in itemList:  | 
|         if not itemID or not itemCnt:  | 
|             continue  | 
|         awardItemDict[itemID] = awardItemDict.get(itemID, 0) + itemCnt * times  | 
|           | 
|     return awardItemDict  | 
|   | 
|   | 
| def SyncLoginAwardPlayerInfo(curPlayer, day=-1, templateID=-1):  | 
|     #֪ͨµ±Ç°´ÎÊý¡¢ÒÑÁì´ÎÊý   | 
|     actLoginAwardInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_LoginAward, {})  | 
|     state = actLoginAwardInfo.get(ShareDefine.ActKey_State, 0)  | 
|     cfgID = actLoginAwardInfo.get(ShareDefine.ActKey_CfgID, 0)  | 
|     if not state or not cfgID:  | 
|         return  | 
|     actIpyData = IpyGameDataPY.GetIpyGameData("ActLoginAward", cfgID)  | 
|     if not actIpyData:  | 
|         return  | 
|     templateIDList = actIpyData.GetTemplateID()  | 
|     if not templateIDList:  | 
|         return  | 
|     if day != -1:  | 
|         if day >= len(templateIDList):  | 
|             return  | 
|         syncDayList = [day]  | 
|     else:  | 
|         syncDayList = range(len(templateIDList))  | 
|           | 
|     packData = ChPyNetSendPack.tagMCActLoginAwardPlayerInfo()  | 
|     packData.DayInfoList = []  | 
|     for cday in syncDayList:  | 
|         tiemInfo = ChPyNetSendPack.tagMCActLoginAwardDayPlayerInfo()  | 
|         tiemInfo.DayIndex = cday  | 
|         tiemInfo.TaskList = []  | 
|         for tID in templateIDList[cday]:  | 
|             if templateID != -1 and tID != templateID:  | 
|                 continue  | 
|             tipyData = IpyGameDataPY.GetIpyGameData('LoginAward', tID)  | 
|             if not tipyData:  | 
|                 continue  | 
|             taskInfo = ChPyNetSendPack.tagMCActLoginAwardTaskInfo()  | 
|             taskInfo.TemplateID = tID  | 
|             taskInfo.CurTimes = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardCurTimes % (cday, tID), 0, ChConfig.Def_PDictType_LoginAward)  | 
|             taskInfo.GotTimes = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_LoginAwardGotTimes % (cday, tID), 0, ChConfig.Def_PDictType_LoginAward)  | 
|             tiemInfo.TaskList.append(taskInfo)  | 
|         tiemInfo.ACount = len(tiemInfo.TaskList)  | 
|         packData.DayInfoList.append(tiemInfo)  | 
|     packData.Count = len(packData.DayInfoList)  | 
|     NetPackCommon.SendFakePack(curPlayer, packData)  | 
|     return  | 
|   | 
|   | 
| def SyncLoginAwardInfo(curPlayer):  | 
|     actLoginAwardInfo = PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_LoginAward, {})  | 
|     state = actLoginAwardInfo.get(ShareDefine.ActKey_State, 0)  | 
|     cfgID = actLoginAwardInfo.get(ShareDefine.ActKey_CfgID, 0)  | 
|     if not cfgID:  | 
|         return  | 
|     actIpyData = IpyGameDataPY.GetIpyGameData("ActLoginAward", cfgID)  | 
|     if not actIpyData:  | 
|         return  | 
|     templateIDList = actIpyData.GetTemplateID()  | 
|     if not templateIDList:  | 
|         return  | 
|     actWorldLVList = actLoginAwardInfo.get(ShareDefine.ActKey_WorldLVList, [])  | 
|     startDateStr, endDateStr = GameWorld.GetOperationActionDateStr(actIpyData)  | 
|     actInfo = ChPyNetSendPack.tagMCActLoginAwardInfo()  | 
|     actInfo.Clear()  | 
|     actInfo.StartDate = startDateStr  | 
|     actInfo.EndtDate = endDateStr  | 
|     actInfo.AdvanceMinutes = actIpyData.GetAdvanceMinutes()  | 
|     actInfo.IsDayReset = actIpyData.GetIsDayReset()  | 
|     actInfo.ResetType = actIpyData.GetResetType()  | 
|     actInfo.LimitLV = actIpyData.GetLVLimit()  | 
|     actInfo.DayInfoList = []  | 
|     tidList = []  | 
|     for day, dayActList in enumerate(templateIDList):  | 
|         dayInfo = ChPyNetSendPack.tagMCActLoginAwardDayInfo()  | 
|         dayInfo.TemplateList = dayActList  | 
|         dayInfo.ActCnt = len(dayActList)  | 
|         for tid in dayActList:  | 
|             if tid not in tidList:  | 
|                 tidList.append(tid)  | 
|         dayInfo.WorldLV = actWorldLVList[day] if day < len(actWorldLVList) else 0  | 
|         actInfo.DayInfoList.append(dayInfo)  | 
|     actInfo.DayCnt = len(actInfo.DayInfoList)  | 
|     actInfo.ActionInfo = []  | 
|     for tid in tidList:  | 
|         ipyData = IpyGameDataPY.GetIpyGameData('LoginAward', tid)  | 
|         if not ipyData:  | 
|             GameWorld.ErrLog('    µÇ¼½±ÀøÄ£°å±íÕÒ²»µ½Ä£°å%s' % tid)  | 
|             continue  | 
|         tInfo = ChPyNetSendPack.tagMCActLoginAwardAction()  | 
|         tInfo.TemplateID = tid  | 
|         tInfo.ActionType = ipyData.GetActionType()  | 
|         tInfo.TotalTimes = ipyData.GetTotalTimes()  | 
|         tInfo.SingleTimes = ipyData.GetSingleTimes()  | 
|         tInfo.ItemInfo = ipyData.GetReward()  | 
|         tInfo.Count = len(tInfo.ItemInfo)  | 
|         actInfo.ActionInfo.append(tInfo)  | 
|     actInfo.TCount = len(actInfo.ActionInfo)  | 
|     NetPackCommon.SendFakePack(curPlayer, actInfo)  | 
|     return  |