Quellcode durchsuchen

Aanmaken games poc

Harry de Boer vor 10 Jahren
Ursprung
Commit
4b5afbec8b
4 geänderte Dateien mit 104 neuen und 6 gelöschten Zeilen
  1. 27 0
      games/game.go
  2. 46 0
      games/games.go
  3. 18 6
      main.go
  4. 13 0
      player/player.go

+ 27 - 0
games/game.go

@@ -0,0 +1,27 @@
+package games
+
+import (
+	"battlecamp-go-server/board"
+	"battlecamp-go-server/player"
+	"time"
+)
+
+type Game struct {
+	Id        int64         `json:"id"`
+	StartTime int64         `json:"startTime"`
+	EndTime   int64         `json:"endTime"`
+	Board     board.Board   `json:"board"`
+	Winner    player.Player `json:"winner"`
+}
+
+func NewGame(cols, rows int) *Game {
+	createTime := time.Now().UnixNano()
+
+	game := &Game{
+		Id:        createTime,
+		StartTime: createTime,
+		Board:     board.New(cols, rows),
+	}
+
+	return game
+}

+ 46 - 0
games/games.go

@@ -0,0 +1,46 @@
+package games
+
+type Games struct {
+	addChan  chan *Game
+	listChan chan listChan
+}
+
+type listChan chan []*Game
+
+func New() Games {
+
+	games := Games{
+		addChan:  make(chan *Game),
+		listChan: make(chan listChan),
+	}
+
+	go serveGames(games)
+	return games
+}
+
+func serveGames(games Games) {
+	gamesMap := make(map[int64]*Game)
+
+	for {
+		select {
+		case game := <-games.addChan:
+			gamesMap[game.Id] = game
+		case r := <-games.listChan:
+			v := make([]*Game, 0, len(gamesMap))
+			for _, value := range gamesMap {
+				v = append(v, value)
+			}
+			r <- v
+		}
+	}
+}
+
+func (games Games) AddGame(game *Game) {
+	games.addChan <- game
+}
+
+func (games Games) ListGames() []*Game {
+	r := make(chan []*Game)
+	games.listChan <- r
+	return <-r
+}

+ 18 - 6
main.go

@@ -13,18 +13,20 @@ func main() {
 	runtime.GOMAXPROCS(runtime.NumCPU())
 
 	router := httprouter.New()
-	router.GET("/", index)
-	router.GET("/games/", gameListHandler)
-	router.GET("/games/:id", gameHandler)
+	router.GET("/", indexHandler)
+	router.GET("/games", gameListHandler)
 	router.PUT("/games", createGameHandler)
 	router.PUT("/games/:rows/:cols", createGameHandler)
-	router.POST("/games/:id/move/:playerId/:direction", moveHandler)
+	router.GET("/games/:id", gameHandler)
+	router.GET("/games/board/:id", boardHandler)
+	router.GET("/games/:id/players", playerListHandler)
 	// TODO: /games/{id}/players/{playerId}
+	router.POST("/games/:id/move/:playerId/:direction", moveHandler)
 
 	log.Fatal(http.ListenAndServe(":8080", router))
 }
 
-func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
+func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
 	w.Write([]byte("Go battlecamp!"))
 }
 
@@ -32,11 +34,16 @@ func gameListHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params
 	http.Error(w, "Not implemented", http.StatusNotImplemented)
 }
 
+func createGameHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	http.Error(w, "Not implemented", http.StatusNotImplemented)
+}
+
 func gameHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+	
 	http.Error(w, "Not implemented", http.StatusNotImplemented)
 }
 
-func createGameHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+func playerListHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	http.Error(w, "Not implemented", http.StatusNotImplemented)
 }
 
@@ -44,7 +51,12 @@ func moveHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	http.Error(w, "Not implemented", http.StatusNotImplemented)
 }
 
+// Request params: x={x}&y={y}&rows={rows}&cols={cols}
 func boardHandler(w http.ResponseWriter, req *http.Request) {
+	x, _ := atoi(req.FormValue("x"))
+	y, _ := atoi(req.FormValue("y"))
+	rows, _ := atoi(req.FormValue("rows"))
+	cols, _ := atoi(req.FormValue("cols"))
 	b := board.New(56, 35)
 	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
 	b.WriteJSON(w)

+ 13 - 0
player/player.go

@@ -0,0 +1,13 @@
+package player
+
+type Player struct {
+	Id    string `json:"id"`
+	Color string `json:"color"`
+	Type  Type   `json:"type"`
+}
+
+type Type int
+
+const (
+	MyPlayer Type = 1
+)