| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package PlayerFamilyParty  | 
| #  | 
| # @todo:¼Ò×åÑç»á¸±±¾  | 
| # @author xdh  | 
| # @date 2014-04-16  | 
| # @version 1.0  | 
| #  | 
| # ÏêϸÃèÊö: ¼Ò×åÑç»á¸±±¾  | 
| #  | 
| #---------------------------------------------------------------------  | 
| #"""Version = 2014-04-16 18:00"""  | 
| #---------------------------------------------------------------------  | 
|   | 
| import GameWorld  | 
| import PlayerFamily  | 
| import ShareDefine  | 
| import PlayerControl  | 
| import IpyGameDataPY  | 
| import PlayerCompensation  | 
| import ChConfig  | 
| import PlayerFamilyRedPacket  | 
| import PlayerTalk  | 
| import PyGameData  | 
|   | 
| import random  | 
|   | 
| FamilyPartyFB_HisQuestionID = 'FamParty_HisQuestionID%s' #µ±Ç°ÌâID - ²»ÖØÖÃ, ²ÎÊý familyID  | 
| FamilyPartyFB_QuestionID = 'FamParty_QuestionID%s' #µ±Ç°ÌâID - ´ð¶Ô»áÖØÖÃ, ²ÎÊý familyID  | 
| FamilyPartyFB_QuestionTick = 'FamParty_QuestionTick%s' #µ±Ç°Ìâ³öÌâʱ¼ä, ²ÎÊý familyID  | 
| FamilyPartyFB_AnswerTick = 'FamParty_AnswerTick%s' #´ð¶Ôʱ¼ä, ²ÎÊý familyID  | 
|   | 
| AnswerState = 1 #¿ªÊ¼´ðÌâ»î¶¯×´Ì¬  | 
| ## Íæ¼ÒµÇ¼  | 
| #  @param None  | 
| #  @return None  | 
| def OnPlayerLogin(curPlayer):  | 
|     NotifyFamilyPartyQuestion(curPlayer)  | 
|     return  | 
|   | 
| def OnPlayerChangeMap(curPlayer, tick):  | 
|     if curPlayer.GetMapID() != ChConfig.Def_FBMapID_FamilyParty:  | 
|         return  | 
|     playerID = curPlayer.GetID()  | 
|     if playerID not in PyGameData.g_partyheroAnswerDict:  | 
|         PyGameData.g_partyheroAnswerDict[playerID] = 0  | 
|     return  | 
|   | 
|   | 
| def NotifyFamilyPartyQuestion(curPlayer):  | 
|     #¸ÕµÇ¼µÄ»ò¸Õ½øÏÉÃ˵IJ¹·¢×î½üµÄÌâÄ¿  | 
|     gameWorld = GameWorld.GetGameWorld()  | 
|     state = gameWorld.GetDictByKey(ShareDefine.Def_Notify_WorldKey_DailyActionState % ShareDefine.DailyActionID_FamilyParty)  | 
|     if state != AnswerState:  | 
|         return  | 
|     curFamilyid = curPlayer.GetFamilyID()  | 
|     if not curFamilyid:  | 
|         return  | 
|     questionID = gameWorld.GetDictByKey(FamilyPartyFB_QuestionID % curFamilyid)  | 
|     if not questionID:  | 
|         return  | 
|     PlayerControl.NotifyCode(curPlayer, 'Party_Topic', [questionID])  | 
|     return  | 
|   | 
| def FamilyPartyStateChange(state, beforeState=0):  | 
|     #»î¶¯×´Ì¬±ä¸ü  | 
|     if state == beforeState:  | 
|         return  | 
|       | 
|     if state == AnswerState:  | 
|         PyGameData.g_questionIDHistory = {}  | 
|         PyGameData.g_familyAnswerDict = {}  | 
|         PyGameData.g_familyPartyTopInfo = []  | 
|         PyGameData.g_partyheroAnswerDict = {}  | 
|         PyGameData.g_familyAnswerPlayerIDDict = {}  | 
|         gameWorld = GameWorld.GetGameWorld()  | 
|         familyManager = GameWorld.GetFamilyManager()  | 
|         for i in range(0, familyManager.GetCount()):  | 
|             family = familyManager.GetAt(i)  | 
|             familyID = family.GetID()  | 
|             gameWorld.SetDict(FamilyPartyFB_QuestionID % familyID, 0)  | 
|             gameWorld.SetDict(FamilyPartyFB_HisQuestionID % familyID, 0)  | 
|             gameWorld.SetDict(FamilyPartyFB_AnswerTick % familyID, 0)  | 
|         GameWorld.SendMapServerMsgEx(ShareDefine.Def_Notify_WorldKey_FamilyPartyInfo, [])  | 
|           | 
|     if state == 0:  | 
|         #»î¶¯½áËã Ç°3ÃûÏÉÃË¡¢´ðÌâÍõ  | 
|         GameWorld.Log("ÏÉÃËÑç»á½áËã g_familyAnswerDict=%s, g_familyPartyTopInfo=%s" % (PyGameData.g_familyAnswerDict, PyGameData.g_familyPartyTopInfo))  | 
|           | 
|         noneStr = 'null'  | 
|         topfamilyNameList = [noneStr, noneStr, noneStr]  | 
|         if PyGameData.g_familyAnswerDict:  | 
|             sortFamilyList = __GetFamilyAnswerRankList()  | 
|             GameWorld.DebugLog("    Ñç»á½áÊø sortFamilyList=%s" % sortFamilyList)  | 
|             redPackID = IpyGameDataPY.GetFuncCfg('PartyReward', 4)  | 
|             for i, familyInfo in enumerate(sortFamilyList):  | 
|                 familyID, familyName = familyInfo[:2]  | 
|                 topfamilyNameList[i] = familyName  | 
|                 #·¢ºì°ü  | 
|                 family = GameWorld.GetFamilyManager().FindFamily(familyID)  | 
|                 if not family:  | 
|                     continue  | 
|                 PlayerFamilyRedPacket.CreatNewFamilyRedPacket(family, family.GetLeaderID(), redPackID)  | 
|                   | 
|         PlayerControl.WorldNotify(0, 'Party_TopThree', topfamilyNameList)  | 
|           | 
|         if PyGameData.g_familyPartyTopInfo:  | 
|             topHeroID, topHeroName = PyGameData.g_familyPartyTopInfo[:2]  | 
|             rewardInfo = IpyGameDataPY.GetFuncEvalCfg('PartyReward', 3)  | 
|             #¸ø½±Àø  | 
|             mailKey, day, itemList = rewardInfo  | 
|             content = ShareDefine.Def_MailFormat % (mailKey, '')  | 
|             GameWorld.DebugLog('·¢ËÍÏÉÃËÑç»á´ðÌâÍõÓʼþ   itemList=%s' % (itemList))  | 
|             PlayerCompensation.SendPersonalItemMailEx('', content, day, [topHeroID], itemList)  | 
|                   | 
|             PlayerControl.WorldNotify(0, 'Party_TopPlayer', [topHeroName])  | 
|               | 
|         #¸ø²ÎÓë½±Àø  | 
|         joinPlayerIDList = PyGameData.g_partyheroAnswerDict.keys()  | 
|         if joinPlayerIDList:  | 
|             joinReward = IpyGameDataPY.GetFuncEvalCfg('PartyReward')  | 
|             if joinReward:  | 
|                 PlayerCompensation.SendMailByKey('FamilyParty', joinPlayerIDList, joinReward)  | 
|           | 
|         #ÐÞ¸ÄΪ´ðÍêÌâÖ±½Ó¸øÃ¿Ìâ½±Àø  | 
| #        joinReward = IpyGameDataPY.GetFuncEvalCfg('PartyReward')  | 
| #        answerRWItemReward = IpyGameDataPY.GetFuncEvalCfg('PartyReward2', 1)  | 
| #        for playerID, ansRightCount in PyGameData.g_partyheroAnswerDict.items():  | 
| #            if answerRWItemReward and len(answerRWItemReward) == 2:  | 
| #                ansTotalCount = len(PyGameData.g_familyAnswerPlayerIDDict.get(playerID, []))  | 
| #                ansWrongCount = max(0, ansTotalCount - ansRightCount)  | 
| #                isAuctionItem = False  | 
| #                rightRewardEx, wrongRewardEx = [], []  | 
| #                rightItemID, wrongItemID = answerRWItemReward  | 
| #                if rightItemID and ansRightCount:  | 
| #                    rightRewardEx = [(rightItemID, ansRightCount, isAuctionItem)]  | 
| #                if wrongItemID and ansWrongCount:  | 
| #                    wrongRewardEx = [(wrongItemID, ansWrongCount, isAuctionItem)]  | 
| #                curJoinReward = joinReward + rightRewardEx + wrongRewardEx  | 
| #            else:  | 
| #                curJoinReward = joinReward  | 
| #            if joinReward:  | 
| #                PlayerCompensation.SendMailByKey('FamilyParty', [playerID], curJoinReward)  | 
|                   | 
|     return  | 
|   | 
| def __GetFamilyAnswerRankList():  | 
|     if not PyGameData.g_familyAnswerDict:  | 
|         return []  | 
|     sortFamilyList = PyGameData.g_familyAnswerDict.items()  | 
|     sortFamilyList.sort(cmp=CmpFunc)  | 
|     rankList = [] #[[familyID, familyName, ´ðÌâÊýÁ¿]]  | 
|     for familyID, info in sortFamilyList[:3]:  | 
|         rankList.append([familyID, info[2], info[0]])  | 
|     return rankList  | 
|   | 
| ## ÅÅÐòº¯Êý  | 
| #  @param elem1 ÔªËØ1  | 
| #  @param elem2 ÔªËØ2  | 
| #  @return ´óС  | 
| #  @remarks ÅÅÐòº¯Êý  | 
| def CmpFunc(elem1, elem2):  | 
|     result = -cmp(elem1[1][0], elem2[1][0])  | 
|     if result == 0:  | 
|         return cmp(elem1[1][1], elem2[1][1])  | 
|     return result  | 
|   | 
| def FamilyParty_Process(tick):  | 
|     #¿ªÊ¼³öÌâ  | 
|     gameWorld = GameWorld.GetGameWorld()  | 
|     state = gameWorld.GetDictByKey(ShareDefine.Def_Notify_WorldKey_DailyActionState % ShareDefine.DailyActionID_FamilyParty)  | 
|     if state != AnswerState:  | 
|         return  | 
|     allQuestionIDList = IpyGameDataPY.GetConfigEx('AllQuestionID')  | 
|     ipyMgr = IpyGameDataPY.IPY_Data()  | 
|     if not allQuestionIDList:  | 
|         allQuestionIDList = []  | 
|         for i in xrange(ipyMgr.GetQuestionBankCount()):  | 
|             ipyData = ipyMgr.GetQuestionBankByIndex(i)  | 
|             questionID = ipyData.GetID()  | 
|             try:  | 
|                 eval(ipyData.GetAnswer())  | 
|             except:  | 
|                 GameWorld.ErrLog("´ðÌâÌâ¿â´ð°¸Òì³££¬²»´¦Àí! questionID=%s,answer=%s" % (questionID, ipyData.GetAnswer()))  | 
|                 continue  | 
|             allQuestionIDList.append(questionID)  | 
|         IpyGameDataPY.SetConfigEx('AllQuestionID', allQuestionIDList)  | 
|         GameWorld.Log("¼ÓÔØÏÉÃËÑç»á´ðÌâÌâ¿âID! %s" % str(allQuestionIDList))  | 
|     if not allQuestionIDList:  | 
|         GameWorld.ErrLog('ÏÉÃËÑç»áÌâ¿âûÓÐÌâÄ¿£¡')  | 
|         return  | 
|     familyManager = GameWorld.GetFamilyManager()  | 
|     for i in range(0, familyManager.GetCount()):  | 
|         family = familyManager.GetAt(i)  | 
|         familyID = family.GetID()  | 
|         lastQuestionID = gameWorld.GetDictByKey(FamilyPartyFB_QuestionID % familyID)  | 
|         if lastQuestionID:  | 
|             #»¹ÓÐÌâδ´ð  | 
|             maxSecond = IpyGameDataPY.GetFuncCfg('FairyCorrectAnswer')  | 
|             if tick - gameWorld.GetDictByKey(FamilyPartyFB_QuestionTick % familyID) > maxSecond * 1000:  | 
|                 ipyData = IpyGameDataPY.GetIpyGameData('QuestionBank', lastQuestionID)  | 
|                 if ipyData:  | 
|                     answer = ipyData.GetAnswer()  | 
|                     #²»ÄÜÓÃreplace, ¿ÉÄܵ¼ÖÂÌæ»»´íÎó, Èç ['Íû]ɽÆÙ²¼'] Îª "['\xcd\xfb\x8f]\xc9\xbd\xc6\xd9\xb2\xbc']" ÖмäÓиö ] Ìæ»»»áµ¼ÖÂÂÒÂë  | 
|                     #answer = answer.replace('[', '')  | 
|                     #answer = answer.replace(']', '')  | 
|                     answer = answer.strip()  | 
|                     if answer.startswith("[") and answer.endswith("]"):  | 
|                         answer = answer[1:-1]  | 
|                     PlayerControl.FamilyNotify(familyID, 'Party_Correct', [maxSecond, GameWorld.GbkToCode(answer)])  | 
|                       | 
|                 for index in xrange(family.GetCount()):  | 
|                     member = family.GetAt(index)  | 
|                     memPlayer = member.GetPlayer()  | 
|                     if memPlayer and memPlayer.GetMapID() == ChConfig.Def_FBMapID_FamilyParty:  | 
|                         rightPlayerID = 0  | 
|                         answerInfo = str([rightPlayerID])  | 
|                         memPlayer.MapServer_QueryPlayerResult(0, 0, "FamilyPartyAnswer", answerInfo, len(answerInfo))  | 
|                         break  | 
|             else:  | 
|                 continue  | 
|         answerTick = gameWorld.GetDictByKey(FamilyPartyFB_AnswerTick % familyID)  | 
|         if not lastQuestionID and tick - answerTick < 5000:  | 
|             continue  | 
|           | 
|         #Ëæ»úÌâÄ¿  | 
|         historyQuestionIDList = PyGameData.g_questionIDHistory.get(familyID, [])  | 
|         for i in xrange(50):  | 
|             questionID = random.choice(allQuestionIDList)  | 
|             # Ëæ»úµ½²»Öظ´µÄ¼´¿É  | 
|             if questionID not in historyQuestionIDList or i == 49:  | 
|                 if questionID not in historyQuestionIDList:  | 
|                     historyQuestionIDList.append(questionID)  | 
|                     PyGameData.g_questionIDHistory[familyID] = historyQuestionIDList  | 
|                 break  | 
|           | 
|         gameWorld.SetDict(FamilyPartyFB_QuestionID % familyID, questionID)  | 
|         gameWorld.SetDict(FamilyPartyFB_HisQuestionID % familyID, questionID)  | 
|         gameWorld.SetDict(FamilyPartyFB_QuestionTick % familyID, tick)  | 
|           | 
|         PlayerControl.FamilyNotify(familyID, 'Party_Topic', [questionID])  | 
|     return  | 
|   | 
| ## ÏÉÃËÆµµÀÁÄÌì  | 
| def OnTalkFamily(curPlayer, content, tick):  | 
|   | 
|     gameWorld = GameWorld.GetGameWorld()  | 
|     state = gameWorld.GetDictByKey(ShareDefine.Def_Notify_WorldKey_DailyActionState % ShareDefine.DailyActionID_FamilyParty)  | 
|     if state != AnswerState:  | 
|         return  | 
|       | 
|     playerID = curPlayer.GetID()  | 
|     curFamilyid = curPlayer.GetFamilyID()  | 
|     if not curFamilyid:  | 
|         return  | 
|       | 
|     ## Ìí¼ÓÍæ¼Ò²ÎÓë´ðÌâµÄÌâÄ¿ID£¬Ö»ÒªÔÚ±¾µØÍ¼´ðÌâÆÚ¼äÏÉÃË·¢ËÍÁÄÌìÐÅÏ¢¾ÍËã  | 
|     ## ÔÚÿÌâÖмäÓÐ5Ã뻺³å£¬ÔÚ´ËÆÚ¼ä¿ÉÄÜËÙ¶ÈÂýµÄÍæ¼Ò»¹ÔڻشðÉÏÒ»Ì⣬ÔÚ»º³åÆÚ¼äÌâÄ¿IDΪ0£¬ËùÒÔÌí¼ÓÌâÄ¿IDÐèÓò»ÖØÖõÄÀúÊ·ÌâÄ¿ID  | 
|     if curPlayer.GetMapID() == ChConfig.Def_FBMapID_FamilyParty:  | 
|         if playerID not in PyGameData.g_familyAnswerPlayerIDDict:  | 
|             PyGameData.g_familyAnswerPlayerIDDict[playerID] = []  | 
|         answerQuestionIDList = PyGameData.g_familyAnswerPlayerIDDict[playerID]  | 
|         hisQuestionID = gameWorld.GetDictByKey(FamilyPartyFB_HisQuestionID % curFamilyid)  | 
|         if hisQuestionID and hisQuestionID not in answerQuestionIDList:  | 
|             answerQuestionIDList.append(hisQuestionID)  | 
|               | 
|     questionID = gameWorld.GetDictByKey(FamilyPartyFB_QuestionID % curFamilyid)  | 
|     if not questionID:  | 
|         return  | 
|     ipyData = IpyGameDataPY.GetIpyGameData('QuestionBank', questionID)  | 
|     if not ipyData:  | 
|         GameWorld.ErrLog('ÏÉÃËÑç»á´ðÌâ ¸ÃÌâ²»´æÔÚ£¬Òì³£ questionID=%s, ÖØÐ³öÌâ' % questionID)  | 
|         gameWorld.SetDict(FamilyPartyFB_QuestionID % curFamilyid, 0)  | 
|         gameWorld.SetDict(FamilyPartyFB_QuestionTick % curFamilyid, 0)  | 
|         return  | 
|       | 
|     answerList = eval(ipyData.GetAnswer())  | 
|     ChContent = PlayerTalk.ChangeVoiceChat(content)  | 
|     ChContent = GameWorld.CodeToGBK(ChContent)  | 
|     ChContent = ChContent.strip()  | 
|     isRight = ChContent in answerList  | 
|     if curPlayer.GetMapID() != ChConfig.Def_FBMapID_FamilyParty:  | 
|         if isRight:  | 
|             PlayerControl.FamilyNotify(curFamilyid, 'FamilyPleaseIntoParty')  | 
|             PlayerControl.NotifyCode(curPlayer, 'FamilyNoIntoParty')  | 
|         return  | 
|       | 
|     GameWorld.DebugLog("    answer=%s  ChContent=%s" % (answerList, ChContent), playerID)  | 
|     if isRight:  | 
|         GameWorld.DebugLog("    ´ð¶ÔÁË! %s" % playerID)  | 
|         playerName = curPlayer.GetName()  | 
|         gameWorld.SetDict(FamilyPartyFB_QuestionID % curFamilyid, 0)  | 
|         gameWorld.SetDict(FamilyPartyFB_AnswerTick % curFamilyid, tick)  | 
|   | 
|         if curFamilyid in PyGameData.g_familyAnswerDict:  | 
|             PyGameData.g_familyAnswerDict[curFamilyid][0] += 1  | 
|             PyGameData.g_familyAnswerDict[curFamilyid][1] = tick  | 
|         else:  | 
|             familyName = PlayerFamily.GetFamilyName_ByID(curFamilyid)  | 
|             PyGameData.g_familyAnswerDict[curFamilyid] = [1, tick, familyName]  | 
|               | 
|         rightCountAward = IpyGameDataPY.GetFuncEvalCfg("PartyReward", 5)  | 
|         if rightCountAward and len(rightCountAward) == 2:  | 
|             perRightCount, addFamilyActivity = rightCountAward  | 
|             if PyGameData.g_familyAnswerDict[curFamilyid][0] % perRightCount == 0:  | 
|                 PlayerControl.FamilyNotify(curFamilyid, "Contribution1", [perRightCount, addFamilyActivity])  | 
|                 GameWorld.SendMapServerMsgEx(ShareDefine.Def_Notify_WorldKey_FamilyPartyAddFamilyActivity, [ChConfig.Def_FBMapID_FamilyParty, curFamilyid, addFamilyActivity])  | 
|                   | 
|         PyGameData.g_partyheroAnswerDict[playerID] = PyGameData.g_partyheroAnswerDict.get(playerID, 0) + 1  | 
|           | 
|         if PyGameData.g_familyPartyTopInfo:  | 
|             if PyGameData.g_partyheroAnswerDict[playerID] > PyGameData.g_partyheroAnswerDict[PyGameData.g_familyPartyTopInfo[0]]:  | 
|                 PyGameData.g_familyPartyTopInfo = [playerID, playerName]  | 
|         else:  | 
|             PyGameData.g_familyPartyTopInfo = [playerID, playerName]  | 
|         msgList = [__GetFamilyAnswerRankList(), PyGameData.g_familyPartyTopInfo[1], PyGameData.g_partyheroAnswerDict[PyGameData.g_familyPartyTopInfo[0]]]  | 
|         GameWorld.SendMapServerMsgEx(ShareDefine.Def_Notify_WorldKey_FamilyPartyInfo, msgList)  | 
|         #֪ͨ´ðÌâ½á¹û  | 
|         answerInfo = str([playerID])  | 
|         curPlayer.MapServer_QueryPlayerResult(0, 0, "FamilyPartyAnswer", answerInfo, len(answerInfo))  | 
|           | 
|     return  |