54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from pymongo import MongoClient
|
||
from bson import ObjectId, json_util
|
||
|
||
# 主环境 (生产环境为production,开发环境为development)
|
||
setting = 'development'
|
||
env = 'coc'
|
||
|
||
# 获取数据集
|
||
|
||
|
||
def col(arg):
|
||
if env == 'coc':
|
||
conn = MongoClient('mongodb://coc:qlSfefSor5@0.0.0.0:12236/coc')
|
||
else:
|
||
conn = MongoClient('mongodb://cherry:fR1jW2xG3bE9@mongo:27017/cherry')
|
||
|
||
if setting == 'development':
|
||
arg += '_test'
|
||
if arg == 'rank':
|
||
return conn[env].rank
|
||
elif arg == 'rank_test':
|
||
return conn[env].rank_test
|
||
else:
|
||
return False
|
||
|
||
# 向排名表里增加或覆写数据
|
||
|
||
|
||
def addRank(nick, count, time):
|
||
try:
|
||
col('rank').update({"cid": cid}, {'$setOnInsert': {"nick": nick}, '$set': {
|
||
"count": count, "time": time}}, {'upsert': 'true'})
|
||
# col('rank').insert_one({"count":count,"time":time,"nick":nick})
|
||
except Exception as e:
|
||
# 失败
|
||
return {'errcode': 401, 'errmsg': '排名表修改失败'}
|
||
return {'errcode': 200, 'errmsg': 'ok'}
|
||
|
||
# 获取排名表所有信息(除了id)
|
||
|
||
|
||
def getRank():
|
||
time_rank = []
|
||
count_rank = []
|
||
try:
|
||
for i in col('rank').find({}, {"_id": 0}).sort([("time", 1), ("count", 1)]).limit(10):
|
||
time_rank.append(i)
|
||
for i in col('rank').find({}, {"_id": 0}).sort([("count", 1), ("time", 1)]).limit(10):
|
||
count_rank.append(i)
|
||
except Exception as e:
|
||
print(e)
|
||
return {'errcode': 411, 'errmsg': '排名表获取失败'}
|
||
return {'errcode': 200, 'time_rank': time_rank, 'count_rank': count_rank, 'errmsg': 'ok'}
|