Compare commits
8 Commits
7b924ffa06
...
master
Author | SHA1 | Date | |
---|---|---|---|
677a6e9e4d | |||
48ea6e62a1 | |||
7558da5c8e | |||
209a4f5caf | |||
74ee26c8d6 | |||
a17107379c | |||
877edb34f5 | |||
56e561c91e |
@ -14,3 +14,6 @@ Currently stores no state and has no user authentication, so using it should jus
|
|||||||
POST /game/{id}/move
|
POST /game/{id}/move
|
||||||
GET /game/{id}/move/{movenum}
|
GET /game/{id}/move/{movenum}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Authorization headers
|
||||||
|
Requests should contain header "Player-id"="red|blue"
|
||||||
|
89
api.go
89
api.go
@ -49,7 +49,7 @@ func (a *API) NewGame(res http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Printf("creating new game %v", a.nextInt)
|
log.Printf("creating new game %v", a.nextInt)
|
||||||
a.games[a.nextInt] = NewSession()
|
a.games[a.nextInt] = NewSession(8)
|
||||||
a.games[a.nextInt].redPlayer.Ready = true
|
a.games[a.nextInt].redPlayer.Ready = true
|
||||||
respondWithJSON(res, http.StatusOK, newGameResp{a.nextInt, "red"})
|
respondWithJSON(res, http.StatusOK, newGameResp{a.nextInt, "red"})
|
||||||
a.nextInt = a.nextInt + 1
|
a.nextInt = a.nextInt + 1
|
||||||
@ -63,18 +63,7 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
|
|||||||
respondWithError(res, http.StatusBadRequest, "Invalid game ID")
|
respondWithError(res, http.StatusBadRequest, "Invalid game ID")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var gr gameReq
|
pid := req.Header.Get("Player-id")
|
||||||
decoder := json.NewDecoder(req.Body)
|
|
||||||
if err := decoder.Decode(&gr); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
respondWithError(res, http.StatusBadRequest, "Invalid request payload")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer req.Body.Close()
|
|
||||||
if gr.PlayerID != "red" && gr.PlayerID != "blue" {
|
|
||||||
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var p *Player
|
var p *Player
|
||||||
|
|
||||||
s, isset := a.games[id]
|
s, isset := a.games[id]
|
||||||
@ -82,14 +71,26 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
|
|||||||
respondWithError(res, http.StatusBadRequest, "No such game")
|
respondWithError(res, http.StatusBadRequest, "No such game")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if gr.PlayerID == "red" {
|
if pid == "red" {
|
||||||
p = s.redPlayer
|
p = s.redPlayer
|
||||||
} else {
|
} else if pid == "blue" {
|
||||||
p = s.bluePlayer
|
p = s.bluePlayer
|
||||||
|
} else {
|
||||||
|
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("sending game state")
|
log.Println("sending game state")
|
||||||
respondWithJSON(res, http.StatusOK, gameResp{s.getBoard(p)})
|
board := s.getBoard(p)
|
||||||
|
rotate := req.Header.Get("Rotate")
|
||||||
|
if rotate == "true" {
|
||||||
|
log.Println("rotating output")
|
||||||
|
//rotateBoard(board)
|
||||||
|
//rotateBoard(board)
|
||||||
|
//rotateBoard(board)
|
||||||
|
|
||||||
|
}
|
||||||
|
respondWithJSON(res, http.StatusOK, gameResp{board})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,23 +102,22 @@ func (a *API) GetGameStatus(res http.ResponseWriter, req *http.Request) {
|
|||||||
respondWithError(res, http.StatusBadRequest, "Invalid game ID")
|
respondWithError(res, http.StatusBadRequest, "Invalid game ID")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var gr gameReq
|
|
||||||
decoder := json.NewDecoder(req.Body)
|
|
||||||
if err := decoder.Decode(&gr); err != nil {
|
|
||||||
respondWithError(res, http.StatusBadRequest, "Invalid resquest payload")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer req.Body.Close()
|
|
||||||
if gr.PlayerID != "red" && gr.PlayerID != "blue" {
|
|
||||||
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s, isset := a.games[id]
|
s, isset := a.games[id]
|
||||||
if !isset {
|
if !isset {
|
||||||
respondWithError(res, http.StatusBadRequest, "No such game")
|
respondWithError(res, http.StatusBadRequest, "No such game")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("sending game status")
|
pid := req.Header.Get("Player-id")
|
||||||
|
var p *Player
|
||||||
|
if pid == "red" {
|
||||||
|
p = s.redPlayer
|
||||||
|
} else if pid == "blue" {
|
||||||
|
p = s.bluePlayer
|
||||||
|
} else {
|
||||||
|
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("sending game status to player %v", p.Team)
|
||||||
respondWithJSON(res, http.StatusOK, gameStatusResp{s.simulator.State, s.moveNum})
|
respondWithJSON(res, http.StatusOK, gameStatusResp{s.simulator.State, s.moveNum})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,14 +132,11 @@ func (a *API) PostMove(res http.ResponseWriter, req *http.Request) {
|
|||||||
var gr gameMovePostReq
|
var gr gameMovePostReq
|
||||||
decoder := json.NewDecoder(req.Body)
|
decoder := json.NewDecoder(req.Body)
|
||||||
if err := decoder.Decode(&gr); err != nil {
|
if err := decoder.Decode(&gr); err != nil {
|
||||||
respondWithError(res, http.StatusBadRequest, "Invalid resquest payload")
|
respondWithError(res, http.StatusBadRequest, "Invalid request payload")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer req.Body.Close()
|
defer req.Body.Close()
|
||||||
if gr.PlayerID != "red" && gr.PlayerID != "blue" {
|
pid := req.Header.Get("Player-id")
|
||||||
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var p *Player
|
var p *Player
|
||||||
|
|
||||||
s, isset := a.games[id]
|
s, isset := a.games[id]
|
||||||
@ -147,10 +144,13 @@ func (a *API) PostMove(res http.ResponseWriter, req *http.Request) {
|
|||||||
respondWithError(res, http.StatusBadRequest, "No such game")
|
respondWithError(res, http.StatusBadRequest, "No such game")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if gr.PlayerID == "red" {
|
if pid == "red" {
|
||||||
p = s.redPlayer
|
p = s.redPlayer
|
||||||
} else {
|
} else if pid == "blue" {
|
||||||
p = s.bluePlayer
|
p = s.bluePlayer
|
||||||
|
} else {
|
||||||
|
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
parsed, err := s.tryMove(p, gr.Move)
|
parsed, err := s.tryMove(p, gr.Move)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -180,17 +180,6 @@ func (a *API) GetMove(res http.ResponseWriter, req *http.Request) {
|
|||||||
respondWithError(res, http.StatusBadRequest, "Invalid move number")
|
respondWithError(res, http.StatusBadRequest, "Invalid move number")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var gr gameMoveReq
|
|
||||||
decoder := json.NewDecoder(req.Body)
|
|
||||||
if err := decoder.Decode(&gr); err != nil {
|
|
||||||
respondWithError(res, http.StatusBadRequest, "Invalid resquest payload")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer req.Body.Close()
|
|
||||||
if gr.PlayerID != "red" && gr.PlayerID != "blue" {
|
|
||||||
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var p *Player
|
var p *Player
|
||||||
|
|
||||||
s, isset := a.games[id]
|
s, isset := a.games[id]
|
||||||
@ -198,10 +187,14 @@ func (a *API) GetMove(res http.ResponseWriter, req *http.Request) {
|
|||||||
respondWithError(res, http.StatusBadRequest, "No such game")
|
respondWithError(res, http.StatusBadRequest, "No such game")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if gr.PlayerID == "red" {
|
pid := req.Header.Get("Player-id")
|
||||||
|
if pid == "red" {
|
||||||
p = s.redPlayer
|
p = s.redPlayer
|
||||||
} else {
|
} else if pid == "blue" {
|
||||||
p = s.bluePlayer
|
p = s.bluePlayer
|
||||||
|
} else {
|
||||||
|
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
moveRes, err := s.getMove(p, move)
|
moveRes, err := s.getMove(p, move)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
122
api_test.go
Normal file
122
api_test.go
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewAPI(t *testing.T) {
|
||||||
|
a := NewAPI()
|
||||||
|
if len(a.games) != 0 {
|
||||||
|
t.Fatalf("games list not empty")
|
||||||
|
}
|
||||||
|
if a.nextInt != 1 {
|
||||||
|
t.Fatalf("nextInt somehow already implemented")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dummyGame(a *API) int {
|
||||||
|
i := a.nextInt
|
||||||
|
a.games[i] = NewSession(8)
|
||||||
|
a.games[i].redPlayer.Ready = true
|
||||||
|
a.games[i].bluePlayer.Ready = true
|
||||||
|
a.games[i].simulator.Setup()
|
||||||
|
initDummy(a.games[i].simulator)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewGame(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
a := NewAPI()
|
||||||
|
var tests = []struct {
|
||||||
|
gid int
|
||||||
|
pid string
|
||||||
|
}{
|
||||||
|
{1, "red"},
|
||||||
|
{1, "blue"},
|
||||||
|
{2, "red"},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
tname := fmt.Sprintf("/game %v", i)
|
||||||
|
t.Run(tname, func(t *testing.T) {
|
||||||
|
r, _ := http.NewRequest("POST", "/game", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
a.NewGame(w, r)
|
||||||
|
resp := w.Result()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatal("failed to create new game")
|
||||||
|
}
|
||||||
|
_, ok := a.games[tt.gid]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("API thinks it created a game but it didn't")
|
||||||
|
}
|
||||||
|
var respStruct newGameResp
|
||||||
|
err := json.NewDecoder(resp.Body).Decode(&respStruct)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("/game returned bad response body: %v", err)
|
||||||
|
}
|
||||||
|
if respStruct.GameID != tt.gid {
|
||||||
|
t.Errorf("Expected game %v, got %v", tt.gid, respStruct.GameID)
|
||||||
|
}
|
||||||
|
if respStruct.PlayerID != tt.pid {
|
||||||
|
t.Errorf("wrong playerID returned")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetGame(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
a := NewAPI()
|
||||||
|
gid := dummyGame(a)
|
||||||
|
var tests = []struct {
|
||||||
|
pid string
|
||||||
|
code int
|
||||||
|
}{
|
||||||
|
{"red", http.StatusOK},
|
||||||
|
{"blue", http.StatusOK},
|
||||||
|
{"green", http.StatusBadRequest},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
tname := fmt.Sprintf("/game from player %v", tt.pid)
|
||||||
|
t.Run(tname, func(t *testing.T) {
|
||||||
|
r, _ := http.NewRequest("GET", "/game", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r = mux.SetURLVars(r, map[string]string{"id": strconv.Itoa(gid)})
|
||||||
|
r.Header.Add("Player-id", tt.pid)
|
||||||
|
r.Header.Add("Rotate", "false")
|
||||||
|
a.GetGame(w, r)
|
||||||
|
resp := w.Result()
|
||||||
|
if resp.StatusCode != tt.code {
|
||||||
|
t.Fatalf("failed to get game: %v", resp.Status)
|
||||||
|
}
|
||||||
|
if resp.StatusCode == http.StatusOK {
|
||||||
|
var respStruct gameResp
|
||||||
|
err := json.NewDecoder(resp.Body).Decode(&respStruct)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("/game returned bad response body: %v", err)
|
||||||
|
}
|
||||||
|
if len(respStruct.GameBoard) == 0 {
|
||||||
|
t.Errorf("bad game board returned")
|
||||||
|
}
|
||||||
|
for j := range respStruct.GameBoard {
|
||||||
|
for i, vt := range respStruct.GameBoard[j] {
|
||||||
|
curr, err := a.games[gid].simulator.Board.GetPiece(i, j)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Strange board position: %v", err)
|
||||||
|
}
|
||||||
|
if curr != nil && !vt.Hidden && curr.Owner.String() != tt.pid && curr.Hidden {
|
||||||
|
t.Errorf("/game returned a piece that should be hidden but isn't")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
17
api_types.go
17
api_types.go
@ -9,16 +9,8 @@ type newGameResp struct {
|
|||||||
PlayerID string `json:"player_id"`
|
PlayerID string `json:"player_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameReq struct {
|
|
||||||
PlayerID string `json:"player_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gameResp struct {
|
type gameResp struct {
|
||||||
GameBoard [8][8]*ViewTile `json:"board"`
|
GameBoard [][]*ViewTile `json:"board"`
|
||||||
}
|
|
||||||
|
|
||||||
type gameStatusReq struct {
|
|
||||||
PlayerID string `json:"player_id"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameStatusResp struct {
|
type gameStatusResp struct {
|
||||||
@ -27,8 +19,7 @@ type gameStatusResp struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type gameMovePostReq struct {
|
type gameMovePostReq struct {
|
||||||
PlayerID string `json:"player_id"`
|
Move string `json:"move"`
|
||||||
Move string `json:"move"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameMovePostRes struct {
|
type gameMovePostRes struct {
|
||||||
@ -38,10 +29,6 @@ type gameMovePostRes struct {
|
|||||||
Error error `json:"error"`
|
Error error `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameMoveReq struct {
|
|
||||||
PlayerID string `json:"player_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gameMoveRes struct {
|
type gameMoveRes struct {
|
||||||
Move string `json:"move"`
|
Move string `json:"move"`
|
||||||
}
|
}
|
||||||
|
@ -7,3 +7,7 @@ services:
|
|||||||
build: .
|
build: .
|
||||||
ports:
|
ports:
|
||||||
- 1379:1379
|
- 1379:1379
|
||||||
|
web:
|
||||||
|
build: nginx
|
||||||
|
ports:
|
||||||
|
- 80:80
|
3
nginx/Dockerfile
Normal file
3
nginx/Dockerfile
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
FROM nginx:latest AS base
|
||||||
|
|
||||||
|
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
|
17
nginx/nginx.conf
Normal file
17
nginx/nginx.conf
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name steve;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_pass http://ui:3000;
|
||||||
|
}
|
||||||
|
|
||||||
|
location /api/ {
|
||||||
|
rewrite ^/api(/.*)$ $1 break;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_pass http://api:1379;
|
||||||
|
}
|
||||||
|
}
|
17
session.go
17
session.go
@ -15,6 +15,7 @@ type Session struct {
|
|||||||
bluePlayer *Player
|
bluePlayer *Player
|
||||||
moveNum int
|
moveNum int
|
||||||
moveList []freego.ParsedCommand
|
moveList []freego.ParsedCommand
|
||||||
|
boardSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
//Player is a player in a match
|
//Player is a player in a match
|
||||||
@ -34,7 +35,7 @@ func (p *Player) Colour() freego.Colour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//NewSession creates a new game session
|
//NewSession creates a new game session
|
||||||
func NewSession() *Session {
|
func NewSession(size int) *Session {
|
||||||
sim := freego.NewGame()
|
sim := freego.NewGame()
|
||||||
return &Session{
|
return &Session{
|
||||||
simulator: sim,
|
simulator: sim,
|
||||||
@ -42,6 +43,7 @@ func NewSession() *Session {
|
|||||||
bluePlayer: &Player{false, freego.Blue},
|
bluePlayer: &Player{false, freego.Blue},
|
||||||
moveNum: 1,
|
moveNum: 1,
|
||||||
moveList: make([]freego.ParsedCommand, 20),
|
moveList: make([]freego.ParsedCommand, 20),
|
||||||
|
boardSize: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,10 +80,13 @@ func (s *Session) getMove(p *Player, num int) (string, error) {
|
|||||||
return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil
|
return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) getBoard(p *Player) [8][8]*ViewTile {
|
func (s *Session) getBoard(p *Player) [][]*ViewTile {
|
||||||
var res [8][8]*ViewTile
|
res := make([][]*ViewTile, s.boardSize)
|
||||||
for i := 0; i < 8; i++ {
|
for i := range res {
|
||||||
for j := 0; j < 8; j++ {
|
res[i] = make([]*ViewTile, s.boardSize)
|
||||||
|
}
|
||||||
|
for i := 0; i < s.boardSize; i++ {
|
||||||
|
for j := 0; j < s.boardSize; j++ {
|
||||||
cur := NewViewTile()
|
cur := NewViewTile()
|
||||||
terrain, err := s.simulator.Board.IsTerrain(i, j)
|
terrain, err := s.simulator.Board.IsTerrain(i, j)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -109,7 +114,7 @@ func (s *Session) getBoard(p *Player) [8][8]*ViewTile {
|
|||||||
cur.Empty = true
|
cur.Empty = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res[i][j] = cur
|
res[j][i] = cur
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
|
5
ui/.prettierrc
Normal file
5
ui/.prettierrc
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"tabWidth": 2,
|
||||||
|
"useTabs": false,
|
||||||
|
"semi": true
|
||||||
|
}
|
@ -8,9 +8,11 @@ FROM base as dependencies
|
|||||||
COPY pages /pages
|
COPY pages /pages
|
||||||
COPY public /public
|
COPY public /public
|
||||||
COPY styles /styles
|
COPY styles /styles
|
||||||
|
COPY api /api
|
||||||
COPY components /components
|
COPY components /components
|
||||||
COPY next* /
|
COPY next* /
|
||||||
COPY tsconfig.json /
|
COPY tsconfig.json /
|
||||||
|
COPY types.ts /
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
20
ui/api/game.api.ts
Normal file
20
ui/api/game.api.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import wretch from "wretch";
|
||||||
|
import { Cell } from "../types";
|
||||||
|
|
||||||
|
const USER_ID_HEADER = "Player-id";
|
||||||
|
|
||||||
|
export const fetchGameState = async (
|
||||||
|
playerId: string,
|
||||||
|
gameId: number
|
||||||
|
): Promise<{ cells: Cell[]; cellWidth: number }> => {
|
||||||
|
const response: { board: Cell[][] } = await wretch(`/api/game/${gameId}`)
|
||||||
|
.headers({
|
||||||
|
[USER_ID_HEADER]: playerId,
|
||||||
|
})
|
||||||
|
.get()
|
||||||
|
.json();
|
||||||
|
return {
|
||||||
|
cells: response.board.flat(),
|
||||||
|
cellWidth: response.board[0].length,
|
||||||
|
};
|
||||||
|
};
|
@ -1,7 +1,25 @@
|
|||||||
import React from 'react'
|
import React from "react";
|
||||||
|
import { Cell } from "../types";
|
||||||
|
import styles from "../styles/board.module.css";
|
||||||
|
|
||||||
const Board = () => (
|
interface BoardProps {
|
||||||
<input/>
|
cells: Cell[];
|
||||||
)
|
cellWidth: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Board = (props: BoardProps) => {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={styles.gridContainer}
|
||||||
|
style={{
|
||||||
|
gridTemplateColumns: `repeat(${props.cellWidth}, 1fr)`,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{props.cells.map((cell) => (
|
||||||
|
<div className={styles.gridCell}>{cell.piece}</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default Board;
|
export default Board;
|
28
ui/components/game.tsx
Normal file
28
ui/components/game.tsx
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import React, { useState, useEffect } from "react";
|
||||||
|
import { fetchGameState } from "../api/game.api";
|
||||||
|
import { Cell } from "../types";
|
||||||
|
import Board from "./board";
|
||||||
|
|
||||||
|
interface GameProps {
|
||||||
|
gameId: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Game = (props: GameProps) => {
|
||||||
|
const [isLoading, setLoading] = useState(false);
|
||||||
|
const [cellWidth, setCellWidth] = useState(4);
|
||||||
|
const [cells, setCells] = useState([] as Cell[]);
|
||||||
|
useEffect(() => {
|
||||||
|
setLoading(true);
|
||||||
|
fetchGameState("red", props.gameId).then(
|
||||||
|
({ cells: cellList, cellWidth: width }) => {
|
||||||
|
setCellWidth(width);
|
||||||
|
setCells(cellList);
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}, [props.gameId]);
|
||||||
|
|
||||||
|
return <Board cellWidth={cellWidth} cells={cells} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Game;
|
13
ui/package-lock.json
generated
13
ui/package-lock.json
generated
@ -10,7 +10,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"next": "12.1.0",
|
"next": "12.1.0",
|
||||||
"react": "17.0.2",
|
"react": "17.0.2",
|
||||||
"react-dom": "17.0.2"
|
"react-dom": "17.0.2",
|
||||||
|
"wretch": "^1.7.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "17.0.21",
|
"@types/node": "17.0.21",
|
||||||
@ -2527,6 +2528,11 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
},
|
||||||
|
"node_modules/wretch": {
|
||||||
|
"version": "1.7.9",
|
||||||
|
"resolved": "https://registry.npmjs.org/wretch/-/wretch-1.7.9.tgz",
|
||||||
|
"integrity": "sha512-uUSze1Z72RiQjyoqr7r1KW+05WDNeqqKOeyJDPhw6EVEaOgp9RQNrr8AQt3OF7qylQbh2iVtT9r0nXIHlbJgqQ=="
|
||||||
|
},
|
||||||
"node_modules/yallist": {
|
"node_modules/yallist": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
@ -4060,6 +4066,11 @@
|
|||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"wretch": {
|
||||||
|
"version": "1.7.9",
|
||||||
|
"resolved": "https://registry.npmjs.org/wretch/-/wretch-1.7.9.tgz",
|
||||||
|
"integrity": "sha512-uUSze1Z72RiQjyoqr7r1KW+05WDNeqqKOeyJDPhw6EVEaOgp9RQNrr8AQt3OF7qylQbh2iVtT9r0nXIHlbJgqQ=="
|
||||||
|
},
|
||||||
"yallist": {
|
"yallist": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"dev": true
|
"dev": true
|
||||||
|
@ -11,7 +11,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"next": "12.1.0",
|
"next": "12.1.0",
|
||||||
"react": "17.0.2",
|
"react": "17.0.2",
|
||||||
"react-dom": "17.0.2"
|
"react-dom": "17.0.2",
|
||||||
|
"wretch": "^1.7.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "17.0.21",
|
"@types/node": "17.0.21",
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
import type { NextPage } from 'next'
|
import type { NextPage } from "next";
|
||||||
import Head from 'next/head'
|
import Head from "next/head";
|
||||||
import Image from 'next/image'
|
import Image from "next/image";
|
||||||
import Board from '../components/board'
|
import Game from "../components/game";
|
||||||
import styles from '../styles/Home.module.css'
|
import styles from "../styles/BoardPage.module.css";
|
||||||
|
|
||||||
const Home: NextPage = () => (
|
const Home: NextPage = () => (
|
||||||
<>
|
<div className={styles.main}>
|
||||||
<Head>
|
<Head>
|
||||||
<title>Free Go Game</title>
|
<title>Free Go Game</title>
|
||||||
<link rel="icon" href="/favicon.ico" />
|
<link rel="icon" href="/favicon.ico" />
|
||||||
</Head>
|
</Head>
|
||||||
<main>
|
<main>
|
||||||
<Board />
|
<Game gameId={1} />
|
||||||
</main>
|
</main>
|
||||||
</>
|
</div>
|
||||||
)
|
);
|
||||||
|
|
||||||
export default Home
|
export default Home;
|
||||||
|
4
ui/styles/BoardPage.module.css
Normal file
4
ui/styles/BoardPage.module.css
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.main {
|
||||||
|
max-width: 900px;
|
||||||
|
margin: auto;
|
||||||
|
}
|
8
ui/styles/board.module.css
Normal file
8
ui/styles/board.module.css
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
.gridContainer {
|
||||||
|
display: grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
.gridCell {
|
||||||
|
border: 1px solid black;
|
||||||
|
padding-top: 100%; /* 100% is supposed to be 1:1 aspect ratio but it doesn't look like it */
|
||||||
|
}
|
6
ui/types.ts
Normal file
6
ui/types.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export interface Cell {
|
||||||
|
piece?: string;
|
||||||
|
terrain: boolean;
|
||||||
|
hidden: boolean;
|
||||||
|
empty: boolean;
|
||||||
|
}
|
10
util.go
10
util.go
@ -21,6 +21,16 @@ func respondWithJSON(res http.ResponseWriter, code int, payload interface{}) {
|
|||||||
res.Write(response)
|
res.Write(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rotateBoard(board [][]*ViewTile) {
|
||||||
|
i := 0
|
||||||
|
temp := board[0]
|
||||||
|
for ; i < len(board)-1; i++ {
|
||||||
|
board[i] = board[i+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
board[i] = temp
|
||||||
|
}
|
||||||
|
|
||||||
//TODO remove this when you can actually setup a game
|
//TODO remove this when you can actually setup a game
|
||||||
func initDummy(g *freego.Game) {
|
func initDummy(g *freego.Game) {
|
||||||
//Setup terrain
|
//Setup terrain
|
||||||
|
13
view_tile.go
13
view_tile.go
@ -12,3 +12,16 @@ type ViewTile struct {
|
|||||||
func NewViewTile() *ViewTile {
|
func NewViewTile() *ViewTile {
|
||||||
return &ViewTile{}
|
return &ViewTile{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (vt *ViewTile) String() string {
|
||||||
|
if vt.Piece != "" {
|
||||||
|
return vt.Piece
|
||||||
|
}
|
||||||
|
if vt.Hidden {
|
||||||
|
return "?"
|
||||||
|
}
|
||||||
|
if vt.Terrain {
|
||||||
|
return "X"
|
||||||
|
}
|
||||||
|
return " "
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user