This repository contains the server-side code for a simple Chess application. The server is built using Node.js, Express, Socket.IO, and MSSQL for database operations. Below is a breakdown of the key components and functionalities.
https://github.com/quyson/simply-chess-client
- Node.js and Express: Used for building the server, client authorization and authentication, handling HTTP requests and MSSQL database connection.
- Socket.IO: Enables real-time bidirectional communication between clients and the server allowing for exchange of chess-related information.
- MSSQL: A relational database used for storing user information and game-related data.
- jsonwebtoken (JWT): Used for user authentication by generating and verifying JSON Web Tokens.
- Bcrypt.js: Used to encrypt password upon user registration and authentication
The server.ts file initializes the Express server, sets up Socket.IO, connects to the database, and handles authentication using JWT. It also defines Socket.IO events for Chess game interactions.
Users need to provide a valid JWT token to connect to the Socket.IO server. The server uses JWT to verify the token's validity and attaches the decoded user payload to the socket.
When a user connects and joins the Chess room, the server initializes a new Chess game using initializeGame. The initializeGame function sets up event listeners for Chess game moves and outcomes.
This module handles the initialization and management of Socket.IO connections for the Chess game. It facilitates real-time communication between players, including creating new games, joining game rooms, sending moves, and handling disconnections.
Sets up Socket.IO event listeners for game-related actions. Handles disconnection events, move handling, game creation, and player joining.
Joins a player to a game room based on the provided gameId. Emits events to notify clients about the player joining and, if applicable, starts the game.
Creates a new game with the given gameId and the current socket's ID. Informs the client about the successful game creation and instructs them to wait for the opponent.
Handles the disconnect event when a player leaves the game. Removes the disconnected socket from the active sockets list.
Sends the player's username to the other player in the game room. Emits the give username event to share the username information.
Handles the player's move and broadcasts it to the opponent. Emits the opponent move event to inform the opponent about the move.
The server connects to the MSSQL database using the mssql library. The ConnectServer function in databaseOperations.ts handles the database connection.
This file contains functions for interacting with the MSSQL database, including user retrieval, creation, and updating statistics after a game.
Retrieves a user from the database based on the provided username.
Creates a new user in the database with the given credentials.
Update a user's win or loss count in the database.
Records a new Chess match in the database, including the winner, loser, and date.
Deletes a user from the database.
The Match class represents a Chess match with information about the winner, loser, and date of the match.
- winner: string: Username of the match winner.
- loser: string: Username of the match loser.
- date: number: Timestamp representing the date of the match.
- constructor(winner: string, loser: string): Creates a new instance of the Match class with the provided winner and loser usernames. Sets the date to the current timestamp.
The User class represents a user in the Chess application. It includes properties such as username, password, wins, losses. The class provides methods to retrieve and update user information.
- ID: number | undefined: User ID (optional).
- username: string: User's username.
- password: string: User's hashed password.
- wins: number: Number of wins.
- losses: number: Number of losses.
- getID(): number | undefined: Retrieves the user's ID.
- getName(): string: Retrieves the user's username.
- getWins(): number: Retrieves the user's number of wins.
- getLosses(): number: Retrieves the user's number of losses.
- setWins(): void: Increments the user's number of wins.
- setLosses(): void: Increments the user's number of losses.
The server is configured to allow connections from the specified client origin using CORS. Socket.IO events are utilized for real-time communication between clients for Chess game moves. MSSQL is employed for storing user data and Chess match information. Feel free to explore and modify the code to add more features, improve functionality, or adapt it to your requirements.