Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Server Browser and Lobby Server (#10, #45, #35), in-game chat (#8) and minor bug fixes (#36) #46

Open
wants to merge 209 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
209 commits
Select commit Hold shift + click to select a range
e23d522
Begin creating the server browser menu
Insprill Aug 8, 2023
764bfc7
Fixed minor issue with CSV parsing so that unix/windows line breaks d…
AMacro May 11, 2024
6daa671
Enhanced "join" interface
AMacro May 12, 2024
e1a3e97
Reworked the saving of last direct connection details
AMacro May 12, 2024
c691e32
refactoring und updating
morm075 May 25, 2024
a6ab704
Merge branch 'feature/server-browser' into feature/server-browser-update
morm075 May 26, 2024
44471ca
Fix CSV.cs
N95JPL May 26, 2024
c43328b
Merge branch 'Localisation-Parsing-Fix' into CSV-Key-Fix
N95JPL May 26, 2024
0c9d431
Dynamic DNS
N95JPL May 26, 2024
bb69a06
Correct UriKind Check
N95JPL May 26, 2024
a4c8453
Continuing to update server browser
morm075 Jun 16, 2024
af9cd6d
Minor fixes and improvements
AMacro Jun 16, 2024
0455990
Merge branch 'updated-server-browser' into updated-server-browser
AMacro Jun 16, 2024
bcf5735
Merge pull request #1 from AMacro/updated-server-browser
morm075 Jun 16, 2024
81aa6ba
Minor adjustments and commenting
morm075 Jun 18, 2024
6e721e5
added button icons
AMacro Jun 20, 2024
c66ee37
minor correction
morm075 Jun 22, 2024
d236a90
PHP and Rust servers implemented
AMacro Jun 22, 2024
0e373f4
Merge branch 'updated-server-browser' of https://github.com/morm075/d…
AMacro Jun 22, 2024
c190545
another correction
morm075 Jun 22, 2024
4b2c6bb
Fixed SSL compilation issues
AMacro Jun 23, 2024
76f5aee
Merge branch 'updated-server-browser' of https://github.com/morm075/d…
morm075 Jun 23, 2024
492938e
Update Read Me.md
AMacro Jun 23, 2024
94f344f
Improved servers and server browser code
AMacro Jun 27, 2024
8329b63
Updates to locale.csv
morm075 Jun 30, 2024
a7ae049
Server browser and lobby server working
AMacro Jun 30, 2024
499dacf
Merge branch 'updated-server-browser' of https://github.com/morm075/d…
AMacro Jun 30, 2024
0fa44a6
Minor UI fixes and version update
AMacro Jun 30, 2024
e5051da
Updated default server to https
AMacro Jul 1, 2024
eb3b948
Refactored server browser for consistency
AMacro Jul 1, 2024
6e8df46
Added auto refresh
AMacro Jul 6, 2024
a99179a
Server details displayed in pane
AMacro Jul 6, 2024
86f8245
Updated translations for server browser details pane
AMacro Jul 6, 2024
30c5988
Fixed translation issue
AMacro Jul 6, 2024
252745d
Updated default server browser text.
AMacro Jul 6, 2024
ab36de5
Reorganised UI components
AMacro Jul 6, 2024
830cd1c
Initial commit
AMacro Jul 7, 2024
ea633a3
Added hotbar blocking and aligned chat window with lower left corner
AMacro Jul 7, 2024
2d3d2df
Bug fixes
AMacro Jul 12, 2024
14e5aae
Added chat commands
AMacro Jul 13, 2024
9ee085c
Added sent message history
AMacro Jul 13, 2024
13184b8
Added auto complete for whisper usernames, enforced some username con…
AMacro Jul 13, 2024
9925783
General tidy up and QoL for server browser
AMacro Jul 13, 2024
00359ad
Bug fixes for lobby server redirects
AMacro Jul 13, 2024
2458ed2
Merge pull request #3 from morm075/updated-server-browser
AMacro Jul 14, 2024
18333e4
Merge pull request #4 from morm075/in-game-chat
AMacro Jul 14, 2024
2b666d4
Merge pull request #6 from AMacro/feature/updated-server-browser
AMacro Jul 14, 2024
c6f795f
Merge pull request #7 from AMacro/feature/in-game-chat
AMacro Jul 14, 2024
db77e3d
Merge pull request #1 from N95JPL/CSV-Key-Fix
AMacro Jul 14, 2024
0871556
Merge branch 'beta' into Localisation-Parsing-Fix
AMacro Jul 14, 2024
fa0fbfb
Merge pull request #8 from AMacro/Localisation-Parsing-Fix
AMacro Jul 14, 2024
1827ded
Minor update to CSV parsing
AMacro Jul 14, 2024
9d3fb99
Squashed commit of the following:
AMacro Jul 14, 2024
36db54a
Squashed commit of the following:
AMacro Jul 14, 2024
24b6f58
Rectified conflicts
AMacro Jul 14, 2024
33592fd
Merge branch 'beta' into Join-Menu-Improvements
AMacro Jul 14, 2024
dc1235d
Merge pull request #2 from N95JPL/Join-Menu-Improvements
AMacro Jul 14, 2024
5f8e730
Add CSV parsing check and log
AMacro Jul 14, 2024
dc93e10
Merge branch 'beta' of https://github.com/AMacro/dv-multiplayer into …
AMacro Jul 14, 2024
13a265f
Preparing for v0.16.0
AMacro Jul 14, 2024
97c10fe
Fixed issue where PHP server does not return correct error code
AMacro Jul 14, 2024
750958e
Fixed issues with servers that prioritise IPV6, disabled job sync code
AMacro Jul 14, 2024
1ebb5f4
Update README.md
morm075 Jul 14, 2024
d2f07ae
Update README.md
morm075 Jul 14, 2024
123cc24
Update README.md
morm075 Jul 14, 2024
6ed3c82
Repair after data loss
AMacro Jul 21, 2024
3219296
Forced connections on IPv4
AMacro Jul 21, 2024
72289e6
Self preservation when deleting cars
AMacro Jul 21, 2024
822c03f
Continuing work on IPv6 and sync issues
AMacro Jul 27, 2024
4a7e195
Multiple bug fixes, sync fix for entering/exiting cars
AMacro Jul 27, 2024
1e09a9f
Optimised car destroy code, improvements to lag due to debug output
AMacro Jul 27, 2024
64a8995
Bug fixes and features
AMacro Jul 28, 2024
298786f
Ready for release
AMacro Jul 28, 2024
2718a78
Fixed brake pressure sync issue
AMacro Aug 2, 2024
0deb0fb
Added example Directory.Build.targets
AMacro Aug 2, 2024
fdf5b7d
Update README.md
AMacro Aug 2, 2024
713e26e
Update README.md
AMacro Aug 2, 2024
08f5e02
Fixed bug where password was not cleared when attempting to join a se…
AMacro Aug 3, 2024
a3aed4e
Merge branch 'beta' of https://github.com/AMacro/dv-multiplayer into …
AMacro Aug 3, 2024
aa821aa
Added versioning system to settings to allow upgrades on new version …
AMacro Aug 10, 2024
eedf22f
Fixed bug in RIgidBodySnapshot.Apply()
AMacro Aug 10, 2024
0cf6e43
Steam locomotive sync improvements
AMacro Aug 10, 2024
ac30237
Disabled chat in single player mode
AMacro Aug 10, 2024
22c2ea2
Implemented potential fix for error accumulation and de-sync
AMacro Aug 11, 2024
ffe0313
Cleanup of unused code / variables
AMacro Aug 11, 2024
b924d1a
Refactor and attempt reactivation of locomotive despawning
AMacro Aug 11, 2024
27f8cbf
Continuing attempt to reactivate loco de-spawning
AMacro Aug 11, 2024
8d727fd
Revert "Implemented potential fix for error accumulation and de-sync"
AMacro Aug 16, 2024
ff4245e
Cleanup for 0.1.8.0 release
AMacro Aug 17, 2024
5b85cf9
implemented potential fix for error accumulation and de-sync
AMacro Aug 18, 2024
77f120a
Fix for single player job expiry bug
AMacro Aug 23, 2024
f5f68cf
Continuing Job Sync
AMacro Aug 25, 2024
3a235a8
Update server browser pane to remove dummy servers
AMacro Aug 30, 2024
12ec3a9
Merge branch 'alpha' into beta
AMacro Aug 30, 2024
85c614a
Fix for joining servers with no password
AMacro Aug 31, 2024
310fcfc
Allowed multiple lines to be entered in details text box
AMacro Aug 31, 2024
a673023
Fixed serialisation and deserialisation of jobs
AMacro Aug 31, 2024
0b2fdc7
Continuing job sync code
AMacro Aug 31, 2024
bc807c5
Fix for early game rerails costing >$0
AMacro Sep 1, 2024
de7d252
Remove redundant code
AMacro Sep 1, 2024
3256da2
Added framework for clients to take and complete jobs
AMacro Sep 1, 2024
84161bd
Start work on LAN discovery and server browser ping
AMacro Sep 6, 2024
00e39ab
Fixed MU cable connection bug
AMacro Sep 7, 2024
7d637c3
Fixed issues with obtaining external IPv4 address
AMacro Sep 8, 2024
ff4becc
fix for null reference on TrainCars without MU capabilities
AMacro Sep 8, 2024
7a1524f
Fixed StationController to JobValidator mapping
AMacro Sep 8, 2024
1cee17d
Started work on item sync
AMacro Sep 8, 2024
8e8e333
Continued work on syncing jobs - framework for job validation
AMacro Sep 8, 2024
b6e6658
Added message box for out of date Multiplayer mod
AMacro Sep 8, 2024
53e2180
Fixed RPC timeouts to work in milliseconds rather than seconds
AMacro Sep 8, 2024
7de3768
Fixed error log, ready for release
AMacro Sep 8, 2024
6ae42d0
Fix project board link in the README
matejdro Sep 10, 2024
1cb7b25
Merge pull request #46 from matejdro/patch-1
AMacro Sep 10, 2024
5c4c9ce
Update README.md
AMacro Sep 13, 2024
e82949a
Completed ping functionality for Server Browser
AMacro Sep 15, 2024
503e4a0
General clean up and warnings fixes
AMacro Sep 15, 2024
38eacc4
Added LAN discovery
AMacro Sep 21, 2024
e35f310
Removed extra logging from Ping and LAN discovery
AMacro Sep 21, 2024
d23098c
Minor UI changes
AMacro Sep 22, 2024
1536178
Update HostGamePane.cs
AMacro Sep 22, 2024
2cef3ef
Continuing job and item sync
AMacro Sep 22, 2024
d95229d
Continuation of item sync
AMacro Sep 24, 2024
42573a0
fixed warning
AMacro Sep 24, 2024
79f6140
Continuation of item sync
AMacro Oct 6, 2024
dbc9b78
Enhanced Hosting pane
AMacro Oct 8, 2024
9038906
Fixed issues with deserialisation
AMacro Oct 8, 2024
73bbaeb
Item Sync continued
AMacro Oct 8, 2024
bb02a7d
Allow servers with names up to 25 chars (minor bug)
AMacro Oct 21, 2024
0809fd2
Increased initial update speed
AMacro Oct 21, 2024
3b19f2d
Refactoring and improving item sync
AMacro Oct 21, 2024
352b357
Begin implementation of item sync for distance from player
AMacro Oct 22, 2024
cbbf773
Continuation of item sync
AMacro Nov 14, 2024
3299567
Reworked item sync
AMacro Nov 14, 2024
82e7eb5
Fixed issues with item sync and added more "smart" objects to the syn…
AMacro Nov 19, 2024
dd44f88
Remove excessive logging for jobs
AMacro Nov 21, 2024
08f469a
Add authority to tracked values (Server update vs Common update)
AMacro Nov 21, 2024
c9a5742
Begin implementation of item update validation and ownership
AMacro Nov 22, 2024
7f38fde
Localisation updates
AMacro Nov 24, 2024
b82d477
Compatibility with B99
AMacro Nov 24, 2024
43c4372
Minor localisation updates
AMacro Nov 24, 2024
b00f47a
Updates to localisation
AMacro Nov 29, 2024
8c22379
Fix for 'LocalFruits' Error
AMacro Nov 30, 2024
f133845
Fixes for common post-B99 bugs
AMacro Dec 1, 2024
8e0c67a
Fix ping issues for private LAN games
AMacro Dec 1, 2024
9d1be93
Fix Garage Unlock
AMacro Dec 1, 2024
4bfaf4d
Additional logging to find source of sync issues
AMacro Dec 1, 2024
adb43ca
Minor fixes
AMacro Dec 1, 2024
ff9cbef
Fix lobby server start-up logging
AMacro Dec 14, 2024
d6ce991
Refactor StationLocoSpawner
AMacro Dec 14, 2024
bedd1df
Fixed issue `Undefined packet 34 in NetDataReader`
AMacro Dec 14, 2024
225c2ad
Clean up logging
AMacro Dec 21, 2024
dd51870
Refactor Networking components
AMacro Dec 21, 2024
e2645f0
Fixed issue with overloading cargo on cars
AMacro Dec 22, 2024
2dea876
Added initialisation delay to NetworkedBogie
AMacro Dec 22, 2024
9891cf9
Re-wrote car spawning method and data structures
AMacro Dec 22, 2024
b778c03
Fix item bugs
AMacro Dec 22, 2024
d7f5d4c
Refactored SpawnPart data for easier maintenance
AMacro Dec 22, 2024
8385434
Rework of coupler interaction messages
AMacro Dec 27, 2024
7ca6943
Minor clean-up of LAN discovery packets
AMacro Dec 27, 2024
3274b8b
Remove reliance on RegEx checks
AMacro Jan 1, 2025
cea0970
Add support for Steam player name
AMacro Jan 2, 2025
9c08cda
Update default settings
AMacro Jan 2, 2025
ab238cc
Edited french locale
Harfeur Jan 2, 2025
edee3db
Intellisense Message fixes
AMacro Jan 3, 2025
829aacb
Fix for dictionaries caching deleted cards
AMacro Jan 3, 2025
e5272c7
Intellisense Message fixes
AMacro Jan 4, 2025
deebcdc
Update brake pressure synchronisation
AMacro Jan 4, 2025
1cb5945
Improve logging of NetworkedTrainCar Sim data
AMacro Jan 4, 2025
5dba907
Complete coupler sync update.
AMacro Jan 4, 2025
5f841d5
General tidy up of code and logging
AMacro Jan 4, 2025
dd576f2
Fix issues with job sync and overview spawns
AMacro Jan 4, 2025
537b4a0
Fix issues with cargo sync
AMacro Jan 4, 2025
4bd5a16
Fix issues with deletion of cars on the client
AMacro Jan 4, 2025
4ece2c6
Temporary patch for restoration / demo locos
AMacro Jan 4, 2025
4607b60
Begin work on loco restoration sync
AMacro Jan 4, 2025
f78eb89
Fix for Main Menu braking on clients when exiting a game
AMacro Jan 4, 2025
206c839
Brake sync fix
AMacro Jan 4, 2025
40e7a53
Minor change to logging
AMacro Jan 4, 2025
f301276
Increase time out of connection attempt to match disconnect time out
AMacro Jan 4, 2025
83445a1
Fix for dictionaries not clearing on return to main menu
AMacro Jan 5, 2025
41a9b36
Add in paint theme and basic restoration sync
AMacro Jan 11, 2025
3874c05
Update job registration and plate update process
AMacro Jan 11, 2025
32fb210
Coupler interaction ready for testing
AMacro Jan 11, 2025
c16b500
Fix bug with persistent cache
AMacro Jan 11, 2025
f05eed5
Fix null reference issue on ChatGUI
AMacro Jan 11, 2025
4b18adc
Enhance SendDestroyTrainCar() to allow packets to be sent to a specif…
AMacro Jan 11, 2025
4b15374
Fix fast travel bug deleting cars client and server side
AMacro Jan 11, 2025
96adda5
Fix IPv6 connection issue
AMacro Jan 11, 2025
715d6dd
Prepare for testing and release
AMacro Jan 11, 2025
0e39fa2
Readded quotes
AMacro Jan 11, 2025
0745a28
Merge pull request #60 from Harfeur/patch-1
AMacro Jan 11, 2025
ea42a10
Improve job validation sync
AMacro Jan 12, 2025
64ee11b
Sync brake heating states
AMacro Jan 12, 2025
c0d59d6
Improve job update handling
AMacro Jan 12, 2025
255f104
Improve jittering of derailed cars
AMacro Jan 12, 2025
084df26
Improve position sync when there's a coupling de-sync
AMacro Jan 12, 2025
27dc6a2
Minor logging improvements
AMacro Jan 12, 2025
c3d8957
Fix for job validation state
AMacro Jan 12, 2025
6c49c55
Improve retrieval of steam user account
AMacro Jan 12, 2025
45739a8
Update PlayerListGUI behaviour
AMacro Jan 13, 2025
2d28321
Potential fix for null gameObject exception
AMacro Jan 13, 2025
70b39a5
Add check for cached null in NetworkedPlayer
AMacro Jan 14, 2025
64efdc9
Remove unsafe dictionary access, use TryGet()
AMacro Jan 14, 2025
7aca2c3
Added null reference checks for map marker updates
AMacro Jan 15, 2025
83017f5
additional logging and null checks for OnCommonHoseConnectedPacket
AMacro Jan 15, 2025
bb72202
Additional logging for LobbyServerManager WebRequests
AMacro Jan 15, 2025
dbd5108
Additional coupler interaction logging
AMacro Jan 16, 2025
fbffecd
Add delta check to OnFireboxUpdate()
AMacro Jan 16, 2025
3ab2d27
Reduce debug logging for map marker updates
AMacro Jan 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,5 @@ MultiplayerAssets/ProjectSettings/*
!MultiplayerAssets/ProjectSettings/ProjectVersion.txt
# Packages
!MultiplayerAssets/Packages
/Lobby Servers/Rust Server/target
*.pem
25 changes: 25 additions & 0 deletions Directory.Build.targets.EXAMPLE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
Update DvInstallDir and UnityInstallDir paths to your local install directories
Do not use trailing slashes

If using code signing:
Update SignToolPath to the directory of signtool.exe
Update Cert-Thumb to the thumbprint of your code signing certificate, otherwise leave blank

Save this updated file as 'Directory.Build.targets'

-->
<Project>
<PropertyGroup>
<DvInstallDir>C:\Program Files (x86)\Steam\steamapps\common\Derail Valley</DvInstallDir>
<UnityInstallDir>C:\Program Files\Unity\Hub\Editor\2019.4.40f1\Editor</UnityInstallDir>
<ReferencePath>
$(DvInstallDir)\DerailValley_Data\Managed\;
$(DvInstallDir)\DerailValley_Data\Managed\UnityModManager\;
$(UnityInstallDir)\Data\Managed\
</ReferencePath>
<AssemblySearchPaths>$(AssemblySearchPaths);$(ReferencePath);</AssemblySearchPaths>
<SignToolPath>C:\Program Files (x86)\Microsoft SDKs\ClickOnce\SignTool\</SignToolPath>
<Cert-Thumb>7cf2b8a98a09ffd407ada2e94f200af24a0e68bc</Cert-Thumb>
</PropertyGroup>
</Project>
11 changes: 11 additions & 0 deletions Lobby Servers/PHP Server/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Enable the RewriteEngine
RewriteEngine On

# Uncomment below to force HTTPS
# RewriteCond %{HTTPS} off
# RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect all non-existing paths to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
10 changes: 10 additions & 0 deletions Lobby Servers/PHP Server/DatabaseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

interface DatabaseInterface {
public function addGameServer($data);
public function updateGameServer($data);
public function removeGameServer($data);
public function listGameServers();
}

?>
99 changes: 99 additions & 0 deletions Lobby Servers/PHP Server/FlatfileDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
include 'DatabaseInterface.php';

class FlatfileDatabase implements DatabaseInterface {
private $filePath;

public function __construct($dbConfig) {
$this->filePath = $dbConfig['flatfile_path'];
}

private function readData() {
if (!file_exists($this->filePath)) {
return [];
}
return json_decode(file_get_contents($this->filePath), true) ?? [];
}

private function writeData($data) {
file_put_contents($this->filePath, json_encode($data, JSON_PRETTY_PRINT));
}

public function addGameServer($data) {
$data['last_update'] = time(); // Set current time as last_update

$servers = $this->readData();
$servers[] = $data;
$this->writeData($servers);

return json_encode([
"game_server_id" => $data['game_server_id'],
"private_key" => $data['private_key'],
]);
}

public function updateGameServer($data) {
$servers = $this->readData();
$updated = false;

foreach ($servers as &$server) {
if ($server['game_server_id'] === $data['game_server_id']) {
$server['current_players'] = $data['current_players'];
$server['time_passed'] = $data['time_passed'];
$server['last_update'] = time(); // Update with current time

$updated = true;
break;
}
}

if ($updated) {
$this->writeData($servers);
return json_encode(["message" => "Server updated"]);
} else {
return json_encode(["error" => "Failed to update server"]);
}
}

public function removeGameServer($data) {
$servers = $this->readData();
$servers = array_filter($servers, function($server) use ($data) {
return $server['game_server_id'] !== $data['game_server_id'];
});
$this->writeData(array_values($servers));
return json_encode(["message" => "Server removed"]);
}

public function listGameServers() {
$servers = $this->readData();
$current_time = time();
$active_servers = [];
$changed = false;

foreach ($servers as $key => $server) {
if ($current_time - $server['last_update'] <= TIMEOUT) {
$active_servers[] = $server;
} else {
$changed = true; // Indicates there's a change if any server is removed
}
}

if ($changed) {
$this->writeData($active_servers); // Write back only if there are changes
}

return json_encode($active_servers);
}

public function getGameServer($game_server_id) {
$servers = $this->readData();
foreach ($servers as $server) {
if ($server['game_server_id'] === $game_server_id) {
return json_encode($server);
}
}
return json_encode(null);
}
}

?>
79 changes: 79 additions & 0 deletions Lobby Servers/PHP Server/MySQLDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
include 'DatabaseInterface.php';

class MySQLDatabase implements DatabaseInterface {
private $pdo;

public function __construct($dbConfig) {
$this->pdo = new PDO("mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']}", $dbConfig['username'], $dbConfig['password']);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

public function addGameServer($data) {
$stmt = $this->pdo->prepare("INSERT INTO game_servers (game_server_id, private_key, ipv4, ipv6, port, server_name, password_protected, game_mode, difficulty, time_passed, current_players, max_players, required_mods, game_version, multiplayer_version, server_info, last_update)
VALUES (:game_server_id, :private_key, :ip, :port, :server_name, :password_protected, :game_mode, :difficulty, :time_passed, :current_players, :max_players, :required_mods, :game_version, :multiplayer_version, :server_info, :last_update)");
$stmt->execute([
':game_server_id' => $data['game_server_id'],
':private_key' => $data['private_key'],
':ipv4' => isset($data['ipv4']) ? $data['ipv4'] : '',
':ipv6' => isset($data['ipv6']) ? $data['ipv6'] : '',
':port' => $data['port'],
':server_name' => $data['server_name'],
':password_protected' => $data['password_protected'],
':game_mode' => $data['game_mode'],
':difficulty' => $data['difficulty'],
':time_passed' => $data['time_passed'],
':current_players' => $data['current_players'],
':max_players' => $data['max_players'],
':required_mods' => $data['required_mods'],
':game_version' => $data['game_version'],
':multiplayer_version' => $data['multiplayer_version'],
':server_info' => $data['server_info'],
':last_update' => time() //use current time
]);
return json_encode([
"game_server_id" => $data['game_server_id'],
"private_key" => $data['private_key']
]);
}

public function updateGameServer($data) {
$stmt = $this->pdo->prepare("UPDATE game_servers
SET current_players = :current_players, time_passed = :time_passed, last_update = :last_update
WHERE game_server_id = :game_server_id");
$stmt->execute([
':current_players' => $data['current_players'],
':time_passed' => $data['time_passed'],
':last_update' => time(), // Update with current time
':game_server_id' => $data['game_server_id']
]);

return $stmt->rowCount() > 0 ? json_encode(["message" => "Server updated"]) : json_encode(["error" => "Failed to update server"]);
}

public function removeGameServer($data) {
$stmt = $this->pdo->prepare("DELETE FROM game_servers WHERE game_server_id = :game_server_id");
$stmt->execute([':game_server_id' => $data['game_server_id']]);
return $stmt->rowCount() > 0 ? json_encode(["message" => "Server removed"]) : json_encode(["error" => "Failed to remove server"]);
}

public function listGameServers() {
// Remove servers that exceed TIMEOUT directly in the SQL query
$stmt = $this->pdo->prepare("DELETE FROM game_servers WHERE last_update < :timeout");
$stmt->execute([':timeout' => time() - TIMEOUT]);

// Fetch remaining servers
$stmt = $this->pdo->query("SELECT * FROM game_servers");
$servers = $stmt->fetchAll(PDO::FETCH_ASSOC);

return json_encode($servers);
}

public function getGameServer($game_server_id) {
$stmt = $this->pdo->prepare("SELECT * FROM game_servers WHERE game_server_id = :game_server_id");
$stmt->execute([':game_server_id' => $game_server_id]);
return json_encode($stmt->fetch(PDO::FETCH_ASSOC));
}
}

?>
149 changes: 149 additions & 0 deletions Lobby Servers/PHP Server/Read Me.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Lobby Server - PHP

This is a PHP implementation of the Derail Valley Lobby Server REST API service. It is designed to run on any standard web hosting and does not rely on long-running/persistent behaviour.
HTTPS support depends on the configuration of the hosting environment.

As this implementation is not persistent in memory, a database is used to store server information. Two options are available for the database engine - a JSON based flatfile or a MySQL database.

## Installing

The following instructions assume you will be using an Apache web server and may need to be modified for other configurations.

1. Copy the following files to your public html folder (consult your web server/web host's documentation)
```
index.php
DatabaseInterface.php
FlatfileDatabase.php
MySQLDatabase.php
.htaccess
```
2. Copy `config.php` to a secure location outside of your public html directory
3. Edit `index.php` and update the path to the config file on line 2:
```php
<?php
include '/path/to/config.php';
...
```

### Setup the database

Choose between a MySQL database and a flatfile database.

#### MySQL

1. Edit `config.php` and set the `type` parameter to `mysql`
2. Update the `host`, `dbname`, `username` and `password` parameters for your MySQL server
3. Copy the `install.php` file to a location you can execute it
4. Edit `install.php` and update the path to the config file on line 2:
```php
<?php
include '/path/to/config.php';
...
```
5. Run `install.php`
6. If `install.php` is in a publically visible location, remove it


#### Flatfile
1. In a secure location (outside of your public html directory), create a directory to store the database
2. Edit `config.php` and set the `type` parameter to `flatfile`
3. Update `flatfile_path` to the directory created in step 1


## Configuration Parameters
The server can be configured using the `config.php` file.

Below are the available parameters along with their defaults:
- `TIMEOUT` (u64): The time-out period in seconds for server removal.
-- Default: `120`
-- `dbConfig`.`type` (string): Type of database for persisting data.
-- Options: `mysql`, `flatfile`
-- Default: `mysql`
- `dbConfig`.`host` (string): hostname of MySQLServer.
-- Required when: `dbConfig`.`type` is `mysql`
-- Default: `localhost`
- `dbConfig`.`dbname` (string): Database name.
- Required when: `dbConfig`.`type` is `mysql`
-- Default: N/A
- `dbConfig`.`username` (string): Username for connecting to the MySQLServer.
-- Required when: `dbConfig`.`type` is `mysql`
-- Default: N/A
- `dbConfig`.`password` (string): Password for connecting to the MySQLServer.
-- Required when: `dbConfig`.`type` is `mysql`
-- Default: N/A
- `dbConfig`.`flatfile_path` (string): Path to secure directory.
-- Required when: `dbConfig`.`type` is `flatfile`
-- Default: N/A

Example `config.php` using MySQL:
```php
<?php

// Timeout value (in seconds)
define('TIMEOUT', 120);

// Database configuration
$dbConfig = [
'type' => 'mysql',
'host' => 'localhost',
'dbname' => 'dv_lobby',
'username' => 'dv_lobby_server',
'password' => 'n16O5+LMpeqI`{E',
'flatfile_path' => '' // Path to store the flatfile database
];
?>
```

Example `config.php` using Flatfile:
```php
<?php

// Timeout value (in seconds)
define('TIMEOUT', 120);

// Database configuration
$dbConfig = [
'type' => 'flatfile',
'host' => '',
'dbname' => '',
'username' => '',
'password' => '',
'flatfile_path' => '/dv_lobby/flatfile.db' // Path to store the flatfile database
];
?>
```

## Security Considerations
This is a non-comprehensive overview of security considerations. You should always use up-to-date best practices and seek professional advice where required.

### Environment variables
Consider using environment variables to store sensitive database credentials (e.g. `dbConfig`.`host`, `dbConfig`.`dbname`, `dbConfig`.`username`, `dbConfig`.`password`) instead of hardcoding them in config.php.
Your `config.php` can be updated to reference the environment variables.

Example:
```php
$dbConfig = [
'type' => 'mysql',
'host' => getenv('DB_HOST'),
'dbname' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASSWORD'),
'flatfile_path' => '/path/to/flatfile.db'
];
```


### File Permissions
Ensure that `config.php` and any other sensitive files outside the web root are only readable by the web server user (chmod 600).
For directories containing flatfile databases, restrict permissions (chmod 700 or 750) to prevent unauthorised access.

### HTTPS (SSL)
Configure your server to use https. Many web hosts provide free SSL certificates via Let's Encrypt.
Consider forcing https via server config/`.httaccess`.

Example:
```apacheconf
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
```
Loading