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

@ -33,9 +33,10 @@ in /etc/pam.d/sshd
const maxInputLength int = 4000
type TownApplication struct {
Email string
// TODO
Email string
HowFound string
Why string
Where string
}
type streams struct {
@ -97,26 +98,17 @@ func cli(s *streams) error {
player := newCharacter("you", "TODO")
currentScene := "start"
sceneTransition := func(text string) {
fmt.Fprintln(msgScroll, heredoc.Doc(`
[purple]----------[-:-:-]
`))
fmt.Fprintln(msgScroll, text)
}
type scene struct {
Name string
Description string
InBuff io.ReadWriter
InLength int
Host *character
}
scenes := map[string]*scene{
"start": {
scenes := []*scene{
{
Name: "start",
Description: heredoc.Doc(`
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"),
InBuff: bytes.NewBuffer([]byte{}),
},
"nodded": {
{
Name: "nodded",
Description: heredoc.Doc(`
The workshop fades away. You hear the sound of a dial up modem
in the distance.
@ -158,7 +151,8 @@ func cli(s *streams) error {
InBuff: bytes.NewBuffer([]byte{}),
Host: newCharacter("the shrike", "a little grey bird. it has a pretty song."),
},
"leaned": {
{
Name: "leaned",
Description: heredoc.Doc(`
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{}),
Host: newCharacter("the vcr", "a black and grey VCR from 1991"),
},
"spun": {
{
Name: "spun",
Description: heredoc.Doc(`
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
@ -199,7 +194,8 @@ func cli(s *streams) error {
InBuff: bytes.NewBuffer([]byte{}),
Host: newCharacter("the mop", "a greying mop with a wooden handle."),
},
"done": {
{
Name: "spun",
Description: heredoc.Doc(`
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) {
trimmed := strings.TrimSpace(msg)
if trimmed == "" {
@ -228,58 +245,23 @@ func cli(s *streams) error {
app.Stop()
case "look":
fmt.Fprintln(msgScroll, "")
fmt.Fprintln(msgScroll, scenes[currentScene].Description)
fmt.Fprintln(msgScroll, currentScene.Description)
// TODO refactor into a state machine
case "nod":
if currentScene != "start" {
return
}
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)
advanceScene("start",
"i'm sorry, before going further could you share an email with me?")
case "lean":
if currentScene != "nodded" {
return
}
if scenes[currentScene].InLength == 0 {
fmt.Fprintln(msgScroll,
scenes[currentScene].Host.Say("phweeturpff"))
return
}
currentScene = "leaned"
sceneTransition(scenes[currentScene].Description)
advanceScene("nodded", "phweeturpff")
case "spin":
if currentScene != "leaned" {
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)
advanceScene("leaned", "hmm did you say something?")
case "open":
if currentScene != "spun" {
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)
advanceScene("spun", "just the one last thing please")
}
return
}
fmt.Fprintln(msgScroll, player.Say(msg))
fmt.Fprintln(scenes[currentScene].InBuff, msg)
scenes[currentScene].InLength += len(msg)
fmt.Fprintln(currentScene.InBuff, msg)
currentScene.InLength += len(msg)
}
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
@ -294,7 +276,7 @@ func cli(s *streams) error {
})
app.SetAfterDrawFunc(func(_ tcell.Screen) {
fmt.Fprintln(msgScroll, scenes[currentScene].Description)
fmt.Fprintln(msgScroll, currentScene.Description)
app.SetAfterDrawFunc(nil)
})