1
This commit is contained in:
parent
27975d1ae4
commit
0e548b4f07
2
third_party/f7
vendored
2
third_party/f7
vendored
@ -1 +1 @@
|
||||
Subproject commit 8b29369ca65d5d474a78085c3e5f93be6be9376a
|
||||
Subproject commit 07f321e38325d429b404331b027fc9da25587469
|
2
third_party/q7
vendored
2
third_party/q7
vendored
@ -1 +1 @@
|
||||
Subproject commit b4ea03b447e075ecfeefa1b22d0a0497ad80c0eb
|
||||
Subproject commit 4af43d56490d566c38021d9687975dae17d9e7ad
|
@ -19,19 +19,10 @@ import functools
|
||||
|
||||
CONFIG_DIR = '../config' if f7.isOnlineEnv() else '/var/data/conf_test/game2003api_rankserver/config'
|
||||
|
||||
def take_pass(elem):
|
||||
def _take_pass(elem):
|
||||
return elem[3]
|
||||
|
||||
def take_coin_num(elem):
|
||||
return elem[4]
|
||||
|
||||
def safeDiv(a, b):
|
||||
if b == 0:
|
||||
return 0
|
||||
else:
|
||||
return a / b
|
||||
|
||||
def getRedis():
|
||||
def _getRedis():
|
||||
redis_conf = json.loads(open(CONFIG_DIR + '/rankserver.redis.cluster.json', 'r').read())
|
||||
for conf in redis_conf:
|
||||
r = redis.Redis(host = conf['host'],
|
||||
@ -46,7 +37,7 @@ def getRedisConf():
|
||||
return redis_conf;
|
||||
|
||||
#数据去重
|
||||
def delRepeatData(row, data_list):
|
||||
def _delRepeatData(row, data_list):
|
||||
temp_list = []
|
||||
for data in data_list:
|
||||
if data[0] == row[0]:
|
||||
@ -55,21 +46,21 @@ def delRepeatData(row, data_list):
|
||||
data_list.remove(temp_data)
|
||||
|
||||
#刷新通关数据
|
||||
def refreshData(row, pass_list):
|
||||
def _refreshData(row, pass_list):
|
||||
pass_list.append((row[0], row[1].decode('utf-8'), row[2], row[3], row[4]))
|
||||
pass_list.sort(key=take_pass, reverse=True)
|
||||
pass_list.sort(key=_take_pass, reverse=True)
|
||||
if (len(pass_list) > 50):
|
||||
del pass_list[50:]
|
||||
|
||||
#更新排行榜
|
||||
def updateRank(r, channel, pass_list):
|
||||
pass_list.sort(key=take_pass, reverse=True)
|
||||
def _updateRank(r, channel, pass_list):
|
||||
pass_list.sort(key=_take_pass, reverse=True)
|
||||
pass_rank = []
|
||||
for pass_index in range(min(50, len(pass_list))):
|
||||
pass_rank.append(pass_list[pass_index])
|
||||
r.set("game2003api:pass_rank_" + channel, json.dumps(pass_rank))
|
||||
|
||||
def internalDayReadMysqlData():
|
||||
def dayReadMysqlData():
|
||||
mysql_conf = json.loads(open(CONFIG_DIR + '/rankserver.mysql.cluster.json', 'r').read())
|
||||
rank_hash = {}
|
||||
for conf in mysql_conf:
|
||||
@ -93,25 +84,25 @@ def internalDayReadMysqlData():
|
||||
channel = f7.getChannelByAccountId(row[0])
|
||||
if channel not in rank_hash:
|
||||
rank_hash[channel] = []
|
||||
refreshData(row, rank_hash[channel])
|
||||
_refreshData(row, rank_hash[channel])
|
||||
last_idx = max(row[5], last_idx)
|
||||
time.sleep(0.001);
|
||||
if not has_data:
|
||||
break
|
||||
r = getRedis()
|
||||
r = _getRedis()
|
||||
for channel in rank_hash:
|
||||
updateRank(r, channel, rank_hash[channel])
|
||||
_updateRank(r, channel, rank_hash[channel])
|
||||
|
||||
#每日定时读取mysql里的数据生成排行榜写入redis后php读取redis返回客户端显示
|
||||
def dayReadMysqlData(rushtime):
|
||||
internalDayReadMysqlData()
|
||||
def _dayReadMysqlData(rushtime):
|
||||
dayReadMysqlData()
|
||||
f7.timer.callAt(q7.getDaySeconds(time.time(), 1) + rushtime,
|
||||
lambda : dayReadMysqlData(rushtime))
|
||||
lambda : _dayReadMysqlData(rushtime))
|
||||
|
||||
#每5分钟读取mysql里发生改变过的数据更新排行榜
|
||||
def readMysqlData(rushtime):
|
||||
def _readMysqlData(rushtime):
|
||||
mysql_conf = json.loads(open(CONFIG_DIR + '/rankserver.mysql.cluster.json', 'r').read())
|
||||
r = getRedis()
|
||||
r = _getRedis()
|
||||
rank_hash = {}
|
||||
for conf in mysql_conf:
|
||||
conn = pymysql.connect(host = conf['host'],
|
||||
@ -135,8 +126,8 @@ def readMysqlData(rushtime):
|
||||
rank_list = r.get('game2003api:pass_rank_' + channel)
|
||||
rank_hash[channel] = [] if not rank_list else json.loads(rank_list)
|
||||
#更新通关榜
|
||||
delRepeatData(row, rank_hash[channel])
|
||||
refreshData(row, rank_hash[channel])
|
||||
_delRepeatData(row, rank_hash[channel])
|
||||
_refreshData(row, rank_hash[channel])
|
||||
temp_idx = int(row[5])
|
||||
if (temp_idx > last_idx) :
|
||||
last_idx = int(row[5])
|
||||
@ -145,10 +136,10 @@ def readMysqlData(rushtime):
|
||||
break
|
||||
|
||||
for channel in rank_hash:
|
||||
updateRank(r, channel, rank_hash[channel])
|
||||
_updateRank(r, channel, rank_hash[channel])
|
||||
|
||||
f7.timer.callLater(rushtime,
|
||||
lambda : readMysqlData(rushtime)
|
||||
lambda : _readMysqlData(rushtime)
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -157,15 +148,13 @@ if __name__ == "__main__":
|
||||
|
||||
conf = json.loads(open(CONFIG_DIR + '/rankserver.json', 'r').read())
|
||||
|
||||
app = make_app()
|
||||
app.listen(conf['listen_port'])
|
||||
conf['rushtime'] = 300
|
||||
f7.timer.callLater(conf['rushtime'],
|
||||
lambda : readMysqlData(conf['rushtime']))
|
||||
lambda : _readMysqlData(conf['rushtime']))
|
||||
|
||||
conf['day_rushtime'] = 5 * 3600
|
||||
f7.timer.callAt(q7.getDaySeconds(time.time(), 1) + conf['day_rushtime'],
|
||||
lambda : dayReadMysqlData(conf['day_rushtime']))
|
||||
lambda : _dayReadMysqlData(conf['day_rushtime']))
|
||||
|
||||
f7.app.listen(conf['listen_port'])
|
||||
f7.app.start()
|
||||
|
@ -9,20 +9,15 @@ import f7
|
||||
import pymysql
|
||||
import hashlib
|
||||
import json
|
||||
import urllib.request
|
||||
import base64
|
||||
import tornado.ioloop
|
||||
import tornado.web
|
||||
import time
|
||||
import datetime
|
||||
import redis
|
||||
import os
|
||||
import functools
|
||||
|
||||
import game2003rank
|
||||
|
||||
def _updateRank_cmd(debug_info):
|
||||
game2003rank.internalDayReadMysqlData()
|
||||
game2003rank.dayReadMysqlData()
|
||||
|
||||
def _clearRank_cmd(debug_info):
|
||||
for conf in game2003rank.getRedisConf():
|
||||
@ -31,10 +26,9 @@ def _clearRank_cmd(debug_info):
|
||||
password = conf['passwd'],
|
||||
charset = 'utf8'
|
||||
)
|
||||
pass_list = []
|
||||
scan_keys = f7.scanRedisKey(r, "game2003api:pass_rank_*")
|
||||
for key in scan_keys :
|
||||
r.set(key, json.dumps(pass_list))
|
||||
r.delete(key)
|
||||
|
||||
def processCmdLine(cmd):
|
||||
cmd_hash = {
|
||||
@ -63,6 +57,8 @@ if __name__ == "__main__":
|
||||
if len(sys.argv) <= 1:
|
||||
pass
|
||||
else:
|
||||
q7.xPrint('pid:' + str(os.getpid()))
|
||||
f7.app.init('/data/logs/game2003_rankserver_cmd/logs')
|
||||
f7.udplog.info('game2003_rankserver_cmd start pid:' + str(os.getpid()))
|
||||
processCmdLine(sys.argv[1])
|
||||
f7.app.start()
|
||||
|
@ -44,8 +44,6 @@ class RankController{
|
||||
$user_list = array();
|
||||
$pass_list = array();
|
||||
$pass_rank = 0;
|
||||
$coin_list = array();
|
||||
$coin_rank = 0;
|
||||
$myname = '';
|
||||
$myavatar_url = '';
|
||||
//个人信息
|
||||
@ -83,7 +81,9 @@ class RankController{
|
||||
|
||||
//通关榜
|
||||
$r = $this->getRedis();
|
||||
$pass_rank_db = $r->get("game2003api:pass_rank");
|
||||
$channel = phpcommon\extractChannel($account_id);
|
||||
$pass_rank_db = $r->get("game2003api:pass_rank_" . $channel);
|
||||
error_log(json_decode($pass_rank_db));
|
||||
$pass_db = json_decode($pass_rank_db);
|
||||
$pass_list = $this->getRank($account_id, $pass_db, $myname, $myavatar_url);
|
||||
$i = 0;
|
||||
@ -98,30 +98,12 @@ class RankController{
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
//财富榜
|
||||
$coin_rank_db = $r->get("game2003api:coin_rank");
|
||||
$coin_db = urldecode($coin_rank_db);
|
||||
$i = 0;
|
||||
foreach ($coin_db as $coin) {
|
||||
$name = '';
|
||||
$avatar_url = '';
|
||||
if ($i > 49) {
|
||||
break;
|
||||
}
|
||||
if ($coin_db[$i][0] == $account_id) {
|
||||
$coin_rank = $i + 1;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
$coin_list = $this->getRank($account_id, $coin_db, $myname, $myavatar_url);
|
||||
echo json_encode(array(
|
||||
'errcode' => 0,
|
||||
'errmsg' => "",
|
||||
'user_list' => $user_list,
|
||||
'pass_rank' => $pass_rank,
|
||||
'pass_list' => $pass_list,
|
||||
'coin_rank' => $coin_rank,
|
||||
'coin_list' => $coin_list,
|
||||
));
|
||||
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ class RoleController{
|
||||
$avatar_url = $_REQUEST['avatar_url'];
|
||||
//登录校验()
|
||||
$login = loginVerify($account_id, $_REQUEST['session_id']);
|
||||
if (!$login) {
|
||||
if (!$login) {
|
||||
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
|
||||
return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user