From 4473127d0a71e619e7ac7e75098fb5040ecb0b5f Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 22 Jul 2021 15:37:25 -0400 Subject: [PATCH] refactor --- main.go => cmd/engine/main.go | 7 ++-- cmd/server/server.go | 47 ++++++++++++++++++++++ go.mod | 2 + board.go => internal/game/board.go | 2 +- card.go => internal/game/card.go | 2 +- cmd.go => internal/game/cmd.go | 2 +- deck.go => internal/game/deck.go | 2 +- game.go => internal/game/game.go | 2 +- game_view.go => internal/game/game_view.go | 2 +- player.go => internal/game/player.go | 2 +- 10 files changed, 59 insertions(+), 11 deletions(-) rename main.go => cmd/engine/main.go (93%) create mode 100644 cmd/server/server.go rename board.go => internal/game/board.go (99%) rename card.go => internal/game/card.go (99%) rename cmd.go => internal/game/cmd.go (97%) rename deck.go => internal/game/deck.go (98%) rename game.go => internal/game/game.go (99%) rename game_view.go => internal/game/game_view.go (99%) rename player.go => internal/game/player.go (95%) diff --git a/main.go b/cmd/engine/main.go similarity index 93% rename from main.go rename to cmd/engine/main.go index eb5ca83..693bfa9 100644 --- a/main.go +++ b/cmd/engine/main.go @@ -13,12 +13,11 @@ func main() { if DEBUG { local() } - } func local() { reader := bufio.NewReader(os.Stdin) - g := NewGame() + g := snengame.NewGame() for { var t, cmd string @@ -31,7 +30,7 @@ func local() { cmd_raw, _ := reader.ReadString('\n') cmd = strings.TrimSpace(cmd_raw) if t == "s" || t == "d" { - c := &Command{ + c := &snengame.Command{ PlayerID: i, Type: CmdType(t), Cmd: cmd, @@ -47,7 +46,7 @@ func local() { fmt.Println("uh oh") } } else if t == "a" { - c := &Command{ + c := &snengame.Command{ PlayerID: i, Type: CmdType(t), Cmd: cmd, diff --git a/cmd/server/server.go b/cmd/server/server.go new file mode 100644 index 0000000..dd26875 --- /dev/null +++ b/cmd/server/server.go @@ -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 + } + } +} diff --git a/go.mod b/go.mod index 220d3b3..cedbaed 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module snengame go 1.16 + +require github.com/gorilla/websocket v1.4.2 diff --git a/board.go b/internal/game/board.go similarity index 99% rename from board.go rename to internal/game/board.go index efe42d4..fe8b484 100644 --- a/board.go +++ b/internal/game/board.go @@ -1,4 +1,4 @@ -package main +package game import "fmt" diff --git a/card.go b/internal/game/card.go similarity index 99% rename from card.go rename to internal/game/card.go index ecef9b6..3a8c378 100644 --- a/card.go +++ b/internal/game/card.go @@ -1,4 +1,4 @@ -package main +package game import "fmt" diff --git a/cmd.go b/internal/game/cmd.go similarity index 97% rename from cmd.go rename to internal/game/cmd.go index 8cbb955..b9d00d7 100644 --- a/cmd.go +++ b/internal/game/cmd.go @@ -1,4 +1,4 @@ -package main +package game type CmdType string diff --git a/deck.go b/internal/game/deck.go similarity index 98% rename from deck.go rename to internal/game/deck.go index e4fc4d9..9b4d044 100644 --- a/deck.go +++ b/internal/game/deck.go @@ -1,4 +1,4 @@ -package main +package game import ( "math/rand" diff --git a/game.go b/internal/game/game.go similarity index 99% rename from game.go rename to internal/game/game.go index 55c31dd..cd3bd45 100644 --- a/game.go +++ b/internal/game/game.go @@ -1,4 +1,4 @@ -package main +package game import ( "fmt" diff --git a/game_view.go b/internal/game/game_view.go similarity index 99% rename from game_view.go rename to internal/game/game_view.go index eaa81d6..23968cf 100644 --- a/game_view.go +++ b/internal/game/game_view.go @@ -1,4 +1,4 @@ -package main +package game import "fmt" diff --git a/player.go b/internal/game/player.go similarity index 95% rename from player.go rename to internal/game/player.go index f2aed75..4877d1f 100644 --- a/player.go +++ b/internal/game/player.go @@ -1,4 +1,4 @@ -package main +package game type Player struct { Name string `json:"name"`