Skip to content

Latest commit

 

History

History
106 lines (86 loc) · 5.35 KB

README.md

File metadata and controls

106 lines (86 loc) · 5.35 KB

Simply Chess Server

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.

Client Link

https://github.com/quyson/simply-chess-client

Technologies Used

  1. Node.js and Express: Used for building the server, client authorization and authentication, handling HTTP requests and MSSQL database connection.
  2. Socket.IO: Enables real-time bidirectional communication between clients and the server allowing for exchange of chess-related information.
  3. MSSQL: A relational database used for storing user information and game-related data.
  4. jsonwebtoken (JWT): Used for user authentication by generating and verifying JSON Web Tokens.
  5. Bcrypt.js: Used to encrypt password upon user registration and authentication

Application Structure

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.

Authentication

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.

Chess Game Initialization

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.

Socket.IO Initialization (initializeGame.ts)

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.

Functions

initializeGame(sio: Server, socket: Socket): void

Sets up Socket.IO event listeners for game-related actions. Handles disconnection events, move handling, game creation, and player joining.

playerJoinsGame(idData: gameData): void

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.

createNewGame(gameId: string): void

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.

onDisconnect(): void

Handles the disconnect event when a player leaves the game. Removes the disconnected socket from the active sockets list.

sendUserName(gameId: string, username: string): void

Sends the player's username to the other player in the game room. Emits the give username event to share the username information.

handleMove(move: string, gameId: string): void

Handles the player's move and broadcasts it to the opponent. Emits the opponent move event to inform the opponent about the move.

MSSQL Database

Database Connection

The server connects to the MSSQL database using the mssql library. The ConnectServer function in databaseOperations.ts handles the database connection.

Database Operations (databaseOperations.ts)

This file contains functions for interacting with the MSSQL database, including user retrieval, creation, and updating statistics after a game.

GetUser:

Retrieves a user from the database based on the provided username.

CreateUser:

Creates a new user in the database with the given credentials.

updateAfterWin and updateAfterLoss:

Update a user's win or loss count in the database.

CreateMatch:

Records a new Chess match in the database, including the winner, loser, and date.

deleteUser:

Deletes a user from the database.

Classes

Match Class (match.ts)

The Match class represents a Chess match with information about the winner, loser, and date of the match.

Properties

  1. winner: string: Username of the match winner.
  2. loser: string: Username of the match loser.
  3. date: number: Timestamp representing the date of the match.
  4. 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.

User Class (user.ts)

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.

Properties

  1. ID: number | undefined: User ID (optional).
  2. username: string: User's username.
  3. password: string: User's hashed password.
  4. wins: number: Number of wins.
  5. losses: number: Number of losses.

Methods

  1. getID(): number | undefined: Retrieves the user's ID.
  2. getName(): string: Retrieves the user's username.
  3. getWins(): number: Retrieves the user's number of wins.
  4. getLosses(): number: Retrieves the user's number of losses.
  5. setWins(): void: Increments the user's number of wins.
  6. setLosses(): void: Increments the user's number of losses.

Additional Notes

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.