refactor stuff

pull/1/head
vilmibm 2023-02-12 00:28:20 +00:00
parent 336622ca62
commit 4a881d3b8b
1 changed files with 46 additions and 64 deletions

View File

@ -34,8 +34,9 @@ const maxInputLength int = 4000
type TownApplication struct { type TownApplication struct {
Email string Email string
HowFound string
// TODO Why string
Where string
} }
type streams struct { type streams struct {
@ -97,26 +98,17 @@ func cli(s *streams) error {
player := newCharacter("you", "TODO") player := newCharacter("you", "TODO")
currentScene := "start"
sceneTransition := func(text string) {
fmt.Fprintln(msgScroll, heredoc.Doc(`
[purple]----------[-:-:-]
`))
fmt.Fprintln(msgScroll, text)
}
type scene struct { type scene struct {
Name string
Description string Description string
InBuff io.ReadWriter InBuff io.ReadWriter
InLength int InLength int
Host *character Host *character
} }
scenes := map[string]*scene{ scenes := []*scene{
"start": { {
Name: "start",
Description: heredoc.Doc(` Description: heredoc.Doc(`
You open your eyes. You open your eyes.
@ -136,7 +128,8 @@ func cli(s *streams) error {
Host: newCharacter("wire guy", "a lil homonculus made of discarded computer cables"), Host: newCharacter("wire guy", "a lil homonculus made of discarded computer cables"),
InBuff: bytes.NewBuffer([]byte{}), InBuff: bytes.NewBuffer([]byte{}),
}, },
"nodded": { {
Name: "nodded",
Description: heredoc.Doc(` Description: heredoc.Doc(`
The workshop fades away. You hear the sound of a dial up modem The workshop fades away. You hear the sound of a dial up modem
in the distance. in the distance.
@ -158,7 +151,8 @@ func cli(s *streams) error {
InBuff: bytes.NewBuffer([]byte{}), InBuff: bytes.NewBuffer([]byte{}),
Host: newCharacter("the shrike", "a little grey bird. it has a pretty song."), Host: newCharacter("the shrike", "a little grey bird. it has a pretty song."),
}, },
"leaned": { {
Name: "leaned",
Description: heredoc.Doc(` Description: heredoc.Doc(`
You sink backwards into the forest. You find yourself floating in darkness. You sink backwards into the forest. You find yourself floating in darkness.
@ -176,7 +170,8 @@ func cli(s *streams) error {
InBuff: bytes.NewBuffer([]byte{}), InBuff: bytes.NewBuffer([]byte{}),
Host: newCharacter("the vcr", "a black and grey VCR from 1991"), Host: newCharacter("the vcr", "a black and grey VCR from 1991"),
}, },
"spun": { {
Name: "spun",
Description: heredoc.Doc(` Description: heredoc.Doc(`
You realize your eyes have been shut. You open them and, in an instant, You realize your eyes have been shut. You open them and, in an instant,
the neon grid and polygons are gone. You're in a convenience store. Outside the neon grid and polygons are gone. You're in a convenience store. Outside
@ -199,7 +194,8 @@ func cli(s *streams) error {
InBuff: bytes.NewBuffer([]byte{}), InBuff: bytes.NewBuffer([]byte{}),
Host: newCharacter("the mop", "a greying mop with a wooden handle."), Host: newCharacter("the mop", "a greying mop with a wooden handle."),
}, },
"done": { {
Name: "spun",
Description: heredoc.Doc(` Description: heredoc.Doc(`
thank you for applying to tilde.town! thank you for applying to tilde.town!
@ -213,6 +209,27 @@ func cli(s *streams) error {
}, },
} }
sceneIx := 0
currentScene := scenes[sceneIx]
advanceScene := func(fromScene, sorryMsg string) {
if currentScene.Name != fromScene {
return
}
if currentScene.InLength == 0 {
fmt.Fprintln(msgScroll, currentScene.Host.Say(sorryMsg))
return
}
sceneIx++
currentScene = scenes[sceneIx]
fmt.Fprintln(msgScroll, heredoc.Doc(`
[purple]----------[-:-:-]
`))
fmt.Fprintln(msgScroll, currentScene.Description)
}
handleInput := func(msg string) { handleInput := func(msg string) {
trimmed := strings.TrimSpace(msg) trimmed := strings.TrimSpace(msg)
if trimmed == "" { if trimmed == "" {
@ -228,58 +245,23 @@ func cli(s *streams) error {
app.Stop() app.Stop()
case "look": case "look":
fmt.Fprintln(msgScroll, "") fmt.Fprintln(msgScroll, "")
fmt.Fprintln(msgScroll, scenes[currentScene].Description) fmt.Fprintln(msgScroll, currentScene.Description)
// TODO refactor into a state machine // TODO refactor into a state machine
case "nod": case "nod":
if currentScene != "start" { advanceScene("start",
return "i'm sorry, before going further could you share an email with me?")
}
if scenes[currentScene].InLength == 0 {
fmt.Fprintln(msgScroll,
scenes[currentScene].Host.Say("i'm sorry, before going further could you share an email with me?"))
return
}
currentScene = "nodded"
sceneTransition(scenes[currentScene].Description)
case "lean": case "lean":
if currentScene != "nodded" { advanceScene("nodded", "phweeturpff")
return
}
if scenes[currentScene].InLength == 0 {
fmt.Fprintln(msgScroll,
scenes[currentScene].Host.Say("phweeturpff"))
return
}
currentScene = "leaned"
sceneTransition(scenes[currentScene].Description)
case "spin": case "spin":
if currentScene != "leaned" { advanceScene("leaned", "hmm did you say something?")
return
}
if scenes[currentScene].InLength == 0 {
fmt.Fprintln(msgScroll,
scenes[currentScene].Host.Say("hmm did you say something?"))
return
}
currentScene = "spun"
sceneTransition(scenes[currentScene].Description)
case "open": case "open":
if currentScene != "spun" { advanceScene("spun", "just the one last thing please")
return
}
if scenes[currentScene].InLength == 0 {
fmt.Fprintln(msgScroll,
scenes[currentScene].Host.Say("just the one last thing please"))
return
}
currentScene = "done"
sceneTransition(scenes[currentScene].Description)
} }
return return
} }
fmt.Fprintln(msgScroll, player.Say(msg)) fmt.Fprintln(msgScroll, player.Say(msg))
fmt.Fprintln(scenes[currentScene].InBuff, msg) fmt.Fprintln(currentScene.InBuff, msg)
scenes[currentScene].InLength += len(msg) currentScene.InLength += len(msg)
} }
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
@ -294,7 +276,7 @@ func cli(s *streams) error {
}) })
app.SetAfterDrawFunc(func(_ tcell.Screen) { app.SetAfterDrawFunc(func(_ tcell.Screen) {
fmt.Fprintln(msgScroll, scenes[currentScene].Description) fmt.Fprintln(msgScroll, currentScene.Description)
app.SetAfterDrawFunc(nil) app.SetAfterDrawFunc(nil)
}) })