This RESTful API is designed to facilitate a multiplayer gaming environment. It enables player authentication and access to game server information. It provides a platform for game servers to register, manage their information, and authenticate players.
- Incorporates Microsoft Identity for user authentication, providing a secure environment for users and their account information.
- Authenticated users are provided a JSON Web Token (JWT) for future API requests, ensuring secure and stateless authentication.
- Role-based authorization to ensure secure access control. The roles include: ‘Admin’, 'Server', and ‘Player’.
- Utilizes token-based interaction between players and game servers, allowing game servers to authenticate players who join without exposing sensitive player account information.
- Provides endpoints for game servers to update themselves through their life cycle. This includes server registration, status management, player validation, and deregistration.
- Players are able to access information on registered game servers.
- A background service that removes game servers if they fail to send a heartbeat to the API within the specified time period.
The API employs a hybrid data storage approach to efficiently handle both persistent and volatile game server and authentication information.
- Authentication Information: Utilizes Microsoft Identity to store user authentication data, including login credentials and roles.
- Persistent Game Server Data: Stores essential information such as IP, port, server name, and player capacity.
- Entity Framework Core: Manages all interactions with the database, including queries and updates, using Entity Framework Core.
- Game Server Data: Maintains data subject to frequent updates, such as player counts, in a thread-safe dictionary. This approach minimizes the load on the database.
The API is documented using Swagger. You can view a static page of the documentation by here.
- Auth Controller: Manages user authentication and registration, issuing JWTS for authenticated sessions.
- Game Server Controller: Manages game server lifecycle operations including registration, status updates, and validation of PlayerJoinTokens.
- Server Browser Controller: Allows players to view active game servers and their details.
- Player Token Controller: Issues PlayerJoinTokens that are used to authenticate players to game servers.
The API uses HTTP Bearer authentication. Secure your requests with a JWT in the Authorization
header formatted as Bearer <token>
.
Note: For simplicity, this diagram omits certain less relevant tables and attributes.
- Admin
- Can create servers.
- Can modify any server, regardless of creator.
- Server
- Can create servers.
- Can modify servers that are created by their own account.
- Player
- Can request player join tokens.
- Cannot create servers.
Customizing Seeded Data
By default, the following accounts are seeded into the database:
-
Admin Account:
- Username: admin
- Password: Admin123$
- Role: Admin
-
Server Account:
- Username: server
- Password: Server123$
- Role: Server
To customize the predefined roles and user accounts before seeding the database, edit the seed data in the OnModelCreating
method of the ApplicationDbContext
. Modify the user details and roles as needed and then apply the changes by running the database migration with the command Update-Database
in the Package Manager Console or dotnet ef database update
in the terminal.
The API is designed to be versatile, suitable for use in any game development environment. A detailed implementation within the Godot engine is available here: GameServer GameClient.
Before you begin the installation, ensure you have the following software installed:
- .NET SDK 8: Required to build and run the .NET application.
- Visual Studio 2019 or later: Recommended IDE for developing .NET applications. You can download it from Visual Studio Official Site.
- SQL Server: Necessary for database operations. You can use any version compatible with .NET 8.
- SQL Server Management Studio (SSMS): Optional but recommended for managing your SQL database visually. Download from SSMS Official Site.
Installation Guide without Visual Studio
Clone the repository containing the ASP.NET project using the following command in your terminal:
git clone https://github.com/robert-caulfield/GameServerAPI
cd [Project Folder]
Navigate to the project directory where the *.csproj
file is located and run:
dotnet restore
This command restores all the project dependencies.
- Open the
appsettings.json
file in the project. - Modify the
ConnectionStrings
section with your SQL Server details if neccessary, this is the default:
"ConnectionStrings": {
"DefaultConnection": "Server=(LocalDb)\\MSSQLLocalDB;Database=GameServerAPI;TrustServerCertificate=True;Trusted_Connection=True;MultipleActiveResultSets=true"
}
If you haven't already installed the Entity Framework CLI, install it globally using the following command:
dotnet tool install --global dotnet-ef
Apply the migrations to your database to create the necessary schema:
dotnet ef database update
Build the project using:
dotnet build
Run the application with:
dotnet run
- The swagger UI can be accessed in your browser at
https://localhost:port/swagger
whereport
is the port number specified inProperties/launchSettings.json
(7242 by default). - You can interact with the API directly from the Swagger interface to test different endpoints.
Installation Guide with Visual Studio
- Open Visual Studio.
- Select 'Open a project or solution' from the start window, or go to
File -> Open -> Project/Solution
from the menu. - Navigate to the folder where you cloned your repository and open the solution file (.sln).
Right-click on the solution in the Solution Explorer and select 'Restore NuGet Packages'. This will ensure all dependencies are correctly installed based on the project configuration.
- Open the
appsettings.json
file in the project. - Modify the
ConnectionStrings
section with your SQL Server details if neccessary, this is the default:
"ConnectionStrings": {
"DefaultConnection": "Server=(LocalDb)\\MSSQLLocalDB;Database=GameServerAPI;TrustServerCertificate=True;Trusted_Connection=True;MultipleActiveResultSets=true"
}
- Open the Package Manager Console by going to
Tools -> NuGet Package Manager -> Package Manager Console
. - Ensure the Default project is set to the project containing your Entity Framework context.
- Run the command:
Update-Database
This command applies any pending migrations to the SQL database, creating it if it does not already exist.
- Build the project by selecting
Build -> Build Solution
. - Run the project by pressing
F5
or clicking the 'Start Debugging' button. This will launch the API in your default browser with the Swagger UI loaded.
- The Swagger UI should open automatically in your browser at
https://localhost:port/swagger
whereport
is the port number specified inProperties/launchSettings.json
(7242 by default). - You can interact with the API directly from the Swagger interface to test different endpoints.
Post-Installation
- Navigate to
appsettings.json
- Replace secret key values with a new strong secret keys Example:
"ApiSettings": {
"Secret": "NewStrongSecretKeyHere1234567890", // Secret key used for user authentication JWTs
"PlayerJoinTokenSecret": "AnotherStrongSecretKeyHere987654321" // Secret key used for PlayerJoinToken JWTs
}
Note: In a production environment these secret keys would be stored in a secure storage solution like Azure Key Vault.
- Navigate to
appsettings.json
- Configure
GameServerManagerSettings
to the needs of your project:
// Used to populate GameServerManagerSettings
"GameServerManagerSettings": {
"HeartbeatEnabled": true, // If true, enables background cleanup service
"HeartbeatTimeout": 90, // Time in seconds before a game server is considered inactive and removed.
"HeartbeatCheckInterval": 30 // Time interval in seconds that the background service cleans up inactive servers
}
- ASP.NET 8
- Microsoft Identity
- Microsoft Entity Framework
- SQL Server