| #!/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 random  | 
|   | 
| FamilyPartyFB_QuestionID = 'FamilyPartyFB_QuestionID%s' #µ±Ç°ÌâID  | 
| FamilyPartyFB_AnswerTick = 'FamilyPartyFB_AnswerTick%s' #´ð¶Ôʱ¼ä  | 
|   | 
|   | 
| g_questionIDHistory = {} #³ö¹ýµÄÌâ¼Ç¼ {familyid:[³ö¹ýµÄÌâid,..]}  | 
| g_familyAnswerDict = {} #ÏÉÃË´ðÌâÊýÁ¿ {familyid:´ðÌâÊýÁ¿,..}  | 
| g_heroAnswerDict = {} #¸öÈË´ðÌâÊýÁ¿ {playerid:´ðÌâÊýÁ¿,..}  | 
|   | 
|   | 
| ## Íæ¼ÒµÇ¼  | 
| #  @param None  | 
| #  @return None  | 
| def OnPlayerLogin(curPlayer):  | 
|     NotifyFamilyPartyQuestion(curPlayer)  | 
|     return  | 
|   | 
| def NotifyFamilyPartyQuestion(curPlayer):  | 
|     #¸ÕµÇ¼µÄ»ò¸Õ½øÏÉÃ˵IJ¹·¢×î½üµÄÌâÄ¿  | 
|     gameWorld = GameWorld.GetGameWorld()  | 
|     state = gameWorld.GetDictByKey(ShareDefine.Def_Notify_WorldKey_FBFuncState % ChConfig.Def_FBMapID_FamilyParty)  | 
|     if state != 1:  | 
|         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):  | 
|     #»î¶¯×´Ì¬±ä¸ü  | 
|     global g_questionIDHistory  | 
|     global g_familyAnswerDict  | 
|     global g_heroAnswerDict  | 
|       | 
|     if state == 1:  | 
|         g_questionIDHistory = {}  | 
|         g_familyAnswerDict = {}  | 
|         g_heroAnswerDict = {}  | 
|         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_AnswerTick % familyID, 0)  | 
|               | 
|       | 
|     if state == 0:  | 
|         #»î¶¯½áËã Ç°3ÃûÏÉÃË¡¢´ðÌâÍõ  | 
|         GameWorld.Log(" ÏÉÃËÑç»á½áËã g_familyAnswerDict=%s, g_heroAnswerDict=%s"%(g_familyAnswerDict, g_heroAnswerDict))  | 
|           | 
|         noneStr = 'null'  | 
|         topfamilyNameList = [noneStr,noneStr,noneStr]  | 
|         if g_familyAnswerDict:  | 
|             sortFamilyList = g_familyAnswerDict.items()  | 
|             sortFamilyList.sort(cmp=CmpFunc)  | 
|             familyIDList = [familyInfo[0] for familyInfo in sortFamilyList][:3]  | 
|             GameWorld.DebugLog("    Ñç»á½áÊø familyIDList=%s"%familyIDList)  | 
|             redPackID = IpyGameDataPY.GetFuncCfg('PartyReward', 4)  | 
|             for i, familyID in enumerate(familyIDList):  | 
|                 familyName = PlayerFamily.GetFamilyName_ByID(familyID)  | 
|                 topfamilyNameList[i] = familyName  | 
|                 #·¢ºì°ü  | 
|                 family = GameWorld.GetFamilyManager().FindFamily(familyID)  | 
|                 if not family:  | 
|                     continue  | 
|                 PlayerFamilyRedPacket.CreatNewFamilyRedPacket(family, family.GetLeaderID(), redPackID)  | 
|         PlayerControl.WorldNotify(0, 'Party_TopThree', topfamilyNameList)  | 
|           | 
|         topHeroName = noneStr  | 
|         if g_heroAnswerDict:  | 
|             rewardInfo = IpyGameDataPY.GetFuncEvalCfg('PartyReward', 3)  | 
|             sortHeroList = g_heroAnswerDict.items()  | 
|             sortHeroList.sort(cmp=CmpFunc)  | 
|             sortHeroidList = [heroInfo[0] for heroInfo in sortHeroList]  | 
|             GameWorld.DebugLog("    Ñç»á½áÊø sortHeroidList=%s"%sortHeroidList)  | 
|             playerManager = GameWorld.GetPlayerManager()  | 
|             for playerID in sortHeroidList:  | 
|                 curPlayer = playerManager.FindPlayerByID(playerID)  | 
|                 if not curPlayer:  | 
|                     continue  | 
| #                if curPlayer.GetMapID() != ChConfig.Def_FBMapID_FamilyParty:  | 
| #                    #²»ÔÚ¸±±¾  | 
| #                    continue  | 
|                 topHeroName = curPlayer.GetName()  | 
|                 #¸ø½±Àø  | 
|                 mailKey, day, itemList = rewardInfo  | 
|                 content = ShareDefine.Def_MailFormat % (mailKey, '')  | 
|                 GameWorld.DebugLog('·¢ËÍÏÉÃËÑç»á´ðÌâÍõÓʼþ   itemList=%s' % (itemList))  | 
|                 PlayerCompensation.SendPersonalItemMailEx('', content, day, [playerID], itemList)  | 
|                 break  | 
|         PlayerControl.WorldNotify(0, 'Party_TopPlayer', [topHeroName])  | 
|     return  | 
|   | 
| ## ÅÅÐòº¯Êý  | 
| #  @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):  | 
|     global g_questionIDHistory  | 
|     #¿ªÊ¼³öÌâ  | 
|     gameWorld = GameWorld.GetGameWorld()  | 
|     state = gameWorld.GetDictByKey(ShareDefine.Def_Notify_WorldKey_FBFuncState % ChConfig.Def_FBMapID_FamilyParty)  | 
|     if state != 1:  | 
|         return  | 
|       | 
|     familyManager = GameWorld.GetFamilyManager()  | 
|     for i in range(0, familyManager.GetCount()):  | 
|         family = familyManager.GetAt(i)  | 
|         familyID = family.GetID()  | 
|       | 
|         if gameWorld.GetDictByKey(FamilyPartyFB_QuestionID % familyID):  | 
|             #»¹ÓÐÌâδ´ð  | 
|             continue  | 
|         answerTick = gameWorld.GetDictByKey(FamilyPartyFB_AnswerTick % familyID)  | 
|         if tick - answerTick < 5000:  | 
|             continue  | 
|           | 
|         #Ëæ»úÌâÄ¿  | 
|         historyQuestionIDList = g_questionIDHistory.get(familyID, [])  | 
|         questionCnt = IpyGameDataPY.IPY_Data().GetQuestionBankCount()  | 
|         for i in xrange(50):  | 
|             questionID = random.randint(1, questionCnt)  | 
|             # Ëæ»úµ½²»Öظ´µÄ¼´¿É  | 
|             if questionID not in historyQuestionIDList or i == 49:  | 
|                 historyQuestionIDList.append(questionID)  | 
|                 g_questionIDHistory[familyID] = historyQuestionIDList  | 
|                 break  | 
|           | 
|         gameWorld.SetDict(FamilyPartyFB_QuestionID % familyID, questionID)  | 
|         PlayerControl.FamilyNotify(familyID, 'Party_Topic', [questionID])  | 
|     return  | 
|   | 
| ## ÏÉÃËÆµµÀÁÄÌì  | 
| def OnTalkFamily(curPlayer, content, tick):  | 
|     global g_familyAnswerDict  | 
|     global g_heroAnswerDict  | 
|       | 
|     gameWorld = GameWorld.GetGameWorld()  | 
|     state = gameWorld.GetDictByKey(ShareDefine.Def_Notify_WorldKey_FBFuncState % ChConfig.Def_FBMapID_FamilyParty)  | 
|     if state != 1:  | 
|         return  | 
|       | 
|     curFamilyid = curPlayer.GetFamilyID()  | 
|     if not curFamilyid:  | 
|         return  | 
|     questionID = gameWorld.GetDictByKey(FamilyPartyFB_QuestionID % curFamilyid)  | 
|     if not questionID:  | 
|         return  | 
|       | 
|     ipyData = IpyGameDataPY.IPY_Data().GetQuestionBankByIndex(questionID-1)  | 
|     answerList = eval(ipyData.GetAnswer())  | 
|     ChContent = PlayerTalk.ChangeVoiceChat(content)  | 
|     ChContent = ChContent.decode(GameWorld.GetCharacterEncoding()).encode(ShareDefine.Def_Game_Character_Encoding)  | 
|     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  | 
|       | 
|     playerID = curPlayer.GetID()  | 
|     GameWorld.DebugLog("    answer=%s  ChContent=%s"%(answerList, ChContent), playerID)  | 
|       | 
|     if isRight:  | 
|         GameWorld.DebugLog("    ´ð¶ÔÁË! %s"%playerID)  | 
|         gameWorld.SetDict(FamilyPartyFB_QuestionID%curFamilyid, 0)  | 
|         gameWorld.SetDict(FamilyPartyFB_AnswerTick%curFamilyid, tick)  | 
|         PlayerControl.FamilyNotify(curFamilyid, 'Party_Answer', [curPlayer.GetName()])  | 
|           | 
|         g_familyAnswerDict[curFamilyid] = [g_familyAnswerDict.get(curFamilyid, [0])[0] + 1, tick]  | 
|         g_heroAnswerDict[playerID] = [g_heroAnswerDict.get(playerID, [0])[0] + 1, tick]  | 
|           | 
|           | 
|         #Í¨ÖªÍæ¼Ò¿É»ñµÃ¹±Ï×  | 
|         curPlayer.MapServer_QueryPlayerResult(0, 0, "FamilyPartyAnswer", '', 0)  | 
|           | 
|     return  |