town/cmd/signup/main.go

350 lines
9.4 KiB
Go
Raw Normal View History

2023-02-03 03:48:40 +00:00
package main
import (
2023-02-20 08:43:14 +00:00
"bytes"
2023-02-23 00:04:26 +00:00
"database/sql"
2023-02-03 03:48:40 +00:00
"fmt"
2023-02-21 05:57:27 +00:00
"io"
"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-23 00:04:26 +00:00
"git.tilde.town/tildetown/town/models"
2023-02-20 08:43:14 +00:00
"git.tilde.town/tildetown/town/signup"
2023-02-05 22:58:22 +00:00
"github.com/MakeNowJust/heredoc/v2"
"github.com/gdamore/tcell/v2"
2023-02-23 00:04:26 +00:00
_ "github.com/mattn/go-sqlite3"
2023-02-03 03:48:40 +00:00
"github.com/rivo/tview"
)
const (
2023-02-20 08:43:14 +00:00
maxInputLength = 10000
2023-02-21 03:56:19 +00:00
logDir = "/town/var/signups/log"
)
2023-02-03 03:48:40 +00:00
type scene struct {
Name string
Description string
Host *character
2023-02-21 05:57:27 +00:00
SorryMsg string
Input *bytes.Buffer
OnAdvance func(*scene)
}
func newScene(name, desc, sorryMsg string, host *character, onAdvance func(*scene)) *scene {
return &scene{
Name: name,
Description: desc,
SorryMsg: sorryMsg,
Host: host,
Input: bytes.NewBuffer([]byte{}),
OnAdvance: onAdvance,
}
}
type sceneManager struct {
scenes []*scene
Current *scene
index int
output io.Writer
Save func()
}
func (m *sceneManager) Advance() bool {
if m.Current.Name == "done" {
return false
}
if m.Current.Input.Len() == 0 {
fmt.Fprintln(m.output, m.Current.Host.Say(m.Current.SorryMsg))
return false
}
m.Current.OnAdvance(m.Current)
m.index++
m.Current = m.scenes[m.index]
if m.Current.Name == "done" {
m.Save()
}
fmt.Fprintln(m.output, heredoc.Doc(`
[purple]----------[-:-:-]
`))
fmt.Fprintln(m.output, m.Current.Description)
return true
}
func newSceneManager(output io.Writer, scenes []*scene) *sceneManager {
return &sceneManager{
output: output,
scenes: scenes,
Current: scenes[0],
}
}
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"
}
2023-02-21 05:57:27 +00:00
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-20 08:43:14 +00:00
2023-02-23 00:04:26 +00:00
db, err := signup.ConnectDB()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
2023-02-20 08:43:14 +00:00
err = _main(logger, db)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(3)
}
2023-02-03 03:48:40 +00:00
}
2023-02-23 00:04:26 +00:00
func _main(l *log.Logger, db *sql.DB) error {
2023-02-13 06:13:07 +00:00
l.Println("starting a session")
2023-02-03 05:03:03 +00:00
pages := tview.NewPages()
mainFlex := tview.NewFlex()
input := tview.NewTextArea()
2023-02-10 00:22:04 +00:00
input.SetBorder(true).SetBorderColor(tcell.ColorPaleTurquoise)
2023-02-21 05:57:27 +00:00
input.SetTitle("press enter 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[-]")
2023-02-21 05:57:27 +00:00
footer := tview.NewTextView()
footer.SetText("type /help and press enter to get help. type /quit and press enter to leave.")
2023-02-21 05:57:27 +00:00
2023-02-03 05:03:03 +00:00
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
mainFlex.SetDirection(tview.FlexRow)
mainFlex.AddItem(title, 1, -1, false)
2023-02-21 05:57:27 +00:00
mainFlex.AddItem(msgScroll, 0, 1, false)
mainFlex.AddItem(input, 4, -1, true)
mainFlex.AddItem(footer, 1, -1, false)
2023-02-03 05:03:03 +00:00
pages.AddPage("main", mainFlex, true, true)
app := tview.NewApplication()
app.SetRoot(pages, true)
2023-03-09 06:42:18 +00:00
player := newCharacter("you", "it's you. how are you?")
2023-02-03 03:48:40 +00:00
2023-02-23 00:04:26 +00:00
su := &models.TownSignup{ID: -1}
save := func() {
2023-02-20 08:43:14 +00:00
su.Created = time.Now()
2023-02-23 00:04:26 +00:00
err := su.Insert(db)
if err != nil {
2023-02-20 08:43:14 +00:00
l.Printf("failed to write to db: %s", err.Error())
l.Printf("dumping values: %#v", su)
return
}
2023-02-10 19:47:09 +00:00
}
2023-02-12 00:28:20 +00:00
scenes := []*scene{
2023-02-21 05:57:27 +00:00
newScene("start", 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?
2023-02-21 05:57:27 +00:00
2023-02-10 00:22:04 +00:00
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-21 05:57:27 +00:00
once you've told me your email you can [-:-:b]/nod[-:-:-] to move on.
2023-02-10 19:47:09 +00:00
`),
2023-02-21 05:57:27 +00:00
"i'm sorry, before going further could you share an email with me?",
newCharacter("wire guy", "a lil homonculus made of discarded computer cables"),
func(s *scene) { su.Email = string(s.Input.Bytes()) }),
newScene("how", 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?
2023-02-21 05:57:27 +00:00
just say your answer out loud. when you've said what you want, [-:-:b]/nod[-:-:-]
to continue.
2023-02-10 19:47:09 +00:00
`),
2023-02-21 05:57:27 +00:00
"phweeturpff",
newCharacter("the shrike", "a little grey bird. it has a pretty song."),
func(s *scene) { su.How = string(s.Input.Bytes()) }),
newScene("what", heredoc.Doc(`
2023-02-21 06:13:51 +00:00
You sink down into soft mossy floor of the forest. You find yourself floating in darkness.
2023-02-10 00:22:04 +00:00
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?
2023-02-21 06:13:51 +00:00
as usual, just say your answer. when you're satisfied, please [-:-:b]/nod[-:-:-]
2023-02-10 19:47:09 +00:00
`),
2023-02-21 05:57:27 +00:00
"hmm did you say something?",
newCharacter("the vcr", "a black and grey VCR from 1991"),
func(s *scene) { su.Why = string(s.Input.Bytes()) }),
newScene("link", 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.
2023-02-21 06:13:51 +00:00
when you're happy you can submit this whole experience with a [-:-:b]/nod[-:-:-]
2023-02-10 19:47:09 +00:00
`),
2023-02-21 05:57:27 +00:00
"just the one last thing please",
newCharacter("the mop", "a greying mop with a wooden handle."),
func(s *scene) { su.Links = string(s.Input.Bytes()) }),
newScene("done", heredoc.Doc(`
2023-02-10 00:22:04 +00:00
thank you for applying to tilde.town!
2023-03-09 06:33:31 +00:00
please be on the look out for an email from [-:-:b]root@tilde.town[-:-:-].
it's almost certain that it will end up in your spam filter, unfortunately.
2023-02-10 00:22:04 +00:00
2023-03-09 08:22:28 +00:00
every application is reviewed by a human. it can take up to 30 days for
an application to be reviewed. not every application is approved.
2023-02-10 00:22:04 +00:00
you can [-:-:b]/quit[-:-:-] now
ok bye have a good one~
2023-02-10 19:47:09 +00:00
`),
2023-02-21 05:57:27 +00:00
"",
newCharacter("the æther", "the very air around you"),
2023-02-21 06:00:10 +00:00
nil),
2023-02-10 00:22:04 +00:00
}
2023-02-21 05:57:27 +00:00
sm := newSceneManager(msgScroll, scenes)
sm.Save = save
2023-02-12 00:28:20 +00:00
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-21 05:57:27 +00:00
case "help":
fmt.Fprintln(msgScroll, sm.Current.Host.Say(`some artificial beings will guide you through applying to tilde.town.
type things, then press enter. to take an action, put a "/" before a word.
for example typing:
/nod
and pressing enter will cause you to nod. some other verbs: /quit /look`))
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-21 05:57:27 +00:00
fmt.Fprintln(msgScroll, sm.Current.Description)
2023-02-10 00:22:04 +00:00
case "nod":
2023-02-21 05:57:27 +00:00
if !sm.Advance() {
fmt.Fprintln(msgScroll, "you nod, but nothing happens.")
fmt.Fprintln(msgScroll)
} else {
l.Println("advancing scene")
}
2023-02-10 00:22:04 +00:00
}
return
2023-02-05 22:58:22 +00:00
}
2023-02-21 05:57:27 +00:00
if sm.Current.Input.Len() > maxInputLength {
fmt.Fprintln(msgScroll,
2023-02-21 05:57:27 +00:00
sm.Current.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-21 05:57:27 +00:00
fmt.Fprintln(sm.Current.Input, msg)
msgScroll.ScrollToEnd()
2023-02-03 03:48:40 +00:00
}
2023-02-20 08:43:14 +00:00
defer func() {
2023-02-21 05:57:27 +00:00
l.Println("exiting")
2023-02-21 03:56:19 +00:00
db.Close()
2023-02-20 08:43:14 +00:00
}()
2023-02-05 22:58:22 +00:00
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
2023-02-21 05:57:27 +00:00
case tcell.KeyEnter:
2023-02-05 22:58:22 +00:00
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-21 05:57:27 +00:00
fmt.Fprintln(msgScroll, sm.Current.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
}