forked from tildetown/town
unfuck input collection
parent
462e8772ec
commit
45dd8efbae
|
@ -1,19 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"encoding/json"
|
||||
|
||||
"github.com/MakeNowJust/heredoc/v2"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -25,17 +24,13 @@ const (
|
|||
type scene struct {
|
||||
Name string
|
||||
Description string
|
||||
InBuff io.ReadWriter
|
||||
InLength int
|
||||
Key string
|
||||
Host *character
|
||||
}
|
||||
|
||||
type townApplication struct {
|
||||
Email io.ReadWriter
|
||||
How io.ReadWriter
|
||||
Why io.ReadWriter
|
||||
Where io.ReadWriter
|
||||
Extra io.ReadWriter
|
||||
When time.Time
|
||||
Answers map[string]string
|
||||
}
|
||||
|
||||
type character struct {
|
||||
|
@ -70,14 +65,15 @@ func main() {
|
|||
}
|
||||
|
||||
logger := log.New(logF, "", log.Ldate|log.Ltime)
|
||||
err = cli(logger)
|
||||
err = _main(logger)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
func cli(l *log.Logger) error {
|
||||
func _main(l *log.Logger) error {
|
||||
l.Println("starting a session")
|
||||
pages := tview.NewPages()
|
||||
mainFlex := tview.NewFlex()
|
||||
innerFlex := tview.NewFlex()
|
||||
|
@ -130,43 +126,20 @@ func cli(l *log.Logger) error {
|
|||
|
||||
player := newCharacter("you", "TODO")
|
||||
|
||||
answers := &townApplication{
|
||||
Email: bytes.NewBuffer([]byte{}),
|
||||
How: bytes.NewBuffer([]byte{}),
|
||||
Why: bytes.NewBuffer([]byte{}),
|
||||
Where: bytes.NewBuffer([]byte{}),
|
||||
Extra: bytes.NewBuffer([]byte{}),
|
||||
townApp := &townApplication{
|
||||
Answers: map[string]string{},
|
||||
}
|
||||
|
||||
save := func() {
|
||||
when := time.Now()
|
||||
// TODO this code sucks
|
||||
email := []byte{}
|
||||
how := []byte{}
|
||||
why := []byte{}
|
||||
where := []byte{}
|
||||
extra := []byte{}
|
||||
_, _ = answers.Email.Read(email)
|
||||
_, _ = answers.Why.Read(why)
|
||||
_, _ = answers.How.Read(how)
|
||||
_, _ = answers.Where.Read(where)
|
||||
_, _ = answers.Extra.Read(extra)
|
||||
d := map[string]interface{}{
|
||||
"when": when,
|
||||
"email": email,
|
||||
"how": how,
|
||||
"why": why,
|
||||
"where": where,
|
||||
"extra": extra,
|
||||
}
|
||||
output, err := yaml.Marshal(d)
|
||||
townApp.When = time.Now()
|
||||
output, err := json.Marshal(townApp)
|
||||
if err != nil {
|
||||
l.Printf("could not serialize stuff: %s", err.Error())
|
||||
l.Printf("dumping values: %v", d)
|
||||
l.Printf("dumping values: %v", townApp)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Create(path.Join(signupDirectory, fmt.Sprintf("%d", when.Unix())))
|
||||
f, err := os.Create(path.Join(signupDirectory, fmt.Sprintf("%d.json", townApp.When.Unix())))
|
||||
if err != nil {
|
||||
l.Printf("could not open signup file: %s", err.Error())
|
||||
l.Printf("dumping values: %s", string(output))
|
||||
|
@ -199,10 +172,11 @@ func cli(l *log.Logger) error {
|
|||
hello, welcome to the application for membership in tilde town.
|
||||
first, please let me know what a good [-:-:b]email address[-:-:-] is for you?
|
||||
just say it out loud. as many times as you need. to get it right.
|
||||
|
||||
when you're ready to move on, [-:-:b]/nod[-:-:-]
|
||||
`),
|
||||
Host: newCharacter("wire guy", "a lil homonculus made of discarded computer cables"),
|
||||
InBuff: answers.Email,
|
||||
Host: newCharacter("wire guy", "a lil homonculus made of discarded computer cables"),
|
||||
Key: "email",
|
||||
},
|
||||
{
|
||||
Name: "nodded",
|
||||
|
@ -224,8 +198,8 @@ func cli(l *log.Logger) error {
|
|||
just say your answer out loud. when you've said what you want, [-:-:b]/lean[-:-:-]
|
||||
against a tree.
|
||||
`),
|
||||
InBuff: answers.How,
|
||||
Host: newCharacter("the shrike", "a little grey bird. it has a pretty song."),
|
||||
Key: "how",
|
||||
Host: newCharacter("the shrike", "a little grey bird. it has a pretty song."),
|
||||
},
|
||||
{
|
||||
Name: "leaned",
|
||||
|
@ -243,8 +217,8 @@ func cli(l *log.Logger) error {
|
|||
as usual, just say your answer. when you're satisfied, please [-:-:b]/spin[-:-:-]
|
||||
around in this void.
|
||||
`),
|
||||
InBuff: answers.Why,
|
||||
Host: newCharacter("the vcr", "a black and grey VCR from 1991"),
|
||||
Key: "why",
|
||||
Host: newCharacter("the vcr", "a black and grey VCR from 1991"),
|
||||
},
|
||||
{
|
||||
Name: "spun",
|
||||
|
@ -267,8 +241,8 @@ func cli(l *log.Logger) error {
|
|||
when you're happy you can submit this whole experience by leaving the
|
||||
store. just [-:-:b]/open[-:-:-] the door.
|
||||
`),
|
||||
InBuff: answers.Where,
|
||||
Host: newCharacter("the mop", "a greying mop with a wooden handle."),
|
||||
Key: "where",
|
||||
Host: newCharacter("the mop", "a greying mop with a wooden handle."),
|
||||
},
|
||||
{
|
||||
Name: "done",
|
||||
|
@ -281,7 +255,7 @@ func cli(l *log.Logger) error {
|
|||
|
||||
ok bye have a good one~
|
||||
`),
|
||||
InBuff: answers.Extra,
|
||||
Key: "extra",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -292,11 +266,10 @@ func cli(l *log.Logger) error {
|
|||
if currentScene.Name != fromScene {
|
||||
return
|
||||
}
|
||||
if currentScene.InLength == 0 {
|
||||
if len(townApp.Answers[currentScene.Key]) == 0 {
|
||||
fmt.Fprintln(msgScroll, currentScene.Host.Say(sorryMsg))
|
||||
return
|
||||
}
|
||||
// TODO put contents of InBuff into answers struct
|
||||
sceneIx++
|
||||
currentScene = scenes[sceneIx]
|
||||
fmt.Fprintln(msgScroll, heredoc.Doc(`
|
||||
|
@ -308,22 +281,22 @@ func cli(l *log.Logger) error {
|
|||
}
|
||||
|
||||
handleInput := func(msg string) {
|
||||
trimmed := strings.TrimSpace(msg)
|
||||
if trimmed == "" {
|
||||
msg = strings.TrimSpace(msg)
|
||||
if msg == "" {
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(trimmed, "/") {
|
||||
split := strings.Split(trimmed, " ")
|
||||
if strings.HasPrefix(msg, "/") {
|
||||
split := strings.Split(msg, " ")
|
||||
if len(split) > 0 {
|
||||
trimmed = split[0]
|
||||
msg = split[0]
|
||||
}
|
||||
switch strings.TrimPrefix(trimmed, "/") {
|
||||
switch strings.TrimPrefix(msg, "/") {
|
||||
case "quit":
|
||||
l.Println("got /quit")
|
||||
app.Stop()
|
||||
case "look":
|
||||
fmt.Fprintln(msgScroll, "")
|
||||
fmt.Fprintln(msgScroll, currentScene.Description)
|
||||
// TODO refactor into a state machine
|
||||
case "nod":
|
||||
advanceScene("start",
|
||||
"i'm sorry, before going further could you share an email with me?")
|
||||
|
@ -337,14 +310,13 @@ func cli(l *log.Logger) error {
|
|||
}
|
||||
return
|
||||
}
|
||||
if currentScene.InLength+len(msg) > maxInputLength {
|
||||
if len(townApp.Answers[currentScene.Key]) > maxInputLength {
|
||||
fmt.Fprintln(msgScroll,
|
||||
currentScene.Host.Say("sorry I've heard more than I can remember :( maybe it's time to move on"))
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(msgScroll, player.Say(msg))
|
||||
fmt.Fprintln(currentScene.InBuff, msg)
|
||||
currentScene.InLength += len(msg)
|
||||
townApp.Answers[currentScene.Key] += ("\n" + msg)
|
||||
}
|
||||
|
||||
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||
|
|
Loading…
Reference in New Issue