From 8b89470fa421bf4e59088feaa21062767832d3ef Mon Sep 17 00:00:00 2001 From: oiuv Date: Wed, 13 Oct 2021 19:47:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0CACHE=5FD=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0cache=E6=8C=87=E4=BB=A4=EF=BC=8C=E5=9C=A8.env=E4=B8=AD?= =?UTF-8?q?=E9=85=8D=E7=BD=AE`CACHE=5FDATA=20:=20true`=E5=8F=AF=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=8E=A9=E5=AE=B6=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 3 +- adm/daemons/cached.c | 123 ++++++++++++++++++++++++ adm/daemons/logind.c | 10 +- adm/daemons/mysqld.c | 2 +- adm/daemons/updated.c | 12 ++- clone/user/user.c | 6 ++ cmds/adm/cache.c | 135 +++++++++++++++++++++++++++ d/register/yanluodian.c | 1 + include/globals.h | 3 +- {feature => inherit/misc}/database.c | 0 10 files changed, 285 insertions(+), 10 deletions(-) create mode 100644 adm/daemons/cached.c create mode 100644 cmds/adm/cache.c rename {feature => inherit/misc}/database.c (100%) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index d676a70fb..5516e9d2d 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -11,7 +11,8 @@ "defines": [ "FLUFFOS", "MUDOS", - "__PACKAGE_DB__" + "__PACKAGE_DB__", + "__USE_SQLITE3__" ], "cStandard": "c89", "cppStandard": "c++11", diff --git a/adm/daemons/cached.c b/adm/daemons/cached.c new file mode 100644 index 000000000..6a3e7cd15 --- /dev/null +++ b/adm/daemons/cached.c @@ -0,0 +1,123 @@ +/** + * 玩家存档缓存接口 + * 缓存玩家数据到db.sqlite,方便网页、全服排行等调用 + */ +#ifdef __USE_SQLITE3__ +// 初始化数据库 +mixed init_db(); +// 玩家数据缓存接口 +mixed insert(object user, int last_touched); +// 玩家数据更新接口 +mixed update(object user); +// 玩家数据删除接口 + +nosave object db; + +void create() +{ + db = new (DATABASE, "", "/data/db.sqlite", "", __USE_SQLITE3__); +} + +mixed init_db() +{ + mixed res; + + db->sql("DROP TABLE IF EXISTS `users`")->exec(); + res = db->sql("CREATE TABLE IF NOT EXISTS `users` ( + `id` VARCHAR(10) PRIMARY KEY NOT NULL, + `name` VARCHAR(10) NOT NULL, + `title` VARCHAR(50) DEFAULT NULL, + `master` VARCHAR(10) DEFAULT NULL, + `mobile` INTEGER DEFAULT NULL, + `age` INTEGER DEFAULT NULL, + `qi` INTEGER DEFAULT NULL, + `jing` INTEGER DEFAULT NULL, + `neili` INTEGER DEFAULT NULL, + `jingli` INTEGER DEFAULT NULL, + `combat_exp` INTEGER DEFAULT NULL, + `kill` INTEGER DEFAULT NULL, + `die` INTEGER DEFAULT NULL, + `updated_at` INTEGER DEFAULT NULL) ")->exec(); + + if (stringp(res)) + { + env("CACHE_DATA", 0); + } + + return res; +} + +mixed insert(object user, int last_touched) +{ + mixed res; + mapping my = user->query_entire_dbase(); + string master = mapp(my["family"]) ? my["family"]["master_name"] : ""; + int kill = 0, die = 0; + + if (mapp(my["combat"])) + { + kill = my["combat"]["MKS"] + my["combat"]["PKS"]; + die = my["combat"]["dietimes"]; + } + + res = db->table("users")->insert(([ + "id" : my["id"], + "name" : my["name"], + "title" : my["title"], + "mobile" : my["mobile"], + "age" : my["age"], + "qi" : my["max_qi"], + "jing" : my["max_jing"], + "neili" : my["max_neili"], + "jingli" : my["max_jingli"], + "combat_exp" : my["combat_exp"], + "master" : master, + "kill" : kill, + "die" : die, + "updated_at" : last_touched, + ])); + + return res; +} + +mixed update(object user) +{ + mixed res; + mapping my = user->query_entire_dbase(); + string master = mapp(my["family"]) ? my["family"]["master_name"] || "" : ""; + int kill = 0, die = 0; + + if (mapp(my["combat"])) + { + kill = my["combat"]["MKS"] + my["combat"]["PKS"]; + die = my["combat"]["dietimes"]; + } + + res = db->table("users")->where("id", my["id"])->update(([ + "name" : my["name"], + "title" : my["title"], + "mobile" : my["mobile"], + "age" : my["age"], + "qi" : my["max_qi"], + "jing" : my["max_jing"], + "neili" : my["max_neili"], + "jingli" : my["max_jingli"], + "combat_exp" : my["combat_exp"], + "master" : master, + "kill" : kill, + "die" : die, + "updated_at" : time(), + ])); + + return res; +} + +mixed delete(object user) +{ + mixed res; + + res = db->table("users")->where("id", user->query("id"))->delete (); + + return res; +} +#endif diff --git a/adm/daemons/logind.c b/adm/daemons/logind.c index da5313492..1a3082287 100644 --- a/adm/daemons/logind.c +++ b/adm/daemons/logind.c @@ -800,13 +800,17 @@ private void init_new_player(object user) // 记录名字 NAME_D->map_name(user->query("name"), user->query("id")); - // 设置必要的环境参数 user->set("env/auto_regenerate", 1); user->set("env/auto_get", 1); user->set("env/wimpy", 60); //设定不自动转宗师频道 user->set("env/no_autoultra", 1); + // 缓存到数据库 + if (env("CACHE_DATA")) + { + CACHE_D->insert(user, time()); + } } varargs void enter_world(object ob, object user, int silent) @@ -921,8 +925,6 @@ varargs void enter_world(object ob, object user, int silent) if (shoe && (! environment(shoe) || ! shoe->query("equipped"))) destruct(shoe); - user->set("registered", 1); - // user->set("born",1); if (! silent) { string term_type; @@ -1016,8 +1018,6 @@ varargs void enter_world(object ob, object user, int silent) { tell_object(this_player(), HBRED "\n你的管理密码没有升级为SHA512加密,为了账号安全请使用" HIY " passwd " NOR HBRED "修改管理密码。" NOR "\n"); } - - write("\n"); /* // 检查是否有新邮件未读 new_mail_n = get_info(user->query("id"), "newmail", "", 0); diff --git a/adm/daemons/mysqld.c b/adm/daemons/mysqld.c index 42d5a9595..8398cdd4e 100644 --- a/adm/daemons/mysqld.c +++ b/adm/daemons/mysqld.c @@ -1,4 +1,4 @@ -inherit F_DATABASE; +inherit DATABASE; // 调用连接对象更新bbs.mud.ren网站账号 varargs mixed update(object ob, string host, string db, string user) { diff --git a/adm/daemons/updated.c b/adm/daemons/updated.c index e8cc23463..7ae00208c 100644 --- a/adm/daemons/updated.c +++ b/adm/daemons/updated.c @@ -300,7 +300,11 @@ string clear_user_data(string user, string cat) // save the data of the user if (flag) ob->save(); - + // 删除数据库缓存 + if (env("CACHE_DATA")) + { + CACHE_D->delete(ob); + } // Destrut the object if create temporate if (login_ob) { @@ -363,6 +367,9 @@ void born_player(object me) // 性格不符不会愤怒之心 if (me->query("character") != "光明磊落" && me->query("character") != "心狠手辣") files -= ({"wrath"}); + // 性格不符不会鬼话连篇 + if (me->query("character") != "狡黠多变" && me->query("character") != "阴险奸诈") + files -= ({"trick"}); // 先天膂力 < 20 不会麒麟血臂 if (me->query("str") < 20) @@ -485,7 +492,7 @@ void zhuan_player(object me) me->delete ("schedule"); // 计划记录 me->delete ("skybook"); // 天书记录(三丹记录) - me->delete ("luohan_winner"); //过阵记录 + me->delete ("luohan_winner"); // 过阵记录 me->delete ("story"); // 中的故事 me->delete ("DiZangPass"); // 转世任务 @@ -495,6 +502,7 @@ void zhuan_player(object me) //获取转生前门派,用于脱离时无损判断 by 薪有所属 menpai1 = me->query("family/family_name"); + //(取消无损背叛师门) //me->set("old_family_name",menpai1); me->set("reborn/family/" + menpai1, 1); me->delete ("reborn/family/mark"); //删除门派脱离记录 diff --git a/clone/user/user.c b/clone/user/user.c index 9d65d4fd8..7a63df7c2 100644 --- a/clone/user/user.c +++ b/clone/user/user.c @@ -160,6 +160,12 @@ int save() res = ::save(); } + // 缓存到数据库 + if (env("CACHE_DATA")) + { + CACHE_D->update(me); + } + #if INSTALL_EXAMINE EXAMINE_D->examine_player(me); #endif diff --git a/cmds/adm/cache.c b/cmds/adm/cache.c new file mode 100644 index 000000000..43fc22d20 --- /dev/null +++ b/cmds/adm/cache.c @@ -0,0 +1,135 @@ +// cache.c +#include + +inherit F_CLEAN_UP; + +private void search_dir(object me); +private void examine_player(string name, int last_touched); + +void create() { seteuid(getuid()); } + +int main(object me, string arg) +{ + if (!SECURITY_D->valid_grant(me, "(admin)")) + return 0; + + if (arg != "-all") + return notify_fail("指令格式:cache -all\n"); + + message_system("系统进行数据处理中,请耐心等候...\n"); + write(HIG "系统开始缓存所有玩家基本数据...\n" HIG "进度:" + process_bar(0) + "\n"); + + search_dir(me); + + return 1; +} + +private void search_dir(object me) +{ + string *dir; + string name; + mixed *ppls; + int count; + int total; + int i; + int j; + + if (!is_root(previous_object())) + return 0; + // 初始化数据库 + CACHE_D->init_db(); + dir = get_dir(DATA_DIR + "login/"); + + count = 0; + total = 0; + for (i = 0; i < sizeof(dir); i++) + { + ppls = get_dir(DATA_DIR + "login/" + dir[i] + "/", -1); + for (j = 0; j < sizeof(ppls); j++) + { + reset_eval_cost(); + if (sscanf(ppls[j][0], "%s.o", name) == 1) + { + examine_player(name, ppls[j][2]); + count++; + } + } + total += j; + message("system", ESC + "[1A" + ESC + "[256D" HIG "进度:" + process_bar((i + 1) * 100 / sizeof(dir)) + "\n", + me ? me : filter_array(all_interactive(), (: wizardp :))); + } +} + +private void examine_player(string name, int last_touched) +{ + object login_ob; + object user_ob; + int online; + mixed *st; + + if (!last_touched) + { + st = stat(DATA_DIR + "login/" + name[0..0] + "/" + name + __SAVE_EXTENSION__); + + if (!arrayp(st) || sizeof(st) < 3) + // 可能没有这个文件 + return; + + last_touched = st[1]; + } + + login_ob = new (LOGIN_OB); + login_ob->set("id", name); + + if (!login_ob->restore()) + { + destruct(login_ob); + return; + } + + if (login_ob->query("id") != name) + { + destruct(login_ob); + return; + } + + if (!objectp(user_ob = find_player(name))) + { + online = 0; + user_ob = LOGIN_D->make_body(login_ob); + if (!user_ob) + { + destruct(login_ob); + return; + } + + if (!user_ob->restore()) + { + destruct(login_ob); + destruct(user_ob); + return; + } + } + else + online = 1; + + CACHE_D->insert(user_ob, last_touched); + + if (!online) + { + destruct(user_ob); + } + destruct(login_ob); +} + +int help(object me) +{ + write(@HELP +指令格式:cache -all + +缓存玩家数据到·data/db.sqlite· + +HELP + ); + return 1; +} diff --git a/d/register/yanluodian.c b/d/register/yanluodian.c index 2c96f3462..51a6d2fcd 100644 --- a/d/register/yanluodian.c +++ b/d/register/yanluodian.c @@ -263,6 +263,7 @@ int do_born(string arg) me->move(obj); me->set("mud_age", 0); me->set("age", 14); + me->set("registered", 1); me->save(); HELP_CMD->main(me, "topics"); diff --git a/include/globals.h b/include/globals.h index 8fea21a23..6a4aa0710 100644 --- a/include/globals.h +++ b/include/globals.h @@ -34,6 +34,7 @@ #define ALIAS_D "/adm/daemons/aliasd" #define BACKUP_D "/adm/daemons/backupd" #define BAN_D "/adm/daemons/band" +#define CACHE_D "/adm/daemons/cached" #define CHANNEL_D "/adm/daemons/channeld" #define CHAR_D "/adm/daemons/chard" #define CHINESE_D "/adm/daemons/chinesed" @@ -110,6 +111,7 @@ #define CHALLENGER "/inherit/char/challenger" #define CHARACTER "/inherit/char/char" #define COMBINED_ITEM "/inherit/item/combined" +#define DATABASE "/inherit/misc/database.c" #define EQUIP "/inherit/misc/equip" #define FIGHTER "/inherit/char/fighter" #define FORCE "/inherit/skill/force" @@ -155,7 +157,6 @@ #define F_CONDITION "/feature/condition.c" #define F_CUTABLE "/feature/cutable.c" #define F_DAMAGE "/feature/damage.c" -#define F_DATABASE "/feature/database.c" #define F_DBASE "/feature/dbase.c" #define F_DEALER "/feature/dealer.c" #define F_EDIT "/feature/edit.c" diff --git a/feature/database.c b/inherit/misc/database.c similarity index 100% rename from feature/database.c rename to inherit/misc/database.c