aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZach Latta <zach@zachlatta.com>2016-03-06 08:00:49 -0500
committerZach Latta <zach@zachlatta.com>2016-03-06 08:00:49 -0500
commit0539dde67b82d8bac5663f956ee7a3dc455fff22 (patch)
treecc39fe71ad67ce9f2307ab457a6618a039530c9e
parent1bdf30bc43fb7cb480f232c80b63ff7465959fbb (diff)
downloadsshtron-0539dde67b82d8bac5663f956ee7a3dc455fff22.tar.gz
Make it so you can exit games
-rw-r--r--game.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/game.go b/game.go
index 6484242..28379b4 100644
--- a/game.go
+++ b/game.go
@@ -42,7 +42,7 @@ func (h *Hub) Run(g *Game) {
h.Sessions[s] = struct{}{}
case s := <-h.Unregister:
if _, ok := h.Sessions[s]; ok {
- fmt.Fprint(s, "End of line.\r\n\r\n")
+ fmt.Fprint(s, "\r\n\r\n~ End of Line ~ \r\n\r\n")
// Unhide the cursor
fmt.Fprint(s, "\033[?25h")
@@ -300,6 +300,9 @@ const (
keyJ = 'j'
keyK = 'k'
keyL = 'l'
+
+ keyCtrlC = 3
+ keyEscape = 27
)
type GameManager struct {
@@ -364,6 +367,12 @@ func (gm *GameManager) Run() {
session.Player.HandleDown()
case keyD, keyL:
session.Player.HandleRight()
+ case keyCtrlC, keyEscape:
+ if g.SessionCount() == 1 {
+ delete(gm.Games, g.Name)
+ }
+
+ g.RemoveSession(session)
}
}
}()
@@ -609,6 +618,10 @@ func (g *Game) AvailableColors() []color.Attribute {
return availableColors
}
+func (g *Game) SessionCount() int {
+ return len(g.hub.Sessions)
+}
+
func (g *Game) Run() {
// Proxy g.Redraw's channel to g.hub.Redraw
go func() {
@@ -698,6 +711,10 @@ func (g *Game) AddSession(s *Session) {
g.hub.Register <- s
}
+func (g *Game) RemoveSession(s *Session) {
+ g.hub.Unregister <- s
+}
+
type Session struct {
c ssh.Channel
Un proyecto texto-plano.xyz