Harry de Boer 10 лет назад
Родитель
Сommit
304d6ee77b
6 измененных файлов с 85 добавлено и 23 удалено
  1. 3 1
      .gitignore
  2. 0 10
      board/board.go
  3. 7 0
      board/tile.go
  4. 52 5
      games/game.go
  5. 7 4
      player/player.go
  6. 16 3
      urlrouter.go

+ 3 - 1
.gitignore

@@ -1 +1,3 @@
-*.exe
+*.exe
+*.iml
+.idea/

+ 0 - 10
board/board.go

@@ -8,9 +8,6 @@ import (
 	"math/rand"
 )
 
-type BoardSummary struct {
-}
-
 type Board struct {
 	Cols   int        `json:"cols"`
 	Rows   int        `json:"rows"`
@@ -59,13 +56,6 @@ func (b *Board) String() string {
 	return buffer.String()
 }
 
-type jsonTile struct {
-	X      int    `json:"x"`
-	Y      int    `json:"y"`
-	Type   string `json:"type"`
-	Player string `json:"player,omitempty"`
-}
-
 func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
 	sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
 

+ 7 - 0
board/tile.go

@@ -2,6 +2,13 @@ package board
 
 type Tile byte
 
+type jsonTile struct {
+	X      int    `json:"x"`
+	Y      int    `json:"y"`
+	Type   string `json:"type"`
+	Player string `json:"player,omitempty"`
+}
+
 const (
 	Water Tile = '~'
 	Ice   Tile = '*'

+ 52 - 5
games/game.go

@@ -3,19 +3,23 @@ package games
 import (
 	"battlecamp-go-server/board"
 	"battlecamp-go-server/player"
+	"math/rand"
+	"sync"
 	"time"
 )
 
 type Game struct {
-	Id        int64         `json:"id"`
-	StartTime int64         `json:"startTime"`
-	EndTime   int64         `json:"endTime"`
-	Board     *board.Board   `json:"board"`
+	Id        int64            `json:"id"`
+	StartTime int64            `json:"startTime"`
+	EndTime   int64            `json:"endTime"`
+	Board     *board.Board     `json:"board"`
+	Players   []*player.Player `json:"-"`
+	mutex     sync.Mutex
 	Winner    *player.Player `json:"winner,omitempty"`
 }
 
 func NewGame(cols, rows int) *Game {
-	createTime := time.Now().UnixNano()/1000
+	createTime := time.Now().UnixNano() / 1000
 
 	game := &Game{
 		StartTime: createTime,
@@ -24,3 +28,46 @@ func NewGame(cols, rows int) *Game {
 
 	return game
 }
+
+func (g *Game) Join(p *player.Player) {
+	g.mutex.Lock()
+	g.placePlayer(p)
+	g.Players = append(g.Players, p)
+	g.mutex.Unlock()
+}
+
+func (g *Game) placePlayer(p *player.Player) *board.Coordinate {
+	xRange := g.Board.Cols / 5
+	yRange := g.Board.Rows
+
+	for {
+		x := rand.Intn(xRange)
+		y := rand.Intn(yRange)
+		
+		if g.isValidPlayerPos(x, y) {
+			return &board.Coordinate{x, y}
+		}
+	}
+
+}
+
+func (g *Game) isValidPlayerPos(x, y int) bool {
+	t := g.Board.Get(x, y)
+
+	if t == board.Rock {
+		return false;
+	}
+	
+	return g.getPlayer(x, y) == nil
+}
+
+func (g *Game) getPlayer(x, y int) *player.Player {
+	
+	for _, p := range g.Players {
+		if (p.Pos.X == x && p.Pos.Y == y) {
+			return p
+		}
+	}
+	
+	return nil	
+}

+ 7 - 4
player/player.go

@@ -1,13 +1,16 @@
 package player
 
+import "battlecamp-go-server/board"
+
 type Player struct {
-	Id    string `json:"id"`
-	Color string `json:"color"`
-	Type  Type   `json:"type"`
+	Id    string           `json:"id"`
+	Color string           `json:"color"`
+	Type  Type             `json:"type"`
+	Pos   board.Coordinate `json:"pos"`
 }
 
 type Type int
 
 const (
-	MyPlayer Type = 1
+	TypeDefault Type = 1
 )

+ 16 - 3
urlrouter.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"battlecamp-go-server/games"
+	"battlecamp-go-server/player"
 	"encoding/json"
 	"fmt"
 	"net/http"
@@ -70,7 +71,16 @@ func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 }
 
 func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-	http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
+	w.Header().Set("Content-Type", "application/json")
+	idString := ps.ByName("gameid")
+	gameId, _ := strconv.ParseInt(idString, 10, 64)
+	var player player.Player
+	d := json.NewDecoder(r.Body)
+	d.Decode(&player)
+	game := currentGames.GetGame(gameId)
+	game.Join(&player)
+	e := json.NewEncoder(w)
+	e.Encode(&player)
 }
 
 func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -81,7 +91,11 @@ func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 }
 
 func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-	http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
+	w.Header().Set("Content-Type", "application/json")
+	idString := ps.ByName("gameid")
+	gameId, _ := strconv.ParseInt(idString, 10, 64)
+	e := json.NewEncoder(w)
+	e.Encode(currentGames.GetGame(gameId).Players)
 }
 
 func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -89,7 +103,6 @@ func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 }
 
 func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-
 	http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
 }