-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
180 lines (159 loc) · 3.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
// An example Bubble Tea server. This will put an ssh session into alt screen
// and continually print up to date terminal information.
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
lm "github.com/charmbracelet/wish/logging"
"github.com/gliderlabs/ssh"
"github.com/yanc0/musshis-heart/musshi"
"github.com/yanc0/musshis-heart/scenes"
)
var host = flag.String("host", "0.0.0.0", "host to listen on")
var port = flag.Int("port", 2222, "port to listen on")
func main() {
flag.Parse()
s, err := wish.NewServer(
wish.WithAddress(fmt.Sprintf("%s:%d", *host, *port)),
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
wish.WithMiddleware(
bm.Middleware(teaHandler),
lm.Middleware(),
),
)
if err != nil {
log.Fatalln(err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Printf("Starting SSH server on %s:%d", *host, *port)
go func() {
if err = s.ListenAndServe(); err != nil {
log.Fatalln(err)
}
}()
go func() {
ticker := time.NewTicker(100 * time.Millisecond)
for range ticker.C {
}
}()
<-done
log.Println("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil {
log.Fatalln(err)
}
}
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
pty, _, active := s.Pty()
if !active {
fmt.Println("no active terminal, skipping")
return nil, nil
}
m := &gameState{
term: pty.Term,
width: pty.Window.Width,
height: pty.Window.Height,
started: false,
}
return m, []tea.ProgramOption{tea.WithAltScreen()}
}
type gameState struct {
term string
width int
height int
started bool
ended bool
musshi *musshi.Musshi
}
func (m gameState) Init() tea.Cmd {
return tickCmd()
}
type tickMsg time.Time
func tickCmd() tea.Cmd {
return tea.Every(time.Millisecond*100, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
func (m gameState) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.height = msg.Height
m.width = msg.Width
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
case "r":
startedAt := time.Now()
m.musshi = musshi.NewMusshi()
// create musshi with two beats
m.musshi.Heart.Beat(startedAt.Add(time.Second * -1))
m.musshi.Heart.Beat(startedAt)
m.started = true
m.ended = false
return m, nil
default:
if m.ended {
return m, nil
}
if !m.started {
startedAt := time.Now()
m.musshi = musshi.NewMusshi()
// create musshi with two beats
m.musshi.Heart.Beat(startedAt.Add(time.Second * -1))
m.musshi.Heart.Beat(startedAt)
m.started = true
m.ended = false
return m, nil
}
m.musshi.Heart.Beat(time.Now())
if !m.musshi.Alive() {
m.musshi.SetDeathTime(time.Now())
m.ended = true
m.started = false
}
}
case tickMsg:
if m.started {
m.musshi.AlterLifeTimeExpectancy()
if !m.musshi.Alive() {
m.musshi.SetDeathTime(time.Now())
m.ended = true
m.started = false
}
}
return m, tickCmd()
}
return m, nil
}
func (m gameState) View() string {
if m.ended {
return scenes.NewEnd(scenes.EndParams{
Width: m.width,
Height: m.height,
Musshi: m.musshi,
}).Render()
}
if !m.started {
return scenes.NewStart(scenes.StartParams{
Width: m.width,
Height: m.height,
}).Render()
}
return scenes.NewGame(scenes.GameParams{
Width: m.width,
Height: m.height,
Musshi: m.musshi,
}).Render()
}