1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-11-21 16:46:50 +02:00

refactor: server/middleware.go

This commit is contained in:
Maas Lalani 2022-02-14 20:06:02 -05:00
parent 917610bf8f
commit 4c67fe6758
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
2 changed files with 24 additions and 14 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
gambit_ed25519
gambit_ed25519.pub

View File

@ -15,39 +15,47 @@ import (
func gambitMiddleware(srv *Server) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
lipgloss.SetColorProfile(termenv.ANSI256)
return func(s ssh.Session) {
_, _, active := s.Pty()
cmds := s.Command()
if len(cmds) < 1 || !active {
s.Write([]byte(help("No TTY")))
s.Exit(1)
return
}
password := ""
id := cmds[0]
if len(cmds) > 1 {
password = cmds[1]
}
g := srv.FindRoom(id)
if g == nil {
room := srv.FindRoom(id)
if room == nil {
log.Printf("room %s is created with password %q", id, password)
g = srv.NewRoom(id, password)
room = srv.NewRoom(id, password)
}
if g.password == password {
p, err := g.AddPlayer(s)
if err != nil {
s.Write([]byte(fmt.Sprintf("%s\n", err)))
s.Exit(1)
return
}
log.Printf("%s joined room %s [%s]", s.User(), id, s.RemoteAddr())
p.StartGame()
log.Printf("%s left room %s [%s]", s.User(), id, s.RemoteAddr())
} else {
if room.password != password {
s.Write([]byte(help("wrong password")))
s.Exit(1)
return
}
p, err := room.AddPlayer(s)
if err != nil {
s.Write([]byte(fmt.Sprintf("%s\n", err)))
s.Exit(1)
return
}
log.Printf("%s joined room %s [%s]", s.User(), id, s.RemoteAddr())
p.StartGame()
log.Printf("%s left room %s [%s]", s.User(), id, s.RemoteAddr())
sh(s)
}
}