-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilerplate.go
153 lines (140 loc) · 3.8 KB
/
boilerplate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package output
import (
"bytes"
"encoding/gob"
"fmt"
C "github.com/ionous/sashimi/console"
"github.com/ionous/sashimi/metal"
"github.com/ionous/sashimi/minicon"
R "github.com/ionous/sashimi/runtime"
"github.com/ionous/sashimi/script"
"github.com/ionous/sashimi/standard/framework"
"github.com/ionous/sashimi/util/ident"
"io"
"io/ioutil"
"os"
)
// implement IConsole for MiniCon
// FIX: rename to standard.miniconsole maybe?
type MiniConsole struct {
*minicon.MiniCon
}
func (this MiniConsole) Readln() (string, bool) {
return this.Update(), true
}
// simplest interface:
func Run(scriptCallback script.InitCallback) {
script.AddScript(scriptCallback)
RunGame(ParseCommandLine())
}
func RunGame(opt Options) (err error) {
script := script.InitScripts()
return RunScript(script, opt)
}
func GetWriter(opt Options) (ret io.Writer) {
if opt.verbose {
ret = os.Stderr
} else {
ret = ioutil.Discard
}
return
}
func GetConsole(opt Options) (ret C.IConsole) {
if opt.hasConsole {
ret = opt.cons
} else if opt.text {
ret = C.NewConsole()
} else {
ret = MiniConsole{minicon.NewMiniCon()}
}
return
}
func RunScript(script *script.Script, opt Options) (err error) {
writer := GetWriter(opt)
if model, e := script.Compile(writer); e != nil {
err = e
} else if opt.dump {
var b bytes.Buffer
enc := gob.NewEncoder(&b)
gob.Register(ident.Id(""))
if e := enc.Encode(model.Model); e != nil {
panic(e)
}
fmt.Println(fmt.Sprintf("size: %d(b) %.2f(k)", b.Len(), float64(b.Len())/1024.0))
model.Model.PrintModel(func(args ...interface{}) { fmt.Println(args...) })
} else {
cons := GetConsole(opt)
defer cons.Close()
//
vals := make(metal.ObjectValueMap)
modelApi := metal.NewMetal(model.Model, vals)
cfg := R.NewConfig().
SetCalls(model.Calls).
SetOutput(NewStandardOutput(cons, writer)).
SetParentLookup(framework.NewParentLookup(modelApi))
g := cfg.MakeGame(modelApi)
err = PlayGame(cons, g)
}
if err != nil {
panic(err)
}
return
}
func PlayGame(cons C.IConsole, g R.Game) (err error) {
return PlayGameUpdate(cons, g, nil)
}
func PlayGameUpdate(cons C.IConsole, g R.Game, endFrame func()) (err error) {
if game, e := framework.NewStandardGame(g); e != nil {
err = e
} else if game, e := game.Start(); e != nil {
err = e
} else if status, ok := g.GetInstance(ident.MakeId("status bar")); !ok {
err = fmt.Errorf("couldn't find status bar")
} else if statusLeft, ok := status.FindProperty("left"); !ok {
err = fmt.Errorf("couldn't find left status")
} else if statusRight, ok := status.FindProperty("right"); !ok {
err = fmt.Errorf("couldn't find right status")
} else {
var left, right string
for {
// update the status bar as needed....
// ( FIX: status change before the text associated with the change has been teletyped )
// ( control code handlers -- needed for changing color in text -- might be better )
newleft, newright := statusLeft.GetValue().GetText(), statusRight.GetValue().GetText()
if left != newleft || right != newright {
if mini, ok := cons.(MiniConsole); ok {
mini.Status.Left, mini.Status.Right = newleft, newright
mini.RefreshDisplay()
}
}
// read new input
if s, ok := cons.Readln(); !ok {
break
} else {
mini, useMini := cons.(MiniConsole)
if useMini {
mini.Flush() // print all remaining teletype text
mini.Println()
mini.Println(">", s)
}
if e := game.Input(s); e != nil {
cons.Println(e.Error())
} else {
// difficult question, if we end the story, do we want to end the turn?
// currently we dont.
// FIX: also we dont call "endFrame" -- should we?
if game.IsQuit() || game.IsComplete() {
if useMini && !game.IsQuit() {
mini.Update()
}
break
}
}
if endFrame != nil {
endFrame()
}
}
}
}
return err
}