Add /ping, update README

This commit is contained in:
Trevor Slocum 2019-10-18 23:26:22 -07:00
parent c0d927729a
commit 40374caa22
6 changed files with 34 additions and 6 deletions

View file

@ -5,7 +5,7 @@
Multiplayer Tetris clone
![](https://netris.rocketnine.space/static/screenshot2.png)
![](https://netris.rocketnine.space/static/screenshot4.png)
## Demo
@ -15,7 +15,15 @@ To play netris without installing:
## Install
To install netris and netris-server:
Choose one of the following methods:
### Download
[**Download netris**](https://netris.rocketnine.space/download/?sort=name&order=desc)
Windows and Linux binaries are available.
### Compile
```
GO111MODULE=on

View file

@ -298,6 +298,10 @@ func handleKeypress(ev *tcell.EventKey) *tcell.EventKey {
logMessage("Stopped profiling CPU usage")
}
} else if strings.HasPrefix(msg, "/ping") {
if activeGame != nil {
activeGame.ProcessAction(event.ActionPing)
}
} else {
if activeGame != nil {
activeGame.Event <- &event.MessageEvent{Message: msg}

View file

@ -18,7 +18,6 @@ import (
"time"
"git.sr.ht/~tslocum/netris/pkg/event"
"git.sr.ht/~tslocum/netris/pkg/game"
"git.sr.ht/~tslocum/netris/pkg/mino"
"github.com/mattn/go-isatty"

View file

@ -10,4 +10,5 @@ const (
ActionMoveRight
ActionSoftDrop
ActionHardDrop
ActionPing
)

View file

@ -51,6 +51,7 @@ type Game struct {
Local bool
sentPing time.Time
*sync.Mutex
}
@ -463,6 +464,18 @@ func (g *Game) HandleReadCommands(in chan GameCommandInterface) {
g.Log(logLevel, "LOCAL handle ", e.Command(), " from ", e.Source(), " ", e)
switch e.Command() {
case CommandPong:
if p, ok := e.(*GameCommandPong); ok {
if len(p.Message) > 1 && p.Message[0] == 'm' {
if i, err := strconv.ParseInt(p.Message[1:], 10, 64); err == nil {
if i == g.sentPing.UnixNano() {
g.Logf(LogStandard, "Server latency is %dms", time.Since(g.sentPing).Milliseconds())
g.sentPing = time.Time{}
}
}
}
}
case CommandMessage:
if p, ok := e.(*GameCommandMessage); ok {
prefix := "* "
@ -687,6 +700,9 @@ func (g *Game) ProcessAction(a event.GameAction) {
p.Matrix.MovePiece(0, -1)
case event.ActionHardDrop:
p.Matrix.HardDropPiece()
case event.ActionPing:
g.sentPing = time.Now()
g.out(&GameCommandPing{Message: fmt.Sprintf("m%d", g.sentPing.UnixNano())})
}
}
}

View file

@ -3,6 +3,7 @@ package game
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net"
"sync"
@ -92,7 +93,7 @@ func (s *ServerConn) handleSendKeepAlive() {
}
// TODO: Only send when necessary
s.Write(&GameCommandPing{})
s.Write(&GameCommandPing{Message: fmt.Sprintf("a%d", time.Now().UnixNano())})
}
}
@ -162,8 +163,7 @@ func (s *ServerConn) handleRead() {
panic(err)
}
// TODO: Verify
processed = true
gc = &gameCommand
} else if msg.Command == CommandMessage {
var gameCommand GameCommandMessage
err := json.Unmarshal(msg.Data, &gameCommand)