|
@@ -1,18 +1,22 @@
|
|
|
-package main
|
|
|
|
|
|
|
+package gameserver
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
- "battlecamp-go-server/games"
|
|
|
|
|
|
|
+ "battlecamp-go-server/game"
|
|
|
"battlecamp-go-server/player"
|
|
"battlecamp-go-server/player"
|
|
|
"battlecamp-go-server/stomp"
|
|
"battlecamp-go-server/stomp"
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
"log"
|
|
"log"
|
|
|
"net/http"
|
|
"net/http"
|
|
|
"strconv"
|
|
"strconv"
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
"github.com/julienschmidt/httprouter"
|
|
"github.com/julienschmidt/httprouter"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-func newUrlRouter() *httprouter.Router {
|
|
|
|
|
|
|
+var gameServer *GameServer
|
|
|
|
|
+
|
|
|
|
|
+func newUrlRouter(gs *GameServer) *httprouter.Router {
|
|
|
|
|
+ gameServer = gs
|
|
|
|
|
+
|
|
|
router := httprouter.New()
|
|
router := httprouter.New()
|
|
|
router.GET("/", showIndex)
|
|
router.GET("/", showIndex)
|
|
|
router.GET("/games", listGames)
|
|
router.GET("/games", listGames)
|
|
@@ -24,11 +28,6 @@ func newUrlRouter() *httprouter.Router {
|
|
|
router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
|
|
router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
|
|
|
router.POST("/games/:gameid/move/:playerid/:direction", movePlayer)
|
|
router.POST("/games/:gameid/move/:playerid/:direction", movePlayer)
|
|
|
|
|
|
|
|
- //router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
|
|
|
|
|
- // log.Printf("Panic: %v\n", something)
|
|
|
|
|
- // http.Error(w, fmt.Sprintf("Internal server error: %v", something), http.StatusInternalServerError)
|
|
|
|
|
- //}
|
|
|
|
|
-
|
|
|
|
|
return router
|
|
return router
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -52,22 +51,22 @@ func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
|
|
|
|
|
|
|
|
func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
e := json.NewEncoder(w)
|
|
e := json.NewEncoder(w)
|
|
|
- e.Encode(currentGames.ListGames())
|
|
|
|
|
|
|
+ e.Encode(gameServer.ListGames())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
xString := ps.ByName("cols")
|
|
xString := ps.ByName("cols")
|
|
|
yString := ps.ByName("rows")
|
|
yString := ps.ByName("rows")
|
|
|
- var game *games.Game
|
|
|
|
|
|
|
+ var game *game.Game
|
|
|
|
|
|
|
|
log.Println("Creating new game...")
|
|
log.Println("Creating new game...")
|
|
|
|
|
|
|
|
if xString != "" && yString != "" {
|
|
if xString != "" && yString != "" {
|
|
|
x, _ := strconv.Atoi(xString)
|
|
x, _ := strconv.Atoi(xString)
|
|
|
y, _ := strconv.Atoi(yString)
|
|
y, _ := strconv.Atoi(yString)
|
|
|
- game = currentGames.AddGame(x, y)
|
|
|
|
|
|
|
+ game = gameServer.AddGame(x, y)
|
|
|
} else {
|
|
} else {
|
|
|
- game = currentGames.AddGame(56, 35)
|
|
|
|
|
|
|
+ game = gameServer.AddGame(56, 35)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
w.WriteHeader(http.StatusCreated)
|
|
@@ -83,7 +82,7 @@ func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
var p player.Player
|
|
var p player.Player
|
|
|
d.Decode(&p)
|
|
d.Decode(&p)
|
|
|
|
|
|
|
|
- game := currentGames.GetGame(gameId)
|
|
|
|
|
|
|
+ game := gameServer.GetGame(gameId)
|
|
|
game.Join(&p)
|
|
game.Join(&p)
|
|
|
|
|
|
|
|
e := json.NewEncoder(w)
|
|
e := json.NewEncoder(w)
|
|
@@ -98,21 +97,21 @@ func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
|
|
id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
|
|
|
e := json.NewEncoder(w)
|
|
e := json.NewEncoder(w)
|
|
|
- e.Encode(currentGames.GetGame(id))
|
|
|
|
|
|
|
+ e.Encode(gameServer.GetGame(id))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
|
|
gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
|
|
|
e := json.NewEncoder(w)
|
|
e := json.NewEncoder(w)
|
|
|
- e.Encode(currentGames.GetGame(gameId).Players)
|
|
|
|
|
|
|
+ e.Encode(gameServer.GetGame(gameId).Players)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
|
|
gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
|
|
|
playerId := ps.ByName("playerid")
|
|
playerId := ps.ByName("playerid")
|
|
|
- g := currentGames.GetGame(gameId)
|
|
|
|
|
|
|
+ g := gameServer.GetGame(gameId)
|
|
|
p := g.GetPlayer(playerId)
|
|
p := g.GetPlayer(playerId)
|
|
|
e := json.NewEncoder(w)
|
|
e := json.NewEncoder(w)
|
|
|
e.Encode(p)
|
|
e.Encode(p)
|
|
@@ -123,7 +122,7 @@ func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
idString := ps.ByName("gameid")
|
|
idString := ps.ByName("gameid")
|
|
|
gameId, _ := strconv.ParseInt(idString, 10, 64)
|
|
gameId, _ := strconv.ParseInt(idString, 10, 64)
|
|
|
playerId := ps.ByName("playerid")
|
|
playerId := ps.ByName("playerid")
|
|
|
- g := currentGames.GetGame(gameId)
|
|
|
|
|
|
|
+ g := gameServer.GetGame(gameId)
|
|
|
p := g.GetPlayer(playerId)
|
|
p := g.GetPlayer(playerId)
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
direction := ps.ByName("direction")
|
|
direction := ps.ByName("direction")
|
|
@@ -148,7 +147,7 @@ func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
|
|
rowsString := req.FormValue("rows")
|
|
rowsString := req.FormValue("rows")
|
|
|
|
|
|
|
|
id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
|
|
id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
|
|
|
- b := currentGames.GetGame(id).Board
|
|
|
|
|
|
|
+ b := gameServer.GetGame(id).Board
|
|
|
|
|
|
|
|
if xString != "" && yString != "" && colsString != "" && rowsString != "" {
|
|
if xString != "" && yString != "" && colsString != "" && rowsString != "" {
|
|
|
x, _ := strconv.Atoi(xString)
|
|
x, _ := strconv.Atoi(xString)
|
|
@@ -167,6 +166,6 @@ func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Para
|
|
|
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
|
|
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
|
|
|
|
|
|
|
|
id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
|
|
id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
|
|
|
- b := currentGames.GetGame(id).Board.String()
|
|
|
|
|
|
|
+ b := gameServer.GetGame(id).Board.String()
|
|
|
w.Write([]byte(b))
|
|
w.Write([]byte(b))
|
|
|
}
|
|
}
|