// named.c 保存所有玩家的名字
#include
#pragma optimize
#pragma save_binary
inherit F_SAVE;
inherit F_DBASE;
static string *family_name = ({ "东方", "独孤", "慕容", "欧阳", "司马",
"西门", "尉迟", "长孙", "诸葛" });
void create()
{
seteuid(getuid());
restore();
}
void remove()
{
save();
}
void mud_shutdown()
{
save();
}
// the name PATH store in dbase
#define PATH(name) (name[0..1] + "/" + name)
// remove the mapping of a name & id
public void remove_name(string name, string id)
{
string old;
string *ms;
if (! stringp(name) || strlen(name) < 2 ||
undefinedp(old = query(PATH(name))))
return;
if (! stringp(old) ||
! sizeof(ms = explode(old, "/") - ({ id })))
{
delete(PATH(name));
if (! sizeof(query(name[0..1])))
delete(name[0..1]);
save();
return;
}
set(PATH(name), implode(ms, "/"));
save();
}
// record the mapping of a name & id
public void map_name(string name, string id)
{
string old;
string *ms;
if (! stringp(name) || strlen(name) < 2)
return;
if (! stringp(old = query(PATH(name))))
set(PATH(name), id);
else
{
ms = explode(old, "/");
if (member_array(id, ms) != -1)
// alreay exist
return;
ms += ({ id });
set(PATH(name), implode(ms, "/"));
}
save();
}
// check name
public string who_is(string name)
{
string id;
if (! stringp(name) || strlen(name) < 2 ||
! stringp(id = query(PATH(name))))
return "没有人叫这个名字。\n";
return "泥潭中叫" + name + "的有:" + id + "\n";
}
// check the id - name
private int really_exist(string name, string id)
{
object ob;
object user;
int result;
if (! stringp(id))
return 0;
ob = find_player(id);
if (objectp(ob))
{
if (ob->name(1) == name)
return 1;
else
return 0;
}
// read the data of user object
ob = new(LOGIN_OB);
ob->set("id", id);
user = LOGIN_D->make_body(ob);
destruct(ob);
if (! objectp(user))
// no such user
return 0;
result = user->restore() && (user->name(1) == name);
destruct(user);
return result;
}
// assure the id matched the name
public void assure_map_name(string name)
{
string id;
string *ids;
if (! stringp(id = query(PATH(name))))
// null name
return;
ids = explode(id, "/");
// examine all the id
ids = filter_array(ids, (: really_exist, name :));
if (! sizeof(ids))
{
// no id matched
delete(PATH(name));
return;
}
// reset name-id mapping
id = implode(ids, "/");
set(PATH(name), id);
}
// If the name to be a new name, dose it invalid ?
public string invalid_new_name(string name)
{
string id;
int i;
int l;
if (! stringp(name) || strlen(name) < 2)
return "不能使用空名字。\n";
if (member_array(name, family_name) != -1)
return "不能使用复姓作为名字。\n";
// 和对有关ID-名字的信息
assure_map_name(name);
if (id = query(PATH(name)))
return "这个名字和 " + id + " 的名字重复了。\n";
if (strlen(name) < 4)
return 0;
l = strlen(name);
for (i = 0; i <= l - 4; i++)
{
// 和对有关ID-名字的信息
assure_map_name(name[i..i + 3]);
if (i + 6 <= l)
assure_map_name(name[i..i + 5]);
if (member_array(name[i..i + 3], family_name) == -1 &&
stringp(id = query(PATH(name[i..i + 3]))))
return "这个名字和 " + id + " 的名字太接近了。\n";
if ((i + 6 <= l) &&
stringp(id = query(PATH(name[i..i + 5]))))
return "这个名字和 " + id + " 的名字太接近了。\n";
}
return 0;
}
// change a user's name
public varargs string change_name(object me, string new_name, int force)
{
string result;
mapping dbase;
if (stringp(me->name(1)))
{
remove_name(me->name(1), me->query("id"));
result = invalid_new_name(new_name);
} else
result = 0;
if (force || ! result)
{
dbase = me->query_entire_dbase();
dbase["name"] = new_name;
}
map_name(me->name(1), me->query("id"));
return result;
}
// how many name
public int query_total_name()
{
string *ks;
int total;
int i;
total = 0;
ks = keys(query_entire_dbase());
for (i = 0; i < sizeof(ks); i++)
total += sizeof(query(ks[i]));
return total;
}
string query_save_file() { return DATA_DIR "named"; }
// 中文姓名检测程序
#include
inherit F_SAVE;
inherit F_DBASE;
#define NAME_FILE "/data/name.o"
mapping name=([ ]);
void create()
{
seteuid(ROOT_UID);
set("channel_id", "姓名检测精灵");
if (file_size(NAME_FILE)<=0)
{
name=([]);
save();
}
restore();
}
string query_save_file()
{
return NAME_FILE;
}
int check_name(string user_name)
{
int i,len;
string *key=keys(name);
for (i=0;i < sizeof(name);i++) {
if (user_name==name[key[i]]) {
// write(name[key[i]]);
write(WHT"\n对不起,这个名字和 "HIC+key[i]+NOR WHT" 的
名字是一样的。\n"NOR);
return 0;
}
else {
if (strlen(user_name) > strlen(name[key[i]])&&
strsrch(user_name,name[key[i]])!=-1&&strlen(user_name)>
=4) {
write(WHT"\n对不起,这个名字和 "HIC+key[i]+NOR
WHT" 的名字过于相似了。\n"NOR);
return 0;
}
if (strlen(user_name) < strlen(name[key[i]])&&strlen(user
_name)>=4&&
strsrch(name[key[i]],user_name)!=-1) {
write(WHT"\n对不起,这个名字和 "HIC+key[i]+NOR
WHT" 的名字太过相似了。\n"NOR);
return 0;
}
}
}
return 1;
}
void add_name(string id,string user_name)
{
if (stringp(id)&&stringp(user_name)
&&!stringp(name[id])) {
name[id]=user_name;
save();
restore();
log_file("name",sprintf("属于 %s 的中文姓名 %s 被添加到姓名数据
中。\n",id,name[id]));
return;
}
}
void del_name(string id)
{
if (stringp(id)&&stringp(name[id])) {
log_file("name",sprintf("属于 %s 的中文姓名 %s 从姓名数据中删除
。\n",id,name[id]));
map_delete(name,id);
save();
restore();
return;
}
}
string who_is(string user_name)
{
string *names = keys(name);
int i;
string msg = WHT "江湖中叫" + user_name + NOR WHT + "的有 ", *user = ({
});
for (i = 0; i < sizeof(names); i++)
{
if (name[names[i]] == user_name)
{
user += ({ names[i] });
}
}
if (! sizeof(user))
return WHT"江湖中没有叫" + user_name + NOR WHT "这个名字的。\n"
NOR;
else
{
msg += (implode(user, "/") + "。" NOR);
return msg;
}
}
大家比较比较?
尊重作者 转载请注明出处52mud.com