41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from pymongo import MongoClient
|
||
from bson import ObjectId, json_util
|
||
|
||
# 主环境 (生产环境为production,开发环境为development)
|
||
setting = 'development'
|
||
|
||
# 获取数据集
|
||
def col(arg):
|
||
conn = MongoClient('mongodb://coc:qlSfefSor5@mongo:27017/coc')
|
||
if setting == 'development':
|
||
arg += '_test'
|
||
elif arg == 'rank':
|
||
return conn.coc.rank
|
||
elif arg == 'rank_test':
|
||
return conn.coc.rank_test
|
||
else:
|
||
return False
|
||
|
||
# 向排名表里增加或覆写数据
|
||
def addRank(nick, count, time):
|
||
try:
|
||
# col('rank').update({"cid":cid}, {'$setOnInsert':{"nick":nick}, '$set':{"grade":grade,"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'} |