go-pattern-examples/behavior/09_memento/memento_test.go

45 lines
843 B
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package memento
2020-05-05 13:44:47 +03:00
import (
"testing"
"time"
)
2020-04-21 17:50:21 +03:00
2020-05-05 13:44:47 +03:00
func TestGameArchive(t *testing.T) {
2020-04-21 17:50:21 +03:00
2020-05-05 13:44:47 +03:00
gamerole := GamePlayer{hp: 1000, mp: 232, level: 20}
2020-04-21 17:50:21 +03:00
2020-05-05 13:44:47 +03:00
datakeeper := RoleStatusCaretaker{memens: make(map[string]*RoleStatusMemento)}
archive1 := gamerole.Create("第一次存档")
//交给管数据的人,存起来
datakeeper.SaveStatus(archive1)
//模拟,随机玩会儿游戏
time.Sleep(time.Millisecond * 1132)
//更新角色当前状态
gamerole = GamePlayer{hp: 500, mp: 10, level: 30}
//看一下状态
gamerole.Status()
archive2 := gamerole.Create("第二次存档")
//交给管数据的人,存起来
datakeeper.SaveStatus(archive2)
//准备恢复第一次的存档
//查找档案
restore1 := datakeeper.RetriveStatus("第一次存档")
//载入档案
gamerole.Load(restore1)
//看一下状态
gamerole.Status()
2020-04-21 17:50:21 +03:00
}