aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAli Yousuf <aly.yousuf7@gmail.com>2017-05-02 20:00:29 +0500
committerAli Yousuf <aly.yousuf7@gmail.com>2017-05-02 20:32:02 +0500
commit323d7201729ddd30b5c6ecfff740f23dc36a413e (patch)
tree932a1740fc053de24b686eb4f2f95dc31004b58c
parent49a9743c766568aa78d99dac66efb3b8bd569d82 (diff)
downloadsshtron-323d7201729ddd30b5c6ecfff740f23dc36a413e.tar.gz
Add ability to choose color
Signed-off-by: Ali Yousuf <aly.yousuf7@gmail.com>
-rw-r--r--README.md6
-rw-r--r--game.go87
-rw-r--r--main.go5
3 files changed, 55 insertions, 43 deletions
diff --git a/README.md b/README.md
index 29e14cd..da68c57 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,12 @@ _Controls: WASD or vim keybindings to move (**do not use your arrow keys**). Esc
**Code quality disclaimer:** _SSHTron was built in ~20 hours at [BrickHack 2](https://brickhack.io/). Here be dragons._
+## Want to choose color yourself?
+
+There are total 7 colors to choose from: Red, Green, Yellow, Blue, Magenta, Cyan and White
+
+ $ ssh red@sshtron.zachlatta.com
+
## Running Your Own Copy
Clone the project and `cd` into its directory. These instructions assume that you have your `GOPATH` setup correctly.
diff --git a/game.go b/game.go
index ffa80cf..1b9193d 100644
--- a/game.go
+++ b/game.go
@@ -7,6 +7,7 @@ import (
"io"
"math/rand"
"sort"
+ "strings"
"time"
"github.com/dustinkirkland/golang-petname"
@@ -383,51 +384,57 @@ func (gm *GameManager) GameCount() int {
return len(gm.Games)
}
-func (gm *GameManager) Run() {
- for {
- select {
- case c := <-gm.HandleChannel:
- g := gm.getGameWithAvailability()
- if g == nil {
- g = NewGame(gameWidth, gameHeight)
- gm.Games[g.Name] = g
+func (gm *GameManager) HandleNewChannel(c ssh.Channel, color string) {
+ g := gm.getGameWithAvailability()
+ if g == nil {
+ g = NewGame(gameWidth, gameHeight)
+ gm.Games[g.Name] = g
+
+ go g.Run()
+ }
+
+ colorOptions := g.AvailableColors()
+ finalColor := colorOptions[0]
+
+ // choose the requested color if available
+ color = strings.ToLower(color)
+ for _, clr := range colorOptions {
+ if strings.ToLower(playerColorNames[clr]) == color {
+ finalColor = clr
+ break
+ }
+ }
+
+ session := NewSession(c, g.WorldWidth(), g.WorldHeight(), finalColor)
+ g.AddSession(session)
- go g.Run()
+ go func() {
+ reader := bufio.NewReader(c)
+ for {
+ r, _, err := reader.ReadRune()
+ if err != nil {
+ fmt.Println(err)
+ break
}
- session := NewSession(c, g.WorldWidth(), g.WorldHeight(),
- g.AvailableColors()[0])
- g.AddSession(session)
-
- go func() {
- reader := bufio.NewReader(c)
- for {
- r, _, err := reader.ReadRune()
- if err != nil {
- fmt.Println(err)
- break
- }
-
- switch r {
- case keyW, keyZ, keyK, keyComma:
- session.Player.HandleUp()
- case keyA, keyQ, keyH:
- session.Player.HandleLeft()
- case keyS, keyJ, keyO:
- session.Player.HandleDown()
- case keyD, keyL, keyE:
- session.Player.HandleRight()
- case keyCtrlC, keyEscape:
- if g.SessionCount() == 1 {
- delete(gm.Games, g.Name)
- }
-
- g.RemoveSession(session)
- }
+ switch r {
+ case keyW, keyZ, keyK, keyComma:
+ session.Player.HandleUp()
+ case keyA, keyQ, keyH:
+ session.Player.HandleLeft()
+ case keyS, keyJ, keyO:
+ session.Player.HandleDown()
+ case keyD, keyL, keyE:
+ session.Player.HandleRight()
+ case keyCtrlC, keyEscape:
+ if g.SessionCount() == 1 {
+ delete(gm.Games, g.Name)
}
- }()
+
+ g.RemoveSession(session)
+ }
}
- }
+ }()
}
type Game struct {
diff --git a/main.go b/main.go
index 15b8341..bd26db0 100644
--- a/main.go
+++ b/main.go
@@ -20,7 +20,7 @@ const (
func handler(conn net.Conn, gm *GameManager, config *ssh.ServerConfig) {
// Before use, a handshake must be performed on the incoming
// net.Conn.
- _, chans, reqs, err := ssh.NewServerConn(conn, config)
+ sshConn, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil {
fmt.Println("Failed to handshake with new client")
return
@@ -66,7 +66,7 @@ func handler(conn net.Conn, gm *GameManager, config *ssh.ServerConfig) {
}
}(requests)
- gm.HandleChannel <- channel
+ gm.HandleNewChannel(channel, sshConn.User())
}
}
@@ -102,7 +102,6 @@ func main() {
// Create the GameManager
gm := NewGameManager()
- go gm.Run()
fmt.Printf(
"Listening on port %s for SSH and port %s for HTTP...\n",
Un proyecto texto-plano.xyz