fz-survival-datapack/tools/score_move.py

57 lines
2.1 KiB
Python
Raw Normal View History

2021-11-16 00:04:08 +08:00
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
# -------------------------------
# Census loader
# Copyright © 2021 by shenjackyuanjie
# All rights reserved
# -------------------------------
import os
import json
USERCACHE_PATH = os.path.join('server', 'usercache.json')
STATS_DIR = os.path.join('server', 'world', 'stats')
DATA_DIR = os.path.join('server', 'world', 'scripts')
CENSUS_DIR = os.path.join('server', 'world', 'scripts', 'shared', 'census')
# 引号内字符匹配错误
# text in the quote is not matched
if __name__ == '__main__':
# 加载玩家名列表
with open(USERCACHE_PATH, 'r') as cache_file:
uuid_cache = json.load(cache_file) # type: list
uuid_list = {}
# 如果玩家名称不是bot就存进列表里
for name in uuid_cache:
if name['name'][:3].lower() == 'bot':
continue
uuid_list[name['name']] = name['uuid']
# 检测是否存在census的文件夹
if 'shared' not in os.listdir(DATA_DIR):
os.mkdir(DATA_DIR + '/shared')
if 'census' not in os.listdir(DATA_DIR + '/shared'):
os.mkdir(CENSUS_DIR)
for uuid in uuid_list:
# 加载玩家的统计数据
with open(os.path.join(STATS_DIR, uuid_list[uuid] + '.json'), 'r') as stats_file:
stats = json.load(stats_file) # type: dict
# 计算玩家的分数
player_score = {}
for score_type in stats['stats']: # 类型,比如 mined
player_score[score_type[10:]] = {} # 创建字典套娃
count = 0
for score in stats['stats'][score_type]: # 类型中的每一种"东西"
count += stats['stats'][score_type][score] # 统计每种东西的数量
player_score[score_type[10:]][score[10:]] = stats['stats'][score_type][score]
player_score[score_type[10:]]['total'] = count
# pprint(player_score)
file_name = f'{CENSUS_DIR}/{uuid}.json'
with open(file_name, 'w') as census_file:
json.dump(player_score, census_file)