refactor
This commit is contained in:
parent
89c69d6037
commit
4473127d0a
@ -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,
|
47
cmd/server/server.go
Normal file
47
cmd/server/server.go
Normal 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
2
go.mod
@ -1,3 +1,5 @@
|
||||
module snengame
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/gorilla/websocket v1.4.2
|
||||
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package game
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package game
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package game
|
||||
|
||||
type CmdType string
|
||||
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package game
|
||||
|
||||
import (
|
||||
"math/rand"
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package game
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package game
|
||||
|
||||
type Player struct {
|
||||
Name string `json:"name"`
|
Loading…
Reference in New Issue
Block a user