fz-survival-datapack/scripts/census.sc

260 lines
9.8 KiB
Python
Raw Normal View History

2021-09-16 23:52:32 +08:00
// by shenjack
2022-05-11 20:13:29 +08:00
// write at carpet 1.16.5 1.4.44
2021-09-16 23:52:32 +08:00
// 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)
2022-05-11 20:13:29 +08:00
global_version = '1.3.2';
2021-09-16 23:52:32 +08:00
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',
2022-05-11 20:13:29 +08:00
'print' -> ['print_score', 'mined'],
2021-09-16 23:52:32 +08:00
'reload' -> 'reload_script',
'score load' -> 'load_scores',
'score save' -> 'save_scores',
2022-05-11 20:13:29 +08:00
'score print' -> ['print_score', 'mined'],
2021-09-16 23:52:32 +08:00
'debug level' -> ['debug_level', null],
'debug level <debug_level>' -> 'debug_level',
'debug add_mine <player> <block>' -> 'add_mine',
},
'arguments' -> {
'player' -> {
2022-05-11 20:13:29 +08:00
'type' -> 'players',
2021-09-16 23:52:32 +08:00
},
'block' -> {
'type' -> 'string',
},
'debug_level' -> {
'type' -> 'int',
'min' -> 0,
'max' -> 4,
'suggest' -> [0, 1, 2, 3, 4]
}
};
};
2022-05-11 20:13:29 +08:00
// 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}',
}
}
}
}
}
};
2021-09-16 23:52:32 +08:00
__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));
2022-05-11 20:13:29 +08:00
if(say, run('say ' + message)));
2021-09-16 23:52:32 +08:00
);
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);
);
2022-05-11 20:13:29 +08:00
// fmt_cmd(cmd, msg, hint, do = false) -> (
// if(do,
// command = '!/' + global_filename + cmd,
// command = '/' + global_filename + cmd);
// return(command);
// );
gloal_help_config = {};
2021-09-16 23:52:32 +08:00
help() -> (
print(format('y ' + '-'*10 + '帮助信息' + '-'*10));
2022-05-11 20:13:29 +08:00
print(format('m 提醒:大部分信息都可以点击来执行相对应的命令,所以请小心点击', '^g 包括我! 点我来执行 /' + global_filename, '!/' + global_filename));
2021-09-16 23:52:32 +08:00
print(format('e /' + global_filename + ' -> ', 'c 同 /' + global_filename + ' help'));
2022-05-11 20:13:29 +08:00
print(format('e /' + global_filename + ' help -> ', 'c 显示这条帮助信息', '^g 你就在看着我呢!', '!/' + global_filename + ' help'));
print(format('e /' + global_filename + ' reload -> ', 'c 重新加载script', '^g 获得成就:我重载我自己', '!/' + global_filename + ' reload'));
2021-09-16 23:52:32 +08:00
print(format('e /' + global_filename + ' score load -> ', 'c 加载各项榜单', '^rub 警告:这会覆盖游戏内的现有数据(所以请自行输入命令)'));
2022-05-11 20:13:29 +08:00
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'));
2021-09-16 23:52:32 +08:00
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() -> (
2022-05-11 20:13:29 +08:00
save_scores();
2021-09-16 23:52:32 +08:00
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 输出
);
2022-05-11 20:13:29 +08:00
score_print(type) -> (
2021-09-16 23:52:32 +08:00
// 目前只会输出挖掘榜
printl(global_score, 0, false, true);
print('§7' + '-'*10 + '挖掘榜' + '-'*10); // 输出标题
mine = [];
max_len = 1;
// 初始化两个数据
for(global_score,
2022-05-11 20:13:29 +08:00
if(global_score:_:'mined':'total' == null || global_score:_:'mined':'total' == '0',
continue(););
2021-09-16 23:52:32 +08:00
max_len = max(max_len, length(_));
// 如果这个名字的长度大于之前的就把最大长度换成这个名字的
2022-05-11 20:13:29 +08:00
put(mine, 0, [_, global_score:_:'mined':'total'], 'insert'));
2021-09-16 23:52:32 +08:00
// 把所有的数据加到mine里备用等待排序
mine = sort_key(mine, -_:1); // 根据每个挖掘数据的挖掘量进行排序(排序用的分数是挖掘量的倒数,这样可以倒序排列)
printl(mine, 1, false, true);
for(mine,
print(format(' ' + _:0 + ': ' + ' '*(max_len-length(_:0))+ _:1 + ' block')));
// 依次输出数据
);
2022-05-11 20:13:29 +08:00
print_score(type) -> (
// 输出指定类型的榜单
mapper = {
'mined' -> '挖掘',
'killed' -> '击杀'
};
maped_type = mapper:type;
task_thread('temp', 'score_print', type);
);
2021-09-16 23:52:32 +08:00
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) -> (
2022-05-11 20:13:29 +08:00
score = {'mined'-> {'total'-> 0},
2021-09-16 23:52:32 +08:00
'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);
2022-05-11 20:13:29 +08:00
splayer = query(player, 'name');
2021-09-16 23:52:32 +08:00
if(!player(splayer),
return());
// 如果玩家列表里没有输入的玩家就返回
if(!has(global_score:splayer),
make_player_score(splayer));
// 如果榜单里没有玩家就加入玩家的榜单
2022-05-11 20:13:29 +08:00
if(!has(global_score:splayer:'mined', block),
put(global_score:splayer:'mined':block, 1),
2021-09-16 23:52:32 +08:00
// 如果玩家的记录里没有这个方块就把这个方块加到挖掘列表里
2022-05-11 20:13:29 +08:00
score = get(global_score:splayer:'mined':block);
put(global_score:splayer:'mined':block, score + 1));
2021-09-16 23:52:32 +08:00
// 如果记录里有就 += 1
2022-05-11 20:13:29 +08:00
full_score = get(global_score:splayer:'mined':'total');
put(global_score:splayer:'mined':'total', full_score + 1);
2021-09-16 23:52:32 +08:00
// print(global_score);
);
// 加载debug信息
debug_info = read_file('main', 'json');
if(debug_info,
global_debug_level = debug_info:'debug_level');
// 基本信息初始化
// 加载分数
load_scores();