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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
#
##@package EventReport
#
# @todo:ʼþ»ã±¨
# @author hxp
# @date 2015-1-14
# @version 1.9
# @Note£º  EventReport_EventReportÓ÷¨ µÚÒ»¸ö²ÎÊý´«postÄÚÈÝurlencode£¬µÚ2,3,4²»Ó㻵ÚÎå¸ö²ÎÊý0´ú±íget£¬1Ϊpost£»µÚÁù¸ö²ÎÊýΪurl
#
# @change: "2015-01-28 22:30" hxp ¹Ø±Õʼþ»ã±¨
# @change: "2015-02-06 20:40" Alee Ê¼þ»ã±¨·¢ËÍÖÁºÏ·þÖ÷·þ
# @change: "2015-06-08 20:30" hxp Ôö¼ÓchannelCode
# @change: "2015-07-13 14:00" hxp Ôö¼ÓpidÐÅÏ¢
# @change: "2016-07-18 19:00" hxp 9377¶À´úʼþ»ã±¨°æ
# @change: "2016-07-27 20:00" Alee Ð޸Ĵò¿ªÎļþ³¬Ê±¹Ø±Õ
# @change: "2016-08-30 23:00" hxp ×Ô¶¨ÒåÔÚÏßÈËÊý; ÁÄÌì¼à¿Ø
# @change: "2016-09-10 11:00" hxp ÁÄÌìÄÚÈÝÌæ»»»»ÐÐ
# @change: "2017-07-01 15:30" hxp Æ½Ì¨ÐÅÏ¢¸ÄΪ´ÓÍæ¼ÒÕ˺ÅÖÐÈ¡£¬Ö§³Ö»ì·þģʽ
# @change: "2017-07-04 15:00" hxp Ôö¼Ó»ì·þģʽÏ¿ÉÇø·Ö¸÷ƽ̨×ÔÉíÇø·þºÏ·þºóµÄƽ̨Ö÷·þID
# ÏêϸÃèÊö: Ê¼þ»ã±¨
#
#---------------------------------------------------------------------
#"""Version = 2017-07-04 15:00"""
#---------------------------------------------------------------------
 
import ReadChConfig
import ShareDefine
import GameWorld
 
import datetime
import random
import time
import md5
import os
import re
import urllib
import IpyGameDataPY
 
 
EventFilepath = "D:\\EventServer\\PythonScribe\\EventLog\\" 
 
g_wFileName = ""
g_writeHandle = None  # ²»¿ÉÖ±½Óµ÷ÓÃ
g_whStartTime = 0     # ´´½¨Îļþʱ¼ä
Def_WriteTime = 2   # Îļþ³ÖÐøÐ´Èëʱ¼ä£º·ÖÖÓ
 
def OnTimeCloseScribeTxt():
    global g_whStartTime
    global g_writeHandle
    global g_wFileName
    try:
        if g_whStartTime == 0:
            return
        
        #Îļþ´´½¨³¬¹ý
        if time.time() - g_whStartTime < 60*(Def_WriteTime + 1):
            return
        
        if g_writeHandle == None:
            return
        g_writeHandle.close()
        g_wFileName = ""
        g_whStartTime = 0
    except:
        GameWorld.ErrLog("OnTimeCloseScribeTxt ³ö´í")
 
## ³õʼ»¯Ê¼þ
#  @param None
#  @return ÎÞ·µ»ØÖµ
def InitDllAppID():
 
    appID = "mobile"
    key = "mobile"
 
    GameWorld.GetGameWorld().EventReport_SetEventReportParam(appID, key)
    GameWorld.Log("³õʼ»¯Ê¼þ±¨¸æ: appID=%s,key=%s OK!" % (appID, key))
    return
 
## Ê¼þ±¨¸æ¼Ç¼
#  @param eventActionID Ê¼þid
#  @param eventParam Ê¼þ²ÎÊý
#  @param curPlayer 
#  @return None
def EventReport(eventActionID, eventParam, curPlayer=None, OperatorID=""):
    # ×é³ÉÀý×Ó eventParam µÄ¸ñʽ±ØÐëÊÇ xx=yy&zz=cc
    #  "http://192.168.0.249:12000/event_receiver?EventID=3099&OperatorID=test&PlayerCount=102&Time=2018-02-08 18:30:30&ProductID=snxxz&RegionName=s1"
    
    reportActionIDList = IpyGameDataPY.GetFuncEvalCfg("EventReport", 3)
    if reportActionIDList and eventActionID not in reportActionIDList:
        #GameWorld.DebugLog("·ÇÐèÒª»ã±¨µÄʼþID! %s" % eventActionID)
        return
    if eventActionID in IpyGameDataPY.GetFuncEvalCfg("EventReport", 1):
        #GameWorld.DebugLog("²»ÐèÒª»ã±¨µÄʼþ! %s" % eventActionID)
        return
    
    if not curPlayer and not OperatorID:
        return
 
    ProductID = ReadChConfig.GetPyMongoConfig("EventReport", "ProductID")
    ReportUrl = ReadChConfig.GetPyMongoConfig("EventReport", "ReportUrl")
    
    playerInfo = ""
    if curPlayer:
        playerInfo = "&AccountID=%s&IP=%s&DeviceFlag=%s"%(GameWorld.GetPlatformAccID(curPlayer.GetAccID()),
                                                            curPlayer.GetIP(), curPlayer.GetDeviceFlag())
        #=======================================================================
        # #UTF8 ÐèҪת³Éurl±àÂë²Å¿ÉÓÃ
        # playerInfo = urllib.urlencode({"RoleID": curPlayer.GetName(),
        #                  "AccountID": GameWorld.GetPlatformAccID(curPlayer.GetAccID()),
        #                  "IP": curPlayer.GetIP(),
        #                  "Level": curPlayer.GetLV()}) 
        #=======================================================================
        OperatorID = GameWorld.GetPlayerPlatform(curPlayer)
        RegionName = GameWorld.GetPlayerServerSID(curPlayer)
        
    else:
        # ºÏ·þÇé¿ö£¬Íæ¼ÒÈ¡×Ô¼º·þ·¢ËÍ£¬·ÇÍæ¼ÒÊý¾Ý°´Ö¸¶¨Æ½Ì¨ÅäÖ÷¢
        sid = GameWorld.GetPlayerMainServerID(OperatorID)
        if not sid:
            GameWorld.ErrLog("GetPlayerMainServerID: %s-%s"%(OperatorID, sid))
            return
        RegionName = 's%s'%sid
        
    if eventParam:
        eventParam = "&%s"%eventParam
        
    getUrl = "%s?ProductID=%s&OperatorID=%s&RegionName=%s&EventID=%s%s&Time=%s%s"%(\
             ReportUrl, ProductID, OperatorID, RegionName, eventActionID, playerInfo,
             urllib.quote(str(datetime.datetime.today()).split('.')[0]), eventParam)
    GameWorld.DebugLog("EventReport: %s"%getUrl)
    
    # µÚÎå¸ö²ÎÊý0´ú±íget·¢ËÍ  1´ú±ípost
    GameWorld.GetGameWorld().EventReport_EventReport("", "", "", "", 0, getUrl)
    return
 
 
 
## Ð´Ê¼þ±¨¸æÎļþ
#  @param eventClass 
#  @return None
def WriteEvent(eventClass):
    if GameWorld.IsCrossServer():
        return
    
    if eventClass.GetScribeEventName() not in ReadChConfig.GetEvalChConfig("EventReportID"):
        return
    
    try:
        if not os.path.isdir(EventFilepath):
            os.makedirs(EventFilepath)
        
        #³¢ÊÔдµ½Í¬Ò»¸öÎļþµ«ÊÇwindow »áÓÐbug£¬Ö»Äܸ÷×Ô³ÌÐò·Ö¿ªÐ´Èë
        fp_w = GetWriteIO()
        fp_w.write("%s\t%s\n"%(eventClass.GetScribeEventName(), eventClass.GetCurEventStr()))
        fp_w.flush()
    except:
        GameWorld.ErrLog("scribeʼþдÈë³ö´í¡£")
    return
 
# ·ÖÖÓ×¼µãУ׼
def FixTime():
    curTime = datetime.datetime.today()
    curTime = curTime + datetime.timedelta(minutes=Def_WriteTime-curTime.minute%Def_WriteTime)
    tmp = str(curTime).split(".")[0][:-3].replace(':', '-')
    return tmp.replace(" ", "_")
 
def GetLogFileName(fileStr):
    sessionid = md5.md5(str(random.random()) + str(time.time())).hexdigest()
    return EventFilepath + sessionid + fileStr + '.log'
 
def GetWriteIO():
    global g_wFileName
    global g_writeHandle
    global g_whStartTime
    
    fileTime = FixTime()
    if g_wFileName and fileTime in g_wFileName:
        #µ±Ç°ÎļþÖ±½Ó·µ»Ø
        return g_writeHandle
    curFileName = GetLogFileName(fileTime)
 
    if g_writeHandle != None:
        #¹Ø±Õ¾ÉÎļþ
        g_writeHandle.close()
    
    g_wFileName = curFileName
    g_writeHandle = open(g_wFileName, 'a+')
    g_whStartTime = time.time()
    return g_writeHandle
 
 
 
# Ã¿¸ö×ֶζ¼Ó¦¸ÃתΪ×Ö·û´®£¬²»È»join»á±¨´í
 
class ScribeEvent(object):
    def __init__(self):
        #±ØÐë×Ö¶Î
        self.product_slug = 'yhlz'  # ÓÎÏ·±êÖ¾
        self.agent_name = ReadChConfig.GetPyMongoConfig("platform", "PlatformName") # Æ½Ì¨±êÖ¾
        self.gameserver_no = ReadChConfig.GetPyMongoConfig("platform", "ServerID")[1:] # Çø·þ
        self.time = ""    #GameWorld.GetCurrentDataTimeStr()
        
    def SetEventAgentInfo(self, accIDPlatform):
        # ÉèÖôúÀíÔËÓªÉÌÐÅÏ¢
        # @param accIDPlatform: ÕâÀïÈ¡Õ˺ÅËùÊôƽ̨£¬Ö§³Ö»ì·þ, Èç¹ûÈ¡²»µ½£¬ÔòÓÃĬÈÏÅäÖõÄÔËÓªÉÌÐÅÏ¢
        if not accIDPlatform:
            return
        self.agent_name = accIDPlatform
        self.gameserver_no = str(GameWorld.GetPlayerMainServerID(self.agent_name))
        return
    
    #¼´Ê¹ÊǷDZØÐë×Ö¶ÎÒ²Ó¦¸Ã´«ËÍ£¬¸÷×Ö¶ÎÓÃ,·Ö¸ô£¬²¢ÇÒÓÃË«ÒýºÅ°üº¬£¬²Î¿¼¸ñʽ'"1","","","12314"'
    #±ÜÃâÐ޸ĸñʽµÄÇé¿ö£¬ÕâÀïͳһÐ޸ļ´¿É
    def GetEventStr(self, tmpList):
        return '\"%s\"'%'","'.join(tmpList)
    
# ÔÚÏßÈËÊý
class concurrency(ScribeEvent):
    def __init__(self):
        #±ØÐë×Ö¶Î
        super(concurrency, self).__init__()
        self.concurrency = 0   # ÔÚÏßÈËÊý
        #·Ç±ØÐë×Ö¶Î
        
        #¼´Ê±ÊǷDZØÐë×Ö¶ÎÒ²Ó¦¸Ã´«ËÍ£¬¸÷×Ö¶ÎÓÃ,·Ö¸ô£¬²¢ÇÒÓÃË«ÒýºÅ°üº¬£¬²Î¿¼¸ñʽ'"1","","","12314"'
    
    def GetCurEventStr(self):
        if not self.time:
            self.time = GameWorld.GetCurrentDataTimeStr()
        tmpList = [self.product_slug, self.agent_name, 
                   self.gameserver_no, self.time, str(self.concurrency)]
        
        return super(concurrency, self).GetEventStr(tmpList)
    
    def GetScribeEventName(self): return ShareDefine.Def_UserAction_KeepOnline
    
    
# 3.3.15. custom©\concurrency£¨Õû5·ÖÖÓ·Ö·þÇø×Ô¶¨ÒåÔÚÏßÍæ¼ÒÈËÊý£©
# ÓÃÓÚ×Ô¶¨ÒåÍæ¼ÒÔÚÏßÈËÊýÏà¹ØÍ³¼Æ£¬ÊÕ¼¯ÐèÒªµ¥¶Àͳ¼ÆµÄ×Ô¶¨ÒåÀàÐ͵ÄÔÚÏßÊý¾Ý¡£
class custom_concurrency(ScribeEvent):
    def __init__(self):
        #±ØÐë×Ö¶Î
        super(custom_concurrency, self).__init__()
        self.concurrency = 0   # ÔÚÏßÈËÊý
        self.type_name = ""   # ×Ô¶¨ÒåÀàÐÍ
        
        #·Ç±ØÐë×Ö¶Î
        
        #¼´Ê±ÊǷDZØÐë×Ö¶ÎÒ²Ó¦¸Ã´«ËÍ£¬¸÷×Ö¶ÎÓÃ,·Ö¸ô£¬²¢ÇÒÓÃË«ÒýºÅ°üº¬£¬²Î¿¼¸ñʽ'"1","","","12314"'
        return
    
    def GetCurEventStr(self):
        if not self.time:
            self.time = GameWorld.GetCurrentDataTimeStr()
        tmpList = [self.product_slug, self.agent_name, self.gameserver_no, self.time, str(self.concurrency), self.type_name]
        
        return super(custom_concurrency, self).GetEventStr(tmpList)
    
    def GetScribeEventName(self): return ShareDefine.Def_UserAction_CustomConcurrency
    
# 3.3.9. custom-events£¨×Ô¶¨Òåʼþ£©
# ÓÃÓÚͳ¼ÆÈÎºÎÆÚÍûÈ¥¸ú×ÙµÄÊý¾Ý
class custom_events(ScribeEvent):
    def __init__(self):
        #±ØÐë×Ö¶Î
        super(custom_events, self).__init__()
        self.account_id = "" # Õ˺ŠID£¬Æ½Ì¨ÏÂΨһ£¬ÇÒÖÕÉú²»±ä
        self.event_name = "" # Ê¼þÃû³Æ
        self.session_id = "" # »á»°ID
        
        #·Ç±ØÐë×Ö¶Î
        self.ip = "" # IP µØÖ·
        self.account_name = "" # Õ˺ŵǼÃû
        self.account_type = 0 # Õ˺ÅÀàÐÍ
        self.chr_name = "" # Íæ¼Ò½ÇÉ«Ãû
        self.chr_level = 0 # Íæ¼Ò½ÇÉ«µÈ¼¶
        self.comments = "" # ±¸×¢»ò¸½¼ÓÐÅÏ¢ char(255)
 
        #¼´Ê±ÊǷDZØÐë×Ö¶ÎÒ²Ó¦¸Ã´«ËÍ£¬¸÷×Ö¶ÎÓÃ,·Ö¸ô£¬²¢ÇÒÓÃË«ÒýºÅ°üº¬£¬²Î¿¼¸ñʽ'"1","","","12314"'
        return
    
    def GetCurEventStr(self):
        if not self.time:
            self.time = GameWorld.GetCurrentDataTimeStr()
        tmpList = [self.product_slug, self.agent_name, self.ip, self.gameserver_no, self.account_id, self.account_name, 
                   str(self.account_type), self.time, self.chr_name, str(self.chr_level), self.event_name,
                   self.comments, self.session_id]
        
        return super(custom_events, self).GetEventStr(tmpList)
    
    def GetScribeEventName(self): return ShareDefine.Def_UserAction_CustomEvents
 
    
# 3.3.13. chat-log £¨ÁÄÌìÏûÏ¢£©
# ÓÃÓÚ¼à¿ØÍæ¼ÒÁÄÌì
class chat_log(ScribeEvent):
    def __init__(self):
        #±ØÐë×Ö¶Î
        super(chat_log, self).__init__()
        self.account_id = "" # Õ˺ŠID£¬Æ½Ì¨ÏÂΨһ£¬ÇÒÖÕÉú²»±ä
        self.chr_name = "" # Íæ¼Ò½ÇÉ«Ãû
        self.content = "" # ÁÄÌìÄÚÈÝ
        self.cmc_name = "" # ÁÄÌìÆµµÀ±êʶ
        
        #·Ç±ØÐë×Ö¶Î
        self.ip = "" # IP µØÖ·
        self.account_name = "" # Õ˺ŵǼÃû
        self.account_type = 0 # Õ˺ÅÀàÐÍ
        self.chr_level = 0 # Íæ¼Ò½ÇÉ«µÈ¼¶
        self.object = "" # Ë½ÁĶÔÏó
        self.addinfo = "" # ¶îÍâÐÅÏ¢
 
        #¼´Ê±ÊǷDZØÐë×Ö¶ÎÒ²Ó¦¸Ã´«ËÍ£¬¸÷×Ö¶ÎÓÃ,·Ö¸ô£¬²¢ÇÒÓÃË«ÒýºÅ°üº¬£¬²Î¿¼¸ñʽ'"1","","","12314"'
        return
    
    def GetCurEventStr(self):
        if not self.time:
            self.time = GameWorld.GetCurrentDataTimeStr()
        tmpList = [self.product_slug, self.agent_name, self.ip, self.gameserver_no, self.account_id, self.account_name, 
                   str(self.account_type), self.time, self.chr_name, str(self.chr_level), self.object, self.content,
                   self.addinfo, self.cmc_name]
        
        return super(chat_log, self).GetEventStr(tmpList)
    
    def GetScribeEventName(self): return ShareDefine.Def_UserAction_ChatLog
    
## =================================================================================================
 
## ÔÚÏß
def WriteEvent_concurrency(activePlayerCount, tjgOnlineCnt, OperatorID):
    # Ö»´«ÕæÊµÍæ¼Ò£¬Èúǫ́ÏÔÊ¾ÕæÊµÔÚÏßÍæ¼Ò
    realPlayer = activePlayerCount-tjgOnlineCnt
    EventReport(ShareDefine.Def_UserAction_KeepOnline, 
                "PlayerCount=%s&TotalPlayerCount=%s"%(realPlayer, realPlayer), None, OperatorID)
    return
 
 
## ×Ô¶¨ÒåÔÚÏß
def WriteEvent_custom_concurrency(platform, concurrency, type_name):
    customConcurrency = custom_concurrency()
    customConcurrency.SetEventAgentInfo(platform)
    customConcurrency.concurrency = concurrency
    customConcurrency.type_name = type_name
    WriteEvent(customConcurrency)
    return
 
def WriteEvent_chat_log(curPlayer, content, cmc_name, tagName="", addinfo=""):
    return
    #===========================================================================
    # '''
    # @todo: Ð´ÁÄÌì¼à¿Ø¼Ç¼
    # @param content: ÁÄÌìÄÚÈÝ
    # @param cmc_name: ÁÄÌìÆµµÀ±êʶ
    # @param tagName: Ë½ÁĶÔÏó
    # @param addinfo: ¶îÍâÐÅÏ¢
    # '''
    # 
    # chatlogEvent = chat_log()
    # chatlogEvent.SetEventAgentInfo(GameWorld.GetPlayerPlatform(curPlayer))
    # chatlogEvent.account_id = GameWorld.GetPlatformAccID(curPlayer.GetAccID())
    # chatlogEvent.chr_name = curPlayer.GetName()
    # chatlogEvent.content = __GetEventChatContent(content)
    # chatlogEvent.cmc_name = cmc_name
    # chatlogEvent.ip = curPlayer.GetIP()
    # chatlogEvent.account_name = chatlogEvent.account_id
    # chatlogEvent.account_type = 0
    # chatlogEvent.chr_level = 0
    # chatlogEvent.object = tagName
    # chatlogEvent.addinfo = addinfo
    # WriteEvent(chatlogEvent)
    # return
    #===========================================================================
 
def __GetEventChatContent(content):
    '''
    <a color="255,255,0" href="GOTO 10000,74,60">[µØÍ¼:ĺ¹âÖ®³Ç(74,60)]</a>
    <A color="205,0,0" onmouseover="ShowInfo ITEM,55986" DATA="07 04 01 00 B2 DA 00 00 00 01 00 01 
        28 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
        00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 3C 00 00 00 7B 27 32 38 27 3A 5B 
        27 31 30 32 33 34 27 5D 2C 27 31 39 27 3A 5B 27 33 35 34 27 2C 27 38 35 36 27 2C 27 33 35 
        34 27 2C 27 37 35 38 27 2C 27 39 35 31 27 2C 27 31 30 35 32 27 5D 7D 04 00 00 00 00 00 00 
        00 00 00 00 00 00 00 00 00 00 ">[ÉñÊ¥µÄ°ÁÊÀÖ®¹­ +40]</a>
    '''
    tempMatch = re.search("<a color=.*?>.*?</a>", content)
    if tempMatch:
        tempStr = tempMatch.group()
        markIndex = tempStr.index(">") + 1
        repStr = tempStr[markIndex:tempStr.index("<", markIndex)]
        content = content.replace(tempStr, repStr)
        
    tempMatch = re.search("<A color=.*?>.*?</a>", content)
    if tempMatch:
        tempStr = tempMatch.group()
        markIndex = tempStr.index(">") + 1
        repStr = tempStr[markIndex:tempStr.index("<", markIndex)]
        content = content.replace(tempStr, repStr)
        
    # Ìæ»»»»ÐÐ
    content = content.replace("\r", "") 
    content = content.replace("\n", "") 
    return content