mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Load board from the server 🎉
This commit is contained in:
parent
4ee91a6228
commit
1b37e5f38a
@ -17,6 +17,7 @@ defmodule Chess.Router do
|
|||||||
pipe_through :browser # Use the default browser stack
|
pipe_through :browser # Use the default browser stack
|
||||||
|
|
||||||
get "/", PageController, :index
|
get "/", PageController, :index
|
||||||
|
resources "/games", GameController
|
||||||
end
|
end
|
||||||
|
|
||||||
# Other scopes may use custom stacks.
|
# Other scopes may use custom stacks.
|
||||||
|
|||||||
@ -1,15 +1,16 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import $ from "jquery";
|
import $ from "jquery";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
|
import { setBoard } from "../store/actions";
|
||||||
|
|
||||||
import ChessBoardSquare from "./chess-board-square";
|
import ChessBoardSquare from "./chess-board-square";
|
||||||
|
|
||||||
class ChessBoard extends React.Component {
|
class ChessBoard extends React.Component {
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
const { gameId } = this.props;
|
const { gameId, store } = this.props;
|
||||||
|
|
||||||
$.ajax({ method: "GET", url: "/api/games/" + gameId })
|
$.ajax({ method: "GET", url: "/api/games/" + gameId })
|
||||||
.then(() => console.log("Oh, hai!"));
|
.then((data) => store.dispatch(setBoard(data)));
|
||||||
}
|
}
|
||||||
|
|
||||||
getBoard() {
|
getBoard() {
|
||||||
@ -52,7 +53,10 @@ class ChessBoard extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function mapStateToProps(state) {
|
function mapStateToProps(state) {
|
||||||
return { selectedSquare: state.selectedSquare }
|
return {
|
||||||
|
board: state.board,
|
||||||
|
selectedSquare: state.selectedSquare
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default connect(mapStateToProps)(ChessBoard);
|
export default connect(mapStateToProps)(ChessBoard);
|
||||||
|
|||||||
@ -3,6 +3,9 @@ import movePiece from "./move-piece";
|
|||||||
|
|
||||||
const chessBoardReducer = (state = defaultState, action) => {
|
const chessBoardReducer = (state = defaultState, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
case "SET_BOARD":
|
||||||
|
return Object.assign({}, state, { board: action.board });
|
||||||
|
|
||||||
case "MOVE_PIECE":
|
case "MOVE_PIECE":
|
||||||
const newState = {
|
const newState = {
|
||||||
board: movePiece(state.board, action.from, action.to),
|
board: movePiece(state.board, action.from, action.to),
|
||||||
|
|||||||
@ -1,6 +1,14 @@
|
|||||||
|
const SET_BOARD = "SET_BOARD";
|
||||||
const SELECT_PIECE = "SELECT_PIECE";
|
const SELECT_PIECE = "SELECT_PIECE";
|
||||||
const MOVE_PIECE = "MOVE_PIECE";
|
const MOVE_PIECE = "MOVE_PIECE";
|
||||||
|
|
||||||
|
export const setBoard = (board) => {
|
||||||
|
return {
|
||||||
|
type: SET_BOARD,
|
||||||
|
board: board
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const selectPiece = (coords) => {
|
export const selectPiece = (coords) => {
|
||||||
return {
|
return {
|
||||||
type: SELECT_PIECE,
|
type: SELECT_PIECE,
|
||||||
|
|||||||
@ -2,50 +2,14 @@ const defaultState = {
|
|||||||
selectedSquare: null,
|
selectedSquare: null,
|
||||||
|
|
||||||
board: {
|
board: {
|
||||||
8: {
|
8: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
a: { type: "rook", colour: "black" },
|
7: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
b: { type: "knight", colour: "black" },
|
|
||||||
c: { type: "bishop", colour: "black" },
|
|
||||||
d: { type: "queen", colour: "black" },
|
|
||||||
e: { type: "king", colour: "black" },
|
|
||||||
f: { type: "bishop", colour: "black" },
|
|
||||||
g: { type: "knight", colour: "black" },
|
|
||||||
h: { type: "rook", colour: "black" }
|
|
||||||
},
|
|
||||||
7: {
|
|
||||||
a: { type: "pawn", colour: "black" },
|
|
||||||
b: { type: "pawn", colour: "black" },
|
|
||||||
c: { type: "pawn", colour: "black" },
|
|
||||||
d: { type: "pawn", colour: "black" },
|
|
||||||
e: { type: "pawn", colour: "black" },
|
|
||||||
f: { type: "pawn", colour: "black" },
|
|
||||||
g: { type: "pawn", colour: "black" },
|
|
||||||
h: { type: "pawn", colour: "black" }
|
|
||||||
},
|
|
||||||
6: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
6: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
5: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
5: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
4: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
4: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
3: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
3: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
2: {
|
2: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
a: { type: "pawn", colour: "white" },
|
1: { a: null, b: null, c: null, d: null, e: null, f: null, g: null, h: null },
|
||||||
b: { type: "pawn", colour: "white" },
|
|
||||||
c: { type: "pawn", colour: "white" },
|
|
||||||
d: { type: "pawn", colour: "white" },
|
|
||||||
e: { type: "pawn", colour: "white" },
|
|
||||||
f: { type: "pawn", colour: "white" },
|
|
||||||
g: { type: "pawn", colour: "white" },
|
|
||||||
h: { type: "pawn", colour: "white" }
|
|
||||||
},
|
|
||||||
1: {
|
|
||||||
a: { type: "rook", colour: "white" },
|
|
||||||
b: { type: "knight", colour: "white" },
|
|
||||||
c: { type: "bishop", colour: "white" },
|
|
||||||
d: { type: "queen", colour: "white" },
|
|
||||||
e: { type: "king", colour: "white" },
|
|
||||||
f: { type: "bishop", colour: "white" },
|
|
||||||
g: { type: "knight", colour: "white" },
|
|
||||||
h: { type: "rook", colour: "white" }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,51 @@ defmodule Chess.Api.GameView do
|
|||||||
use Chess.Web, :view
|
use Chess.Web, :view
|
||||||
|
|
||||||
def render("show.json", %{ id: 1 }) do
|
def render("show.json", %{ id: 1 }) do
|
||||||
%{ foo: :bar }
|
%{
|
||||||
|
"8" => %{
|
||||||
|
a: %{ type: "rook", colour: "black" },
|
||||||
|
b: %{ type: "knight", colour: "black" },
|
||||||
|
c: %{ type: "bishop", colour: "black" },
|
||||||
|
d: %{ type: "queen", colour: "black" },
|
||||||
|
e: %{ type: "king", colour: "black" },
|
||||||
|
f: %{ type: "bishop", colour: "black" },
|
||||||
|
g: %{ type: "knight", colour: "black" },
|
||||||
|
h: %{ type: "rook", colour: "black" }
|
||||||
|
},
|
||||||
|
"7" => %{
|
||||||
|
a: %{ type: "pawn", colour: "black" },
|
||||||
|
b: %{ type: "pawn", colour: "black" },
|
||||||
|
c: %{ type: "pawn", colour: "black" },
|
||||||
|
d: %{ type: "pawn", colour: "black" },
|
||||||
|
e: %{ type: "pawn", colour: "black" },
|
||||||
|
f: %{ type: "pawn", colour: "black" },
|
||||||
|
g: %{ type: "pawn", colour: "black" },
|
||||||
|
h: %{ type: "pawn", colour: "black" }
|
||||||
|
},
|
||||||
|
"6" => %{ a: nil, b: nil, c: nil, d: nil, e: nil, f: nil, g: nil, h: nil },
|
||||||
|
"5" => %{ a: nil, b: nil, c: nil, d: nil, e: nil, f: nil, g: nil, h: nil },
|
||||||
|
"4" => %{ a: nil, b: nil, c: nil, d: nil, e: nil, f: nil, g: nil, h: nil },
|
||||||
|
"3" => %{ a: nil, b: nil, c: nil, d: nil, e: nil, f: nil, g: nil, h: nil },
|
||||||
|
"2" => %{
|
||||||
|
a: %{ type: "pawn", colour: "white" },
|
||||||
|
b: %{ type: "pawn", colour: "white" },
|
||||||
|
c: %{ type: "pawn", colour: "white" },
|
||||||
|
d: %{ type: "pawn", colour: "white" },
|
||||||
|
e: %{ type: "pawn", colour: "white" },
|
||||||
|
f: %{ type: "pawn", colour: "white" },
|
||||||
|
g: %{ type: "pawn", colour: "white" },
|
||||||
|
h: %{ type: "pawn", colour: "white" }
|
||||||
|
},
|
||||||
|
"1" => %{
|
||||||
|
a: %{ type: "rook", colour: "white" },
|
||||||
|
b: %{ type: "knight", colour: "white" },
|
||||||
|
c: %{ type: "bishop", colour: "white" },
|
||||||
|
d: %{ type: "queen", colour: "white" },
|
||||||
|
e: %{ type: "king", colour: "white" },
|
||||||
|
f: %{ type: "bishop", colour: "white" },
|
||||||
|
g: %{ type: "knight", colour: "white" },
|
||||||
|
h: %{ type: "rook", colour: "white" }
|
||||||
|
}
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user