town/cmd/signup/main.go

340 lines
8.9 KiB
Go
Raw Normal View History

2023-02-03 03:48:40 +00:00
package main
import (
"fmt"
"log"
2023-02-03 03:48:40 +00:00
"os"
"path"
2023-02-03 03:48:40 +00:00
"strings"
"time"
2023-02-03 03:48:40 +00:00
2023-02-13 06:13:07 +00:00
"encoding/json"
2023-02-05 22:58:22 +00:00
"github.com/MakeNowJust/heredoc/v2"
"github.com/gdamore/tcell/v2"
2023-02-03 03:48:40 +00:00
"github.com/rivo/tview"
)
const (
2023-02-14 07:33:53 +00:00
maxInputLength = 10000
signupDirectory = "/town/signups"
logDir = "/town/var/signup"
)
2023-02-03 03:48:40 +00:00
type scene struct {
Name string
Description string
2023-02-13 06:13:07 +00:00
Key string
Host *character
}
2023-02-03 03:48:40 +00:00
type townApplication struct {
2023-02-13 06:13:07 +00:00
When time.Time
Answers map[string]string
}
2023-02-03 03:48:40 +00:00
type character struct {
Name string
Description string
}
2023-02-03 03:48:40 +00:00
func newCharacter(name, description string) *character {
return &character{
Name: name,
Description: description,
}
}
2023-02-10 19:47:09 +00:00
func (c *character) Say(msg string) string {
verb := "says"
if c.Name == "you" {
verb = "say"
}
return fmt.Sprintf("[-:-:b]%s[-:-:-] %s: '%s'",
c.Name,
verb,
strings.TrimSpace(msg))
2023-02-03 05:03:03 +00:00
}
2023-02-03 03:48:40 +00:00
func main() {
logFile := path.Join(logDir, fmt.Sprintf("%d", time.Now().Unix()))
logF, err := os.Create(logFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
logger := log.New(logF, "", log.Ldate|log.Ltime)
2023-02-13 06:13:07 +00:00
err = _main(logger)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
2023-02-03 03:48:40 +00:00
}
2023-02-13 06:13:07 +00:00
func _main(l *log.Logger) error {
l.Println("starting a session")
2023-02-03 05:03:03 +00:00
pages := tview.NewPages()
mainFlex := tview.NewFlex()
innerFlex := tview.NewFlex()
input := tview.NewTextArea()
2023-02-10 00:22:04 +00:00
input.SetBorder(true).SetBorderColor(tcell.ColorPaleTurquoise)
input.SetTitle("press ctrl+d to send")
2023-02-10 19:47:09 +00:00
input.SetMaxLength(2000)
2023-02-03 05:03:03 +00:00
title := tview.NewTextView()
title.SetDynamicColors(true)
title.SetTextAlign(tview.AlignCenter)
title.SetText("[purple]the tilde town sign up portal[-]")
msgScroll := tview.NewTextView()
msgScroll.SetDynamicColors(true)
2023-02-10 00:22:04 +00:00
msgScroll.SetBackgroundColor(tcell.ColorBlack)
msgScroll.SetTextColor(tcell.ColorWhite)
2023-02-03 05:03:03 +00:00
sidebar := tview.NewTextView()
sidebar.SetBorder(true)
sidebar.SetDynamicColors(true)
2023-02-10 00:22:04 +00:00
sidebar.SetBackgroundColor(tcell.ColorBlack)
sidebar.SetTextColor(tcell.ColorGray)
2023-02-05 22:58:22 +00:00
sidebar.SetText(heredoc.Doc(`
[-:-:b]hey here are some hints[-:-:-]
quit by pressing [-:-:b]ctrl-c[-:-:-]
(your responses won't be saved)
type stuff. send it with [-:-:b]ctrl+d[-:-:-]
try a [-:-:b]verb[-:-:-] using [-:-:b]/[-:-:-] like:
2023-02-10 00:22:04 +00:00
[-:-:b]/nod[-:-:-]
2023-02-05 22:58:22 +00:00
`))
2023-02-03 05:03:03 +00:00
innerFlex.SetDirection(tview.FlexColumn)
2023-02-10 00:22:04 +00:00
innerFlex.AddItem(msgScroll, 0, 3, false)
2023-02-03 05:03:03 +00:00
innerFlex.AddItem(sidebar, 0, 1, false)
mainFlex.SetDirection(tview.FlexRow)
mainFlex.AddItem(title, 1, -1, false)
mainFlex.AddItem(innerFlex, 0, 1, false)
mainFlex.AddItem(input, 5, -1, true)
pages.AddPage("main", mainFlex, true, true)
app := tview.NewApplication()
app.SetRoot(pages, true)
2023-02-05 22:58:22 +00:00
player := newCharacter("you", "TODO")
2023-02-03 03:48:40 +00:00
2023-02-13 06:13:07 +00:00
townApp := &townApplication{
Answers: map[string]string{},
}
save := func() {
2023-02-13 06:13:07 +00:00
townApp.When = time.Now()
output, err := json.Marshal(townApp)
if err != nil {
l.Printf("could not serialize stuff: %s", err.Error())
2023-02-13 06:13:07 +00:00
l.Printf("dumping values: %v", townApp)
return
}
2023-02-13 06:13:07 +00:00
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))
return
}
defer f.Close()
_, err = f.Write(output)
if err != nil {
l.Printf("failed to write to file: %s", err.Error())
l.Printf("dumping values: %s", string(output))
return
}
2023-02-10 19:47:09 +00:00
}
2023-02-12 00:28:20 +00:00
scenes := []*scene{
{
Name: "start",
2023-02-10 19:47:09 +00:00
Description: heredoc.Doc(`
2023-02-10 00:22:04 +00:00
You open your eyes.
You're in some kind of workshop.
Wires and computers in various state of disrepair are strewn across
tables and shelves. It smells faintly of burnt cedar.
The wires and components before you slowly drag
themselves into the shape of a small humanoid.
[-:-:b]wire guy says:[-:-:-]
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.
2023-02-13 06:13:07 +00:00
2023-02-10 00:22:04 +00:00
when you're ready to move on, [-:-:b]/nod[-:-:-]
2023-02-10 19:47:09 +00:00
`),
2023-02-13 06:13:07 +00:00
Host: newCharacter("wire guy", "a lil homonculus made of discarded computer cables"),
Key: "email",
2023-02-10 00:22:04 +00:00
},
2023-02-12 00:28:20 +00:00
{
Name: "nodded",
2023-02-10 19:47:09 +00:00
Description: heredoc.Doc(`
2023-02-10 00:22:04 +00:00
The workshop fades away. You hear the sound of a dial up modem
in the distance.
Trees spring up out of the ground around you: birches, oaks, maples,
firs, yews, pines, cypresses complete with tiny swamps around their trunks,
junipers, redwoods, cedars, towering palms waving gently in a breeze, eucalyptus,
banyan. the smell is riotous like a canvas with all the colors splashed on. birds
start to sing.
a shrike alights on a branch in front of you.
[-:-:b]the shrike says:[-:-:-]
phweeturpff. how did you find out about the town? did anyone refer you?
just say your answer out loud. when you've said what you want, [-:-:b]/lean[-:-:-]
against a tree.
2023-02-10 19:47:09 +00:00
`),
2023-02-13 06:13:07 +00:00
Key: "how",
Host: newCharacter("the shrike", "a little grey bird. it has a pretty song."),
2023-02-10 00:22:04 +00:00
},
2023-02-12 00:28:20 +00:00
{
Name: "leaned",
2023-02-10 19:47:09 +00:00
Description: heredoc.Doc(`
2023-02-10 00:22:04 +00:00
You sink backwards into the forest. You find yourself floating in darkness.
At the far reaches of your vision you can make out a faint neon grid. Around you
float pieces of consumer electronic appliances from 1980s Earth. A VCR approaches
you and speaks, flapping its tape slot cover with each word.
[-:-:b]the vcr says:[-:-:-]
welcome! thank you for coming this far. just two questions left. what about
tilde town interests you? what kind of stuff might you want to get up to here?
as usual, just say your answer. when you're satisfied, please [-:-:b]/spin[-:-:-]
around in this void.
2023-02-10 19:47:09 +00:00
`),
2023-02-13 06:13:07 +00:00
Key: "why",
Host: newCharacter("the vcr", "a black and grey VCR from 1991"),
2023-02-10 00:22:04 +00:00
},
2023-02-12 00:28:20 +00:00
{
Name: "spun",
2023-02-10 19:47:09 +00:00
Description: heredoc.Doc(`
2023-02-10 00:22:04 +00:00
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
it's dark besides a single pool of light coming from a street lamp. it's illuminating
some litter and a rusty, blue 1994 pontiac grand am.
The shelves around you are stocked with products you've never heard of before like
Visible Pants, Petty Burgers, Gentle Rice, Boo Sponge, Power Banjo, Superware, Kneephones,
and Diet Coagulator. A mop is mopping the floor and turns to you.
[-:-:b]the mop says:[-:-:-]
swishy slop. last question. where online can we get to know you? do you have a personal
website or social media presence? we'll take whatever.
say some links and words out loud, you know the drill.
when you're happy you can submit this whole experience by leaving the
store. just [-:-:b]/open[-:-:-] the door.
2023-02-10 19:47:09 +00:00
`),
2023-02-13 06:13:07 +00:00
Key: "where",
Host: newCharacter("the mop", "a greying mop with a wooden handle."),
2023-02-10 00:22:04 +00:00
},
2023-02-12 00:28:20 +00:00
{
Name: "done",
2023-02-10 19:47:09 +00:00
Description: heredoc.Doc(`
2023-02-10 00:22:04 +00:00
thank you for applying to tilde.town!
please be on the look out for an email from [-:-:b]root@tilde.town[-:-:-]
you can [-:-:b]/quit[-:-:-] now
ok bye have a good one~
2023-02-10 19:47:09 +00:00
`),
2023-02-13 06:13:07 +00:00
Key: "extra",
2023-02-10 00:22:04 +00:00
},
}
2023-02-12 00:28:20 +00:00
sceneIx := 0
currentScene := scenes[sceneIx]
advanceScene := func(fromScene, sorryMsg string) {
if currentScene.Name != fromScene {
return
}
2023-02-13 06:13:07 +00:00
if len(townApp.Answers[currentScene.Key]) == 0 {
2023-02-12 00:28:20 +00:00
fmt.Fprintln(msgScroll, currentScene.Host.Say(sorryMsg))
return
}
sceneIx++
currentScene = scenes[sceneIx]
fmt.Fprintln(msgScroll, heredoc.Doc(`
[purple]----------[-:-:-]
`))
fmt.Fprintln(msgScroll, currentScene.Description)
}
2023-02-05 22:58:22 +00:00
handleInput := func(msg string) {
2023-02-13 06:13:07 +00:00
msg = strings.TrimSpace(msg)
if msg == "" {
2023-02-10 00:22:04 +00:00
return
}
2023-02-13 06:13:07 +00:00
if strings.HasPrefix(msg, "/") {
split := strings.Split(msg, " ")
2023-02-10 00:22:04 +00:00
if len(split) > 0 {
2023-02-13 06:13:07 +00:00
msg = split[0]
2023-02-10 00:22:04 +00:00
}
2023-02-13 06:13:07 +00:00
switch strings.TrimPrefix(msg, "/") {
2023-02-10 00:22:04 +00:00
case "quit":
2023-02-13 06:13:07 +00:00
l.Println("got /quit")
2023-02-10 20:05:06 +00:00
app.Stop()
2023-02-10 19:47:09 +00:00
case "look":
fmt.Fprintln(msgScroll, "")
2023-02-12 00:28:20 +00:00
fmt.Fprintln(msgScroll, currentScene.Description)
2023-02-10 00:22:04 +00:00
case "nod":
2023-02-12 00:28:20 +00:00
advanceScene("start",
"i'm sorry, before going further could you share an email with me?")
2023-02-10 00:22:04 +00:00
case "lean":
2023-02-12 00:28:20 +00:00
advanceScene("nodded", "phweeturpff")
2023-02-10 00:22:04 +00:00
case "spin":
2023-02-12 00:28:20 +00:00
advanceScene("leaned", "hmm did you say something?")
2023-02-10 00:22:04 +00:00
case "open":
2023-02-12 00:28:20 +00:00
advanceScene("spun", "just the one last thing please")
save()
2023-02-10 00:22:04 +00:00
}
return
2023-02-05 22:58:22 +00:00
}
2023-02-13 06:13:07 +00:00
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
}
2023-02-05 22:58:22 +00:00
fmt.Fprintln(msgScroll, player.Say(msg))
2023-02-13 06:13:07 +00:00
townApp.Answers[currentScene.Key] += ("\n" + msg)
2023-02-03 03:48:40 +00:00
}
2023-02-05 22:58:22 +00:00
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlD:
handleInput(input.GetText())
input.SetText("", false)
return nil
2023-02-03 03:48:40 +00:00
}
2023-02-05 22:58:22 +00:00
return event
})
2023-02-03 03:48:40 +00:00
2023-02-10 00:22:04 +00:00
app.SetAfterDrawFunc(func(_ tcell.Screen) {
2023-02-12 00:28:20 +00:00
fmt.Fprintln(msgScroll, currentScene.Description)
2023-02-10 00:22:04 +00:00
app.SetAfterDrawFunc(nil)
})
2023-02-05 22:58:22 +00:00
return app.Run()
2023-02-03 03:48:40 +00:00
}