2011-11-24 70 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

相關問題