| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package Player.PlayerTask  | 
| #  | 
| # @todo:Íæ¼ÒÈÎÎñ  | 
| # @author hxp  | 
| # @date 2023-12-20  | 
| # @version 1.0  | 
| #  | 
| # ÏêϸÃèÊö: Íæ¼ÒÈÎÎñ  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #"""Version = 2023-12-20 12:30"""  | 
| #-------------------------------------------------------------------------------  | 
|   | 
| import GameWorld  | 
| import IpyGameDataPY  | 
| import PlayerControl  | 
| import NetPackCommon  | 
| import ChPyNetSendPack  | 
| import ItemControler  | 
| import IPY_GameWorld  | 
| import GameFuncComm  | 
| import ShareDefine  | 
| import ChConfig  | 
| import ChEquip  | 
|   | 
| def GetTaskIDList(taskGroup):  | 
|     ## »ñȡij¸öÈÎÎñ·Ö×éËùÓÐÈÎÎñIDÁÐ±í  | 
|     key = "TaskIDListDict"  | 
|     taskIDListDict = IpyGameDataPY.GetConfigEx(key)  | 
|     if not taskIDListDict:  | 
|         taskIDListDict = {}  | 
|         ipyDataMgr = IpyGameDataPY.IPY_Data()  | 
|         for index in xrange(ipyDataMgr.GetTaskCount()):  | 
|             ipyData = ipyDataMgr.GetTaskByIndex(index)  | 
|             taskGroup = ipyData.GetTaskGroup()  | 
|             taskID = ipyData.GetTaskID()  | 
|             if taskGroup not in taskIDListDict:  | 
|                 taskIDListDict[taskGroup] = []  | 
|             taskIDList = taskIDListDict[taskGroup]  | 
|             if taskID not in taskIDList:  | 
|                 taskIDList.append(taskID)  | 
|         IpyGameDataPY.SetConfigEx(key, taskIDListDict)  | 
|         GameWorld.Log("ÈÎÎñIDÁбí: %s" % taskIDListDict)  | 
|     return taskIDListDict.get(taskGroup, [])  | 
|   | 
| def OnPlayerLogin(curPlayer):  | 
|     __fixRefreshTask(curPlayer)  | 
|     if not __giveNewTask(curPlayer, ChConfig.TaskGroup_Main):  | 
|         SyncTaskInfo(curPlayer)  | 
|     return  | 
|   | 
| def __fixRefreshTask(curPlayer):  | 
|     ## ¾ÀÕýË¢ÐÂÈÎÎñֵ״̬µÈ£¬Ò»°ãÓÃÓÚÐÞ¸ÄÈÎÎñÅäÖõȣ¬Íæ¼ÒµÇ¼¼ì²é  | 
|       | 
|     playerID = curPlayer.GetPlayerID()  | 
|     fixGroupList = []  | 
|     for taskGroup in ChConfig.TaskGroupList:  | 
|         taskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskID % taskGroup)  | 
|         if not taskID:  | 
|             continue  | 
|         ipyData = IpyGameDataPY.GetIpyGameData("Task", taskID)  | 
|         if not ipyData:  | 
|             continue  | 
|           | 
|         curValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskValue % taskGroup)  | 
|         curState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskState % taskGroup)  | 
|         if curState == ChConfig.TaskState_CanGet:  | 
|             continue  | 
|           | 
|         needFix = False  | 
|           | 
|         needValue = ipyData.GetNeedValue()  | 
|         taskValue = __GetTaskValue(curPlayer, ipyData)  | 
|           | 
|         # ¿ÉÄÜÐÞ¸ÄÌõ¼þÅäÖú󣬵¼ÖÂÈÎÎñÖµ²»Ò»Ö£¬ÐèÒªÐÞÕý  | 
|         if taskValue and taskValue != curValue:  | 
|             GameWorld.Log("ÐèÒªÐÞÕýÈÎÎñ½ø¶È: taskID=%s,curValue=%s,taskValue=%s" % (taskID, curValue, taskValue), playerID)  | 
|             curValue = taskValue  | 
|             needFix = True  | 
|               | 
|         # ¿ÉÄÜÐ޸ĽµµÍÁËËùÐèÈÎÎñÖµ£¬ÐèÒªÐÞÕýÏÂÍê³É״̬  | 
|         if curValue >= needValue and curState != ChConfig.TaskState_CanGet:  | 
|             GameWorld.Log("ÐèÒªÐÞÕýÈÎÎñ״̬: taskID=%s,curValue=%s,needValue=%s,curState=%s" % (taskID, curValue, needValue, curState), playerID)  | 
|             needFix = True  | 
|               | 
|         if not needFix:  | 
|             continue  | 
|                   | 
|         SetTaskValue(curPlayer, ipyData, taskValue, False)  | 
|         fixGroupList.append(taskGroup)  | 
|           | 
|     if fixGroupList:  | 
|         SyncTaskInfo(curPlayer, fixGroupList)  | 
|           | 
|     return  | 
|   | 
| def __giveNewTask(curPlayer, taskGroup=ChConfig.TaskGroup_Main):  | 
|     ## ¸øÐÂÈÎÎñ  | 
|     taskIDList = GetTaskIDList(taskGroup)  | 
|     if not taskIDList:  | 
|         return  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     taskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskID % taskGroup)  | 
|     lastTaskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskIDLast % taskGroup)  | 
|     newTaskID = 0  | 
|     if not taskID and not lastTaskID:  | 
|         newTaskID = taskIDList[0]  | 
|         GameWorld.DebugLog("¸øµÚÒ»¸öÈÎÎñ: taskGroup=%s,newTaskID=%s" % (taskGroup, newTaskID), playerID)  | 
|           | 
|     elif not taskID and lastTaskID:  | 
|         # Ã»Óе±Ç°ÈÎÎñ£¬¸øÏÂÒ»¸öÈÎÎñ  | 
|         if lastTaskID not in taskIDList:  | 
|             GameWorld.ErrLog("ÕÒ²»µ½ÉÏÒ»´ÎÍê³ÉµÄÈÎÎñID! taskGroup=%s,lastTaskID=%s" % (taskGroup, lastTaskID), playerID)  | 
|             return  | 
|         lastIndex = taskIDList.index(lastTaskID)  | 
|         if lastIndex >= len(taskIDList) - 1:  | 
|             GameWorld.DebugLog("ÒѾÍê³ÉÁËËùÓÐÈÎÎñ: taskGroup=%s,lastTaskID=%s" % (taskGroup, lastTaskID), playerID)  | 
|             return  | 
|         newTaskID = taskIDList[lastIndex + 1]  | 
|         GameWorld.DebugLog("¸øÏÂÒ»¸öÈÎÎñ: taskGroup=%s,newTaskID=%s,lastTaskID=%s" % (taskGroup, newTaskID, lastTaskID), playerID)  | 
|           | 
|     if not newTaskID:  | 
|         return  | 
|     ipyData = IpyGameDataPY.GetIpyGameData("Task", newTaskID)  | 
|     if not ipyData:  | 
|         return  | 
|     taskValue = __GetTaskValue(curPlayer, ipyData)  | 
|       | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskID % taskGroup, newTaskID)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskValue % taskGroup, 0)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskState % taskGroup, ChConfig.TaskState_Doing)  | 
|     GameWorld.DebugLog("½Óµ½ÐÂÈÎÎñ: taskGroup=%s,newTaskID=%s" % (taskGroup, newTaskID), playerID)  | 
|     return SetTaskValue(curPlayer, ipyData, taskValue)  | 
|   | 
| def __GetTaskValue(curPlayer, ipyData):  | 
|     ## »ñÈ¡ÈÎÎñÀàÐ͵±Ç°½ø¶ÈÖµ£¬Ò»°ãÖ±½Ó UpdTaskValue µÄÀàÐÍ»áÓõ½  | 
|     taskValue = 0 # Ä¬ÈÏ0  | 
|     taskType = ipyData.GetTaskType()  | 
|     conds = ipyData.GetTaskConds()  | 
|       | 
|     if taskType == ChConfig.TaskType_LV:  | 
|         taskValue = curPlayer.GetLV()  | 
|           | 
|     elif taskType == ChConfig.TaskType_FBPass:  | 
|         if len(conds) != 2:  | 
|             return 0  | 
|         mapID, lineID = conds  | 
|         passLineID = curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_FBPassLineID % mapID)  | 
|         taskValue = 1 if passLineID >= lineID else 0  | 
|           | 
|     elif taskType == ChConfig.TaskType_EquipColorItem:  | 
|         if not conds:  | 
|             return 0  | 
|         itemColor = conds[0]  | 
|         taskValue = ChEquip.GetPlayerEquipItemCount(curPlayer, None, itemColor)  | 
|           | 
|     elif taskType == ChConfig.TaskType_TreeLV:  | 
|         taskValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TreeLV)  | 
|           | 
|     elif taskType == ChConfig.TaskType_RealmLV:  | 
|         taskValue = curPlayer.GetOfficialRank()  | 
|           | 
|     elif taskType == ChConfig.TaskType_MineWorkerCnt:  | 
|         taskValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_MineWorkerCount)  | 
|           | 
|     elif taskType == ChConfig.TaskType_CreateOrJoinFamily:  | 
|         taskValue = 1 if curPlayer.GetFamilyID() else 0  | 
|           | 
|     return taskValue  | 
|   | 
| def UpdTaskValue(curPlayer, taskType):  | 
|     ## ¸üÐÂÈÎÎñ½ø¶È  | 
|     for taskGroup in ChConfig.TaskGroupList:  | 
|         ipyData = __GetDoingTaskIpyData(curPlayer, taskGroup, taskType)  | 
|         if not ipyData:  | 
|             continue  | 
|         curValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskValue % taskGroup)  | 
|         taskValue = __GetTaskValue(curPlayer, ipyData)  | 
|         if taskValue <= curValue:  | 
|             # ÓÐÔö³¤²Å¸üР | 
|             continue  | 
|         SetTaskValue(curPlayer, ipyData, taskValue)  | 
|     return  | 
|   | 
| def AddTaskValue(curPlayer, taskType, addValue=1, conds=None):  | 
|     ## Ôö¼ÓÈÎÎñ½ø¶È  | 
|     for taskGroup in ChConfig.TaskGroupList:  | 
|         ipyData = __GetDoingTaskIpyData(curPlayer, taskGroup, taskType)  | 
|         if not ipyData:  | 
|             continue  | 
|         if not __CheckTaskCondition(ipyData, conds):  | 
|             continue  | 
|         curValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskValue % taskGroup) + addValue  | 
|         SetTaskValue(curPlayer, ipyData, curValue)  | 
|     return  | 
|   | 
| def __GetDoingTaskIpyData(curPlayer, taskGroup, taskType):  | 
|     ## »ñÈ¡ÕýÔÚ½øÐÐÖеÄÈÎÎñipyData  | 
|     taskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskID % taskGroup)  | 
|     if not taskID:  | 
|         return  | 
|     curState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskState % taskGroup)  | 
|     if curState != ChConfig.TaskState_Doing:  | 
|         return  | 
|     ipyData = IpyGameDataPY.GetIpyGameData("Task", taskID)  | 
|     if not ipyData:  | 
|         return  | 
|     if ipyData.GetTaskType() != taskType:  | 
|         return  | 
|     return ipyData  | 
|   | 
| def __CheckTaskCondition(ipyData, conds=None):  | 
|     ## ¼ì²éÈÎÎñÌõ¼þÊÇ·ñÂú×ã  | 
|     taskType = ipyData.GetTaskType()  | 
|       | 
|     # ¸±±¾xxxÌôÕ½  | 
|     if taskType == ChConfig.TaskType_FBChallenge:  | 
|         taskConds = ipyData.GetTaskConds()  | 
|         if not conds or len(conds) != len(taskConds) or len(conds) < 1:  | 
|             return  | 
|         if conds[0] != taskConds[0]:  | 
|             return  | 
|           | 
|     return True  | 
|   | 
| def SetTaskValue(curPlayer, taskIpyData, value=0, isNotify=True):  | 
|     ## ÉèÖõ±Ç°ÈÎÎñ½ø¶ÈÖµ£¬Í³Ò»´¦ÀíÈÎÎñ״̬¡¢´¥·¢¶îÍâÂß¼µÈ  | 
|     if not taskIpyData:  | 
|         return  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     taskGroup = taskIpyData.GetTaskGroup()  | 
|     taskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskID % taskGroup)  | 
|     if taskIpyData.GetTaskID() != taskID:  | 
|         return  | 
|     curState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskState % taskGroup)  | 
|     if curState == ChConfig.TaskState_CanGet:  | 
|         return  | 
|     taskType = taskIpyData.GetTaskType()  | 
|     finishNeedValue = taskIpyData.GetNeedValue()  | 
|     value = min(value, finishNeedValue, ShareDefine.Def_UpperLimit_DWord)  | 
|     GameWorld.DebugLog("¸üÐÂÈÎÎñ½ø¶È: taskGroup=%s,taskType=%s,taskID=%s,value=%s/%s"   | 
|                        % (taskGroup, taskType, taskID, value, finishNeedValue), playerID)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskValue % taskGroup, value)  | 
|     if value >= finishNeedValue:  | 
|         __OnTaskFinish(curPlayer, taskGroup, taskID, taskIpyData)  | 
|           | 
|     if isNotify:  | 
|         SyncTaskInfo(curPlayer, [taskGroup])  | 
|           | 
|     return True  | 
|   | 
| def __OnTaskFinish(curPlayer, taskGroup, taskID, taskIpyData):  | 
|     GameWorld.DebugLog("ÈÎÎñÍê³É: taskGroup=%s,taskID=%s" % (taskGroup, taskID), curPlayer.GetPlayerID())  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskState % taskGroup, ChConfig.TaskState_CanGet)  | 
|     return  | 
|   | 
| def IsTaskFinish(curPlayer, taskID):  | 
|     ## ÈÎÎñÊÇ·ñÒÑÍê³É - ¹æ¶¨Áì½±²ÅËãÍê³É  | 
|     ipyData = IpyGameDataPY.GetIpyGameData("Task", taskID)  | 
|     if not ipyData:  | 
|         return  | 
|     taskGroup = ipyData.GetTaskGroup()  | 
|     #nowTaskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskID % taskGroup)  | 
|     #if nowTaskID and curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskState % taskGroup) == ChConfig.TaskState_CanGet:  | 
|     #    finishTaskID = nowTaskID  | 
|     #else:  | 
|     #    finishTaskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskIDLast % taskGroup)  | 
|     finishTaskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskIDLast % taskGroup)  | 
|       | 
|     if taskID == finishTaskID:  | 
|         return True  | 
|       | 
|     taskIDList = GetTaskIDList(taskGroup)  | 
|     if finishTaskID not in taskIDList or taskID not in taskIDList:  | 
|         return  | 
|     return taskIDList.index(finishTaskID) >= taskIDList.index(taskID)  | 
|   | 
| def GetTaskAward(curPlayer, taskID):  | 
|     ## ÁìÈ¡ÈÎÎñ½±Àø  | 
|     playerID = curPlayer.GetPlayerID()  | 
|     ipyData = IpyGameDataPY.GetIpyGameData("Task", taskID)  | 
|     if not ipyData:  | 
|         return  | 
|     taskGroup = ipyData.GetTaskGroup()  | 
|     curTaskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskID % taskGroup)  | 
|     if taskID != curTaskID:  | 
|         GameWorld.DebugLog("·Çµ±Ç°ÈÎÎñ: curTaskID=%s" % curTaskID, playerID)  | 
|         return  | 
|     needValue = ipyData.GetNeedValue()  | 
|     awardItemList = ipyData.GetAwardItemList()  | 
|     curState = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskState % taskGroup)  | 
|     curValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskValue % taskGroup)  | 
|     if curState != ChConfig.TaskState_CanGet:  | 
|         GameWorld.DebugLog("ÈÎÎñδÍê³ÉÎÞ·¨Áì½±: taskID=%s,curValue=%s/%s,curState=%s" % (taskID, curValue, needValue, curState), playerID)  | 
|         return  | 
|     if not ItemControler.CheckPackSpaceEnough(curPlayer, awardItemList):  | 
|         return  | 
|       | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskIDLast % taskGroup, taskID)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskID % taskGroup, 0)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskValue % taskGroup, 0)  | 
|     PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_TaskState % taskGroup, 0)  | 
|       | 
|     for itemID, itemCount, isAuctionItem in awardItemList:  | 
|         ItemControler.GivePlayerItem(curPlayer, itemID, itemCount, isAuctionItem, [IPY_GameWorld.rptItem])  | 
|           | 
|     if not __giveNewTask(curPlayer, taskGroup):  | 
|         SyncTaskInfo(curPlayer, [taskGroup])  | 
|           | 
|     GameFuncComm.DoFuncOpenLogic(curPlayer, [taskID])  | 
|     return  | 
|   | 
| def SyncTaskInfo(curPlayer, taskGroupList=None):  | 
|     syncGroupList = taskGroupList if taskGroupList != None else ChConfig.TaskGroupList  | 
|     if not syncGroupList:  | 
|         return  | 
|       | 
|     taskList = []  | 
|     for taskGroup in syncGroupList:  | 
|         task = ChPyNetSendPack.tagMCTask()  | 
|         task.TaskGroup = taskGroup  | 
|         task.TaskID = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskID % taskGroup)  | 
|         task.CurValue = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskValue % taskGroup)  | 
|         task.State = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_TaskState % taskGroup)  | 
|         taskList.append(task)  | 
|           | 
|     if not taskList:  | 
|         return  | 
|       | 
|     clientPack = ChPyNetSendPack.tagMCTaskInfo()  | 
|     clientPack.TaskList = taskList  | 
|     clientPack.TaskCount = len(clientPack.TaskList)  | 
|     NetPackCommon.SendFakePack(curPlayer, clientPack)  | 
|     return  |