介绍
我已经尝试构建这个项目好几个星期了,并尝试了多种我无法理解的解决方案。让我稍微描述一下这个项目。它是一个基于文本的服务器,玩家可以登录(通过 telnet 或客户端),本质上就像一个 MUD。然后他们可以创建“对象”并与之交互,赋予它们“动词”和“属性”。
服务器基本上只是一个“对象”的数据库,每个对象都有一个 ID、一个名称、一个位置(这是另一个对象)、它的内容列表(对象)和一些其他标志。对象可以有“动词”和“属性”。属性只是存储的数据(字符串、整数、浮点数、w/e)。动词是方法/功能。使用诸如“将某些东西放入容器”之类的命令与对象进行交互。服务器的旧版本已经存在,它被称为 LambdaMOO。我正在尝试重新创建它,因为它已经很长时间没有更新了。
您可以更深入地了解对象、动词和属性应如何工作:http ://bit.ly/17XIqjY
一个例子
让我描述一下我想要什么。想象我们有一个对象。对象 #256,它被称为“按钮”。它具有属性“计数”以及从其父级继承的所有默认属性(即“描述”)。它上面有一个“动词”,称为“推”。该动词包含以下代码:
this.count += 1;
this.description = "This button has been pushed " + this.count + " times.";
player.tell("You press the button and feel a chill run down your spine.");
当玩家在服务器上键入“push button”时,“push”动词将运行并输出
You press the button and feel a chill run down your spine.
如果您然后look
在按钮上,您将看到它的更新描述。
请注意,player
在上面的脚本中是指执行动词的玩家的对象。tell
是另一个动词,在玩家对象上。然而,tell
动词有一个标志,表示它可以从其他动词执行。
什么语言?
我的主要问题是“动词”可以使用哪些语言?我试过使用 node.js 和 'vm' 库。我试过使用 C# 来解析 C#。我试过使用 C# 来解析 JavaScript。我不断遇到的问题是我无法控制动词和属性的权限。如果我将它们转换为 JavaScript 中的文字函数,我无法确定它们在哪个对象上运行以及它应该具有哪些权限。如果用户在另一个用户对象上调用函数,如果权限不正确,我无法拦截该调用并停止它。对于动词代码使用哪种语言,我并不完全在意,它只需要“沙盒化”即可。属性仅在用户设置为可读/可写时才需要,与动词相同。
我还需要能够将这些变量注入动词:(主要由键入的命令确定,除非该动词是从另一个动词调用的)
player (object) the player who typed the command
this (object) the object on which this verb was found
caller (object) this will be the same as ‘player’, unless another
verb calls the command in which case it is the object
containing that verb.
verb (string) the first word of the command
argstr (string) everything after the first word of the command
args (list of strings) a list of the words in ‘argstr’
dobjstr (string) the direct object string found during parsing
dobj (object) the direct object value found during matching
prepstr (string) the prepositional phrase found during parsing
iobjstr (string) the indirect object string
iobj (object) the indirect object value
我还需要能够从任何其他对象访问任何对象(只要权限有效)。
// Object #128. Verb: multiply Prep: this none this Perms: +r +x
return (args[0] * args[1]);
// Object #256. Verb: square Prep: this none this Perms: +r +x
return #128:multiply(args[0], args[0]);
// Object #512. Verb: touch Prep: any any this Perms: +r
// Has a property (int) 'size' on it.
this.size = #256:square(this.size);
this.description = "It's a large button, it spans " + this.size + " metres.";
player:tell("You touch the button, it gets bigger.");
然后push button
,用户可以将按钮对象的 size 属性平方。
推荐阅读
我强烈建议您阅读http://bit.ly/17XIqjY上的文档,以更深入地了解系统应该如何工作。
还建议您阅读以下文档,因为 μMOO 基于 LambdaMOO 及其方法: