Skip to content

Commit 7ff541e

Browse files
committed
Second Commit
1 parent 2ad5556 commit 7ff541e

File tree

7 files changed

+245
-131
lines changed

7 files changed

+245
-131
lines changed

answerView.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

formView.go

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
5-
"github.com/charmbracelet/huh"
64
"github.com/charmbracelet/lipgloss"
75
"github.com/mdp/qrterminal/v3"
86
"strings"
@@ -18,7 +16,7 @@ func (m Model) viewFormPage() string {
1816
// Generate the QR code string
1917
qrBuilder := &strings.Builder{}
2018
qrterminal.GenerateWithConfig(
21-
"https://github.com/mdp/qrterminal", // You can use your own content here
19+
m.generatedURL, // You can use your own content here
2220
qrterminal.Config{
2321
Level: qrterminal.L, // Low error correction for smaller QR code
2422
Writer: qrBuilder, // Write QR code output to strings.Builder
@@ -32,29 +30,39 @@ func (m Model) viewFormPage() string {
3230
qrStyled := lipgloss.NewStyle().
3331
Border(lipgloss.RoundedBorder(), true). // Add double-line border around the QR code
3432
BorderForeground(lipgloss.AdaptiveColor(indigo)). // Add some space below the QR code
35-
Render(s.StatusHeader.Render("Qr code to the game") + "\n \n" + qrCodeString)
33+
Render(s.StatusHeader.Render("Qr code to the game") + "\n \n" + qrCodeString + "\n \n" + m.generatedURL)
3634

3735
// Status (right side)
3836
var status string
3937
{
4038

4139
var (
42-
answer1 string
43-
answer2 string
44-
answer3 string
40+
color string
41+
formType string
42+
flux string
43+
inOrbit string
44+
isExo string
4545
)
4646

47-
if m.form.GetString("answer1") != "" {
48-
answer1 = m.form.GetString("answer1")
49-
answer1 = "\n\n" + s.StatusHeader.Render("Answer 1") + "\n" + answer1
47+
if m.form.GetString("color") != "" {
48+
color = m.form.GetString("color")
49+
color = "\n\n" + s.StatusHeader.Render("Planet Color") + "\n" + color
5050
}
51-
if m.form.GetString("answer2") != "" {
52-
answer2 = m.form.GetString("answer2")
53-
answer2 = "\n\n" + s.StatusHeader.Render("Answer 2") + "\n" + answer2
51+
if m.form.GetString("formType") != "" {
52+
formType = m.form.GetString("formType")
53+
formType = "\n\n" + s.StatusHeader.Render("Planet Form") + "\n" + formType
5454
}
55-
if m.form.GetString("answer3") != "" {
56-
answer3 = m.form.GetString("answer3")
57-
answer3 = "\n\n" + s.StatusHeader.Render("Answer 3") + "\n" + answer3
55+
if m.form.GetString("flux") != "" {
56+
flux = m.form.GetString("flux")
57+
flux = "\n\n" + s.StatusHeader.Render("Flux") + "\n" + flux
58+
}
59+
if m.form.GetString("inOrbit") != "" {
60+
inOrbit = m.form.GetString("inOrbit")
61+
inOrbit = "\n\n" + s.StatusHeader.Render("Is in orbit") + "\n" + inOrbit
62+
}
63+
if m.form.GetString("isExoplanet") != "" {
64+
isExo = m.form.GetString("isExoplanet")
65+
isExo = "\n\n" + s.StatusHeader.Render("Is exoplanet") + "\n" + isExo
5866
}
5967

6068
const statusWidth = 28
@@ -64,13 +72,15 @@ func (m Model) viewFormPage() string {
6472
Width(statusWidth).
6573
MarginLeft(statusMarginLeft).
6674
Render(s.StatusHeader.Render("Your Answers") +
67-
answer1 +
68-
answer2 +
69-
answer3)
75+
color +
76+
formType +
77+
flux +
78+
inOrbit +
79+
isExo)
7080
}
7181

7282
errors := m.form.Errors()
73-
header := m.appBoundaryView("Charm Employment Application")
83+
header := m.appBoundaryView("Exoplanet Game Quiz")
7484
if len(errors) > 0 {
7585
header = m.appErrorBoundaryView(m.errorView())
7686
}
@@ -83,47 +93,3 @@ func (m Model) viewFormPage() string {
8393

8494
return s.Base.Render(qrStyled + "\n" + header + "\n" + body + "\n\n" + footer)
8595
}
86-
87-
func FormField() Model {
88-
m := Model{width: maxWidth, state: stateFormPage}
89-
m.lg = lipgloss.DefaultRenderer()
90-
m.styles = NewStyles(m.lg)
91-
92-
m.form = huh.NewForm(
93-
huh.NewGroup(
94-
huh.NewInput().
95-
Key("answer1").
96-
Title("What’s the paramater1").
97-
Prompt("?").
98-
Value(&m.answer1),
99-
100-
huh.NewInput().
101-
Key("answer2").
102-
Title("What’s for lunch?").
103-
Prompt("?").
104-
Value(&m.answer2),
105-
106-
huh.NewInput().
107-
Key("answer3").
108-
Title("What’s for lunch?").
109-
Prompt("?").
110-
Value(&m.answer3),
111-
112-
huh.NewConfirm().
113-
Key("done").
114-
Title("All done?").
115-
Validate(func(v bool) error {
116-
if !v {
117-
return fmt.Errorf("Welp, finish up then")
118-
}
119-
return nil
120-
}).
121-
Affirmative("Yep").
122-
Negative("Wait, no"),
123-
),
124-
).
125-
WithWidth(45).
126-
WithShowHelp(false).
127-
WithShowErrors(false)
128-
return m
129-
}

model.go

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ import (
77
)
88

99
type Model struct {
10-
state state
11-
lg *lipgloss.Renderer
12-
styles *Styles
13-
form *huh.Form
14-
width int
15-
answer1 string
16-
answer2 string
17-
answer3 string
10+
state state
11+
lg *lipgloss.Renderer
12+
styles *Styles
13+
form *huh.Form
14+
width int
15+
color string
16+
formType string
17+
flux string
18+
inOrbit string
19+
isExo string
20+
randomID int
21+
generatedURL string
1822
}
1923

20-
func (m Model) Init() tea.Cmd {
24+
func (m *Model) Init() tea.Cmd {
25+
26+
m.generatedURL = m.GenerateUrl()
2127
return m.form.Init()
2228
}
2329

@@ -28,58 +34,36 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2834
case tea.KeyMsg:
2935
switch msg.String() {
3036
case "esc", "ctrl+c", "q":
31-
return m, tea.Quit
37+
return &m, tea.Quit
3238
}
3339
}
3440

3541
var cmds []tea.Cmd
3642

37-
// Process the form
38-
form, cmd := m.form.Update(msg)
39-
if f, ok := form.(*huh.Form); ok {
40-
m.form = f
41-
cmds = append(cmds, cmd)
42-
}
43+
// Process the main form
44+
if m.state == stateFormPage {
45+
form, cmd := m.form.Update(msg)
46+
if f, ok := form.(*huh.Form); ok {
47+
m.form = f
48+
cmds = append(cmds, cmd)
49+
}
4350

44-
if m.form.State == huh.StateCompleted {
45-
// Switch to the next page after form completion
46-
m.state = stateSummaryPage
47-
}
51+
m.color = m.form.GetString("color")
52+
m.formType = m.form.GetString("formType")
53+
m.flux = m.form.GetString("flux")
54+
m.inOrbit = m.form.GetString("inOrbit")
55+
m.isExo = m.form.GetString("isExoplanet")
4856

49-
return m, tea.Batch(cmds...)
50-
}
57+
}
5158

52-
func (m Model) getRole() (string, string) {
53-
level := m.form.GetString("level")
54-
switch m.form.GetString("class") {
55-
case "Warrior":
56-
switch level {
57-
case "1":
58-
return "Tank Intern", "Assists with tank-related activities. Paid position."
59-
case "9999":
60-
return "Tank Manager", "Manages tanks and tank-related activities."
61-
default:
62-
return "Tank", "General tank. Does damage, takes damage. Responsible for tanking."
63-
}
64-
case "Mage":
65-
switch level {
66-
case "1":
67-
return "DPS Associate", "Finds DPS deals and passes them on to DPS Manager."
68-
case "9999":
69-
return "DPS Operating Officer", "Oversees all DPS activities."
70-
default:
71-
return "DPS", "Does damage and ideally does not take damage. Logs hours in JIRA."
59+
// If the form is completed, validate answers
60+
if m.form.State == huh.StateCompleted && m.state == stateFormPage {
61+
if m.ValidateAnswers() {
62+
m.state = stateCorrectAnswer
63+
} else {
64+
m.state = stateWrongAnswer
7265
}
73-
case "Rogue":
74-
switch level {
75-
case "1":
76-
return "Stealth Junior Designer", "Designs rogue-like activities. Reports to Stealth Lead."
77-
case "9999":
78-
return "Stealth Lead", "Lead designer for all things stealth. Some travel required."
79-
default:
80-
return "Sneaky Person", "Sneaks around and does sneaky things. Reports to Stealth Lead."
81-
}
82-
default:
83-
return "", ""
8466
}
67+
68+
return &m, tea.Batch(cmds...)
8569
}

0 commit comments

Comments
 (0)