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:
parent
917610bf8f
commit
4c67fe6758
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
gambit_ed25519
|
||||
gambit_ed25519.pub
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user