Skip to content

Commit

Permalink
feat(version): v1.0.1 - start
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianjnuwu committed Jan 12, 2025
1 parent 70eae34 commit 58e9bc6
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 126 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
VITE_SOCKET_URL=<YOUR-URL-HERE>
VITE_SOCKET_URL=<YOUR-URL-HERE>
VITE_WEB_HOST=<YOU-IP-HERE>
VITE_WEB_PORT=<YOU-PORT-OPEN>
188 changes: 94 additions & 94 deletions android/gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,94 +1,94 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"typescript": "^5.7.3"
},
"dependencies": {
"express": "^4.21.2",
"socket.io": "^4.8.1"
},
"engines": {
Expand Down
108 changes: 78 additions & 30 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,70 @@
import express from "express";
import { createServer } from "http";
import { Server, Socket } from "socket.io";
import { Room, RoomData, LeaveRoomData, RejoinRoomData, StartGameData, UpdateCookiesData } from "./types/rooms";
import {
Room,
RoomData,
LeaveRoomData,
RejoinRoomData,
StartGameData,
UpdateCookiesData,
} from "./types/rooms";

// Initialize server
const io = new Server();
/**
* Initializes the Express application.
*/
const app = express();

/**
* Creates an HTTP server using the Express application.
*/
const httpServer = createServer(app);

/**
* Initializes Socket.IO with the HTTP server.
*/
const io = new Server(httpServer);

/**
* Stores active rooms on the server.
*/
const ROOMS: Record<string, Room> = {};

/**
* Counter to generate unique identifiers for players.
*/
let roomIdCounter = 0;

/**
* Generates a unique identifier for each player.
* @returns The generated identifier.
* @returns The generated unique identifier.
*/
function generateUuid(): number {
roomIdCounter += 1;
return roomIdCounter;
}

/**
* Generates a random room code.
* Generates a random code to identify a room.
* @returns The generated room code.
*/
function generateCode(): string {
return Math.random().toString(36).substring(2, 8).toUpperCase();
}

/**
* Handles a new client connection.
* @param socket - The socket object representing the client connection.
* Health check route to verify the server's state.
* Returns "pong" as the response.
*/
app.get("/ping", (req, res) => {
res.status(200).send({ message: "pong" });
});

/**
* Handles client connections and sets up event listeners.
*/
io.on("connection", (socket: Socket) => {
console.log(`Client connected: ${socket.id}`);

/**
* Handles player joining or creating a room.
Expand Down Expand Up @@ -81,7 +118,7 @@ io.on("connection", (socket: Socket) => {
io.to(room_code).emit("update_room", { room_player, room });
console.log(
`Player "${room_player}" joined room "${room_code}". Room state:`,
room,
room
);
});

Expand All @@ -98,7 +135,7 @@ io.on("connection", (socket: Socket) => {
}

room.players = room.players.filter(
(player) => player.room_player !== room_player,
(player) => player.room_player !== room_player
);

if (room.players.length === 0) {
Expand All @@ -113,7 +150,7 @@ io.on("connection", (socket: Socket) => {
io.to(room_code).emit("update_room", { room_player, room });

console.log(
`Player ${room_player} (Socket ID: ${socket.id}) left room ${room_code}`,
`Player ${room_player} (Socket ID: ${socket.id}) left room ${room_code}`
);
});

Expand All @@ -127,7 +164,7 @@ io.on("connection", (socket: Socket) => {
if (!room) return;

const player = room.players.find(
(player) => player.room_player === room_player,
(player) => player.room_player === room_player
);
if (player) {
player.socket = socket.id;
Expand Down Expand Up @@ -184,7 +221,7 @@ io.on("connection", (socket: Socket) => {

console.log(
`Game in room "${room_code}" finished! Ranking:`,
ranking,
ranking
);
return;
}
Expand All @@ -201,29 +238,40 @@ io.on("connection", (socket: Socket) => {
* Updates the number of cookies for a player in the room.
* @param data - The data for updating the cookies.
*/
socket.on("update_cookies", ({ room_player, room_code, cookies }: UpdateCookiesData) => {
if (typeof cookies !== "number" || cookies < 0) {
socket.emit("err_socket", { err_socket: "INVALID_COOKIES" });
return;
}

const room = ROOMS[room_code];
socket.on(
"update_cookies",
({ room_player, room_code, cookies }: UpdateCookiesData) => {
if (typeof cookies !== "number" || cookies < 0) {
socket.emit("err_socket", { err_socket: "INVALID_COOKIES" });
return;
}

if (!room) return;
const room = ROOMS[room_code];

const player = room.players.find(
(player) => player.room_player === room_player,
);
if (!room) return;

if (player) {
player.player_data.cookies = cookies;
console.log(
`Player "${room_player}" in room "${room_code}" updated cookies to ${cookies}.`,
const player = room.players.find(
(player) => player.room_player === room_player
);

if (player) {
player.player_data.cookies = cookies;
console.log(
`Player "${room_player}" in room "${room_code}" updated cookies to ${cookies}.`
);
}
}
);

/**
* Handles client disconnection.
*/
socket.on("disconnect", () => {
console.log(`Client disconnected: ${socket.id}`);
});

});

// Start server on port 3000
io.listen(3000);
// Starts the server on port 3000
httpServer.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});
3 changes: 2 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineConfig({
},
},
server: {
port: 8080,
host: process.env.VITE_WEB_HOST,
port: process.env.VITE_WEB_PORT,
},
});

0 comments on commit 58e9bc6

Please sign in to comment.