Skip to content

Commit

Permalink
Merge pull request #1 from gdsc0301/babylon-version
Browse files Browse the repository at this point in the history
Babylon version
  • Loading branch information
gdsc0301 committed Sep 12, 2023
2 parents 22676a8 + a6bd759 commit f8971a8
Show file tree
Hide file tree
Showing 31 changed files with 3,805 additions and 645 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/page.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ permissions:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

#only deploys the main branch
deploy:
name: Build and Deploy
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'

steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/server_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Server Tests

on:
push:
branches:
- master
- babylon-version

jobs:
testing:
name: Server Tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup Bun
uses: oven-sh/setup-bun@v1

- run: bun install
- run: bun test
38 changes: 33 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
FROM node:18-alpine
FROM oven/bun

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Set debconf to run non-interactively
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# Install base dependencies
RUN apt-get update && apt-get install -y -q --no-install-recommends \
apt-transport-https \
build-essential \
ca-certificates \
curl \
git \
libssl-dev \
wget \
&& rm -rf /var/lib/apt/lists/*

ENV NODE_VERSION=20.6.0
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version

COPY . .

ENV PORT=8080

RUN npm install
RUN npm run build
RUN bun install
RUN bun run build

EXPOSE 8080:8080
CMD [ "npm", "run", "start" ]
EXPOSE 8080
CMD [ "bun", "start" ]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build and Deploy](https://github.com/gdsc0301/best-multiplayer-game-possible/actions/workflows/page.yml/badge.svg)](https://github.com/gdsc0301/best-multiplayer-game-possible/actions/workflows/page.yml)
# best-multiplayer-game-possible
[![Build and Deploy](https://github.com/gdsc0301/best-multiplayer-game-possible/actions/workflows/page.yml/badge.svg)](https://github.com/gdsc0301/best-multiplayer-game-possible/actions/workflows/page.yml) [![Server Tests](https://github.com/gdsc0301/best-multiplayer-game-possible/actions/workflows/server_tests.yml/badge.svg)](https://github.com/gdsc0301/best-multiplayer-game-possible/actions/workflows/server_tests.yml)
# BEST MULTIPLAYER GAME POSSIBLE
It's a multiplayer game that will grow gradually, so it will start being a multiplayer game that any amount of people can access, just like a MMO.
Here's the link: https://gdsc0301.github.io/best-multiplayer-game-possible/
11 changes: 0 additions & 11 deletions babel.config.json

This file was deleted.

Binary file added bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>BMPG</title>
</head>
<body>
<span class="welcomeMessage"></span>
<canvas id="app" width="640" height="360"></canvas>
<form id="loginForm">
<span class="welcomeMessage"></span>
<input type="text" placeholder="ip-address:port" value="https://best-multiplayer-game-possible-pvenbf56dq-uc.a.run.app/">
<!-- <input type="text" placeholder="ip-address:port" value="http://localhost:8080"> -->
<input type="text" autocomplete="false" placeholder="Your username"><input type="submit" value="GO!">
<span class="error"></span>
</form>
<script type="module" src="./src/index.js"></script>
<script type="module" src="./src/index.ts"></script>
</body>
</html>
57 changes: 35 additions & 22 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { createHash } from "node:crypto";
import { Player } from './src/Player.js';
import { Response, BAD_REQUEST, OK, UNAUTHORIZED } from './src/Response.js';
import Room from './src/Room.js';
import { Player } from './src/Player';
import { Response, BAD_REQUEST, OK, UNAUTHORIZED } from './src/Response';
import Room from './src/Room';

import express from 'express';
import { Vector3 } from "@babylonjs/core";

const ALLOW_ACCESS_ORIGINS = ['http://localhost:5173', 'https://gdsc0301.github.io'];

const app = express();

const PORT = parseInt(process.env.PORT) || 8080;
const BasicHeaders = {
export const PORT = parseInt(process.env.PORT || '8080');
export const BasicHeaders = {
"Content-Type": "application/json",
"Vary": "Origin",
"Access-Control-Allow-Methods": ["POST", "GET"],
"Access-Control-Allow-Credentials": "true"
};

/** @type {Object.<string, Room>} */
const rooms = {};
const rooms: {[ID: string]: Room} = {};

/**
* @param {Player} player
Expand Down Expand Up @@ -63,7 +63,6 @@ const room_exist = (room_id) => {
}

const get_headers = (origin) => {
console.log(origin, ALLOW_ACCESS_ORIGINS.indexOf(origin));
if(ALLOW_ACCESS_ORIGINS.indexOf(origin) === -1) {
return BasicHeaders;
}else {
Expand Down Expand Up @@ -91,19 +90,21 @@ app.get('/', (req, res) => {
});

app.get('/login', (req, res) => {
const player_email = req.query['player_email'];
const player_email = req.query['player_email']+'';

const new_player = new Player(player_email);
const new_player_room = assign_room_for(new_player);

const body = new Response(new_player_room);

res.set(get_headers(req.headers.origin));
res.status(OK).json((new Response(new_player_room)));
res.status(OK).json(body);
return;
});

app.get('/room', (req, res) => {
const player_email = req.query['player_email'];
const room_id = req.query['room_id'];
const room_id = req.query['room_id']+'';

const response = new Response();
const player_is_here = rooms[room_id].player_is_here(player_email);
Expand All @@ -117,14 +118,15 @@ app.get('/room', (req, res) => {

res.set(get_headers(req.headers.origin));
res.status(OK).json(response);

return;
});

app.post('/player_update', (req, res) => {
const player_email = req.query['player_email'];
const room_id = req.query['room_id'];
const player_email = req.query['player_email']+'';
const room_id = req.query['room_id']+'';

let response = undefined;
let response = new Response();
const body = JSON.parse(req.body);
if(!body) {
response = new Response(req.body, BAD_REQUEST, 'Invalid player data')
Expand All @@ -135,21 +137,32 @@ app.post('/player_update', (req, res) => {
res.set(get_headers(req.headers.origin));
}

const targetPlayer = rooms[room_id].get_player(player_email);
if(targetPlayer)
targetPlayer.setPosition(body.params.x, body.params.y);
else
response = (new Response({}, BAD_REQUEST, 'Invalid player email'));
const updateData = body.params;

const areYouThere = rooms[room_id].player_is_here(player_email);
if(areYouThere){
const newPos = new Vector3(updateData.position._x, updateData.position._y, 0);
const newRot = new Vector3(updateData.rotation._x, updateData.rotation._y, updateData.rotation._z);
rooms[room_id].players[player_email].update(newPos, newRot);
}else{
response = new Response({}, BAD_REQUEST, 'Invalid player email');
}

res.status(OK).json(response);
return;
});

app.get('/logout', (req, res) => {
const player_email = req.query['player_email'];
const room_id = req.query['room_id'];
const player_email = req.query['player_email']+'';
const room_id = req.query['room_id']+'';

if(!rooms[room_id]) {
res.set(get_headers(req.headers.origin));
res.status(BAD_REQUEST).json((new Response({}, BAD_REQUEST, 'Invalid room ID')));
return;
}

rooms[room_id]?.remove_player(player_email);
rooms[room_id].remove_player(player_email);

res.set(get_headers(req.headers.origin));
res.end();
Expand Down
Loading

0 comments on commit f8971a8

Please sign in to comment.