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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package GameWorldLogic.FBProcess.GameLogic_CrossGrassland
#
# @todo:¿ç·þ²ÝÔ°
# @author hxp
# @date 2019-04-15
# @version 1.0
#
# ÏêϸÃèÊö: ¿ç·þ²ÝÔ°
#
#-------------------------------------------------------------------------------
#"""Version = 2019-04-15 16:30"""
#-------------------------------------------------------------------------------
 
import GameWorld
import GameWorldProcess
import PlayerFairyDomain
import NPCCustomRefresh
import IpyGameDataPY
import PyGameData
import NPCCommon
import PlayerControl
import ChConfig
import FBCommon
import ItemControler
import random
 
FBDict_SyncFBNPC = 'SyncFBNPC' #ÊÇ·ñÐèҪͬ²½¸±±¾NPC
 
def DoResetCrossGrassland(curPlayer, eventType, fdeventID):
    ## ²ÝÔ°ÖØÖÃ
    
    resetCollectType = 10 + eventType
    NPCCommon.DoResetCollectNPCTimeByType(curPlayer, [resetCollectType])
    
    # ±¦Ïä¹Ö´ÎÊýÖØÖÃ
    if eventType == PlayerFairyDomain.FDEventType_GrasslandXian:
        npcID = IpyGameDataPY.GetFuncCfg("CrossGrasslandCfg", 1)
        if npcID:
            NPCCommon.UpdateNPCAttackCount(curPlayer, npcID, 0)
          
    ResetGrasslandAwardRecord(curPlayer)  
    return
 
## ÊÇ·ñÐèÒª×ö½øÈ븱±¾Í¨Óüì²éÌõ¼þÂß¼­£¬Ä¬ÈÏÐèÒª¼ì²é
def OnNeedCheckCanEnterFBComm(curPlayer, mapID, lineID):
    curState = PlayerFairyDomain.GetFairyDomainFBEventState(curPlayer, mapID, lineID)
    return curState != PlayerFairyDomain.FDEventState_Visiting
 
def OnEnterFBEvent(curPlayer, mapID, lineID, tick):
    curState = PlayerFairyDomain.GetFairyDomainFBEventState(curPlayer, mapID, lineID)
    return curState in [PlayerFairyDomain.FDEventState_CanVisit, PlayerFairyDomain.FDEventState_Visiting]
 
## ½øÈë¿ç·þ¸±±¾×¢²áÊý¾ÝǰÂß¼­
## @return: ÊÇ·ñ¿ÉÒÔ×¢²áǰÍù¿ç·þ¸±±¾£¬´Îº¯ÊýÖпÉÒÔдһЩ¿Û³ýÏûºÄÂß¼­µÈ
def OnRegEnterCrossFB(curPlayer, mapID, lineID):
    curState = PlayerFairyDomain.GetFairyDomainFBEventState(curPlayer, mapID, lineID)
    if curState == PlayerFairyDomain.FDEventState_CanVisit:
        delResult = FBCommon.DelFBEnterTicket(curPlayer, mapID, lineID)
        isOK = delResult[0]
        if not isOK:
            return False
        return PlayerFairyDomain.SetFairyDomainFBEventState(curPlayer, mapID, lineID, PlayerFairyDomain.FDEventState_Visiting)
    
    if curState == PlayerFairyDomain.FDEventState_Visiting:
        return True
    
    return False
 
## ¿ç·þ¹¦ÄÜÏß·Êý¾Ý»º´æ£¬Ï´οªÆôͬÑù¹¦ÄÜÏß·ʱ»áÓøÃÊý¾Ý½øÐл¹Ô­Ö®Ç°µÄ¸±±¾×´Ì¬
def OnGetCrossFuncLineDataCache():
    refreshNPCInfo = NPCCustomRefresh.GetCopyMapRandomRefreshNPCInfo()
    return refreshNPCInfo
 
## ¿ªÆô¸±±¾
def OnOpenFB(tick):
    gameWorld = GameWorld.GetGameWorld()
    realMapID, copyMapID = gameWorld.GetRealMapID(), gameWorld.GetCopyMapID()
    key = (realMapID, copyMapID)
    if key in PyGameData.g_crossFuncLineDataCache:
        refreshNPCInfo = PyGameData.g_crossFuncLineDataCache.pop(key)
        GameWorld.DebugLog("¸±±¾¿ªÆô¸ù¾Ý±£´æµÄÐéÄâÏß·±êÊÔµãË¢¹ÖÐÅϢˢ¹Ö: realMapID=%s,copyMapID=%s,refreshNPCInfo=%s" % (realMapID, copyMapID, refreshNPCInfo))
        NPCCustomRefresh.OnFBOpenSetRandomRefreshNPCInfo(refreshNPCInfo, tick)
        
    return
 
## ½ø¸±±¾
def DoEnterFB(curPlayer, tick):
    playerID = curPlayer.GetPlayerID()
    zoneID = FBCommon.GetCrossDynamicLineMapZoneID()
    funcLineID = FBCommon.GetCrossDynamicLineMapFuncLineID()
    crossMapID = PlayerControl.GetCrossMapID(curPlayer)
    GameWorld.Log("DoEnterFB zoneID=%s,funcLineID=%s,crossMapID=%s" % (zoneID, funcLineID, crossMapID), playerID)
    FBCommon.Sync_FBNPC(curPlayer=curPlayer)
    if crossMapID == ChConfig.Def_FBMapID_CrossGrasslandXian:
        boxNPCID = IpyGameDataPY.GetFuncCfg("CrossGrasslandCfg", 1)
        if boxNPCID:
            NPCCommon.SyncNPCAttackCount(curPlayer, [boxNPCID])
    return
 
## ¸±±¾×ÜÂß¼­¼ÆÊ±Æ÷
def OnProcess(tick):
    gameFB = GameWorld.GetGameFB()
    if gameFB.GetGameFBDictByKey(FBDict_SyncFBNPC):
        gameFB.SetGameFBDict(FBDict_SyncFBNPC, 0)
        FBCommon.Sync_FBNPC()
        
    return
#
### ¹Ø±Õ¸±±¾
#def OnCloseFB(tick):
#    return
#
### Íæ¼ÒÍ˳ö¸±±¾
#def DoExitFB(curPlayer, tick):
#    return
 
##Íæ¼ÒÖ÷¶¯À뿪¸±±¾.
def DoPlayerLeaveFB(curPlayer, tick):
    
    gameWorld = GameWorld.GetGameWorld()
    #×îºóÒ»ÈËÍ˳ö¸±±¾Ôò¹Ø±ÕµØÍ¼
    if gameWorld.GetMapCopyPlayerManager().GetPlayerCount() == 1:
        GameWorldProcess.CloseFB(tick)
        
    return
 
## ¿Í»§¶Ë½øÈë×Ô¶¨Ò峡¾°
def OnEnterCustomScene(curPlayer, mapID, lineID):
    curState = PlayerFairyDomain.GetFairyDomainFBEventState(curPlayer, mapID, lineID)
    PlayerFairyDomain.SetFairyDomainFBEventState(curPlayer, mapID, lineID, PlayerFairyDomain.FDEventState_Visiting)
    
    if curState == PlayerFairyDomain.FDEventState_CanVisit:
        refreshMapNPCDict = IpyGameDataPY.GetFuncEvalCfg("CrossGrasslandCfg", 2)
        npcCountDict = refreshMapNPCDict.get((mapID, lineID), {})
        for npcID, npcCount in npcCountDict.items():
            PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_GrasslandNPCCount % npcID, npcCount)
            
        if mapID == ChConfig.Def_FBMapID_CrossGrasslandXian:
            FBCommon.DelFBEnterTicket(curPlayer, mapID, lineID)
    else:
        DoCheckUpdateGrasslandEnd(curPlayer)
        
    SyncCustomSceneNPCCount(curPlayer, mapID, lineID)
    if mapID == ChConfig.Def_FBMapID_CrossGrasslandXian:
        boxNPCID = IpyGameDataPY.GetFuncCfg("CrossGrasslandCfg", 1)
        if boxNPCID:
            NPCCommon.SyncNPCAttackCount(curPlayer, [boxNPCID])
            
    return
 
## ×Ô¶¨Ò峡¾°²É¼¯OK£¬Ðè×Ô´øÊÇ·ñÔÊÐí²É¼¯µÄÅжÏ
def OnCustomSceneCollectOK(curPlayer, mapID, lineID, npcID):
    curState = PlayerFairyDomain.GetFairyDomainFBEventState(curPlayer, mapID, lineID)
    if curState != PlayerFairyDomain.FDEventState_Visiting:
        return False
    curCount = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_GrasslandNPCCount % npcID)
    if not curCount:
        return False
    return True
 
def DecCustomSceneNPCCount(curPlayer, npcID):
    ## ¼õÉÙ²ÝÔ°×Ô¶¨Ò峡¾°NPC£¬Ä¬ÈϼõÉÙÒ»¸ö
    mapID, lineID = GetGrasslandMapID(curPlayer)
    if not mapID:
        return
    curCount = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_GrasslandNPCCount % npcID)
    updCount = max(0, curCount - 1)
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_GrasslandNPCCount % npcID, updCount)
    SyncCustomSceneNPCCount(curPlayer, mapID, lineID)
    return
 
def SyncCustomSceneNPCCount(curPlayer, mapID, lineID):
    ## Í¨Öª×Ô¶¨Ò峡¾°NPCÊý
    refreshMapNPCDict = IpyGameDataPY.GetFuncEvalCfg("CrossGrasslandCfg", 2)
    npcCountDict = refreshMapNPCDict.get((mapID, lineID), {})
    npcNowCountDict = {}
    for npcID in npcCountDict.keys():
        npcNowCountDict[npcID] = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_GrasslandNPCCount % npcID)
    if mapID == ChConfig.Def_FBMapID_CrossGrasslandXian:
        boxNPCID = IpyGameDataPY.GetFuncCfg("CrossGrasslandCfg", 1)
        if boxNPCID:
            npcNowCountDict[boxNPCID] = 1
    NPCCommon.SyncNPCCntInfo(curPlayer, mapID, npcNowCountDict)
    return
 
def RecordGrasslandAward(curPlayer, addItemList):
    ## ¼Ç¼²ÝÔ°½±ÀøÐÅÏ¢
    mapID = GetGrasslandMapID(curPlayer)[0]
    if not mapID:
        return
    
    for itemInfo in addItemList:
        if not isinstance(itemInfo, list):
            continue
        itemID, itemCount = itemInfo[:2]
        
        newIndex = None
        for i in xrange(20):
            itemCountInfo = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_GrasslandDropCount % i)
            if not itemCountInfo:
                newIndex = i
                break
            recItemID = itemCountInfo/100
            if recItemID == itemID:
                updRecValue = itemCountInfo + itemCount
                PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_GrasslandDropCount % i, updRecValue)
                break
        if newIndex != None:
            newRecValue = itemID*100 + itemCount
            PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_GrasslandDropCount % newIndex, newRecValue)
            
    return
 
def ResetGrasslandAwardRecord(curPlayer):
    ## ÖØÖòÝÔ°½±ÀøÐÅÏ¢¼Ç¼
    for i in xrange(20):
        itemCountInfo = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_GrasslandDropCount % i)
        if not itemCountInfo:
            break
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_GrasslandDropCount % i, 0)
    return
 
    
def GetGrasslandMapID(curPlayer):
    grasslandMapIDList = [ChConfig.Def_FBMapID_CrossGrasslandLing, ChConfig.Def_FBMapID_CrossGrasslandXian]
    crossMapID = PlayerControl.GetCrossMapID(curPlayer)
    customMapID = PlayerControl.GetCustomMapID(curPlayer)
    if crossMapID in grasslandMapIDList:
        mapID = crossMapID
        lineID = curPlayer.NomalDictGetProperty(ChConfig.Def_Player_Dict_ReqCrossFBFuncLine)
    elif customMapID in grasslandMapIDList:
        mapID = customMapID
        lineID = PlayerControl.GetCustomLineID(curPlayer)
    else:
        return 0, 0
    return mapID, lineID
 
def DoCheckUpdateGrasslandEnd(curPlayer):
    ## ¼ì²é¸üвÝÔ°ÒѰݷÃÍê³É
    
    mapID, lineID = GetGrasslandMapID(curPlayer)
    if not mapID:
        return
    
    # ²É¼¯´ÎÊýÊÇ·ñÒÑÓÃÍê
    if mapID == PlayerControl.GetCustomMapID(curPlayer):
        refreshMapNPCDict = IpyGameDataPY.GetFuncEvalCfg("CrossGrasslandCfg", 2)
        npcCountDict = refreshMapNPCDict.get((mapID, lineID), {})
        npcIDList = npcCountDict.keys()
    else:
        collNPCIpyDataList = IpyGameDataPY.GetIpyGameDataListNotLog("MapRefreshNPC", mapID)
        if not collNPCIpyDataList:
            return
        
        npcIDList = []
        for collIpyData in collNPCIpyDataList:
            npcIDList += collIpyData.GetNPCIDList()
            
    for npcID in npcIDList:
        collectNPCIpyData = IpyGameDataPY.GetIpyGameData("CollectNPC", npcID)
        if not collectNPCIpyData:
            return
        limitMaxTime = collectNPCIpyData.GetMaxCollectCount()
        totalCollTime = NPCCommon.GetTodayCollectCount(curPlayer, npcID)
        if totalCollTime < limitMaxTime:
            GameWorld.DebugLog("²ÝÔ°NPC²É¼¯´ÎÊýδÓÃÍê! npcID=%s,totalCollTime=%s < limitMaxTime=%s" % (npcID, totalCollTime, limitMaxTime))
            return
            
    # ±¦Ïä¹Ö¹¥»÷´ÎÊýÊÇ·ñÒÑÓÃÍê
    boxNPCID = IpyGameDataPY.GetFuncCfg("CrossGrasslandCfg", 1)
    if mapID == ChConfig.Def_FBMapID_CrossGrasslandXian and boxNPCID:
        boxNPCIpyData = IpyGameDataPY.GetIpyGameDataNotLog("TreasureNPC", boxNPCID)
        if not boxNPCIpyData:
            return
        attackCountDropWeightInfo = boxNPCIpyData.GetAttackCountDropWeightInfo()
        if not attackCountDropWeightInfo:
            return
        maxAttackCount = max(attackCountDropWeightInfo)
        attackCount = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_NPCAttackCount % boxNPCID)
        if attackCount < maxAttackCount:
            GameWorld.DebugLog("²ÝÔ°±¦Ïä¹Ö¹¥»÷´ÎÊýδÓÃÍê! boxNPCID=%s,attackCount=%s < maxAttackCount=%s" % (boxNPCID, attackCount, maxAttackCount))
            return
        
    PlayerFairyDomain.SetFairyDomainFBEventState(curPlayer, mapID, lineID, PlayerFairyDomain.FDEventState_Visited)
    PlayerControl.SetCustomMap(curPlayer, 0, 0)
    GameWorld.DebugLog("ÉèÖòÝÔ°ÒÑÍê³É!mapID=%s, lineID=%s" % (mapID, lineID))
    
    # Í¨Öª½áËã
    awardItemList = []
    for i in xrange(20):
        itemCountInfo = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_GrasslandDropCount % i)
        if not itemCountInfo:
            break
        isAuctionItem = 0
        itemID, itemCount = itemCountInfo/100, itemCountInfo%100
        awardItemList.append([itemID, itemCount, isAuctionItem])
    extraAward = [] #¶îÍâÎïÆ·½±Àø
    ipyData = IpyGameDataPY.GetIpyGameDataByCondition("FairyDomain", {"MapID":mapID, "LineID":lineID})
    if ipyData:
        fdEventID = ipyData.GetID()
        extraAward = PlayerFairyDomain.GetFairyAppointAward(curPlayer, fdEventID)
    if not extraAward:
        extraAward = FBCommon.GetFBLineReward(mapID, lineID)
    if extraAward:
        ItemControler.GivePlayerItemOrMail(curPlayer, extraAward)
        awardItemList += extraAward
    overDict = {FBCommon.Over_itemInfo:FBCommon.GetJsonItemList(awardItemList)}
    FBCommon.NotifyFBOver(curPlayer, mapID, lineID, 1, overDict)
    ResetGrasslandAwardRecord(curPlayer)
    return
 
def DoFB_NPCDead(curNPC):
    #GameWorld.DebugLog("DoFB_NPCDead ÉèÖÃÐèҪͬ²½¸±±¾NPC!")
    GameWorld.GetGameFB().SetGameFBDict(FBDict_SyncFBNPC, 1)
    return
 
def OnNPCRebornInFB(curNPC):
    #GameWorld.DebugLog("OnNPCRebornInFB ÉèÖÃÐèҪͬ²½¸±±¾NPC!")
    GameWorld.GetGameFB().SetGameFBDict(FBDict_SyncFBNPC, 1)
    return
 
 
## ¿É·ñɨµ´
def OnPlayerFBSweepAsk(curPlayer, mapID, lineID, sweepCnt, isFinish, dataEx):
    curState = PlayerFairyDomain.GetFairyDomainFBEventState(curPlayer, mapID, lineID)
    if curState not in [PlayerFairyDomain.FDEventState_CanVisit, PlayerFairyDomain.FDEventState_Visiting]:
        GameWorld.DebugLog("çÎç¿ÎÞ·¨É¨µ´! ²»´æÔÚ¸Ãʼþ»òÎÞ·¨½øÈ룡 mapID=%s,lineID=%s,curState=%s" % (mapID, lineID, curState))
        return False
    return True
 
## É¨µ´½á¹û
def OnPlayerFBSweepResult(curPlayer, mapID, lineID, sweepCnt, isFinish, dataEx):
    
    jsonItemList = []
    refreshMapNPCDict = IpyGameDataPY.GetFuncEvalCfg("CrossGrasslandCfg", 2)
    npcCountDict = refreshMapNPCDict.get((mapID, lineID), {})
    GameWorld.DebugLog("²Ý԰ɨµ´: mapID=%s,lineID=%s,npcCountDict=%s" % (mapID, lineID, npcCountDict))
    if npcCountDict:
        npcIDList = npcCountDict.keys()
        doCount = sum(npcCountDict.values()) * 2
        while npcIDList and doCount >= 0:
            doCount -= 1
            npcID = random.choice(npcIDList)
            collectNPCIpyData = IpyGameDataPY.GetIpyGameData("CollectNPC", npcID)
            if not collectNPCIpyData:
                npcIDList.remove(npcID)
                continue
            awardItemInfo = NPCCommon.DoGiveCollectNPCAward(curPlayer, npcID, collectNPCIpyData, isSweep=True)
            if awardItemInfo == None:
                npcIDList.remove(npcID)
                GameWorld.DebugLog("    É¨µ´npcID=%s,ûÓн±ÀøÁË£¬ÒƳý£¡ npcIDList=%s" % (npcID, npcIDList))
                continue
            jsonItemList.extend(awardItemInfo)
            GameWorld.DebugLog("    npcID=%s,awardItemInfo=%s" % (npcID, awardItemInfo))
            
 
    extraAward = FBCommon.GetFBLineReward(mapID, lineID)
    if extraAward:
        ItemControler.GivePlayerItemOrMail(curPlayer, extraAward)
        jsonItemList += FBCommon.GetJsonItemList(extraAward)
    GameWorld.DebugLog("    É¨µ´jsonItemList=%s" % jsonItemList)
    
    ipyData = IpyGameDataPY.GetIpyGameDataByCondition('FairyDomain', {'MapID':mapID, 'LineID':lineID})
    if ipyData:
        fdeventID = ipyData.GetID()
        PlayerFairyDomain.SetFairyDomainEventState(curPlayer, fdeventID, PlayerFairyDomain.FDEventState_Visited)
    
    isPass = 1
    overDict = {FBCommon.Over_itemInfo:jsonItemList, FBCommon.Over_isSweep:1}
    FBCommon.NotifyFBOver(curPlayer, mapID, lineID, isPass, overDict)
    return