#!/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