Skip to content

Commit

Permalink
refactor(webapp): migrate server project to server.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ben196888 committed Jun 4, 2024
1 parent f7c094b commit e02769e
Show file tree
Hide file tree
Showing 62 changed files with 494 additions and 304 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

4 changes: 1 addition & 3 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ You can join our discussions on [Discord](https://discord.gg/JnTHGnxwYS).
| Sub-Project | Description |
| ----------------------------------------------- | ----------------------------------------------- |
| [google-spreadsheet](./google-spreadsheet/) | Prototype - Presented using Google Spreadsheets |
| [client](./client/) | Web frontend - Game interface |
| [server](./server/) | Web backend - Handles room allocation, player state synchronization, and game logic |
| [packages/game](./packages/game/) | Web game logic - Manages game rules |
| [packages/webapp](./packages/webapp/) | Webapp - web game website |
| [homepage](./homepage/) | [Official Website](https://openstartervillage.ocf.tw/)

## Licensing Terms
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ English version: [README.en.md](./README.en.md)
| 子專案 | 說明 |
| --------------------------------------------- | --------------------------------------------------- |
| [`google-spreadsheet`](./google-spreadsheet/) | 原型版本 - 以 Google Spreadsheet 呈現 |
| [`client`](./client/) | 網頁版前端 - 遊戲界面 |
| [`server`](./server/) | 網頁版後端 - 處理遊戲房間分配、同步玩家狀態和遊戲邏輯 |
| [`packages/game`](./packages/game/) | 網頁版遊戲邏輯 - 處理遊戲規則 |
| [`packages/webapp`](./packages/webapp/) | 網頁版遊戲 - 處理遊戲規則 |
| [`homepage`](./homepage/) | [官方網站 - Official Website](https://openstartervillage.ocf.tw/)

## 授權條款
Expand Down
52 changes: 20 additions & 32 deletions WEBAPP.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,29 @@ Open StarTer Village - Web App is a single-page application project designed for
git clone [email protected]:ocftw/open-star-ter-village.git open-star-ter-village
```

> Note: Open StarTer Village Game requires node >= 12.0.0
> Note: Open StarTer Village Game requires node >= 18.0.0
### Get Started

```shell
yarn
yarn build:webapp
yarn deploy:webapp
yarn start:webapp
yarn run webapp install
yarn run webapp build
yarn run webapp start
```

## Deployment Options

### Deploying to Heroku (Please note, we are exploring alternative solutions to Heroku due to certain limitations)
### Deploy to Vercel

```shell
heroku create
git push heroku main
heroku open
```

or

[![Deploy to Heroku (Exploring Alternative Solutions)](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/ocftw/open-star-ter-village/tree/main)
TBD - We are working on this feature.

## How to Contribute to the Source Code

Open StarTer Village - WebApp is a TypeScript monorepo configured with Yarn 2 using the workspace-tools plugin. The core game engine is based on [boardgame.io](https://boardgame.io) on both the server and client side. The Open StarTer Village - WebApp project is divided into three parts: core game logic, client UI, and the game server. Below is a table showing their npm project names and locations:

| Project Name | Description |
| ---------------------------------------------- | -------------------- |
| [`@open-star-ter-village/webapp-game`](./packages/game/) | The core game logic |
| [`@open-star-ter-village/webapp-client`](./client/) | The client UI |
| [`@open-star-ter-village/webapp-server`](./server/) | The game server |
| Project Name | Description |
| ---------------------------------------------- | ----------------------- |
| [`@open-star-ter-village/webapp`](./packages/webapp/) | The web game app |

### How to Start the Local Development Environment

Expand All @@ -57,32 +46,31 @@ Open StarTer Village - Web App uses Yarn 2 workspaces to manage the project. Ple
The following command will start the client UI (Create React App) on a development server, the server (Koa) using ts-node with watchmon, and the game core (pure TypeScript) in watch compile mode.

```shell
yarn dev:webapp
yarn run webapp dev
```

Everything is set up and ready for you to start coding!

#### Local Build

Open StarTer Village - Web App has three primary outcomes: `client` and `server`, which serve the game on the platform, and other outputs used as libraries in parts of `client` and `server`.
The following command will build nextjs webapp into `packages/webapp/.next` and server into `packages/webapp/dist`.

```shell
yarn build:webapp
yarn run webapp build
```

Project output table
| Project Name | Output Location | Description |
| -------------------------------------------- | ----------------------- | ----------------------- |
| `@open-star-ter-village/webapp-game` | ./packages/game/dist | The core game logic |
| `@open-star-ter-village/webapp-client` | ./client/build | The client UI |
| `@open-star-ter-village/webapp-server` | ./server/build | The game server |
#### Local Start

> Note: The output folders are ignored by Git, but you can find them after the build process.
After building the project, you can start the server and webapp with the following command:

```shell
yarn run webapp start
```

#### Unit Test (Currently not available)
#### Unit Test (Currently only available for game core)

```shell
yarn test
yarn run webapp test
```

## How to Contribute to Assets
Expand Down
19 changes: 0 additions & 19 deletions app.json

This file was deleted.

6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,5 @@
"all:build": "yarn workspaces foreach -pt run build",
"all:start": "yarn workspaces foreach -pt run start",
"all:lint": "yarn workspaces foreach -pt run lint"
},
"devDependencies": {
"@types/koa-static": "^4.0.2"
},
"dependencies": {
"koa-static": "^5.0.0"
}
}
19 changes: 14 additions & 5 deletions packages/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"dev": "concurrently \"yarn dev:next\" \"yarn dev:server\"",
"dev:next": "next dev",
"dev:server": "ts-node-dev -P tsconfig.server.json --respawn --transpile-only --ignore-watch .next src/server.ts",
"build": "yarn build:next && yarn build:server",
"build:next": "next build",
"build:server": "tsc -P tsconfig.server.json",
"start": "yarn start:next && yarn start:server",
"start:next": "next start",
"start:server": "node dist/server.js",
"lint": "next lint",
"test": "jest"
},
Expand All @@ -23,12 +29,15 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20",
"@types/node": "^20.14.0",
"@types/react": "^18",
"@types/react-dom": "^18",
"concurrently": "^8.2.2",
"eslint": "^8",
"eslint-config-next": "14.2.3",
"jest": "^29.7.0",
"typescript": "^5"
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"typescript": "^5.4.5"
}
}
4 changes: 2 additions & 2 deletions packages/webapp/src/components/BoardGame.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client, BoardProps } from 'boardgame.io/react';
import { Local } from 'boardgame.io/multiplayer';
import { SocketIO } from 'boardgame.io/multiplayer'
import { OpenStarTerVillage } from '@/game';
import Table from '@/components/Table/Table';
import Players from '@/components/Players/Players';
Expand All @@ -18,7 +18,7 @@ const Board: React.FC<BoardProps<OpenStarTerVillageType.State.Root>> = (props) =
const Boardgame = Client({
game: OpenStarTerVillage,
board: Board,
multiplayer: Local(),
multiplayer: SocketIO({ server: 'localhost:8000' }),
});

export default Boardgame;
43 changes: 43 additions & 0 deletions packages/webapp/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Server, Origins } from "boardgame.io/server";
import next from 'next';
import url from "url";
import { OpenStarTerVillage } from "./game";

async function serve() {
const port = Number(process.env.PORT) || 8000;
const apiPort = Number(process.env.API_PORT) || 8080;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

const server = Server({
games: [OpenStarTerVillage],
origins: [
// Allow localhost to connect, except when NODE_ENV is 'production'.
Origins.LOCALHOST_IN_DEVELOPMENT,
],
});
const mainServerConfig = {
port,
callback: () => console.log(`Main server running on port ${port}...`),
};
const lobbyConfig = {
apiPort,
apiCallback: () => console.log(`Lobby api running on port ${apiPort}`),
};

await app.prepare();
server.run({
...mainServerConfig,
lobbyConfig,
}, () => {
server.app.use(async (ctx, next) => {
const parsedUrl = url.parse(ctx.req.url!, true);
await handle(ctx.req, ctx.res, parsedUrl);
ctx.respond = false;
await next();
});
});
}

serve();
15 changes: 15 additions & 0 deletions packages/webapp/tsconfig.server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"lib": ["ES2017"],
"outDir": "./dist",
"strict": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": true,
},
"include": ["./src/server.ts", "./src/game/**/*.ts"],
"exclude": ["node_modules"]
}
6 changes: 0 additions & 6 deletions server/nodemon.json

This file was deleted.

23 changes: 0 additions & 23 deletions server/package.json

This file was deleted.

44 changes: 0 additions & 44 deletions server/src/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions server/tsconfig.build.json

This file was deleted.

3 changes: 0 additions & 3 deletions server/tsconfig.json

This file was deleted.

19 changes: 0 additions & 19 deletions tsconfig.build.json

This file was deleted.

16 changes: 0 additions & 16 deletions tsconfig.json

This file was deleted.

Loading

0 comments on commit e02769e

Please sign in to comment.