This commit is contained in:
stryan 2021-07-22 15:37:25 -04:00
parent 89c69d6037
commit 4473127d0a
10 changed files with 59 additions and 11 deletions

View File

@ -13,12 +13,11 @@ func main() {
if DEBUG { if DEBUG {
local() local()
} }
} }
func local() { func local() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
g := NewGame() g := snengame.NewGame()
for { for {
var t, cmd string var t, cmd string
@ -31,7 +30,7 @@ func local() {
cmd_raw, _ := reader.ReadString('\n') cmd_raw, _ := reader.ReadString('\n')
cmd = strings.TrimSpace(cmd_raw) cmd = strings.TrimSpace(cmd_raw)
if t == "s" || t == "d" { if t == "s" || t == "d" {
c := &Command{ c := &snengame.Command{
PlayerID: i, PlayerID: i,
Type: CmdType(t), Type: CmdType(t),
Cmd: cmd, Cmd: cmd,
@ -47,7 +46,7 @@ func local() {
fmt.Println("uh oh") fmt.Println("uh oh")
} }
} else if t == "a" { } else if t == "a" {
c := &Command{ c := &snengame.Command{
PlayerID: i, PlayerID: i,
Type: CmdType(t), Type: CmdType(t),
Cmd: cmd, Cmd: cmd,

47
cmd/server/server.go Normal file
View File

@ -0,0 +1,47 @@
package engine
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{} // use default options
func serve() {
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(w, r)
})
err := http.ListenAndServe(":7636", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func serveWs(w http.ResponseWriter, r *http.Request) {
// Upgrade our raw HTTP connection to a websocket based one
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("Error during connection upgradation:", err)
return
}
defer conn.Close()
// The event loop
for {
var cmd Command
var resp CommandResult
err := conn.ReadJSON(&cmd)
if err != nil {
log.Println("Error during message reading:", err)
break
}
log.Printf("Received: %s", cmd)
err = conn.WriteJSON(resp)
if err != nil {
log.Println("Error during message writing:", err)
break
}
}
}

2
go.mod
View File

@ -1,3 +1,5 @@
module snengame module snengame
go 1.16 go 1.16
require github.com/gorilla/websocket v1.4.2

View File

@ -1,4 +1,4 @@
package main package game
import "fmt" import "fmt"

View File

@ -1,4 +1,4 @@
package main package game
import "fmt" import "fmt"

View File

@ -1,4 +1,4 @@
package main package game
type CmdType string type CmdType string

View File

@ -1,4 +1,4 @@
package main package game
import ( import (
"math/rand" "math/rand"

View File

@ -1,4 +1,4 @@
package main package game
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package main package game
import "fmt" import "fmt"

View File

@ -1,4 +1,4 @@
package main package game
type Player struct { type Player struct {
Name string `json:"name"` Name string `json:"name"`