-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加CACHE_D,增加cache指令,在.env中配置
CACHE_DATA : true
可缓存玩家数据
- Loading branch information
Showing
10 changed files
with
285 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// cache.c | ||
#include <ansi.h> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.