1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/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