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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package 
#
# @todo:¶ÁÈ¡PY±í
# @author hxp
# @date 2018-05-10
# @version 1.0
#
# ÏêϸÃèÊö: ¶ÁÈ¡PY±í
#
#-------------------------------------------------------------------------------
#"""Version = 2018-05-10 12:00"""
#-------------------------------------------------------------------------------
 
import FormulaControl
import ChConfig
import LogUI
 
import hashlib
import os
import gc
 
'''±í½á¹¹¶¨Òå×Öµä
{
    ±íÃû:(
        (×Ö¶ÎÀàÐÍ, ×Ö¶ÎÃû, ÊÇ·ñ²éѯË÷Òý), 
        ...
        ),
    ...
}
'''
Def_IpyTable = {
                #<%Table_Def%>
                }
 
#<%Table_Class%>
 
 
def Log(msg, playerID=0, par=0):
    LogUI.Msg("%s\t%s\t%s" % (par, playerID, msg))
    return
    
def ErrLog(msg, playerID=0, par=0):
    LogUI.Msg("%s\t%s\t###Error:%s" % (par, playerID, msg))
    return
    
## PY±íÊý¾Ý¹ÜÀíÀà
class IPY_DataMgr():
    
    def __init__(self):
        Log("IPY_DataMgr Init...")
        self.fileMD5Dict = {} # Êý¾Ý±íÎļþmd5×Öµä, ÓÃÓÚ¶Ô±ÈÎļþ²îÒìÅжÏÊÇ·ñÖØ¶Á {dtName:md5, ...}
        self.ipyConfigEx = {} # ×Ô¶¨ÒåÊý¾Ý»º´æ {key:configData, ...}
        self.ipyDataIndexMap = {} # Êý¾Ý±íË÷Òý²éѯ»º´æ {dtName:{args:[index, ...], ...}, ...}
        self.ipyDataIndexMapEx = {} # Êý¾Ý±í×Ô¶¨Òå²éѯÌõ¼þ²éѯ»º´æ {dtName_(findKey,...):{(findKeyValue, ...):[index, ...], ...}, ...}
        self.ipyFuncConfigDict = {} # ¹¦ÄÜÅäÖÃ±í»º´æ {key:IPY_FuncConfig, ...}
        self.classSizeDict = {} # Êý¾Ý±íÀà¶ÔÏóÕ¼ÓÃÄÚ´æ´óС {dtName:size, ...}
        self.IpyDataClear(True)
        return
    
    def Recycle(self):
        Log("IPY_DataMgr Recycle")
        for tableName in Def_IpyTable.keys():
            if not hasattr(self, "ipy%sLen" % tableName):
                continue
            cacheList = getattr(self, "ipy%sCache" % tableName)
            del cacheList
            delattr(self, "ipy%sLen" % tableName)
            delattr(self, "ipy%sCache" % tableName)
            Log("Recycle IPY_%s" % tableName)
            
        del self.fileMD5Dict, self.ipyConfigEx, self.ipyDataIndexMap, self.ipyDataIndexMapEx, self.ipyFuncConfigDict, self.classSizeDict
        self.fileMD5Dict = {}
        self.ipyConfigEx = {}
        self.ipyDataIndexMap = {}
        self.ipyDataIndexMapEx = {}
        self.ipyFuncConfigDict = {}
        self.classSizeDict = {}
        gc.collect()
        return
    
    def LoadAll(self):
        ## ¼ÓÔØÈ«²¿Êý¾Ý£¬²âÊÔÄÚ´æÓã¬Êµ¼ÊÓ¦ÓÃÖв»µ÷ÓÃ
        for tableName in Def_IpyTable.keys():
            setattr(self, "ipy%sLen" % tableName, 0)
        self.IpyDataClear()
        return
    
    def IpyDataClear(self, onlyCheck=False):
        Log("IPY_DataMgr Reload... onlyCheck=%s" % onlyCheck)
        if not onlyCheck:
            self.ipyConfigEx = {}
        #<%Ipy_Cache_Init%>
        Log("IPY_DataMgr ReloadOK! onlyCheck=%s" % onlyCheck)
        return
    
    def CheckLoadData(self, dtName):
        if hasattr(self, "ipy%sLen" % dtName):
            return
        setattr(self, "ipy%sLen" % dtName, 0) # ÉèÖøÃÊôÐÔ£¬Í¨¹ýÅжÏÊÇ·ñÓиÃÊôÐÔ¾ö¶¨ÊÇ·ñ¼ÓÔØ
        self.__LoadFileData(dtName)
        return
    
    def __LoadFileData(self, tableName, onlyCheck=False):
        # @param onlyCheck: ÊÇ·ñ½ö¼ì²é¸ñʽ£¬Ò»°ãÆô¶¯Ê±ÓÃ
        curPath = "<%LoadStructPath%>" + "\\PySysDB\\tag" + tableName + ".txt"
        if not os.path.isfile(curPath):
            ErrLog("can not find file = %s,%s" % (tableName, curPath))
            raise Exception("can not find file = %s,%s" % (tableName, curPath))
        
        if not onlyCheck:
            if not hasattr(self, "ipy%sLen" % tableName):
                # Ã»ÓиÃÊôÐԵIJ»¼ÓÔØ
                return
        
        fileObj = open(curPath, 'rb')
        content = fileObj.read()
        fileObj.close()
        
        if not onlyCheck:
            md5_obj = hashlib.md5()
            md5_obj.update(content)
            newMD5Code = md5_obj.hexdigest()
            if tableName in self.fileMD5Dict:
                oldMD5Code = self.fileMD5Dict[tableName]
                if newMD5Code == oldMD5Code:
                    return getattr(self, "ipy%sCache" % tableName)
                
                if tableName in self.ipyDataIndexMap:
                    self.ipyDataIndexMap.pop(tableName)
                for dtName_Findkey in self.ipyDataIndexMapEx.keys():
                    findStr = "%s_" % tableName
                    if findStr in dtName_Findkey:
                        self.ipyDataIndexMapEx.pop(dtName_Findkey)
                if tableName == "FuncConfig":
                    self.ipyFuncConfigDict = {}
            self.fileMD5Dict[tableName] = newMD5Code
            
        dataIndex = 0
        indexDict = {}
        cacheList = []
        fieldList = Def_IpyTable[tableName]
        infoList = content.split('\r\n')
        curClassSizeTotal = 0
        dataCount = 0
        Class = eval("IPY_%s" % tableName)
        for line in xrange(len(infoList)):
            if line == 0:
                continue
            if not infoList[line]:
                #ErrLog("line not content! %s, line=%s" % (tableName, line))
                continue
            rowList = infoList[line].split('\t')
            if len(fieldList) != len(rowList):
                ErrLog("field count error!, %s, line=%s, len=%s,rowCount=%s" % (tableName, line, len(fieldList), len(rowList)))
                raise Exception("field count error!, %s, line=%s, len=%s,rowCount=%s" % (tableName, line,len(fieldList), len(rowList)))
            if tableName == "FuncConfig":
                classObj = self.__LoadFuncConfigData(fieldList, rowList, onlyCheck)
                dataCount += 1
                if onlyCheck:
                    continue
                curClassSizeTotal += ChConfig.GetSizeof(classObj)
                continue
            try:
                indexKey = []
                valueList = []
                for j, value in enumerate(rowList):
                    fieldType, fieldName, isIndex = fieldList[j]
                    if fieldType == "char":
                        attrValue = value
                    elif fieldType == "dict":
                        attrValue = self.__StrToDict(value)
                        if type(attrValue) != dict:
                            raise
                    elif fieldType == "list":
                        attrValue = self.__StrToList(value)
                        if type(attrValue) not in [list, tuple]:
                            raise
                    elif fieldType == "eval":
                        attrValue = self.__StrToEval(value)
                    elif fieldType == "float":
                        attrValue = float(value)
                    else:
                        attrValue = 0 if not value.isdigit() else int(value)
                    valueList.append(attrValue)
                    if isIndex:
                        indexKey.append(attrValue)
                dataCount += 1
                if onlyCheck:
                    continue
                classObj = Class()
                setattr(classObj, "attrTuple", tuple(valueList))
                curClassSizeTotal += ChConfig.GetSizeof(classObj)
                cacheList.append(classObj)
                indexKey = tuple(indexKey)
                indexList = indexDict.get(indexKey, [])
                indexList.append(dataIndex)
                indexDict[indexKey] = indexList
                dataIndex += 1
            except BaseException:
                ErrLog("SetIpyDataError: tableName=%s,line=%s,fieldName=%s,fieldType=%s,value=%s" % (tableName, line, fieldName, fieldType, value))
                raise
        if onlyCheck:
            Log("CheckIpydata: %s, dataCount=%s OK" % (tableName, dataCount))
            return
        if tableName != "FuncConfig":
            self.ipyDataIndexMap[tableName] = indexDict
        self.classSizeDict[tableName] = curClassSizeTotal
        classSizeTotal = sum(self.classSizeDict.values())
        alreadyLoad = len(self.classSizeDict)
        setattr(self, "ipy%sCache" % tableName, cacheList)
        setattr(self, "ipy%sLen" % tableName, len(cacheList))
        Log("LoadIpydata: %s, dataCount=%s, alreadyLoad=%s" % (tableName, dataCount, alreadyLoad), curClassSizeTotal, classSizeTotal)
        return
    
    def __LoadFuncConfigData(self, fieldList, rowList, onlyCheck):
        key = rowList[0]
        valueList = [key]
        for i, strValue in enumerate(rowList):
            if i == 0:
                continue
            try:
                strValue = strValue.lstrip().rstrip()
                if strValue.isdigit():
                    configValue = int(strValue)
                elif (strValue.startswith("{") and strValue.endswith("}")) \
                    or (strValue.startswith("[") and strValue.endswith("]")) \
                    or (strValue.startswith("(") and strValue.endswith(")")):
                    configValue = eval(strValue)
                elif "_" in strValue and type(self.__StrToDict(strValue)) == dict:
                    configValue = self.__StrToDict(strValue)
                elif ChConfig.Def_Str_Montant in strValue:
                    configValue = eval("(%s,)" % strValue.replace(ChConfig.Def_Str_Montant, ","))
                elif strValue in ["-", ""]:
                    configValue = ""
                else:
                    configValue = self.__ToFloat(strValue)
            except BaseException:
                ErrLog("SetIpyDataError: tableName=%s,key=%s,i=%s,value=%s" % ("FuncConfig", key, i, strValue))
                raise
            if onlyCheck:
                continue
            valueList.append(configValue)
        if onlyCheck:
            return
        funcConfigObj = IPY_FuncConfig()
        setattr(funcConfigObj, "attrTuple", tuple(valueList))
        self.ipyFuncConfigDict[key] = funcConfigObj
        return funcConfigObj
    
    def __ToFloat(self, strValue):
        try:
            value = float(strValue)
        except:
            return strValue
        return value
    
    def __StrToEval(self, strValue):
        if not strValue or strValue in ["0", "-", ""]:
            return
        return eval(strValue)
    
    def __StrToDict(self, strValue):
        setDict = {}
        if "{" in strValue and "}" in strValue:
            setDict = eval(strValue)
        elif strValue in ["0", "-", ""]:
            pass
        else:
            keyValueList = strValue.split(ChConfig.Def_Str_Montant)
            for keyValue in keyValueList:
                if "_" not in keyValue:
                    continue
                kv = keyValue.split("_")
                if len(kv) != 2:
                    return
                key, value = kv
                if key.isdigit():
                    key = int(key)
                if value.isdigit():
                    value = int(value)
                setDict[key] = value
        return setDict
    
    def __StrToList(self, strValue):
        setList = []
        if ("[" in strValue and "]" in strValue) or ("(" in strValue and ")" in strValue):
            setList = eval(strValue)
        elif strValue in ["0", "-", ""]:
            pass
        elif strValue.isdigit():
            setList = (int(strValue),)
        else:
            for value in strValue.split(ChConfig.Def_Str_Montant):
                if value.isdigit():
                    value = int(value)
                setList.append(value)
            if setList:
                setList = tuple(setList)
        return setList
    
#<%Ipy_Cache_Func%>
 
IPYData = IPY_DataMgr()
def IPY_Data(): return IPYData
 
def GetConfigEx(key):
    '''»ñÈ¡×Ô¶¨Òåkey»º´æÊý¾Ý
    '''
    if key in IPYData.ipyConfigEx:
        return IPYData.ipyConfigEx[key]
    return
def SetConfigEx(key, configData):
    '''ÉèÖÃ×Ô¶¨Òåkey»º´æÊý¾Ý
    ÓÐЩ±íµÄÅäÖÃÄÚÈÝ¿ÉÄÜÔÚʵ¼Ê¹¦ÄÜʹÓÃÖÐÖ±½ÓʹÓñíÊý¾ÝµÄ»°»á±È½ÏÂé·³£¬±ÈÈçÿ´Î¶¼Òª±éÀú»ñȡһЩ±íÊý¾Ý
    Èç¹û¾­¹ýÒ»²ãÊý¾Ýת»»ºóÔÙÀ´Ê¹ÓøÃÊý¾ÝµÄ»°»á¼ò»¯¹¦ÄÜÂß¼­»òÌá¸ßЧÂÊ£¬Ôò¿ÉÒÔͨ¹ýº¯Êý±£´æÒ»Ð©×Ô¶¨ÒåµÄ»º´æÄÚÈÝ£¬·½±ã¹¦ÄÜʹÓÃ
    Ò²¿ÉÒÔÊÊÓÃÓÚÆäËû×Ô¶¨Ò建´æ´æ´¢
    '''
    IPYData.ipyConfigEx[key] = configData
    return configData
 
def GetIpyGameData(dtName, *args):
    '''»ñÈ¡±íÊý¾Ý£¬ÊÊÓÃÓÚÊý¾ÝΨһµÄ£¬·µ»Øµ¥ÌõÊý¾ÝʵÀý
    @param dtName: ±íÃû£¬²»º¬tag
    @param args: ½¨±íʱÉèÖõÄË÷Òý×Ö¶Î˳Ðò¶ÔÓ¦µÄ²éѯֵ
    @return: ¶ÔÓ¦²éѯÌõ¼þµÄ ipyData Êý¾ÝʵÀý£¬Ö»·µ»Øµ¥¸öʵÀý
    @ʹÓÃ˵Ã÷: IpyGameDataPY.GetIpyGameData(±íÃû, Ë÷Òý1²éѯֵ, Ë÷Òý2²éѯֵ, ¡­ )
    '''
    IPYData.CheckLoadData(dtName)
    if dtName not in IPYData.ipyDataIndexMap:
        ErrLog("Can not found ipyData dtName=%s" % (dtName))
        return
    indexDict = IPYData.ipyDataIndexMap[dtName]
    if args not in indexDict:
        ErrLog("Can not found ipyData dtName=%s,indexValue=%s" % (dtName, args))
        return
    indexList = indexDict[args]
    return getattr(IPYData, "ipy%sCache" % dtName)[indexList[0]]
 
def GetIpyGameDataList(dtName, *args):
    '''»ñÈ¡±íÊý¾Ý£¬ÊÊÓÃÓÚ²éѯ½á¹ûÓжàÌõÊý¾ÝµÄ
    @param dtName: ±íÃû£¬²»º¬tag
    @param args: ½¨±íʱÉèÖõÄË÷Òý×Ö¶Î˳Ðò¶ÔÓ¦µÄ²éѯֵ
    @return: ¶ÔÓ¦²éѯÌõ¼þµÄ ipyData Êý¾ÝʵÀýÁбí
    @ʹÓÃ˵Ã÷: Óë GetIpyGameData º¯ÊýÏàͬ
    '''
    IPYData.CheckLoadData(dtName)
    if dtName not in IPYData.ipyDataIndexMap:
        ErrLog("Can not found ipyDataList dtName=%s" % (dtName))
        return
    indexDict = IPYData.ipyDataIndexMap[dtName]
    if args not in indexDict:
        ErrLog("Can not found ipyDataList dtName=%s,indexValue=%s" % (dtName, args))
        return
    indexList = indexDict[args]
    dataCache = getattr(IPYData, "ipy%sCache" % dtName)
    return [dataCache[i] for i in indexList]
 
def GetIpyGameDataNotLog(dtName, *args):
    '''Óë GetIpyGameData º¯ÊýÏàͬ, Ö»ÊÇÕÒ²»µ½Êý¾Ýʱ²»»áÊä³öÈÕÖ¾
    '''
    IPYData.CheckLoadData(dtName)
    if dtName not in IPYData.ipyDataIndexMap:
        #ErrLog("Can not found ipyData dtName=%s" % (dtName))
        return
    indexDict = IPYData.ipyDataIndexMap[dtName]
    if args not in indexDict:
        #ErrLog("Can not found ipyData dtName=%s,indexValue=%s" % (dtName, args))
        return
    indexList = indexDict[args]
    return getattr(IPYData, "ipy%sCache" % dtName)[indexList[0]]
 
def GetIpyGameDataListNotLog(dtName, *args):
    '''Óë GetIpyGameDataList º¯ÊýÏàͬ, Ö»ÊÇÕÒ²»µ½Êý¾Ýʱ²»»áÊä³öÈÕÖ¾
    '''
    IPYData.CheckLoadData(dtName)
    if dtName not in IPYData.ipyDataIndexMap:
        #ErrLog("Can not found ipyDataList dtName=%s" % (dtName))
        return
    indexDict = IPYData.ipyDataIndexMap[dtName]
    if args not in indexDict:
        #ErrLog("Can not found ipyDataList dtName=%s,indexValue=%s" % (dtName, args))
        return
    indexList = indexDict[args]
    dataCache = getattr(IPYData, "ipy%sCache" % dtName)
    return [dataCache[i] for i in indexList]
 
def GetIpyGameDataByCondition(dtName, keyDict={}, returnList=False, isLogNone=True):
    '''¸ù¾Ý×Ô¶¨Òå²éѯÌõ¼þ²éѯ±íÊý¾Ý£¬ÓÉÓÚĿǰֻ֧³Ö½¨Á¢Ò»×é²éѯË÷Òý£¬ËùÒÔʹÓÃÆäËû²éѯÌõ¼þ²é±íʱֻÄÜͨ¹ý¸Ãº¯Êý²éÕÒ
    @param dtName: ±íÃû£¬²»º¬tag
    @param keyDict: ²éѯÌõ¼þ×Öµä {²éѯ×Ö¶ÎÃû:²éѯֵ, ...}
    @param returnList: ÊÇ·ñÒÔÁбíµÄÐÎʽ·µ»Ø²éѯÊý¾Ý£¬Ä¬ÈÏ·ñ
    @param isLogNone: ÕÒ²»µ½Êý¾ÝʱÊÇ·ñÊý¾ÝÈÕÖ¾£¬Ä¬ÈÏÊÇ
    @return: ÕÒ²»µ½Êý¾Ýʱ·µ»Ø None£¬ÓÐÊý¾Ýʱ¸ù¾Ý²ÎÊýÊÇ·ñ·µ»ØÁÐ±í·µ»Ø¶ÔÓ¦µÄÊý¾ÝʵÀý»òÊý¾ÝʵÀýÁбí
    '''
    IPYData.CheckLoadData(dtName)
    fieldList = keyDict.keys()
    valueList = keyDict.values()
    findFieldKey = "%s_%s" % (dtName, fieldList)
    findValueKey = tuple(valueList)
    
    cacheList = getattr(IPYData, "ipy%sCache" % dtName)
    if findFieldKey not in IPYData.ipyDataIndexMapEx:
        indexMapDict = {}
        for index, iData in enumerate(cacheList):
            valuekey = tuple([getattr(iData, "Get%s" % field)() for field in fieldList])
            indexList = indexMapDict.get(valuekey, [])
            indexList.append(index)
            indexMapDict[valuekey] = indexList        
        IPYData.ipyDataIndexMapEx[findFieldKey] = indexMapDict
        
    indexMapDict = IPYData.ipyDataIndexMapEx[findFieldKey]
    if findValueKey not in indexMapDict:
        if isLogNone:
            ErrLog("GetIpyGameDataByCondition can not found data! %s %s" % (dtName, keyDict))
        return
    indexList = indexMapDict[findValueKey]
    if not returnList:
        return cacheList[indexList[0]]
    return [cacheList[index] for index in indexList]
 
def GetFuncCfgIpyData(key):
    '''¶Á¹¦ÄÜÅäÖñíÅäÖÃʵÀý
    @param key: ÅäÖÃkey
    @return: Ö±½Ó·µ»Ø¸ÃÅäÖÃkey¶ÔÓ¦µÄÅäÖÃipyDataʵÀý
    '''
    IPYData.CheckLoadData("FuncConfig")
    if key not in IPYData.ipyFuncConfigDict:
        ErrLog("Can not found ipyData FuncConfig key=%s!" % key)
        return ""
    return IPYData.ipyFuncConfigDict[key]
 
def GetFuncCfg(key, index=1):
    '''¶Á¹¦ÄÜÅäÖñíÅäÖÃרÓú¯Êý
    @param key: ÅäÖÃkey
    @param index: µÚ¼¸¸öÅäÖÃÖµ£¬Ö§³Ö1~5
    @return: Ö±½Ó·µ»Ø¶ÔÓ¦µÄÊý¾ÝÀàÐÍ int¡¢str£¬²»ÓÃÔÙÊÖ¶¯×ªint
    '''
    IPYData.CheckLoadData("FuncConfig")
    if key not in IPYData.ipyFuncConfigDict:
        ErrLog("Can not found ipyData FuncConfig key=%s!" % key)
        return ""
    cfgObj = IPYData.ipyFuncConfigDict[key]
    if index == 1:
        return cfgObj.attrTuple[1]
    if index == 2:
        return cfgObj.attrTuple[2]
    if index == 3:
        return cfgObj.attrTuple[3]
    if index == 4:
        return cfgObj.attrTuple[4]
    if index == 5:
        return cfgObj.attrTuple[5]
    ErrLog("Can not found ipyData FuncConfig key=%s,index=%s!" % (key, index))
    return ""
 
def GetFuncEvalCfg(key, index=1, defaultValue=[]):
    '''¶ÁÈ¡¹¦ÄÜÅäÖñíÅäÖÃÁÐ±í¡¢×Öµä¸ñʽרÓú¯Êý
    @param key: ÅäÖÃkey
    @param index: µÚ¼¸¸öÅäÖÃÖµ£¬Ö§³Ö1~5
    @return: Ö±½Ó·µ»Ø¶ÔÓ¦µÄÊý¾ÝÀàÐÍ list¡¢dict¡¢tuple£¬²»ÓÃÔÙeval
    
    ÓÉÓڲ߻®ÓÐ×Ô¶¨ÒåµÄÁбí½á¹¹ obj|¡­ , µ±¸ÃÁбíÅäÖÃÖ»ÓÐÒ»¸öÔªËØÊ±£¬´ËʱÅäÖõÄÄÚÈÝΪµ¥¸öÊýÖµ£¬¼ÓÔØµÄÅäÖõÄʱºò´ËÌõÊý¾Ý»á±»×ªÎªintÐÍ
    ¹ÊʹÓøÃרÓú¯Êý·µ»ØÁбí½á¹¹£¬·½±ã¹¦ÄÜ¿ª·¢Ê±²»ÓÃÔÙ¿¼ÂÇÁбíΪintʱµÄÇé¿ö£»
    µ±È»Èç¹ûÅäÖõÄÄÚÈݱ¾Éí¾ÍΪpythonµÄÁÐ±í¡¢×Öµä½á¹¹µÄ»°¿ÉʹÓÃÉÏÃæµÄº¯Êý
    ²»¹ýΪÁËͳһ£¬½¨Ò鹦ÄÜÅäÖñí¶ÁÁÐ±í¡¢×Öµäʱ¶¼Ê¹Óøú¯Êý
    '''
    IPYData.CheckLoadData("FuncConfig")
    if key not in IPYData.ipyFuncConfigDict:
        ErrLog("Can not found ipyData FuncConfig key=%s!" % key)
        return defaultValue
    cfgObj = IPYData.ipyFuncConfigDict[key]
    if index == 1:
        curConfig = cfgObj.attrTuple[1]
    elif index == 2:
        curConfig = cfgObj.attrTuple[2]
    elif index == 3:
        curConfig = cfgObj.attrTuple[3]
    elif index == 4:
        curConfig = cfgObj.attrTuple[4]
    elif index == 5:
        curConfig = cfgObj.attrTuple[5]
    else:
        ErrLog("Can not found ipyData FuncConfig key=%s,index=%s!" % (key, index))
        return defaultValue
    curType = type(curConfig)
    if curType in [list, tuple, dict]:
        return curConfig
    if curType == int:
        return [curConfig]
    return defaultValue
 
def GetFuncCompileCfg(key, index=1):
    '''»ñÈ¡¹¦ÄÜÅäÖñíÒѱàÒë¹ýµÄ¹«Ê½
    @param key: ÅäÖÃkey
    @param index: µÚ¼¸¸öÅäÖÃÖµ£¬Ö§³Ö1~5
    @return: ·µ»ØÒѱàÒë¹ýµÄ¹«Ê½
    '''
    return FormulaControl.GetCompileFormula(key + str(index), str(GetFuncCfg(key, index)))
 
def InterpolationSearch(dtName, keyName, keyValue, conditionDict={}):
    '''²éѯÌõ¼þÏÂÓë¶ÔÓ¦²éѯ×ֶβο¼ÖµÏà½üµÄÊý¾ÝʵÀý£»²Î¿¼ÖµÐ¡ÓÚÅäÖñí×îСֵʱ·µ»Ønone£¬´óÓÚ×î´óֵʱ·µ»Ø×î´óÖµ¶ÔÓ¦µÄʵÀý
    @param dtName: ±íÃû£¬²»º¬tag
    @param keyName: ²Î¿¼×Ö¶ÎÃû
    @param keyValue: ²Î¿¼×Ö¶ÎÖµ£¬´óÓÚµÈÓÚ×Ö¶Îֵʱ·µ»Ø¶ÔÓ¦Êý¾Ý
    @param conditionDict: ²éѯÌõ¼þ£¬{²éѯ×Ö¶ÎÃû:×Ö¶ÎÖµ, ...}
    @return: ÕÒ²»µ½Êý¾Ý·µ»Ø None £¬ ·ñÔò·µ»Ø¶ÔÓ¦µÄ ipyData Êý¾ÝʵÀý
    '''
    IPYData.CheckLoadData(dtName)
    if not conditionDict:
        dataList = getattr(IPYData, "ipy%sCache" % dtName)
    else:
        dataList = GetIpyGameDataByCondition(dtName, conditionDict, True)
    if not dataList:
        return
    
    low = 0
    lowData = dataList[0]
    lowValue = getattr(lowData, "Get%s" % keyName)()
    if keyValue < lowValue:
        return
    
    high = len(dataList) - 1
    highData = dataList[high]
    highValue = getattr(highData, "Get%s" % keyName)()
    if keyValue >= highValue:
        return highData
    
    near = low + int((high - low) * (keyValue - lowValue)/(highValue - lowValue))
    
    nearData = dataList[near]
    nearValue = getattr(nearData, "Get%s" % keyName)()
    
    if keyValue < nearValue:
        for i in xrange(near - 1, low - 1, -1):
            nearData = dataList[i]
            nearValue = getattr(nearData, "Get%s" % keyName)()
            if nearValue <= keyValue:
                return nearData
            
    elif keyValue > nearValue:
        for i in xrange(near + 1, high + 1):
            nearData = dataList[i]
            nearValue = getattr(nearData, "Get%s" % keyName)()
            if nearValue > keyValue:
                return dataList[i - 1]
            
    return nearData
 
#if __name__ == "__main__":
#    IPYDataTotalSize = ChConfig.GetSizeof(IPYData)
#    classSizeSum = sum(IPYData.classSizeDict.values())
#    Log("Êý¾ÝÀàÄÚ´æ: %s b = %s M" % (classSizeSum, str(classSizeSum/float(1024*1024))))
#    Log("×ÜÕ¼ÓÃÄÚ´æ: %s b = %s M" % (IPYDataTotalSize, str(IPYDataTotalSize/float(1024*1024))))
#    for key in ["OpenBagItem", "RuneUnlock", "IceLodeNeedPoint", "RealmDifficulty", "TongTianLing", "TongTianLingaaa"]:
#        for i in range(1, 6):
#            v = GetFuncCfg(key, i)
#            Log("key:%s,i=%s,value=%s\t%s" % (key, i, type(v), str(v)))
#            
#    ipyData = GetIpyGameData("FlashGiftbag", 303)
#    giftbagIpyDataList = GetIpyGameDataByCondition("FlashGiftbag", {"GiftbagType":1002}, True, True)
#    for ipyData in giftbagIpyDataList:
#        print 
#        print type(ipyData.GetGiftbagID()), ipyData.GetGiftbagID()
#        print type(ipyData.GetGiftbagType()), ipyData.GetGiftbagType()
#        print type(ipyData.GetOriginalRMB()), ipyData.GetOriginalRMB()
#        print type(ipyData.GetBuyCountLimit()), ipyData.GetBuyCountLimit()
#        print type(ipyData.GetGiftItemList()), ipyData.GetGiftItemList()
#        print type(ipyData.GetMainItemID()), ipyData.GetMainItemID()
#        print type(ipyData.GetNotifyKey()), ipyData.GetNotifyKey()
#        
#    collNPCIpyDataList = GetIpyGameDataListNotLog("MapRefreshNPC", 32050)
#    if collNPCIpyDataList:
#        npcIDList = []
#        for ipyData in collNPCIpyDataList:
#            print 
#            print type(ipyData.GetMapID()), ipyData.GetMapID()
#            print type(ipyData.GetNPCIDList()), ipyData.GetNPCIDList()
#            print type(ipyData.GetRefreshMarkList()), ipyData.GetRefreshMarkList()
#            npcIDList += ipyData.GetNPCIDList()
#        print "npcIDList", npcIDList
#    else:
#        print "None"
#        
#    ipyData = GetIpyGameData("RuneCompound", 4415)
#    if ipyData:
#        print 
#        print type(ipyData.GetTagItemID()), ipyData.GetTagItemID()
#        print type(ipyData.GetNeedItem()), ipyData.GetNeedItem()
#        print type(ipyData.GetNeedMJ()), ipyData.GetNeedMJ()
#    else:
#        print "None"
#        
#    ipyData = GetIpyGameData("DailyGiftbag", 703, 105)
#    if ipyData:
#        print 
#        print type(ipyData.GetGiftbagType()), ipyData.GetGiftbagType()
#        print type(ipyData.GetGiftbagID()), ipyData.GetGiftbagID()
#        print type(ipyData.GetBuyCountLimit()), ipyData.GetBuyCountLimit()
#        print type(ipyData.GetGiftItemList()), ipyData.GetGiftItemList()
#    else:
#        print "None"