Pārlūkot izejas kodu

Refactored gameserver

Ronald Peterson 10 gadi atpakaļ
vecāks
revīzija
43290f087e
5 mainītis faili ar 46 papildinājumiem un 40 dzēšanām
  1. 1 2
      game/game.go
  2. 1 1
      game/games_test.go
  3. 22 12
      gameserver/gameserver.go
  4. 19 20
      gameserver/urlrouter.go
  5. 3 5
      main.go

+ 1 - 2
games/game.go → game/game.go

@@ -1,4 +1,4 @@
-package games
+package game
 
 import (
 	"battlecamp-go-server/board"
@@ -18,7 +18,6 @@ type Game struct {
 	Players    []*player.Player `json:"-"`
 	mutex      sync.Mutex
 	Winner     *player.Player `json:"winner,omitempty"`
-	gameServer *GameServer
 }
 
 func NewGame(cols, rows int) *Game {

+ 1 - 1
games/games_test.go → game/games_test.go

@@ -1,4 +1,4 @@
-package games
+package game
 
 import (
 	"testing"

+ 22 - 12
games/gameserver.go → gameserver/gameserver.go

@@ -1,7 +1,12 @@
-package games
+package gameserver
 
 import (
+	"battlecamp-go-server/flag"
+	"battlecamp-go-server/game"
 	"battlecamp-go-server/stomp"
+	"log"
+	"net/http"
+	"strconv"
 )
 
 type GameServer struct {
@@ -11,15 +16,15 @@ type GameServer struct {
 }
 
 type addGameChan struct {
-	game *Game
+	game *game.Game
 	id   chan int64
 }
 
-type gamesChan chan []*Game
+type gamesChan chan []*game.Game
 
 type getGame struct {
 	id         int64
-	returnChan chan *Game
+	returnChan chan *game.Game
 }
 
 func New() GameServer {
@@ -34,8 +39,13 @@ func New() GameServer {
 	return gameServer
 }
 
+func (gs *GameServer) Serve() {
+
+	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*flag.Port), newUrlRouter(gs)))
+}
+
 func serveGames(gameServer GameServer) {
-	gamesMap := make(map[int64]*Game)
+	gamesMap := make(map[int64]*game.Game)
 	gameCounter := int64(0)
 
 	for {
@@ -46,7 +56,7 @@ func serveGames(gameServer GameServer) {
 			gamesMap[addGameChan.game.Id] = addGameChan.game
 			addGameChan.id <- addGameChan.game.Id
 		case r := <-gameServer.listGamesChan:
-			v := make([]*Game, 0, len(gamesMap))
+			v := make([]*game.Game, 0, len(gamesMap))
 			for _, value := range gamesMap {
 				v = append(v, value)
 			}
@@ -62,8 +72,8 @@ type stompGameStart struct {
 	GameId int64  `json:"gameId"`
 }
 
-func (games GameServer) AddGame(x, y int) *Game {
-	game := NewGame(x, y)
+func (games GameServer) AddGame(x, y int) *game.Game {
+	game := game.NewGame(x, y) // TODO
 	addGame := addGameChan{
 		game: game,
 		id:   make(chan int64),
@@ -81,16 +91,16 @@ func (games GameServer) AddGame(x, y int) *Game {
 	return game
 }
 
-func (games GameServer) ListGames() []*Game {
-	r := make(chan []*Game)
+func (games GameServer) ListGames() []*game.Game {
+	r := make(chan []*game.Game)
 	games.listGamesChan <- r
 	return <-r
 }
 
-func (games GameServer) GetGame(id int64) *Game {
+func (games GameServer) GetGame(id int64) *game.Game {
 	getGame := getGame{
 		id:         id,
-		returnChan: make(chan *Game),
+		returnChan: make(chan *game.Game),
 	}
 	games.getGameChan <- getGame
 	return <-getGame.returnChan

+ 19 - 20
urlrouter.go → gameserver/urlrouter.go

@@ -1,18 +1,22 @@
-package main
+package gameserver
 
 import (
-	"battlecamp-go-server/games"
+	"battlecamp-go-server/game"
 	"battlecamp-go-server/player"
 	"battlecamp-go-server/stomp"
 	"encoding/json"
 	"log"
 	"net/http"
 	"strconv"
-	
+
 	"github.com/julienschmidt/httprouter"
 )
 
-func newUrlRouter() *httprouter.Router {
+var gameServer *GameServer
+
+func newUrlRouter(gs *GameServer) *httprouter.Router {
+	gameServer = gs
+
 	router := httprouter.New()
 	router.GET("/", showIndex)
 	router.GET("/games", listGames)
@@ -24,11 +28,6 @@ func newUrlRouter() *httprouter.Router {
 	router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
 	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
 }
 
@@ -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) {
 	e := json.NewEncoder(w)
-	e.Encode(currentGames.ListGames())
+	e.Encode(gameServer.ListGames())
 }
 
 func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	xString := ps.ByName("cols")
 	yString := ps.ByName("rows")
-	var game *games.Game
+	var game *game.Game
 
 	log.Println("Creating new game...")
 
 	if xString != "" && yString != "" {
 		x, _ := strconv.Atoi(xString)
 		y, _ := strconv.Atoi(yString)
-		game = currentGames.AddGame(x, y)
+		game = gameServer.AddGame(x, y)
 	} else {
-		game = currentGames.AddGame(56, 35)
+		game = gameServer.AddGame(56, 35)
 	}
 
 	w.WriteHeader(http.StatusCreated)
@@ -83,7 +82,7 @@ func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	var p player.Player
 	d.Decode(&p)
 
-	game := currentGames.GetGame(gameId)
+	game := gameServer.GetGame(gameId)
 	game.Join(&p)
 
 	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")
 	id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
 	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) {
 	w.Header().Set("Content-Type", "application/json")
 	gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
 	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) {
 	w.Header().Set("Content-Type", "application/json")
 	gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
 	playerId := ps.ByName("playerid")
-	g := currentGames.GetGame(gameId)
+	g := gameServer.GetGame(gameId)
 	p := g.GetPlayer(playerId)
 	e := json.NewEncoder(w)
 	e.Encode(p)
@@ -123,7 +122,7 @@ func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	idString := ps.ByName("gameid")
 	gameId, _ := strconv.ParseInt(idString, 10, 64)
 	playerId := ps.ByName("playerid")
-	g := currentGames.GetGame(gameId)
+	g := gameServer.GetGame(gameId)
 	p := g.GetPlayer(playerId)
 	w.Header().Set("Content-Type", "application/json")
 	direction := ps.ByName("direction")
@@ -148,7 +147,7 @@ func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
 	rowsString := req.FormValue("rows")
 
 	id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
-	b := currentGames.GetGame(id).Board
+	b := gameServer.GetGame(id).Board
 
 	if xString != "" && yString != "" && colsString != "" && rowsString != "" {
 		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")
 
 	id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
-	b := currentGames.GetGame(id).Board.String()
+	b := gameServer.GetGame(id).Board.String()
 	w.Write([]byte(b))
 }

+ 3 - 5
main.go

@@ -3,16 +3,14 @@ package main
 
 import (
 	"log"
-	"net/http"
 	"os"
 	"runtime/pprof"
-	"strconv"
 
 	"battlecamp-go-server/flag"
-	"battlecamp-go-server/games"
+	"battlecamp-go-server/gameserver"
 )
 
-var currentGames games.GameServer = games.New()
+var gameServer gameserver.GameServer = gameserver.New()
 
 func main() {
 	initLogging()
@@ -20,7 +18,7 @@ func main() {
 
 	initCliFlags()
 
-	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*flag.Port), newUrlRouter()))
+	gameServer.Serve()
 }
 
 func initLogging() {