1
This commit is contained in:
parent
eac13f84e8
commit
8ed631e80d
@ -10,17 +10,48 @@ import tornado.ioloop
|
||||
import tornado.web
|
||||
import time
|
||||
import datetime
|
||||
import redis
|
||||
|
||||
#定时读取mysql里的数据生成排行榜写入redis后php读取redis返回客户端显示
|
||||
|
||||
def info(msg):
|
||||
print(str(datetime.datetime.now()) + '[INFO] ' + msg)
|
||||
|
||||
def updateRedis():
|
||||
pass
|
||||
def take_kills(elem):
|
||||
return elem[3]
|
||||
|
||||
def readMysqlData():
|
||||
def take_alive_time(elem):
|
||||
return elem[4]
|
||||
|
||||
def take_harms(elem):
|
||||
return elem[5]
|
||||
|
||||
def take_win_times(elem):
|
||||
return elem[6]
|
||||
|
||||
def take_game_times(elem):
|
||||
return elem[7]
|
||||
|
||||
def safeDiv(a, b):
|
||||
if b == 0:
|
||||
return 0
|
||||
else:
|
||||
return a / b
|
||||
|
||||
def getRedis():
|
||||
redis_conf = json.loadsmysql_conf = json.loads(open('/var/data/conf_test/game2001api_rankserver/config/rankserver.redis.cluster.json', 'r').read())
|
||||
for conf in redis_conf:
|
||||
r = redis.Redis(host = conf['host'],
|
||||
port = conf['port'],
|
||||
password = conf['passwd'],
|
||||
charset = 'utf8'
|
||||
)
|
||||
return r;
|
||||
|
||||
|
||||
def readMysqlData(rushtime):
|
||||
mysql_conf = json.loads(open('/var/data/conf_test/game2001api_rankserver/config/rankserver.mysql.cluster.json', 'r').read())
|
||||
array = []
|
||||
for conf in mysql_conf:
|
||||
conn = pymysql.connect(host = conf['host'],
|
||||
port = conf['port'],
|
||||
@ -30,9 +61,40 @@ def readMysqlData():
|
||||
charset = 'utf8'
|
||||
)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT accountid, user_name FROM user')
|
||||
cursor.execute('SELECT accountid, user_name, avatar_url, kills, alive_time, harm, win_times, game_times FROM user;')
|
||||
for row in cursor:
|
||||
print(row[0])
|
||||
kill = safeDiv(row[3], row[7])
|
||||
alive_time = safeDiv(row[4], row[7])
|
||||
harm = safeDiv(row[5], row[7])
|
||||
win_times = safeDiv(row[6], row[7])
|
||||
array.append((row[0], row[1], row[2], kill, alive_time, harm, win_times, row[6]))
|
||||
|
||||
r = getRedis()
|
||||
array.sort(key=take_kills, reverse=True)
|
||||
kill_rank = json.dumps(array)
|
||||
r.set("game2001api: kill_rank", kill_rank)
|
||||
|
||||
array.sort(key=take_alive_time, reverse=True)
|
||||
alive_rank = json.dumps(array)
|
||||
r.set("game2001api: alive_rank", alive_rank)
|
||||
|
||||
array.sort(key=take_harms, reverse=True)
|
||||
harm_rank = json.dumps(array)
|
||||
r.set("game2001api: harm_rank", harm_rank)
|
||||
|
||||
array.sort(key=take_win_times, reverse=True)
|
||||
rate_rank = json.dumps(array)
|
||||
r.set("game2001api: rate_rank", rate_rank)
|
||||
|
||||
array.sort(key=take_game_times, reverse=True)
|
||||
|
||||
win_rank = json.dumps(array)
|
||||
r.set("game2001api: win_rank", win_rank)
|
||||
|
||||
tornado.ioloop.IOLoop.current().call_later(rushtime,
|
||||
lambda : readMysqlData(rushtime)
|
||||
)
|
||||
|
||||
|
||||
class SelfCheckingHandler(tornado.web.RequestHandler):
|
||||
|
||||
@ -50,9 +112,13 @@ def make_app():
|
||||
])
|
||||
|
||||
if __name__ == "__main__":
|
||||
readMysqlData()
|
||||
conf = json.loads(open('/var/data/conf_test/game2001api_rankserver/config/rankserver.json', 'r').read())
|
||||
|
||||
app = make_app()
|
||||
app.listen(conf['listen_port'])
|
||||
conf['rushtime'] = 300
|
||||
|
||||
tornado.ioloop.IOLoop.current().call_later(conf['rushtime'],
|
||||
lambda : readMysqlData(conf['rushtime'])
|
||||
)
|
||||
tornado.ioloop.IOLoop.current().start()
|
||||
|
@ -15,6 +15,17 @@ class RankController{
|
||||
return $conn;
|
||||
}
|
||||
|
||||
protected function getRedis()
|
||||
{
|
||||
$redis_conf = getRedisConfig();
|
||||
$r = new phpcommon\Redis(array(
|
||||
'host' => $redis_conf['host'],
|
||||
'port' => $redis_conf['port'],
|
||||
'passwd' => $redis_conf['passwd'],
|
||||
));
|
||||
return $r;
|
||||
}
|
||||
|
||||
public function rankInfo()
|
||||
{
|
||||
$account_id = $_REQUEST['account_id'];
|
||||
@ -35,115 +46,112 @@ class RankController{
|
||||
$win_rank = 0;
|
||||
|
||||
//击杀榜
|
||||
$rows = $conn->execQuery('SELECT * FROM user ORDER BY kills / game_times DESC;');
|
||||
$i = 1;
|
||||
if ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
if ($row['accountid'] == $account_id) {
|
||||
$kill_rank = $i;
|
||||
|
||||
$r = $this->getRedis();
|
||||
$kill_rank_db = $r->get("game2001api: kill_rank");
|
||||
$kill_db = json_decode($kill_rank_db);
|
||||
$i = 0;
|
||||
foreach ($kill_db as $kill) {
|
||||
if ($kill_db[$i][0] == $account_id) {
|
||||
$kill_rank = $i + 1;
|
||||
}
|
||||
array_push($kill_list, array(
|
||||
'account_id' => $row['accountid'],
|
||||
'name' => $row['user_name'],
|
||||
'avatar_url' => $row['avatar_url'],
|
||||
'kill' => phpcommon\safeDiv($row['kills'], $row['game_times']),
|
||||
'alive'=> phpcommon\safeDiv($row['alive_time'], $row['game_times']),
|
||||
'harm' => phpcommon\safeDiv($row['harm'], $row['game_times']),
|
||||
'win_rate' => phpcommon\safeDiv($row['win_times'], $row['game_times']),
|
||||
'win_game' => $row['win_times']
|
||||
'account_id' => $kill_db[$i][0],
|
||||
'name' => $kill_db[$i][1],
|
||||
'avatar_url' => $kill_db[$i][2],
|
||||
'kill' => $kill_db[$i][3],
|
||||
'alive'=> $kill_db[$i][4],
|
||||
'harm' => $kill_db[$i][5],
|
||||
'win_rate' => $kill_db[$i][6],
|
||||
'win_game' => $kill_db[$i][7]
|
||||
));
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
//生存榜
|
||||
$rows = $conn->execQuery('SELECT * FROM user ORDER BY alive_time / game_times DESC;');
|
||||
$i = 1;
|
||||
if ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
if ($row['accountid'] == $account_id) {
|
||||
$alive_rank = $i;
|
||||
$alive_rank_db = $r->get("game2001api: alive_rank");
|
||||
$alive_db = json_decode($alive_rank_db);
|
||||
$i = 0;
|
||||
foreach ($alive_db as $alive) {
|
||||
if ($alive_db[$i][0] == $account_id) {
|
||||
$alive_rank = $i + 1;
|
||||
}
|
||||
array_push($alive_list, array(
|
||||
'account_id' => $row['accountid'],
|
||||
'name' => $row['user_name'],
|
||||
'avatar_url' => $row['avatar_url'],
|
||||
'kill' => phpcommon\safeDiv($row['kills'], $row['game_times']),
|
||||
'alive'=> phpcommon\safeDiv($row['alive_time'], $row['game_times']),
|
||||
'harm' => phpcommon\safeDiv($row['harm'], $row['game_times']),
|
||||
'win_rate' => phpcommon\safeDiv($row['win_times'], $row['game_times']),
|
||||
'win_game' => $row['win_times']
|
||||
'account_id' => $alive_db[$i][0],
|
||||
'name' => $alive_db[$i][1],
|
||||
'avatar_url' => $alive_db[$i][2],
|
||||
'kill' => $alive_db[$i][3],
|
||||
'alive'=> $alive_db[$i][4],
|
||||
'harm' => $alive_db[$i][5],
|
||||
'win_rate' => $alive_db[$i][6],
|
||||
'win_game' => $alive_db[$i][7]
|
||||
));
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
//伤害榜
|
||||
$rows = $conn->execQuery('SELECT * FROM user ORDER BY harm / game_times DESC;');
|
||||
$i = 1;
|
||||
if ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
if ($row['accountid'] == $account_id) {
|
||||
$harm_rank = $i;
|
||||
$harm_rank_db = $r->get("game2001api: harm_rank");
|
||||
$harm_db = json_decode($harm_rank_db);
|
||||
$i = 0;
|
||||
foreach ($harm_db as $harm) {
|
||||
if ($harm_db[$i][0] == $account_id) {
|
||||
$harm_rank = $i + 1;
|
||||
}
|
||||
array_push($harm_list, array(
|
||||
'account_id' => $row['accountid'],
|
||||
'name' => $row['user_name'],
|
||||
'avatar_url' => $row['avatar_url'],
|
||||
'kill' => phpcommon\safeDiv($row['kills'], $row['game_times']),
|
||||
'alive'=> phpcommon\safeDiv($row['alive_time'], $row['game_times']),
|
||||
'harm' => phpcommon\safeDiv($row['harm'], $row['game_times']),
|
||||
'win_rate' => phpcommon\safeDiv($row['win_times'], $row['game_times']),
|
||||
'win_game' => $row['win_times']
|
||||
'account_id' => $harm_db[$i][0],
|
||||
'name' => $harm_db[$i][1],
|
||||
'avatar_url' => $harm_db[$i][2],
|
||||
'kill' => $harm_db[$i][3],
|
||||
'alive'=> $harm_db[$i][4],
|
||||
'harm' => $harm_db[$i][5],
|
||||
'win_rate' => $harm_db[$i][6],
|
||||
'win_game' => $harm_db[$i][7]
|
||||
));
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
//胜率榜
|
||||
$rows = $conn->execQuery('SELECT * FROM user ORDER BY win_times / game_times DESC;');
|
||||
$i = 1;
|
||||
if ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
if ($row['accountid'] == $account_id) {
|
||||
$rate_rank = $i;
|
||||
$rate_rank_db = $r->get("game2001api: rate_rank");
|
||||
$rate_db = json_decode($rate_rank_db);
|
||||
$i = 0;
|
||||
foreach ($rate_db as $rate) {
|
||||
if ($rate_db[$i][0] == $account_id) {
|
||||
$rate_rank = $i + 1;
|
||||
}
|
||||
array_push($rate_list, array(
|
||||
'account_id' => $row['accountid'],
|
||||
'name' => $row['user_name'],
|
||||
'avatar_url' => $row['avatar_url'],
|
||||
'kill' => phpcommon\safeDiv($row['kills'], $row['game_times']),
|
||||
'alive'=> phpcommon\safeDiv($row['alive_time'], $row['game_times']),
|
||||
'harm' => phpcommon\safeDiv($row['harm'], $row['game_times']),
|
||||
'win_rate' => phpcommon\safeDiv($row['win_times'], $row['game_times']),
|
||||
'win_game' => $row['win_times']
|
||||
'account_id' => $rate_db[$i][0],
|
||||
'name' => $rate_db[$i][1],
|
||||
'avatar_url' => $rate_db[$i][2],
|
||||
'kill' => $rate_db[$i][3],
|
||||
'alive'=> $rate_db[$i][4],
|
||||
'harm' => $rate_db[$i][5],
|
||||
'win_rate' => $rate_db[$i][6],
|
||||
'win_game' => $rate_db[$i][7]
|
||||
));
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
//胜场榜
|
||||
$rows = $conn->execQuery('SELECT * FROM user ORDER BY win_times DESC;');
|
||||
$i = 1;
|
||||
if ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
if ($row['accountid'] == $account_id) {
|
||||
$win_rank = $i;
|
||||
$win_rank_db = $r->get("game2001api: win_rank");
|
||||
$win_db = json_decode($win_rank_db);
|
||||
$i = 0;
|
||||
foreach ($win_db as $win) {
|
||||
if ($win_db[$i][0] == $account_id) {
|
||||
$win_rank = $i + 1;
|
||||
}
|
||||
array_push($win_list, array(
|
||||
'account_id' => $row['accountid'],
|
||||
'name' => $row['user_name'],
|
||||
'avatar_url' => $row['avatar_url'],
|
||||
'kill' => phpcommon\safeDiv($row['kills'], $row['game_times']),
|
||||
'alive'=> phpcommon\safeDiv($row['alive_time'], $row['game_times']),
|
||||
'harm' => phpcommon\safeDiv($row['harm'], $row['game_times']),
|
||||
'win_rate' => phpcommon\safeDiv($row['win_times'], $row['game_times']),
|
||||
'win_game' => $row['win_times']
|
||||
'account_id' => $win_db[$i][0],
|
||||
'name' => $win_db[$i][1],
|
||||
'avatar_url' => $win_db[$i][2],
|
||||
'kill' => $win_db[$i][3],
|
||||
'alive'=> $win_db[$i][4],
|
||||
'harm' => $win_db[$i][5],
|
||||
'win_rate' => $win_db[$i][6],
|
||||
'win_game' => $win_db[$i][7]
|
||||
));
|
||||
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode(array(
|
||||
'errcode' => 0,
|
||||
'errmsg' => "",
|
||||
|
Loading…
x
Reference in New Issue
Block a user