| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package Player.PlayerActTotalRecharge  | 
| #  | 
| # @todo:ÀۼƳäÖµ»î¶¯  | 
| # @author hxp  | 
| # @date 2018-7-16  | 
| # @version 1.0  | 
| #  | 
| # ÏêϸÃèÊö: ÀۼƳäÖµXÔª»î¶¯£¬Ö§³Ö¶à¸öÀÛ³ä»î¶¯Í¬Ê±´æÔÚ£¬ÐèÒª¶à¸öÔËÓª»î¶¯Ê±¼ä±íÅäÖà  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #"""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 ItemCommon  | 
| import GameWorld  | 
| import ChConfig  | 
| import CommFunc  | 
|   | 
| def GetTemplateID(cfgID, dayIndex):  | 
|     if cfgID == None or dayIndex == None:  | 
|         return 0  | 
|     ipyData = IpyGameDataPY.GetIpyGameData("ActTotalRecharge", 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_TotalRecharge, {}).values():  | 
|         actNum = actInfo.get(ShareDefine.ActKey_ActNum, 0)  | 
|         isReset = __CheckPlayerTotalRechargeAction(curPlayer, actNum)  | 
|         if not isReset:  | 
|             # »î¶¯ÖÐͬ²½»î¶¯ÐÅÏ¢  | 
|             if actInfo.get(ShareDefine.ActKey_State):  | 
|                 Sync_TotalRechargeActionInfo(curPlayer, actNum)  | 
|                 Sync_TotalRechargeInfo(curPlayer, actNum)  | 
|                   | 
|     return  | 
|   | 
| def RefreshTotalRechargeActionInfo(actNum):  | 
|     ## ÊÕµ½GameServerͬ²½µÄ»î¶¯ÐÅÏ¢£¬Ë¢Ð»ÐÅÏ¢  | 
|     playerManager = GameWorld.GetPlayerManager()  | 
|     for index in xrange(playerManager.GetPlayerCount()):  | 
|         curPlayer = playerManager.GetPlayerByIndex(index)  | 
|         if curPlayer.GetID() == 0:  | 
|             continue  | 
|         __CheckPlayerTotalRechargeAction(curPlayer, actNum)  | 
|     return  | 
|   | 
| def __CheckPlayerTotalRechargeAction(curPlayer, actNum):  | 
|     ## ¼ì²éÍæ¼ÒÀۼƳäÖµ»î¶¯Êý¾ÝÐÅÏ¢  | 
|       | 
|     playerID = curPlayer.GetPlayerID()  | 
|     actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_TotalRecharge, actNum)  | 
|     TotalRechargeID = actInfo.get(ShareDefine.ActKey_ID, 0)  | 
|     state = actInfo.get(ShareDefine.ActKey_State, 0)  | 
|       | 
|     playerTotalRechargeID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeID % actNum) # Íæ¼ÒÉíÉϵĻID  | 
|       | 
|     # »î¶¯ID ÏàͬµÄ»°²»´¦Àí  | 
|     if TotalRechargeID == playerTotalRechargeID:  | 
|         GameWorld.DebugLog("ÀۼƳäÖµ»î¶¯ID²»±ä£¬²»´¦Àí£¡actNum=%s" % actNum, curPlayer.GetPlayerID())  | 
|         return  | 
|     actWorldLV = actInfo.get(ShareDefine.ActKey_WorldLV, 0)  | 
|     playerWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeWorldLV % actNum)  | 
|     templateID = GetTemplateID(actInfo.get(ShareDefine.ActKey_CfgID, 0), actInfo.get(ShareDefine.ActKey_DayIndex, 0))  | 
|     playerTemplateID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeTemplateID % actNum)  | 
|       | 
|     GameWorld.DebugLog("ÀۼƳäÖµÖØÖÃ! actNum=%s,TotalRechargeID=%s,playerTotalRechargeID=%s,state=%s,templateID=%s,playerTemplateID=%s"   | 
|                        % (actNum, TotalRechargeID, playerTotalRechargeID, state, templateID, playerTemplateID), playerID)  | 
|       | 
|     # Î´ÁìÈ¡µÄ½±ÀøÓʼþ·¢·Å  | 
|     __SendTotalRechargeMail(curPlayer, playerTemplateID, playerWorldLV, actNum)  | 
|       | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeID % actNum, TotalRechargeID)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeTemplateID % actNum, templateID)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeWorldLV % actNum, actWorldLV)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeGold % actNum, 0)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeAwardRecord % actNum, 0)  | 
|       | 
|     Sync_TotalRechargeActionInfo(curPlayer, actNum)  | 
|     Sync_TotalRechargeInfo(curPlayer, actNum)  | 
|     return True  | 
|   | 
| def __SendTotalRechargeMail(curPlayer, playerTemplateID, playerWorldLV, actNum):  | 
|     # Óʼþ·¢·Å½±Àø£¬ÊÊÓÃÓڻ½áÊøºó²¹·¢Î´ÁìÈ¡½±Àø        »ò        ÏßÏ»×Ô¶¯·¢·ÅÂú×ãÁìÈ¡Ìõ¼þµÄ½±Àø  | 
|       | 
|     if not playerTemplateID:  | 
|         return  | 
|       | 
|     curRechargeGold = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeGold % actNum)  | 
|     if not curRechargeGold:  | 
|         return  | 
|       | 
|     ipyDataList = IpyGameDataPY.GetIpyGameDataList("TotalRechargeTemplate", playerTemplateID)  | 
|     if not ipyDataList:  | 
|         return  | 
|       | 
|     playerID = curPlayer.GetPlayerID()  | 
|     batchPlayerIDList, batchAddItemList, batchParamList = [], [], []  | 
|     awardRecord = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeAwardRecord % actNum)  | 
|     job = curPlayer.GetJob()  | 
|     for ipyData in ipyDataList:  | 
|         awardIndex = ipyData.GetAwardIndex()  | 
|         if awardRecord & pow(2, awardIndex):  | 
|             continue  | 
|           | 
|         needGold = CommFunc.RMBToCoin(ipyData.GetNeedGold())  | 
|         if curRechargeGold < needGold:  | 
|             continue  | 
|         awardRecord |= pow(2, awardIndex)   | 
|           | 
|         awardItemList = __GetItemList(ipyData.GetAwardItem(), job, playerWorldLV)  | 
|         batchPlayerIDList.append([playerID])  | 
|         batchAddItemList.append(awardItemList)  | 
|         batchParamList.append([str(ipyData.GetNeedGold())]) # Ç®Ö§³ÖСÊý£¬ÏÔʾ²ÎÊý´«×Ö·û´®  | 
|           | 
|     if batchPlayerIDList:  | 
|         PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeAwardRecord % actNum, awardRecord)  | 
|         PlayerControl.SendMailBatch("TotalRechargeMail%s" % actNum, batchPlayerIDList, batchAddItemList, batchParamList)  | 
|           | 
|     return  | 
|   | 
| def __GetItemList(itemDict, job, worldLV):  | 
|     #{ÊÀ½çµÈ¼¶·¶Î§:{Ö°Òµ:[(ÎïÆ·ID,¸öÊý,ÊÇ·ñ°ó¶¨), ...]}}  | 
|     itemInfoDict = GameWorld.GetDictValueByRangeKey(itemDict, worldLV, {})  | 
|     if str(job) not in itemInfoDict:  | 
|         GameWorld.ErrLog('ÀۼƳäÖµ½±ÀøÎ´ÅäÖøÃÖ°ÒµitemDict=%s,job=%s' % (itemDict, job))  | 
|         return []  | 
|     else:  | 
|         return itemInfoDict[str(job)]  | 
|       | 
|   | 
| def AddTotalRechargeGold(curPlayer, addGold, coinType):  | 
|     if addGold <= 0:  | 
|         return  | 
|       | 
|     for actInfo in PyGameData.g_operationActionDict.get(ShareDefine.OperationActionName_TotalRecharge, {}).values():  | 
|         actNum = actInfo.get(ShareDefine.ActKey_ActNum, 0)  | 
|           | 
|         if not actInfo.get(ShareDefine.ActKey_State):  | 
|             GameWorld.DebugLog("ÀۼƳäÖµ»î¶¯µ±Ç°Î´¿ªÆô! actNum=%s" % actNum)  | 
|             continue  | 
|           | 
|         cfgID = actInfo.get(ShareDefine.ActKey_CfgID, 0)  | 
|         if not cfgID:  | 
|             continue  | 
|           | 
|         ipyData = IpyGameDataPY.GetIpyGameData("ActTotalRecharge", cfgID)  | 
|         if not ipyData:  | 
|             continue  | 
|           | 
|         ctgTypeEffValue = ipyData.GetCTGTypeEffValue()  | 
|         if not ctgTypeEffValue & pow(2, coinType):  | 
|             GameWorld.DebugLog("Íæ¼ÒÀۼƳäÖµ»î¶¯£¬³äÖµÀàÐͶԸûÎÞЧ! actNum=%s,coinType=%s,ctgTypeEffValue=%s"   | 
|                                % (actNum, coinType, ctgTypeEffValue), curPlayer.GetPlayerID())  | 
|             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("ÀۼƳäÖµ»î¶¯Êý¾ÝÒì³£!actID=%s,templateID=%s" % (actID, templateID), curPlayer.GetPlayerID())  | 
|             continue  | 
|         PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeID % actNum, actID)  | 
|         PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeTemplateID % actNum, templateID)  | 
|           | 
|         curRechargeGold = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeGold % actNum)  | 
|         updRechargeGold = curRechargeGold + addGold  | 
|         PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeGold % actNum, updRechargeGold)  | 
|         Sync_TotalRechargeInfo(curPlayer, actNum)  | 
|         GameWorld.DebugLog("Íæ¼ÒÀۼƳäÖµ»î¶¯: actNum=%s,actID=%s,templateID=%s,curRechargeGold=%s,addGold=%s,updRechargeGold=%s"   | 
|                            % (actNum, actID, templateID, curRechargeGold, addGold, updRechargeGold), curPlayer.GetPlayerID())  | 
|           | 
|         # ÏßÏ»£¬¼ì²é·¢·Å½±Àø  | 
|         if ipyData.GetIsOfflineAct():  | 
|             playerWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeWorldLV % actNum)  | 
|             __SendTotalRechargeMail(curPlayer, templateID, playerWorldLV, actNum)  | 
|               | 
|     return  | 
|       | 
|       | 
| def OnGetTotalRechargeAward(curPlayer, awardIndex, actNum):  | 
|     '''ÁìÈ¡ÀۼƳäÖµ½±Àø  | 
|     @param awardIndex: ½±ÀøË÷Òý  | 
|     @param actNum: »î¶¯±àºÅ£¬Èç11 »ò  12 ´ú±í²»Í¬µÄ»î¶¯  | 
|     '''  | 
|     actNum = GameWorld.ToIntDef(actNum, 0)  | 
|     if actNum <= 0:  | 
|         GameWorld.DebugLog("ûÓÐÖ¸¶¨ÁìÈ¡µÄÀÛ³ä»î¶¯±àºÅ! actNum=%s" % actNum)  | 
|         return  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_TotalRecharge, actNum)  | 
|                   | 
|     TotalRechargeID = 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  | 
|       | 
|     playerTotalRechargeID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeID % actNum) # Íæ¼ÒÉíÉϵĻID  | 
|     if TotalRechargeID != playerTotalRechargeID:  | 
|         return  | 
|       | 
|     awardRecord = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeAwardRecord % actNum)  | 
|     if awardRecord & pow(2, awardIndex):  | 
|         GameWorld.DebugLog("ÒѾÁìÈ¡¹ý¸ÃÀۼƳäÖµ»î¶¯½±Àø! actNum=%s,awardIndex=%s" % (actNum, awardIndex), playerID)  | 
|         return  | 
|       | 
|     ipyDataList = IpyGameDataPY.GetIpyGameDataList("TotalRechargeTemplate", templateID)  | 
|     if not ipyDataList:  | 
|         return  | 
|       | 
|     awardIpyData = None  | 
|     for ipyData in ipyDataList:  | 
|         if ipyData.GetAwardIndex() == awardIndex:  | 
|             awardIpyData = ipyData  | 
|             break  | 
|           | 
|     if not awardIpyData:  | 
|         GameWorld.DebugLog("ÕÒ²»µ½¸Ã·µÀû»î¶¯µµÎ»Ë÷Òý½±Àø!actNum=%s,templateID=%s,awardIndex=%s" % (actNum, templateID, awardIndex), playerID)  | 
|         return  | 
|       | 
|     needGold = CommFunc.RMBToCoin(awardIpyData.GetNeedGold())  | 
|     actWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeWorldLV % actNum)  | 
|     awardItemList = __GetItemList(ipyData.GetAwardItem(), curPlayer.GetJob(), actWorldLV)  | 
|       | 
|     curRechargeGold = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeGold % actNum)  | 
|     if curRechargeGold < needGold:  | 
|         GameWorld.DebugLog("ËùÐè³äÖµ¶î¶È²»×㣬ÎÞ·¨ÁìÈ¡!actNum=%s,templateID=%s,awardIndex=%s,needGold=%s,curRechargeGold=%s"   | 
|                            % (actNum, templateID, awardIndex, needGold, curRechargeGold), playerID)  | 
|         return  | 
|       | 
|     # ¼ì²é±³°ü  | 
|     if not ItemControler.CheckPackSpaceEnough(curPlayer, awardItemList):  | 
|         return  | 
|       | 
|     awardRecord |= pow(2, awardIndex)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TotalRechargeAwardRecord % actNum, awardRecord)  | 
|     Sync_TotalRechargeInfo(curPlayer, actNum)  | 
|       | 
|     notifyKey = awardIpyData.GetNotifyKey()  | 
|     if notifyKey:  | 
|         PlayerControl.WorldNotify(0, notifyKey, [curPlayer.GetPlayerName(), str(awardIpyData.GetNeedGold())]) # Ç®Ö§³ÖСÊý£¬ÏÔʾ²ÎÊý´«×Ö·û´®  | 
|           | 
|     for itemID, itemCount, _ in awardItemList:  | 
|         ItemControler.GivePlayerItem(curPlayer, itemID, itemCount, 0, [IPY_GameWorld.rptItem])  | 
|           | 
|     addDataDict = {"TemplateID":templateID, "NeedGold":needGold, "AwardIndex":awardIndex,  | 
|                    "ItemList":str(awardItemList), "ActNum":actNum}  | 
|     DataRecordPack.DR_FuncGiveItem(curPlayer, "TotalRechargeAward", addDataDict)  | 
|     return  | 
|   | 
| def Sync_TotalRechargeInfo(curPlayer, actNum):  | 
|     ## Í¨ÖªÀۼƳäÖµÍæ¼ÒÊý¾ÝÐÅÏ¢  | 
|       | 
|     actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_TotalRecharge, actNum)  | 
|     if not actInfo.get(ShareDefine.ActKey_State):  | 
|         return  | 
|       | 
|     cfgID = actInfo.get(ShareDefine.ActKey_CfgID)  | 
|     ipyData = IpyGameDataPY.GetIpyGameData("ActTotalRecharge", cfgID)  | 
|     if not ipyData:  | 
|         return  | 
|       | 
|     if ipyData.GetIsOfflineAct():  | 
|         #ÏßÏ»²»Ï·¢  | 
|         return  | 
|       | 
|     playerActInfo = ChPyNetSendPack.tagMCTotalRechargePlayerInfo()  | 
|     playerActInfo.ActNum = actNum  | 
|     playerActInfo.GoldTotal = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeGold % actNum)  | 
|     playerActInfo.AwardRecord = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeAwardRecord % actNum)  | 
|     NetPackCommon.SendFakePack(curPlayer, playerActInfo)  | 
|     return  | 
|   | 
| def Sync_TotalRechargeActionInfo(curPlayer, actNum):  | 
|     ## Í¨ÖªÀۼƳäÖµ»î¶¯ÐÅÏ¢  | 
|       | 
|     actInfo = GameWorld.GetActInfo(ShareDefine.OperationActionName_TotalRecharge, actNum)  | 
|     if not actInfo.get(ShareDefine.ActKey_State):  | 
|         return  | 
|       | 
|     cfgID = actInfo.get(ShareDefine.ActKey_CfgID)  | 
|     ipyData = IpyGameDataPY.GetIpyGameData("ActTotalRecharge", cfgID)  | 
|     if not ipyData:  | 
|         return  | 
|       | 
|     if ipyData.GetIsOfflineAct():  | 
|         #ÏßÏ»²»Ï·¢  | 
|         return  | 
|       | 
|     templateIDList = ipyData.GetTemplateIDList()  | 
|     if not templateIDList:  | 
|         return  | 
|     job = curPlayer.GetJob()  | 
|     actWorldLV = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TotalRechargeWorldLV % actNum)  | 
|     startDateStr, endDateStr = GameWorld.GetOperationActionDateStr(ipyData)  | 
|     startDateSync = actInfo.get(ShareDefine.ActKey_StartDateSync, startDateStr)  | 
|     actInfo = ChPyNetSendPack.tagMCActTotalRechargeInfo()  | 
|     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("TotalRechargeTemplate", templateID)  | 
|         if not ipyDataList:  | 
|             continue  | 
|           | 
|         dayInfo = ChPyNetSendPack.tagMCTotalRechargeAwardDay()  | 
|         dayInfo.AwardInfo = []  | 
|         for ipyData in ipyDataList:  | 
|             awardInfo = ChPyNetSendPack.tagMCTotalRechargeAward()  | 
|             awardInfo.AwardIndex = ipyData.GetAwardIndex()  | 
|             awardInfo.NeedGold = CommFunc.RMBToCoin(ipyData.GetNeedGold())  | 
|             awardInfo.AwardItem = []  | 
|             awardItemList = __GetItemList(ipyData.GetAwardItem(), job, actWorldLV)  | 
|             for awardItemInfo in awardItemList:  | 
|                 itemID, itemCount = awardItemInfo[:2]  | 
|                 isBind = awardItemInfo[2] if len(awardItemInfo) > 2 else 0  | 
|                 awardItem = ChPyNetSendPack.tagMCTotalRechargeAwardItem()  | 
|                 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  | 
|   | 
|   |