hxp
2021-11-17 ffa8a645ed6a92a3c723bbf5c7f1eb4d5425c826
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
#!/usr/bin/python
# -*- coding: GBK -*-
#
#
##@package PlayerDBOper.py
#
# @todo:¹ÜÀíÊý¾Ý¿â
#
# @author whx
# @date 2012-11-09 10:00
# @version 1.8
# @note:
#
# @change: "2012-11-30 19:00" hyx ReloadÊ±ÖØÐ¼ÓÔØÊý¾Ý
# @change: "2012-12-03 11:55" hyx »¹Ô­Êý¾Ý±íÃû
# @change: "2012-12-29 13:30" jiang Ôö¼Ó¶ÁÈ¡±í³É¹¦ÒÔºóµÄ²Ù×÷
# @change: "2013-01-11 11:00" wdb Íò¹úÕ½±¨ÃûÐÞ¸ÄΪÔÚ¿ç·þ·þÎñÆ÷
# @change: "2015-04-02 19:00" hxp Ö§³Ö×Ô¶¨Ò幦ÄÜÀàÐÍÊý¾Ý²Ù×÷
# @change: "2015-04-09 11:00" hxp Ôö¼Ó´úÊÕÖ²ÎïÀëÏßÖ÷È˲éѯ
# @change: "2015-11-13 17:00" hxp Ôö¼ÓÍõÕßÕù°Ô
# @change: "2016-07-25 21:00" hxp ¼ò»¯´úÂë
#------------------------------------------------------------------------------ 
#"""Version = 2016-07-25 21:00"""
#------------------------------------------------------------------------------ 
import GameWorld
import ChConfig
import IPY_GameServer
import ReadChConfig
import base64
import cPickle
 
#[isBase64, gbk]
EncodingList = ReadChConfig.GetEvalChConfig("EncodingTex")
 
#Êý¾Ý¿â²Ù×÷ÃüÁîÁбí
g_dbCommandList = []
g_proccessCmdInfo = [] 
 
#Íæ¼Ò±í
Table_DBPlayer = "tagDBPlayer"
 
#-------------------------------------------------------------------------
 
#//04 04 Êý¾Ý¿â²Ù×÷½á¹û#tagDGDBOperResult
#struct tagDGDBOperResult
#{
#      tagHead     Head;
#       BYTE    Result;             //½á¹û£º1³É¹¦£¬0ʧ°Ü
#       DWORD     ResultSetLen;
#       char     ResultSet[ResultSetLen];    //½á¹û¼¯
#       DWORD     ErrorMsgLen;
#       char     ErrorMsg[ErrorMsgLen];     //´íÎóÐÅÏ¢
# };
## DB·µ»Øµ½GameServerÐÅÏ¢
#  @param None None
#  @return None
def GetDGDBOperResultInfo(index, tick):
    
    ##tagDGGeneralDBOperResult:Type: 116,Result: 1,ResultSetLen: 357,
    #ResultSet: [{u'a': 33, u'c': u'dddd', u'_id': ObjectId('50641259cd84ceddac3e6945')}, 
    #{u'a': 33, u'_id': ObjectId('506412cdcd84ceddac3e6946'), u'b': 33}, 
    #{u'a': 33, u'_id': ObjectId('506412eacd84ceddac3e6948'), u'b': 33}, 
    #{u'a': 33, u'_id': ObjectId('5064136acd84ceddac3e694a'), u'b': u'dfsss'}, 
    #{u'a': 33, u'_id': ObjectId('50641375cd84ceddac3e694b'), u'b': u'dfsss'}],
    #ErrMsgLen: 0,ErrMsg: (null),
    
    dbOperResultInfo = IPY_GameServer.IPY_DGDBOperResult()
    result = dbOperResultInfo.GetResult()  #½á¹û£º1³É¹¦£¬0ʧ°Ü
    
    if not result:
        #ÊÕµ½£¬ÉèÖÿÉÒÔ¼ÌÐø·¢ËÍÃüÁî
        __ResetDBOper()
        GameWorld.ErrLog("²Ù×÷DBʧ°Ü£¡£¡£¡")
        return
    
    resultSet = dbOperResultInfo.GetResultSet()
    
    if not resultSet:
        #ÊÕµ½£¬ÉèÖÿÉÒÔ¼ÌÐø·¢ËÍÃüÁî
        __ResetDBOper()
        return
    
    resultSet = eval(resultSet.replace('ObjectId', 'str'))
    #È¡¿âת±àÂë
    resultSetList = TranslateOutDBList(resultSet)
    
    #´¦Àí±í¶ÁÈ¡³É¹¦×´Ì¬
    __DoLogic_DGDBOperResult(resultSetList)
    
    #ÊÕµ½£¬ÉèÖÿÉÒÔ¼ÌÐø·¢ËÍÃüÁî
    __ResetDBOper()
    return
 
 
## ´¦Àí±í¶ÁÈ¡³É¹¦×´Ì¬
#  @param resultSetList: ²éѯ½á¹û
#  @return: None
def __DoLogic_DGDBOperResult(resultSetList):
    global g_proccessCmdInfo
    
    if not g_proccessCmdInfo:
        return
    
    callFunc, extendValueList = g_proccessCmdInfo
 
    if callFunc:
        return callFunc(resultSetList, extendValueList)
    
    return
 
 
## Ìí¼Ó½ødb²Ù×÷Áбí
#  @param operList [type, ²Ù×÷ÃüÁî×Öµä]
#  @return None
def AddDBCommandList(operList):
    global g_dbCommandList
    
    GameWorld.Log("Ìí¼ÓDBComand operList=%s"%operList)
    
    g_dbCommandList.append(operList)
    return
 
 
## ¶¨Ê±µ÷ÓÃ
#  @param dictInfoList ²åÈëµÄ×ÖµäÁбí
#  @return None
def PyDBProccess(tick):
    global g_dbCommandList
    global g_proccessCmdInfo
    
    #ÁбíûÓÐÐÅÏ¢
    if len(g_dbCommandList) <= 0:
        return
    
    #ÊÇ·ñÍ£Ö¹·¢ËÍÃüÁî
    isStopSendInfo = GameWorld.GetGameWorld().GetDictByKey(ChConfig.Def_PlayerKey_IsStopSendDBCommand)
    
    #ÕýÔڵȴýDB»ØÓ¦
    if isStopSendInfo:
        sendTick = GameWorld.GetGameWorld().GetDictByKey(ChConfig.Def_PlayerKey_SendDBComTick)
        #»¹Î´30Ãë
        if tick - sendTick < ChConfig.TYPE_Tick_Time[ChConfig.TYPE_SendDBCommandTick]: 
            return
        
        #³¬¹ý30ÃëµÈ´ýʱ¼ä
        #¿ÉÒÔ¼ÌÐø·¢ËÍÃüÁî
        __ResetDBOper()
        GameWorld.ErrLog("wait for DB responses overtime")
        return
    #ÉèÖõȴý½á¹ûÔÙ·¢ËÍ
    GameWorld.GetGameWorld().SetDict(ChConfig.Def_PlayerKey_IsStopSendDBCommand, 1)
    
    sendDict, callFunc, extendValueList = g_dbCommandList.pop(0)
    
    #0£º²»·µ»Ø£¬1£ºDB·µ»ØÐÅÏ¢µ½GameServer
    sendMsg = cPickle.dumps(sendDict, 2)
    GameWorld.GetGameWorld().SendDBOper(1, sendMsg, len(sendMsg))
    GameWorld.Log("send dbcommand:%s"%sendDict)
    
    g_proccessCmdInfo = [callFunc, extendValueList]
    
    #ÉèÖ÷¢ËÍÃüÁîµÄʱ¼ä
    GameWorld.GetGameWorld().SetDict(ChConfig.Def_PlayerKey_SendDBComTick, tick)
    
    return
        
 
## ÖØÖÃÊý¾Ý¿â²Ù×÷
#  @param None
#  @return None    
def __ResetDBOper():
    global g_proccessCmdInfo
    g_proccessCmdInfo = []
    GameWorld.GetGameWorld().SetDict(ChConfig.Def_PlayerKey_IsStopSendDBCommand, 0)
    return
 
#operDictÊǸöPython×Öµä
#Ô¤¶¨ÒåµÄKEY:
#collection:±íÃû(±ØÐè)
#oper:²Ù×÷(±ØÐè),Ŀǰ֧³Ödrop/insert/update/remove/find
#docs:Êý¾Ý¼¯×Öµä»ò×ÖµäÁбí(·Ç±ØÐè)
#spec:Ìõ¼þ×Öµä(·Ç±ØÐè)
 
## ²åÈëÊý¾Ý¿â
#  @param collection ±íÃû
#  @param dictInfoList ²åÈëµÄ×ÖµäÁбí
#  @return None
def InsertDBOper(collection, dictInfoList, callFunc=None, extendValueList=[]):
    resultList = TranslateInDBList(dictInfoList)
    
    operDict = {'collection':collection, 'oper':'insert', 'docs':resultList}
    
    operList = [operDict, callFunc, extendValueList]
    AddDBCommandList(operList) 
    return
 
 
## ²éѯÊý¾Ý
#  @param collection ±íÃû
#  @param findDict ²éÕÒµÄ×ÖµäÖµ
#  @param fields ²éѯµÄÖ¸¶¨·µ»Ø×Ö¶Î
#  @param callFunc »Øµ÷º¯Êý
#  @param extendValueList À©Õ¹ÊýÖµÁбí
#  @return None
def FindDBOper(collection, findDict, fields={}, callFunc=None, extendValueList=[]):
    findDict = TranslateInDBDict(findDict)
    
    operDict = {'collection':collection, 'oper':'find', 'spec':findDict, 'fields':fields}
    
    operList = [operDict, callFunc, extendValueList]
    AddDBCommandList(operList) 
    
    return
 
 
## ¸üÐÂÊý¾Ý
#  @param collection ±íÃû
#  @param tagFieldDict ¶¨Î»¼Ç¼
#  @param updateDict ¸üеÄÄÚÈÝ
#  @return None
def UpdateDBOper(collection, tagFieldDict, updateDict, callFunc=None, extendValueList=[]):
    tagFieldDict = TranslateInDBDict(tagFieldDict)
    updateDict = TranslateInDBDict(updateDict)
    
    operDict = {'collection':collection, 'oper':'update', 'spec':tagFieldDict, 'docs':{'$set':updateDict}}
    operList = [operDict, callFunc, extendValueList]
    AddDBCommandList(operList) 
    return
    
 
## É¾³ý¼Ç¼
#  @param collection ±íÃû
#  @param tagFieldDict ¶¨Î»¼Ç¼
#  @return None
def RemoveDBOper(collection, tagFieldDict, callFunc=None, extendValueList=[]):
    tagFieldDict = TranslateInDBDict(tagFieldDict)
    
    operDict = {'collection':collection, 'oper':'remove', 'spec':tagFieldDict}
    operList = [operDict, callFunc, extendValueList]
    AddDBCommandList(operList) 
    return
 
 
## É¾³ý±í
#  @param collection ±íÃû
#  @return None
def DropDBOper(collection, callFunc=None, extendValueList=[]):
    operDict = {'collection':collection, 'oper':'drop'}
    operList = [operDict, callFunc, extendValueList]
    AddDBCommandList(operList)  
    return
 
 
#--------------------------³öÈë¿âתÂë------------------------------------
 
## ´æÈëÊý¾Ý¿âת»¯×Öµä±àÂë
#  @param dictInfoList Ô­×ÖµäÁбí
#  @return None
def TranslateInDBList(dictInfoList):
    resultList = []
    
    for infoDict in dictInfoList:
        resDict = TranslateInDBDict(infoDict)
        resultList.append(resDict)
    
    return resultList
 
 
 
## ´æÈëÊý¾Ý¿âת»¯×Öµä±àÂë
#  @param tagDict Ô­×Öµä
#  @return None
def TranslateInDBDict(tagDict):
    resultDict = {}
    
    for key, value in tagDict.items():
        resultDict[key] = IncomingText(value)
    
    return resultDict
 
 
## È¡³öÊý¾Ý¿âת»¯×Öµä±àÂë
#  @param dictInfoList Ô­×ÖµäÁбí
#  @return None
def TranslateOutDBList(dictInfoList):
    resultList = []
    
    for infoDict in dictInfoList:
        resDict = TranslateOutDBDict(infoDict)
        resultList.append(resDict)
    
    return resultList
 
 
 
## È¡³öÊý¾Ý¿âת»¯×Öµä±àÂë
#  @param tagDict Ô­×Öµä
#  @return None
def TranslateOutDBDict(tagDict):
    resultDict = {}
    
    for key, value in tagDict.items():
        resultDict[key] = OutgoingText(value)
    
    return resultDict
        
        
## È¡³öÊý¾Ý
#  @param text Ô­×Ö·û
#  @return None
def OutgoingText(text):
    if isinstance(text, unicode):
        if EncodingList[0]:
            return base64.b64decode(text)
        return UnicodeToEncoding(EncodingList[1], text)[1]
    #²»ÊÇunicode,²»´¦Àí
    return text
 
 
## Èë¿â
#  @param text Ô­×Ö·û
#  @return None
def IncomingText(text):
    if isinstance(text, str):
        if EncodingList[0]:
            return base64.b64encode(text)
        return EncodingToUnicode(EncodingList[1], text)[1]
    #²»ÊÇ×Ö·û´®£¬²»´¦Àí
    return text
 
 
## ±àÂë
#  @param srcEncoding ±àÂë¸ñʽ
#  @param input ×Ö·û´®
#  @return None
def EncodingToUnicode(srcEncoding, input):
    try:
        result = unicode(input, srcEncoding)    #translate to utf-8
    except:
        GameWorld.Log("##EncodingToUnicode error, input=%s, srcEncoding=%s"%(input, srcEncoding))
        return False, "error"
    return True, result
 
 
## ±àÂë
#  @param srcEncoding ±àÂë¸ñʽ
#  @param input ×Ö·û´®
#  @return None
def UnicodeToEncoding(dstEncoding, input):
    try:
        result = input.encode(dstEncoding)
    except:
        GameWorld.Log("##UnicodeToEncoding error, input=%s, dstEncoding=%s"%(input, dstEncoding))
        return False, "Error"
    return True, result