Skip to content

Commit

Permalink
Single server.js for deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
skvrahul committed May 14, 2020
1 parent 7efc421 commit 3398ed3
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 196 deletions.
138 changes: 0 additions & 138 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@testing-library/user-event": "^7.2.1",
"boardgame.io": "^0.39.6",
"esm": "^3.2.25",
"koa-static": "^5.0.0",
"ky": "^0.19.1",
"npm-run-all": "^4.1.5",
"react": "^16.13.1",
Expand Down
38 changes: 18 additions & 20 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const express = require('express');
const favicon = require('express-favicon');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
const proxy = require('http-proxy-middleware');
// server.js
import { Server } from "boardgame.io/server";
import path from "path";
import serve from "koa-static";
import { UdaipurGame } from "./src/game/game";
const server = Server({ games: [UdaipurGame] });
const PORT = process.env.PORT || 8000;

var apiProxy = proxy.createProxyMiddleware('/games/', {target: 'http://localhost:8000'});

app.use(favicon(__dirname + '/build/favicon.ico'));
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'build')));
app.use(apiProxy)
app.get('/ping', function (req, res) {
return res.send('pong');
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
// Build path relative to the server.js file
const frontEndAppBuildPath = path.resolve(__dirname, "./build");
server.app.use(serve(frontEndAppBuildPath));
server.run(PORT, () => {
server.app.use(
async (ctx, next) =>
await serve(frontEndAppBuildPath)(
Object.assign(ctx, { path: "index.html" }),
next
)
);
});
console.log('listening on port ', port);
app.listen(port);
23 changes: 0 additions & 23 deletions server/combined.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import HomePage from "./components/homepage.jsx";
import { GAME_SERVER_URL } from "./config.js";
import Lobby from "./components/lobby.jsx";

const UdaipurClient = Client({
game: UdaipurGame,
board: UdaipurBoard,
multiplayer: SocketIO({ server: GAME_SERVER_URL }),
});
const renderUdaipurClient = () => {
return <UdaipurClient playerID="0"></UdaipurClient>;
};
Expand All @@ -27,7 +22,6 @@ function App() {
render={(props) => <HomePage {...props} history={history} />}
/>
<Route path="/lobby/:id" component={Lobby} />
<Route path="/play" component={renderUdaipurClient} />
<Route
path="*"
render={(props) => <HomePage {...props} history={history} />}
Expand Down
5 changes: 4 additions & 1 deletion src/api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { GAME_NAME, GAME_SERVER_URL } from "./config";
import ky from "ky";
const server = APP_PRODUCTION
? `https://${window.location.hostname}`
: GAME_SERVER_URL;

export class LobbyAPI {
constructor() {
this.api = ky.create({
prefixUrl: `${GAME_SERVER_URL}/games/${GAME_NAME}`,
prefixUrl: `${server}/games/${GAME_NAME}`,
});
}
async createRoom(numPlayers) {
Expand Down
35 changes: 30 additions & 5 deletions src/components/board.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "./styles/boardLayout.css";
import "./styles/resultsPage.css";
import BoardCards from "./boardCards";
import PlayerCards from "./playerCards";
import { MoveValidate } from "../game/moveValidation";

class UdaipurBoard extends Component {
constructor(props) {
Expand Down Expand Up @@ -63,19 +64,32 @@ class UdaipurBoard extends Component {
handleTrade = () => {
console.log("Handling trade!");
console.log(this.state.handSelected);
this.props.moves.trade(this.state.handSelected);
const { G, ctx } = this.props;
const validate = MoveValidate.trade(G, ctx, this.state.handSelected);
if (validate.valid) {
this.props.moves.trade(this.state.handSelected);
} else {
return this.alertError(validate.message);
}
this.clearSelection();
};
handleTakeOne = () => {
console.log(this.state.boardSelected);
const { G, ctx } = this.props;
const validate = MoveValidate.takeOne(G, ctx, this.state.boardSelected[0]);

// Some validation before performing the move
if (this.state.boardSelected.length !== 1) {
this.clearSelection();
return this.alertError(
"Please select only 1 card to take from the Board!"
);
} else if (!validate.valid) {
this.clearSelection();
return this.alertError(validate.message);
} else {
this.props.moves.takeOne(this.state.boardSelected[0]);
this.clearSelection();
}
const resp = this.props.moves.takeOne(this.state.boardSelected[0]);
this.clearSelection();
};
handleTakeCamels = () => {
const board = this.props.G.board;
Expand All @@ -93,10 +107,21 @@ class UdaipurBoard extends Component {
};
handleTakeMany = () => {
console.log("Handling take many!");
this.props.moves.takeMany(
const { G, ctx } = this.props;
const validate = MoveValidate.takeMany(
G,
ctx,
this.state.boardSelected,
this.state.handSelected
);
if (validate.valid) {
this.props.moves.takeMany(
this.state.boardSelected,
this.state.handSelected
);
} else {
return this.alertError(validate.message);
}
this.clearSelection();
};

Expand Down
Loading

0 comments on commit 3398ed3

Please sign in to comment.