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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
 
import ConfigParser
import pymongo
import os
import base64
 
BASE64_ENCODE_CNT = 3
XOR_KEY = 151
def GetEncodePsw(psw):
    ret = ""
    try:
        for i in range(BASE64_ENCODE_CNT):
            psw = base64.decodestring(psw)
    except:
#        msg = error.formatMsg('error', error.ERROR_NO_40, '%s'%traceback.format_exc())
#        mylog.error(msg)   
        return False, ret
    
    for i in psw:
        ret += chr(ord(i) ^ XOR_KEY)
    return True, ret
 
class DBOper():
    
    def __init__(self, serverPath):
        dbIniCfg = ConfigParser.ConfigParser()
        dbIni = os.path.join(serverPath, "db\PyMongoDataServer\PyMongoDataServer.ini")
        dbIniCfg.read(dbIni)
        
        dbAuthCfg = ConfigParser.ConfigParser()
        dbAuthIni = os.path.join(serverPath, "db\PyMongoDataServer\Config\config.ini")
        dbAuthCfg.read(dbAuthIni)
        
        self.authOK = False
        encodeOK, authPwd = GetEncodePsw(dbAuthCfg.get("auth", "userdb_pwd"))
        if not encodeOK:
            return
        
        authUser = dbAuthCfg.get("auth", "userdb_user")
        self.con = pymongo.Connection(dbIniCfg.get("connect", "USER_DB_IP"), 27017)
        self.authOK = self.con.admin.authenticate(authUser, authPwd)
        self.db = self.con[dbIniCfg.get("connect", "USER_DB_NAME")]
        return
    
    def findPlayerInfoByAccID(self, accIDList, fields={"AccID":1, "PlayerID":1, "PlayerName":1}):
        ''' 根据账号查询玩家信息,支持多账号
        @param accIDList: 要查询的账号列表
        '''
        if not self.authOK:
            return
        col = self.db["tagDBPlayer"]
        spec = {"AccID":{"$in":accIDList}}
        fields['_id'] = 0
        ret = col.find(spec, fields)
#        ret.count()
#        for info in ret:
#            playerID = info['PlayerID']
        return ret
    
    def findPlayerInfoByName(self, playerNameList, fields={"AccID":1, "PlayerID":1, "PlayerName":1}):
        ''' 根据玩家名查询玩家信息,支持多玩家
        @param playerNameList: 要查询的玩家名列表
        '''
        if not self.authOK:
            return
        col = self.db["tagDBPlayer"]
        spec = {"PlayerName":{"$in":playerNameList}}
        fields['_id'] = 0
        return col.find(spec, fields)
    
    def close(self):
        if self.con:
            self.con.disconnect()
        return