This commit is contained in:
aozhiwei 2024-08-06 09:14:12 +08:00
parent 56b174cf31
commit 968832e5c4
12 changed files with 0 additions and 564 deletions

View File

@ -1 +0,0 @@
../../third_party/f7/f7

View File

@ -1 +0,0 @@
../../third_party/q7/q7

View File

@ -1,9 +0,0 @@
[
{
"instance_id" : 1,
"host" : "127.0.0.1",
"port" : 3306,
"user" : "root",
"passwd" : "keji178"
}
]

View File

@ -1,9 +0,0 @@
[
{
"instance_id" : 1,
"host" : "127.0.0.1",
"port" : 6379,
"user" : "root",
"passwd" : ""
}
]

View File

@ -1,9 +0,0 @@
[
{
"instance_id" : 1,
"host" : "127.0.0.1",
"port" : 3306,
"user" : "root",
"passwd" : "keji178"
}
]

View File

@ -1,9 +0,0 @@
[
{
"instance_id" : 1,
"host" : "127.0.0.1",
"port" : 6379,
"user" : "root",
"passwd" : ""
}
]

View File

@ -1,46 +0,0 @@
[
{
"table_name": "version",
"where": ""
},
{
"table_name": "user",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "bag",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "sign",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "quest",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "share_achievement",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "activity",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "passinfo",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "shop",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "equip",
"where": "accountid LIKE '$channel_%'"
},
{
"table_name": "shop_data",
"where": "accountid LIKE '$channel_%'"
}
]

View File

@ -1,5 +0,0 @@
[
{
"key_prefix": "game2004"
}
]

View File

@ -1,44 +0,0 @@
# -*- coding: utf-8 -*-
#!/usr/bin/python
import os
import sys
import time
import json
import redis
import datetime
import pprint
argv = sys.argv
host = argv[1]
port = argv[2]
script_name = argv[3]
def writeToRedis(conn, cmdline):
assert len(cmdline) > 1
# print(cmdline)
if cmdline[0] == 'hset':
conn.hset(cmdline[1], cmdline[2], cmdline[3])
elif cmdline[0] == 'zadd':
conn.zadd(cmdline[1], {cmdline[3]: cmdline[2]})
elif cmdline[0] == 'sadd':
conn.sadd(cmdline[1], cmdline[2])
elif cmdline[0] == 'set':
conn.set(cmdline[1], cmdline[2])
elif cmdline[0] == 'expire':
conn.expire(cmdline[1], cmdline[2])
else:
assert False
conn = redis.Redis(host = host,
port = port,
db = 0,
decode_responses = True
)
with open(script_name, 'r') as f:
for line in f:
line = line.strip()
if line == '':
continue
writeToRedis(conn, json.loads(line))

View File

@ -1,143 +0,0 @@
# -*- coding: utf-8 -*-
#!/usr/bin/python
import pymysql
import hashlib
import json
import urllib.request
import base64
import time
import datetime
import redis
import os
import sys
import functools
CONFIG_DIR = './'
context = {
'channel': 0,
'dbname_prefix': ''
}
EXCLUDE_KEYS = {
'game2004api:kill_rank_6006': 1,
'game2004api:win_rank_6006': 1,
'game2004api:integral_rank_6006': 1
}
def scanKeys(r, key_prefix, matched_keys):
scan_key = key_prefix + '*'
keys = []
cursor, keys = r.scan(0, scan_key, 1000)
scan_count = 0
while cursor != 0 or len(keys) > 0:
for key in keys:
matched_keys[key] = key
#end
keys = []
if cursor != 0:
cursor, keys = r.scan(cursor, scan_key, 1000)
scan_count += 1
if scan_count > 10000:
break
#end while cursor
def excludeKey(key):
global EXCLUDE_KEYS
return key in EXCLUDE_KEYS
def saveKeys(r, keys, curr_file):
for key in keys:
if excludeKey(key):
print('[WARNING]exclude key ' + key)
continue
data = r.get(key)
expire = r.ttl(key)
curr_file.write(json.dumps(
[
'set',
key,
data
]
) + '\n')
curr_file.write(json.dumps(
[
'expire',
key,
expire
]
) + '\n')
if expire < 0:
print('[ERROR] ' + key + ' ttl < 0')
if expire > 3600 * 24:
print('[ERROR] ' + key + ' ttl > 3600 * 24')
def exportRedis(context, redis_conf, redis_rule_conf):
redis_dir = CONFIG_DIR + 'out/' + context['channel'] + '/redis/'
if not os.path.exists(redis_dir):
os.mkdir(redis_dir)
curr_file = open(redis_dir + context['channel'] + '.redis', 'w')
for conf in redis_conf:
r = redis.Redis(host = conf['host'],
port = conf['port'],
password = conf['passwd'],
decode_responses = True
)
for rule in redis_rule_conf:
matched_keys = {}
scanKeys(r, rule['key_prefix'], matched_keys)
saveKeys(r, matched_keys, curr_file)
#end for conf
def exportMysql(context, db_conf, db_rule_conf):
dbsh_file = open(context['out_dir'] + 'exportdb.sh', 'w')
for conf in db_conf:
db_dir = CONFIG_DIR + 'out/' + context['channel'] + '/' + str(conf['instance_id'])
if not os.path.exists(db_dir):
os.mkdir(db_dir)
for rule in db_rule_conf:
line = ''
if rule['where'] == '':
line = 'mysqldump -h %s -P %d -u%s -p%s %s %s > %d/%s.sql' % (
conf['host'],
conf['port'],
conf['user'],
conf['passwd'],
context['dbname_prefix'] + str(conf['instance_id']),
rule['table_name'],
conf['instance_id'],
rule['table_name']
)
else:
line = 'mysqldump -h %s -P %d -u%s -p%s %s %s --where="%s"> %d/%s.sql' % (
conf['host'],
conf['port'],
conf['user'],
conf['passwd'],
context['dbname_prefix'] + str(conf['instance_id']),
rule['table_name'],
rule['where'].replace('$channel', context['channel']),
conf['instance_id'],
rule['table_name']
)
dbsh_file.write(line + "\n")
def main():
global context
if not os.path.exists(CONFIG_DIR + 'out/'):
os.mkdir(CONFIG_DIR + 'out/')
if not os.path.exists(CONFIG_DIR + 'out/' + context['channel']):
os.mkdir(CONFIG_DIR + 'out/' + context['channel'])
context['out_dir'] = CONFIG_DIR + 'out/' + context['channel'] + '/'
db_conf = json.loads(open(CONFIG_DIR + context['channel'] + '/mysql.json', 'r').read())
redis_conf = json.loads(open(CONFIG_DIR + context['channel'] + '/redis.json', 'r').read())
db_rule_conf = json.loads(open(CONFIG_DIR + 'common/mysql_rule.json', 'r').read())
redis_rule_conf = json.loads(open(CONFIG_DIR + 'common/redis_rule.json', 'r').read())
exportRedis(context, redis_conf, redis_rule_conf)
#exportMysql(context, db_conf, db_rule_conf)
if __name__ == "__main__":
#context['channel'] = sys.argv[1]
#context['dbname_prefix'] = sys.argv[2]
context['channel'] = '0'
context['dbname_prefix'] = 'gamedb2004'
main()

View File

@ -1,215 +0,0 @@
# -*- coding: utf-8 -*-
#!/usr/bin/python
import sys
sys.path.append('../local_packages')
import q7
import f7
import pymysql
import hashlib
import json
import urllib.request
import base64
import time
import datetime
import redis
import os
import functools
import test
CONFIG_DIR = '../config' if f7.isOnlineEnv() else '/var/data/conf_test/game2006api_rankserver/config'
def _take_pass(elem):
return elem[3]
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'],
port = conf['port'],
password = conf['passwd'],
charset = 'utf8'
)
return r;
def getRedisConf():
redis_conf = json.loads(open(CONFIG_DIR + '/rankserver.redis.cluster.json', 'r').read())
return redis_conf;
def checkchannel(channel):
if channel == 6006:
return False
else:
return True
def info(msg):
print(str(datetime.datetime.now()) + '[INFO] ' + msg)
def take_kills(elem):
return elem[3]
def take_game_times(elem):
return elem[7]
def take_integral_times(elem):
return elem[8]
def safeDiv(a, b):
if b == 0:
return 0
else:
return a / b
def getDaySeconds(time_val, incdays):
time_zone = 8
dayseconds = int((time_val + time_zone * 3600)/3600/24 + incdays) * 3600 * 24 - 3600 * time_zone;
return dayseconds
#数据去重
def _delRepeatData(row, data_list):
temp_list = []
for data in data_list:
if data[0] == row[0]:
temp_list.append(data)
for temp_data in temp_list:
data_list.remove(temp_data)
#刷新数据
def _refreshData(row, data_list, data_info):
key_info = data_info
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])
num = row[9]
if (time.time() > row[10]):
num = 0
data_list.append((row[0], row[1].decode('utf-8'), row[2], kill, alive_time, harm, win_times, row[6], num))
data_list.sort(key=key_info, reverse=True)
if (len(data_list) > 50):
del data_list[50:]
def _updateScoreRank(r, channel, integral_list):
integral_list.sort(key=take_integral_times, reverse=True)
integral_rank = []
for integral_index in range(min(50, len(integral_list))):
integral_rank.append(integral_list[integral_index])
r.set("game2006api:integral_rank_" + channel, json.dumps(integral_rank))
def fullUpdateRank():
f7.udplog.info('fullUpdateRank begin')
mysql_conf = json.loads(open(CONFIG_DIR + '/rankserver.mysql.cluster.json', 'r').read())
integral_hash = {}
for conf in mysql_conf:
conn = pymysql.connect(host = conf['host'],
port = conf['port'],
user = conf['user'],
passwd = conf['passwd'],
db = 'gamedb2006_' + str(conf['instance_id']),
charset = 'utf8'
)
cursor = conn.cursor()
last_idx = 0
temp_idx = 0
while 1:
cursor.execute('SELECT accountid, user_name, avatar_url, kills, alive_time,'
' harm, win_times, game_times, idx, integral, season_time '
'FROM user WHERE idx > %s LIMIT 0, 1000' % (last_idx))
has_data = False
for row in cursor:
has_data = True
#更新积分榜
channel = f7.getChannelByAccountId(row[0])
if checkchannel(channel):
if channel not in integral_hash:
integral_hash[channel] = []
_refreshData(row, integral_hash[channel], take_integral_times)
last_idx = max(row[8], last_idx)
time.sleep(0.001);
if not has_data:
break
#end while
cursor.close()
conn.close()
r = _getRedis()
for channel in integral_hash:
_updateScoreRank(r, channel, integral_hash[channel])
f7.udplog.info('fullUpdateRank end')
#每日定时读取mysql里的数据生成排行榜写入redis后php读取redis返回客户端显示
def _fullUpdateRank(rushtime):
def done_callback():
f7.timer.callAt(q7.getDaySeconds(time.time(), 1) + rushtime,
lambda : _fullUpdateRank(rushtime))
f7.app.createAsyncTask(done_callback, fullUpdateRank, ())
#每5分钟读取mysql里发生改变过的数据更新排行榜
def incrementUpdateRank():
mysql_conf = json.loads(open(CONFIG_DIR + '/rankserver.mysql.cluster.json', 'r').read())
r = _getRedis()
integral_hash = {}
for conf in mysql_conf:
conn = pymysql.connect(host = conf['host'],
port = conf['port'],
user = conf['user'],
passwd = conf['passwd'],
db = 'gamedb2006_' + str(conf['instance_id']),
charset = 'utf8'
)
cursor = conn.cursor()
last_idx = 0
temp_idx = 0
while 1:
cursor.execute('SELECT accountid, user_name, avatar_url, kills, alive_time,'
' harm, win_times, game_times, idx, integral, season_time, kill_modifytime FROM user '
' WHERE kill_modifytime > %s AND idx > %s LIMIT 0, 1000' % (time.time() - 300, last_idx))
has_data = False
for row in cursor:
has_data = True
channel = f7.getChannelByAccountId(row[0])
if checkchannel(channel):
#更新积分榜
if channel not in integral_hash:
integral_list = r.get('game2006api:integral_rank_' + channel)
integral_hash[channel] = [] if not integral_list else json.loads(integral_list)
_delRepeatData(row, integral_hash[channel])
_refreshData(row, integral_hash[channel], take_integral_times)
last_idx = max(row[8], last_idx)
time.sleep(0.001);
if not has_data:
break
#end while
cursor.close()
conn.close()
for channel in integral_hash:
_updateScoreRank(r, channel, integral_hash[channel])
f7.udplog.info('incrementUpdateRank end')
def _incrementUpdateRank(rushtime):
def done_callback():
f7.timer.callLater(rushtime,
lambda : _incrementUpdateRank(rushtime))
f7.app.createAsyncTask(done_callback, incrementUpdateRank, ())
if __name__ == "__main__":
q7.xPrint('pid %d' % os.getpid())
test.say()
f7.app.init('/data/logs/game2006_rankserver/logs')
f7.udplog.info('rankserver start pid:' + str(os.getpid()))
conf = json.loads(open(CONFIG_DIR + '/rankserver.json', 'r').read())
conf['rushtime'] = 300
f7.timer.callLater(conf['rushtime'],
lambda : _incrementUpdateRank(conf['rushtime']))
conf['day_rushtime'] = 5 * 3600
f7.timer.callAt(q7.getDaySeconds(time.time(), 1) + conf['day_rushtime'],
lambda : _fullUpdateRank(conf['day_rushtime']))
f7.app.listen(conf['listen_port'])
f7.app.start()

View File

@ -1,73 +0,0 @@
# -*- coding: utf-8 -*-
#!/usr/bin/python
import sys
sys.path.append('../local_packages')
import q7
import f7
import pymysql
import hashlib
import json
import time
import datetime
import redis
import os
if f7.isOnlineEnv():
import game2006_rankserver as app
else:
import app
def _updateRank_cmd(debug_info):
app.fullUpdateRank()
def _clearRank_cmd(debug_info):
for conf in app.getRedisConf():
r = redis.Redis(host = conf['host'],
port = conf['port'],
password = conf['passwd'],
charset = 'utf8'
)
kill_keys = f7.scanRedisKey(r, "game2006api:kill_rank_*")
win_keys = f7.scanRedisKey(r, "game2006api:win_rank_*")
integral_keys = f7.scanRedisKey(r, "game2006api:integral_rank_*")
for key in kill_keys :
r.delete(key)
for key in win_keys :
r.delete(key)
for key in integral_keys :
r.delete(key)
def processCmdLine(cmd):
cmd_hash = {
'updateRank': _updateRank_cmd,
'clearRank': _clearRank_cmd,
}
precmd_hash = {
}
postcmd_hash = {
}
debug_info = {
'record_count': 0,
'param1': 0,
'param2': 0,
'param3': 0,
'param4': 0
}
if cmd in precmd_hash:
precmd_hash[cmd](debug_info)
if cmd in cmd_hash:
cmd_hash[cmd](debug_info)
if cmd in postcmd_hash:
postcmd_hash[cmd](debug_info)
if __name__ == "__main__":
if len(sys.argv) <= 1:
pass
else:
q7.xPrint('pid:' + str(os.getpid()))
f7.app.init('/data/logs/game2006_rankserver_cmd/logs')
f7.udplog.info('game2006_rankserver_cmd start pid:' + str(os.getpid()))
processCmdLine(sys.argv[1])
f7.app.start()