From 88ed3ed3d47732e2df598fb7feace60e6900d779 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Thu, 7 Nov 2019 17:47:24 -0800 Subject: [PATCH] Add /nick command --- CHANGELOG.md | 1 + cmd/netris/gui_input.go | 109 ++++++++++++++++++++++++---------------- pkg/event/action.go | 1 + pkg/game/game.go | 6 +++ 4 files changed, 73 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6946d6..b57c05d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ 0.1.3: - Animate game over display +- Add /nick command 0.1.2: - Resolve early game over issue diff --git a/cmd/netris/gui_input.go b/cmd/netris/gui_input.go index 7dbdd8e..2529288 100644 --- a/cmd/netris/gui_input.go +++ b/cmd/netris/gui_input.go @@ -386,60 +386,81 @@ func handleKeypress(ev *tcell.EventKey) *tcell.EventKey { if inputActive { switch k { case tcell.KeyEnter: + defer setInputStatus(false) + 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:]) + if strings.TrimSpace(msg) == "" { + return nil + } - 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)) - } + 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 + return nil + } + 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("Stopped profiling CPU usage") - } - } else if strings.HasPrefix(msg, "/version") { - v := game.Version - if v == "" { - v = "unknown" - } + 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 { + profileName := strings.TrimSpace(msg[5:]) - 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) + 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 { - if activeGame != nil { - activeGame.Event <- &event.MessageEvent{Message: msg} - } else { - logMessage("Message not sent - not currently connected to any game") - } + 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) diff --git a/pkg/event/action.go b/pkg/event/action.go index ad8959d..9d3f38e 100644 --- a/pkg/event/action.go +++ b/pkg/event/action.go @@ -12,4 +12,5 @@ const ( ActionHardDrop ActionPing ActionStats + ActionNick ) diff --git a/pkg/game/game.go b/pkg/game/game.go index dd73a31..007eaad 100644 --- a/pkg/game/game.go +++ b/pkg/game/game.go @@ -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())})