Skip to content

Commit 8b89470

Browse files
committed
增加CACHE_D,增加cache指令,在.env中配置CACHE_DATA : true可缓存玩家数据
1 parent 624c3ca commit 8b89470

10 files changed

Lines changed: 285 additions & 10 deletions

File tree

.vscode/c_cpp_properties.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"defines": [
1212
"FLUFFOS",
1313
"MUDOS",
14-
"__PACKAGE_DB__"
14+
"__PACKAGE_DB__",
15+
"__USE_SQLITE3__"
1516
],
1617
"cStandard": "c89",
1718
"cppStandard": "c++11",

adm/daemons/cached.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* 玩家存档缓存接口
3+
* 缓存玩家数据到db.sqlite,方便网页、全服排行等调用
4+
*/
5+
#ifdef __USE_SQLITE3__
6+
// 初始化数据库
7+
mixed init_db();
8+
// 玩家数据缓存接口
9+
mixed insert(object user, int last_touched);
10+
// 玩家数据更新接口
11+
mixed update(object user);
12+
// 玩家数据删除接口
13+
14+
nosave object db;
15+
16+
void create()
17+
{
18+
db = new (DATABASE, "", "/data/db.sqlite", "", __USE_SQLITE3__);
19+
}
20+
21+
mixed init_db()
22+
{
23+
mixed res;
24+
25+
db->sql("DROP TABLE IF EXISTS `users`")->exec();
26+
res = db->sql("CREATE TABLE IF NOT EXISTS `users` (
27+
`id` VARCHAR(10) PRIMARY KEY NOT NULL,
28+
`name` VARCHAR(10) NOT NULL,
29+
`title` VARCHAR(50) DEFAULT NULL,
30+
`master` VARCHAR(10) DEFAULT NULL,
31+
`mobile` INTEGER DEFAULT NULL,
32+
`age` INTEGER DEFAULT NULL,
33+
`qi` INTEGER DEFAULT NULL,
34+
`jing` INTEGER DEFAULT NULL,
35+
`neili` INTEGER DEFAULT NULL,
36+
`jingli` INTEGER DEFAULT NULL,
37+
`combat_exp` INTEGER DEFAULT NULL,
38+
`kill` INTEGER DEFAULT NULL,
39+
`die` INTEGER DEFAULT NULL,
40+
`updated_at` INTEGER DEFAULT NULL) ")->exec();
41+
42+
if (stringp(res))
43+
{
44+
env("CACHE_DATA", 0);
45+
}
46+
47+
return res;
48+
}
49+
50+
mixed insert(object user, int last_touched)
51+
{
52+
mixed res;
53+
mapping my = user->query_entire_dbase();
54+
string master = mapp(my["family"]) ? my["family"]["master_name"] : "";
55+
int kill = 0, die = 0;
56+
57+
if (mapp(my["combat"]))
58+
{
59+
kill = my["combat"]["MKS"] + my["combat"]["PKS"];
60+
die = my["combat"]["dietimes"];
61+
}
62+
63+
res = db->table("users")->insert(([
64+
"id" : my["id"],
65+
"name" : my["name"],
66+
"title" : my["title"],
67+
"mobile" : my["mobile"],
68+
"age" : my["age"],
69+
"qi" : my["max_qi"],
70+
"jing" : my["max_jing"],
71+
"neili" : my["max_neili"],
72+
"jingli" : my["max_jingli"],
73+
"combat_exp" : my["combat_exp"],
74+
"master" : master,
75+
"kill" : kill,
76+
"die" : die,
77+
"updated_at" : last_touched,
78+
]));
79+
80+
return res;
81+
}
82+
83+
mixed update(object user)
84+
{
85+
mixed res;
86+
mapping my = user->query_entire_dbase();
87+
string master = mapp(my["family"]) ? my["family"]["master_name"] || "" : "";
88+
int kill = 0, die = 0;
89+
90+
if (mapp(my["combat"]))
91+
{
92+
kill = my["combat"]["MKS"] + my["combat"]["PKS"];
93+
die = my["combat"]["dietimes"];
94+
}
95+
96+
res = db->table("users")->where("id", my["id"])->update(([
97+
"name" : my["name"],
98+
"title" : my["title"],
99+
"mobile" : my["mobile"],
100+
"age" : my["age"],
101+
"qi" : my["max_qi"],
102+
"jing" : my["max_jing"],
103+
"neili" : my["max_neili"],
104+
"jingli" : my["max_jingli"],
105+
"combat_exp" : my["combat_exp"],
106+
"master" : master,
107+
"kill" : kill,
108+
"die" : die,
109+
"updated_at" : time(),
110+
]));
111+
112+
return res;
113+
}
114+
115+
mixed delete(object user)
116+
{
117+
mixed res;
118+
119+
res = db->table("users")->where("id", user->query("id"))->delete ();
120+
121+
return res;
122+
}
123+
#endif

adm/daemons/logind.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -800,13 +800,17 @@ private void init_new_player(object user)
800800

801801
// 记录名字
802802
NAME_D->map_name(user->query("name"), user->query("id"));
803-
804803
// 设置必要的环境参数
805804
user->set("env/auto_regenerate", 1);
806805
user->set("env/auto_get", 1);
807806
user->set("env/wimpy", 60);
808807
//设定不自动转宗师频道
809808
user->set("env/no_autoultra", 1);
809+
// 缓存到数据库
810+
if (env("CACHE_DATA"))
811+
{
812+
CACHE_D->insert(user, time());
813+
}
810814
}
811815

812816
varargs void enter_world(object ob, object user, int silent)
@@ -921,8 +925,6 @@ varargs void enter_world(object ob, object user, int silent)
921925
if (shoe && (! environment(shoe) || ! shoe->query("equipped")))
922926
destruct(shoe);
923927

924-
user->set("registered", 1);
925-
// user->set("born",1);
926928
if (! silent)
927929
{
928930
string term_type;
@@ -1016,8 +1018,6 @@ varargs void enter_world(object ob, object user, int silent)
10161018
{
10171019
tell_object(this_player(), HBRED "\n你的管理密码没有升级为SHA512加密,为了账号安全请使用" HIY " passwd " NOR HBRED "修改管理密码。" NOR "\n");
10181020
}
1019-
1020-
write("\n");
10211021
/*
10221022
// 检查是否有新邮件未读
10231023
new_mail_n = get_info(user->query("id"), "newmail", "", 0);

adm/daemons/mysqld.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
inherit F_DATABASE;
1+
inherit DATABASE;
22
// 调用连接对象更新bbs.mud.ren网站账号
33
varargs mixed update(object ob, string host, string db, string user)
44
{

adm/daemons/updated.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,11 @@ string clear_user_data(string user, string cat)
300300
// save the data of the user
301301
if (flag)
302302
ob->save();
303-
303+
// 删除数据库缓存
304+
if (env("CACHE_DATA"))
305+
{
306+
CACHE_D->delete(ob);
307+
}
304308
// Destrut the object if create temporate
305309
if (login_ob)
306310
{
@@ -363,6 +367,9 @@ void born_player(object me)
363367
// 性格不符不会愤怒之心
364368
if (me->query("character") != "光明磊落" && me->query("character") != "心狠手辣")
365369
files -= ({"wrath"});
370+
// 性格不符不会鬼话连篇
371+
if (me->query("character") != "狡黠多变" && me->query("character") != "阴险奸诈")
372+
files -= ({"trick"});
366373

367374
// 先天膂力 < 20 不会麒麟血臂
368375
if (me->query("str") < 20)
@@ -485,7 +492,7 @@ void zhuan_player(object me)
485492
me->delete ("schedule"); // 计划记录
486493
me->delete ("skybook"); // 天书记录(三丹记录)
487494

488-
me->delete ("luohan_winner"); //过阵记录
495+
me->delete ("luohan_winner"); // 过阵记录
489496
me->delete ("story"); // 中的故事
490497

491498
me->delete ("DiZangPass"); // 转世任务
@@ -495,6 +502,7 @@ void zhuan_player(object me)
495502

496503
//获取转生前门派,用于脱离时无损判断 by 薪有所属
497504
menpai1 = me->query("family/family_name");
505+
//(取消无损背叛师门)
498506
//me->set("old_family_name",menpai1);
499507
me->set("reborn/family/" + menpai1, 1);
500508
me->delete ("reborn/family/mark"); //删除门派脱离记录

clone/user/user.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ int save()
160160
res = ::save();
161161
}
162162

163+
// 缓存到数据库
164+
if (env("CACHE_DATA"))
165+
{
166+
CACHE_D->update(me);
167+
}
168+
163169
#if INSTALL_EXAMINE
164170
EXAMINE_D->examine_player(me);
165171
#endif

cmds/adm/cache.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// cache.c
2+
#include <ansi.h>
3+
4+
inherit F_CLEAN_UP;
5+
6+
private void search_dir(object me);
7+
private void examine_player(string name, int last_touched);
8+
9+
void create() { seteuid(getuid()); }
10+
11+
int main(object me, string arg)
12+
{
13+
if (!SECURITY_D->valid_grant(me, "(admin)"))
14+
return 0;
15+
16+
if (arg != "-all")
17+
return notify_fail("指令格式:cache -all\n");
18+
19+
message_system("系统进行数据处理中,请耐心等候...\n");
20+
write(HIG "系统开始缓存所有玩家基本数据...\n" HIG "进度:" + process_bar(0) + "\n");
21+
22+
search_dir(me);
23+
24+
return 1;
25+
}
26+
27+
private void search_dir(object me)
28+
{
29+
string *dir;
30+
string name;
31+
mixed *ppls;
32+
int count;
33+
int total;
34+
int i;
35+
int j;
36+
37+
if (!is_root(previous_object()))
38+
return 0;
39+
// 初始化数据库
40+
CACHE_D->init_db();
41+
dir = get_dir(DATA_DIR + "login/");
42+
43+
count = 0;
44+
total = 0;
45+
for (i = 0; i < sizeof(dir); i++)
46+
{
47+
ppls = get_dir(DATA_DIR + "login/" + dir[i] + "/", -1);
48+
for (j = 0; j < sizeof(ppls); j++)
49+
{
50+
reset_eval_cost();
51+
if (sscanf(ppls[j][0], "%s.o", name) == 1)
52+
{
53+
examine_player(name, ppls[j][2]);
54+
count++;
55+
}
56+
}
57+
total += j;
58+
message("system", ESC + "[1A" + ESC + "[256D" HIG "进度:" + process_bar((i + 1) * 100 / sizeof(dir)) + "\n",
59+
me ? me : filter_array(all_interactive(), (: wizardp :)));
60+
}
61+
}
62+
63+
private void examine_player(string name, int last_touched)
64+
{
65+
object login_ob;
66+
object user_ob;
67+
int online;
68+
mixed *st;
69+
70+
if (!last_touched)
71+
{
72+
st = stat(DATA_DIR + "login/" + name[0..0] + "/" + name + __SAVE_EXTENSION__);
73+
74+
if (!arrayp(st) || sizeof(st) < 3)
75+
// 可能没有这个文件
76+
return;
77+
78+
last_touched = st[1];
79+
}
80+
81+
login_ob = new (LOGIN_OB);
82+
login_ob->set("id", name);
83+
84+
if (!login_ob->restore())
85+
{
86+
destruct(login_ob);
87+
return;
88+
}
89+
90+
if (login_ob->query("id") != name)
91+
{
92+
destruct(login_ob);
93+
return;
94+
}
95+
96+
if (!objectp(user_ob = find_player(name)))
97+
{
98+
online = 0;
99+
user_ob = LOGIN_D->make_body(login_ob);
100+
if (!user_ob)
101+
{
102+
destruct(login_ob);
103+
return;
104+
}
105+
106+
if (!user_ob->restore())
107+
{
108+
destruct(login_ob);
109+
destruct(user_ob);
110+
return;
111+
}
112+
}
113+
else
114+
online = 1;
115+
116+
CACHE_D->insert(user_ob, last_touched);
117+
118+
if (!online)
119+
{
120+
destruct(user_ob);
121+
}
122+
destruct(login_ob);
123+
}
124+
125+
int help(object me)
126+
{
127+
write(@HELP
128+
指令格式cache -all
129+
130+
缓存玩家数据到·data/db.sqlite·
131+
132+
HELP
133+
);
134+
return 1;
135+
}

d/register/yanluodian.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ int do_born(string arg)
263263
me->move(obj);
264264
me->set("mud_age", 0);
265265
me->set("age", 14);
266+
me->set("registered", 1);
266267
me->save();
267268
HELP_CMD->main(me, "topics");
268269

include/globals.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#define ALIAS_D "/adm/daemons/aliasd"
3535
#define BACKUP_D "/adm/daemons/backupd"
3636
#define BAN_D "/adm/daemons/band"
37+
#define CACHE_D "/adm/daemons/cached"
3738
#define CHANNEL_D "/adm/daemons/channeld"
3839
#define CHAR_D "/adm/daemons/chard"
3940
#define CHINESE_D "/adm/daemons/chinesed"
@@ -110,6 +111,7 @@
110111
#define CHALLENGER "/inherit/char/challenger"
111112
#define CHARACTER "/inherit/char/char"
112113
#define COMBINED_ITEM "/inherit/item/combined"
114+
#define DATABASE "/inherit/misc/database.c"
113115
#define EQUIP "/inherit/misc/equip"
114116
#define FIGHTER "/inherit/char/fighter"
115117
#define FORCE "/inherit/skill/force"
@@ -155,7 +157,6 @@
155157
#define F_CONDITION "/feature/condition.c"
156158
#define F_CUTABLE "/feature/cutable.c"
157159
#define F_DAMAGE "/feature/damage.c"
158-
#define F_DATABASE "/feature/database.c"
159160
#define F_DBASE "/feature/dbase.c"
160161
#define F_DEALER "/feature/dealer.c"
161162
#define F_EDIT "/feature/edit.c"

0 commit comments

Comments
 (0)