Browse Source

Implement listGames.

Harry de Boer 10 năm trước cách đây
mục cha
commit
a4671119b0
3 tập tin đã thay đổi với 12 bổ sung9 xóa
  1. 2 2
      board/board.go
  2. 2 2
      games/game.go
  3. 8 5
      main.go

+ 2 - 2
board/board.go

@@ -17,8 +17,8 @@ type Board struct {
 const maxIceWidth = 26
 
 // Create a new randomly generated board.
-func New(cols, rows int) Board {
-	b := Board{cols, rows, make([]Tile, rows*cols)}
+func New(cols, rows int) *Board {
+	b := &Board{cols, rows, make([]Tile, rows*cols)}
 
 	for y := 0; y < rows; y++ {
 		for x := 0; x < cols; x++ {

+ 2 - 2
games/game.go

@@ -10,8 +10,8 @@ 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"`
+	Board     *board.Board   `json:"-"`
+	Winner    *player.Player `json:"winner,omitempty"`
 }
 
 func NewGame(cols, rows int) *Game {

+ 8 - 5
main.go

@@ -2,7 +2,7 @@
 package main
 
 import (
-	"battlecamp-go-server/board"
+	"encoding/json"
 	"battlecamp-go-server/games"
 	"log"
 	"net/http"
@@ -47,7 +47,9 @@ func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
 }
 
 func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
-	http.Error(w, "listGames not implemented", http.StatusNotImplemented)
+	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
+	e := json.NewEncoder(w)
+	e.Encode(currentGames.ListGames())
 }
 
 func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -64,6 +66,7 @@ func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	}
 
 	currentGames.AddGame(game)
+	w.WriteHeader(http.StatusCreated)
 }
 
 func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -71,6 +74,9 @@ func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 }
 
 func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+
+	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
+
 	http.Error(w, "showGame not implemented", http.StatusNotImplemented)
 }
 
@@ -88,7 +94,4 @@ func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
 	//y, _ := strconv.Atoi(req.FormValue("y"))
 	//rows, _ := strconv.Atoi(req.FormValue("rows"))
 	//cols, _ := strconv.Atoi(req.FormValue("cols"))
-	b := board.New(56, 35)
-	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
-	b.WriteJSON(w)
 }