2011-11-24 67 views
0

我是新来snakeyaml和yaml一般。我需要它来存储关于“房间”的信息MUD这是使用YAML的有效方法吗?

为客房的条目会是这个样子:

room: 
    id: 12 
    entry: "Long string" 
    description: "Longer more precise string" 
    objects: 
    ids: 1,23 

object: 
    id: 1 
    name: "chest" 
    description: "looks pretty damn old" 
    on-text: "the chest has been opened!" 
    off-text: "the chest has been closed!" 

基本上,每个房间都有一个id和一些文字显示给玩家,当他们进入/搜索。它也有一组“对象”,它们本身在同一个yaml文件中声明。

这个配置是否在我的yaml文件中?另外,我需要提取到阵列中的每个房间,每个对象,所以它看起来是这样的:

[12, "long string", "Longer more precise string", [1, "chest", "looks pretty damn old", "the chest has been opened!", "the chest has been closed!"], [ ... item 23 ... ]] 

这种配置很容易让我来解析该文件并创建GenericRoom和GenericObject类通过使一个单循环并通过数组位置引用每个值。这是SnakeYAML能为我做的吗?我一直在玩一些例子,但是在实际的YAML中缺乏知识使我很难获得好的结果。

回答

2

有了这个,你要的对象连接到房间自己:

room: 
    id: 12 
    entry: "Long string" 
    objects: [1, 23] 

objects: 
    - { id: 1, text: bla bla } 
    - { id: 2, text: bla bla 2 } 
    - { id: 23, text: bla bla 23} 

或SnakeYAML可以从锚和别名受益: (使用别名前锚必须定义)

objects: 
    - &id001 {id: 1, text: bla bla } 
    - &id002 {id: 2, text: bla bla 2 } 
    - &id023 {id: 23, text: bla bla 23 } 

room: 
    id: 12 
    entry: "Long string" 
    objects: [ *id001, *id023] 

(你可以在这里查看你的文档:http://www.yaml.org/spec/1.2/spec.html#id2765878

相关问题