88ed3ed3d47732e2df598fb7feace60e6900d779 — Trevor Slocum 13 days ago 227a638
Add /nick command
4 files changed, 72 insertions(+), 43 deletions(-)

M CHANGELOG.md
M cmd/netris/gui_input.go
M pkg/event/action.go
M pkg/game/game.go
M CHANGELOG.md => CHANGELOG.md +1 -0
@@ 1,5 1,6 @@
 0.1.3:
 - Animate game over display
+- Add /nick command
 
 0.1.2:
 - Resolve early game over issue

M cmd/netris/gui_input.go => cmd/netris/gui_input.go +64 -43
@@ 386,60 386,81 @@ func handleKeypress(ev *tcell.EventKey) *tcell.EventKey {
 	if inputActive {
 		switch k {
 		case tcell.KeyEnter:
-			msg := inputView.GetText()
-			if msg != "" {
-				if strings.HasPrefix(msg, "/cpu") {
-					if profileCPU == nil {
-						if len(msg) < 5 {
-							logMessage("Profile name must be specified")
-						} else {
-							profileName := strings.TrimSpace(msg[5:])
-
-							var err error
-							profileCPU, err = os.Create(profileName)
-							if err != nil {
-								log.Fatal(err)
-							}
+			defer setInputStatus(false)
 
-							err = pprof.StartCPUProfile(profileCPU)
-							if err != nil {
-								log.Fatal(err)
-							}
+			msg := inputView.GetText()
+			if strings.TrimSpace(msg) == "" {
+				return nil
+			}
 
-							logMessage(fmt.Sprintf("Started profiling CPU usage as %s", profileName))
-						}
+			msgl := strings.ToLower(msg)
+			switch {
+			case strings.HasPrefix(msgl, "/nick"):
+				if activeGame != nil && len(msg) > 6 {
+					var oldnick string
+					activeGame.Lock()
+					if p, ok := activeGame.Players[activeGame.LocalPlayer]; ok {
+						oldnick = p.Name
+						p.Name = game.Nickname(msg[6:])
 					} else {
-						pprof.StopCPUProfile()
-						profileCPU.Close()
-						profileCPU = nil
-
-						logMessage("Stopped profiling CPU usage")
+						return nil
 					}
-				} else if strings.HasPrefix(msg, "/version") {
-					v := game.Version
-					if v == "" {
-						v = "unknown"
+					activeGame.ProcessActionL(event.ActionNick)
+					if p, ok := activeGame.Players[activeGame.LocalPlayer]; ok {
+						p.Name = oldnick
 					}
+					activeGame.Unlock()
+				}
+			case strings.HasPrefix(msgl, "/ping"):
+				if activeGame != nil {
+					activeGame.ProcessAction(event.ActionPing)
+				}
+			case strings.HasPrefix(msgl, "/stats"):
+				if activeGame != nil {
+					activeGame.ProcessAction(event.ActionStats)
+				}
+			case strings.HasPrefix(msgl, "/version"):
+				v := game.Version
+				if v == "" {
+					v = "unknown"
+				}
 
-					logMessage(fmt.Sprintf("netris version %s", v))
-				} else if strings.HasPrefix(msg, "/ping") {
-					if activeGame != nil {
-						activeGame.ProcessAction(event.ActionPing)
-					}
-				} else if strings.HasPrefix(msg, "/stats") {
-					if activeGame != nil {
-						activeGame.ProcessAction(event.ActionStats)
-					}
-				} else {
-					if activeGame != nil {
-						activeGame.Event <- &event.MessageEvent{Message: msg}
+				logMessage(fmt.Sprintf("netris version %s", v))
+			case strings.HasPrefix(msgl, "/cpu"):
+				if profileCPU == nil {
+					if len(msg) < 5 {
+						logMessage("Profile name must be specified")
 					} else {
-						logMessage("Message not sent - not currently connected to any game")
+						profileName := strings.TrimSpace(msg[5:])
+
+						var err error
+						profileCPU, err = os.Create(profileName)
+						if err != nil {
+							log.Fatal(err)
+						}
+
+						err = pprof.StartCPUProfile(profileCPU)
+						if err != nil {
+							log.Fatal(err)
+						}
+
+						logMessage(fmt.Sprintf("Started profiling CPU usage as %s", profileName))
 					}
+				} else {
+					pprof.StopCPUProfile()
+					profileCPU.Close()
+					profileCPU = nil
+
+					logMessage("Stopped profiling CPU usage")
+				}
+			default:
+				if activeGame != nil {
+					activeGame.Event <- &event.MessageEvent{Message: msg}
+				} else {
+					logMessage("Message not sent - not currently connected to any game")
 				}
 			}
 
-			setInputStatus(false)
 			return nil
 		case tcell.KeyPgUp:
 			scrollMessages(-1)

M pkg/event/action.go => pkg/event/action.go +1 -0
@@ 12,4 12,5 @@ const (
 	ActionHardDrop
 	ActionPing
 	ActionStats
+	ActionNick
 )

M pkg/game/game.go => pkg/game/game.go +6 -0
@@ 763,6 763,10 @@ func (g *Game) ProcessAction(a event.GameAction) {
 	g.Lock()
 	defer g.Unlock()
 
+	g.ProcessActionL(a)
+}
+
+func (g *Game) ProcessActionL(a event.GameAction) {
 	if p, ok := g.Players[g.LocalPlayer]; ok {
 		if p.Matrix == nil {
 			return


@@ 781,6 785,8 @@ func (g *Game) ProcessAction(a event.GameAction) {
 			p.Matrix.MovePiece(0, -1)
 		case event.ActionHardDrop:
 			p.Matrix.HardDropPiece()
+		case event.ActionNick:
+			g.out(&GameCommandNickname{Nickname: Nickname(p.Name)})
 		case event.ActionPing:
 			g.sentPing = time.Now()
 			g.out(&GameCommandPing{Message: fmt.Sprintf("m%d", g.sentPing.UnixNano())})