261 lines
9.8 KiB
Python
261 lines
9.8 KiB
Python
// by shenjack
|
||
// write at carpet 1.16.5 1.4.44
|
||
// with carpet-extra-1.16.5-1.4.30
|
||
// with carpet-tis-addition-mc1.16.4-1.15.0+build.492
|
||
// with fabric-api-0.26.0+1.16 (because on server Api Version > 0.26 will cause bug)
|
||
|
||
global_version = '1.3.3';
|
||
global_carpet_version = split('\\+v',system_info('scarpet_version'));
|
||
global_require_carpet_version = ['1.4.30', 210407];
|
||
global_filename = system_info('app_name');
|
||
global_score_path = 'census/';
|
||
|
||
// debug级别
|
||
global_debug_level = 2;
|
||
// 0 -> show all message
|
||
// 1 -> show most debug
|
||
// 2 -> show all info
|
||
// 3 -> show all warn
|
||
// 4 -> show all error
|
||
|
||
global_score = {};
|
||
global_player_name = [];
|
||
// 分数相关初始化
|
||
|
||
__config() -> {
|
||
'stay_loaded' -> true,
|
||
'scope' -> 'global',
|
||
// 'allow_command_conflicts' -> true,
|
||
'legacy_command_type_support' -> true,
|
||
'requires' -> {
|
||
'minecraft' -> '>=1.16', // best at 1.16+ haven't test in 1.15.2 and 1.14.4
|
||
'carpet' -> '>=1.4.31' // best at 1.4.31 because fix issue of read_file()
|
||
},
|
||
'commands' -> {
|
||
'' -> 'help',
|
||
'help' -> 'help',
|
||
'print' -> ['print_score', 'mined'],
|
||
'reload' -> 'reload_script',
|
||
'score load' -> 'load_scores',
|
||
'score save' -> 'save_scores',
|
||
'score print' -> ['print_score', 'mined'],
|
||
'debug level' -> ['debug_level', null],
|
||
'debug level <debug_level>' -> 'debug_level',
|
||
'debug add_mine <player> <block>' -> 'add_mine',
|
||
},
|
||
'arguments' -> {
|
||
'player' -> {
|
||
'type' -> 'players',
|
||
},
|
||
'block' -> {
|
||
'type' -> 'string',
|
||
},
|
||
'debug_level' -> {
|
||
'type' -> 'int',
|
||
'min' -> 0,
|
||
'max' -> 4,
|
||
'suggest' -> [0, 1, 2, 3, 4]
|
||
}
|
||
};
|
||
};
|
||
|
||
// import('lib', 'callme');
|
||
|
||
help_message = {
|
||
'name': 'census',
|
||
'version': global_version,
|
||
'information': 'census 是一个用于统计玩家各类数据的 script',
|
||
'commands': {
|
||
'info': '同 /{app_name} help 显示帮助信息',
|
||
'hint': '然而并没有什么隐藏信息(确信)',
|
||
'run': '{app_name} help',
|
||
'sub_command': {
|
||
'help': {
|
||
'info': '显示你现在在看的这条帮助信息!',
|
||
'hint': 'g 你就在看着我呢~',
|
||
'run': '{command}',
|
||
},
|
||
'reload': {
|
||
'info': '重新加载script',
|
||
'hint': 'g 获得成就:我重载我自己',
|
||
'run': '{command}',
|
||
},
|
||
'score': {
|
||
'sub_command': {
|
||
'load': {
|
||
'info': '从数据文件夹中加载数据并覆盖现有数据',
|
||
'hint': 'rub 本操作会覆盖现有数据,所以请自行输入命令',
|
||
'copy': '{command}'
|
||
},
|
||
'save': {
|
||
'info': '保存现有数据',
|
||
'hint': 'ub 提醒:这个操作会覆盖文件数据(虽说其实没啥事)',
|
||
'run': '{command}',
|
||
},
|
||
'print': {
|
||
'info': '打印分数',
|
||
'hint': '获得成就:我打印了分数',
|
||
'run': '{command}',
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
};
|
||
|
||
|
||
__on_close() -> (
|
||
// 保存一些debug信息
|
||
debug_info = {'debug_level' -> global_debug_level};
|
||
write_file('main', 'json', debug_info);
|
||
// 保存分数
|
||
save_scores();
|
||
);
|
||
|
||
printl(message, level = 2, say = false, reply = true) -> (
|
||
if(global_debug_level <= level,
|
||
if(reply, print(message));
|
||
if(say, run('say ' + message)));
|
||
);
|
||
|
||
debug_level(level) -> (
|
||
if(level == null,
|
||
print('目前DEBUG等级为: ' + global_debug_level);
|
||
return(0));
|
||
global_debug_level = level;
|
||
print('DEBUG等级已经设置为: ' + global_debug_level);
|
||
logger(type = 'info', 'DEBUG等级已经设置为: ' + global_debug_level);
|
||
);
|
||
|
||
// fmt_cmd(cmd, msg, hint, do = false) -> (
|
||
// if(do,
|
||
// command = '!/' + global_filename + cmd,
|
||
// command = '/' + global_filename + cmd);
|
||
// return(command);
|
||
// );
|
||
|
||
gloal_help_config = {};
|
||
|
||
help() -> (
|
||
print(format('y ' + '-'*10 + '帮助信息' + '-'*10));
|
||
print(format('m 提醒:大部分信息都可以点击来执行相对应的命令,所以请小心点击', '^g 包括我! 点我来执行 /' + global_filename, '!/' + global_filename));
|
||
print(format('e /' + global_filename + ' -> ', 'c 同 /' + global_filename + ' help'));
|
||
print(format('e /' + global_filename + ' help -> ', 'c 显示这条帮助信息', '^g 你就在看着我呢!', '!/' + global_filename + ' help'));
|
||
print(format('e /' + global_filename + ' reload -> ', 'c 重新加载script', '^g 获得成就:我重载我自己', '!/' + global_filename + ' reload'));
|
||
print(format('e /' + global_filename + ' score load -> ', 'c 加载各项榜单', '^rub 警告:这会覆盖游戏内的现有数据(所以请自行输入命令)'));
|
||
print(format('e /' + global_filename + ' score save -> ', 'c 保存各项榜单', '^ub 提醒:这会覆盖文件数据', '!/' + global_filename + ' score save'));
|
||
print(format('e /' + global_filename + ' score print -> ', 'c 输出各项榜单数据', '^gs 吐槽一下,scarpet的字符串处理真的麻烦死了', '!/' + global_filename + ' score print'));
|
||
print(format('e /' + global_filename + ' debug level -> ', 'c 显示DEBUG信息等级', '^g (没啥可吐槽的)', '!/' + global_filename + ' debug level'));
|
||
print(format('e /' + global_filename + ' debug level <debug_level> -> ', 'c 设置DEBUG信息等级为<debug_level> ', '^g <debug_level> 为 0-4的正整数,默认为2', '?/' + global_filename + ' debug level '));
|
||
print(format('y [系统信息]' + global_filename + ' 版本: ' + global_version));
|
||
print(format('y [系统信息]' + global_filename + ' 文件名: ' + global_filename));
|
||
print(format('y [插件信息]' + global_filename + ' 数据存储路径: ' + global_score_path));
|
||
print(format('e 感谢 @Harvey_Husky 延皓 的支持! script中一部分源码就来自哈姥姥!', '^g 哈姥姥真好用(', '?https://gitee.com/harvey-husky/FZ-sDatapack'));
|
||
);
|
||
|
||
reload_script() -> (
|
||
save_scores();
|
||
print('script ' + global_filename + ' 重载中···');
|
||
run(str('script load ' + global_filename));
|
||
);
|
||
|
||
load_scores() -> (
|
||
print('重新加载计分数据中···');
|
||
global_score = {}; // 重置分数
|
||
files = list_files(global_score_path, 'shared_json'); // 列出所有文件
|
||
printl(files, 1, true, true); // DEBUG 输出
|
||
for(files,
|
||
data = read_file(_, 'shared_json'); // 获取文件内json
|
||
name = split('/', _):1; // 获取文件名(玩家名称
|
||
global_score:name = data; // 存储进计分表
|
||
printl(data + '\n\n' + _, 1, false, true)); // DEBUG 输出
|
||
print('计分板数据加载完成');
|
||
);
|
||
|
||
score_print(type) -> (
|
||
// 目前只会输出挖掘榜
|
||
printl(global_score, 0, false, true);
|
||
print('§7' + '-'*10 + '挖掘榜' + '-'*10); // 输出标题
|
||
mine = [];
|
||
max_len = 1;
|
||
// 初始化两个数据
|
||
for(global_score,
|
||
if(global_score:_:'mined':'total' == null || global_score:_:'mined':'total' == '0',
|
||
continue(););
|
||
max_len = max(max_len, length(_));
|
||
// 如果这个名字的长度大于之前的,就把最大长度换成这个名字的
|
||
put(mine, 0, [_, global_score:_:'mined':'total'], 'insert'));
|
||
// 把所有的数据加到mine里备用,等待排序
|
||
mine = sort_key(mine, -_:1); // 根据每个挖掘数据的挖掘量进行排序(排序用的分数是挖掘量的倒数,这样可以倒序排列)
|
||
printl(mine, 1, false, true);
|
||
for(mine,
|
||
print(format(' ' + _:0 + ': ' + ' '*(max_len-length(_:0))+ _:1 + ' block')));
|
||
// 依次输出数据
|
||
|
||
);
|
||
|
||
print_score(type) -> (
|
||
// 输出指定类型的榜单
|
||
mapper = {
|
||
'mined' -> '挖掘',
|
||
'killed' -> '击杀'
|
||
};
|
||
maped_type = mapper:type;
|
||
task_thread('temp', 'score_print', type);
|
||
);
|
||
|
||
save_scores() -> (
|
||
print('计分数据保存中');
|
||
for(global_score,
|
||
write_file(global_score_path + _, 'shared_json', global_score:_);
|
||
printl(_ + '\n' + global_score_path + _, 1, false, true));
|
||
print('计分数据保存完毕');
|
||
);
|
||
|
||
make_player_score(player) -> (
|
||
score = {'mined'-> {'total'-> 0},
|
||
'kill'-> {}}; // 基本信息
|
||
put(global_score, player, score); // 加到列表里
|
||
);
|
||
|
||
__on_player_dies(player) -> (
|
||
pos = query(player, 'pos');
|
||
dim = query(player, 'dimension');
|
||
die_pos = str('[x: %d, y:%d, z:%d]', pos);
|
||
if(dim == 'the_nether', dim = '地狱');
|
||
if(dim == 'overworld', dim = '主世界');
|
||
run(str('say ' + str(player) + '在' + dim + ' ' + die_pos + '死亡'));
|
||
);
|
||
|
||
__on_player_breaks_block(player, block) -> (
|
||
add_mine(player, block);
|
||
);
|
||
|
||
add_mine(player, block) -> (
|
||
block = str(block);
|
||
splayer = query(player, 'name');
|
||
if(!player(splayer),
|
||
return());
|
||
// 如果玩家列表里没有输入的玩家就返回
|
||
if(!has(global_score:splayer),
|
||
make_player_score(splayer));
|
||
// 如果榜单里没有玩家就加入玩家的榜单
|
||
if(!has(global_score:splayer:'mined', block),
|
||
put(global_score:splayer:'mined':block, 1),
|
||
// 如果玩家的记录里没有这个方块就把这个方块加到挖掘列表里
|
||
score = get(global_score:splayer:'mined':block);
|
||
put(global_score:splayer:'mined':block, score + 1));
|
||
// 如果记录里有就 += 1
|
||
full_score = get(global_score:splayer:'mined':'total');
|
||
put(global_score:splayer:'mined':'total', full_score + 1);
|
||
// print(global_score);
|
||
);
|
||
|
||
// 加载debug信息
|
||
debug_info = read_file('main', 'json');
|
||
if(debug_info,
|
||
global_debug_level = debug_info:'debug_level');
|
||
// 基本信息初始化
|
||
// 加载分数
|
||
load_scores();
|