Skip to content

Commit

Permalink
Fixes for save data syncing, warp on connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Apr 6, 2024
1 parent 73f6266 commit 545af94
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 87 deletions.
20 changes: 17 additions & 3 deletions HKMP/Game/Client/ClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ internal class ClientManager : IClientManager {
/// </summary>
private readonly Dictionary<ushort, ClientPlayerData> _playerData;

/// <summary>
/// Whether we are automatically connected to an in-game hosted server.
/// This is used to determine whether to apply save data from the server to the client and warp them to a bench.
/// </summary>
private bool _autoConnect;

#endregion

#region IClientManager properties
Expand Down Expand Up @@ -236,8 +242,11 @@ ModSettings modSettings
packetManager.RegisterClientPacketHandler<ChatMessage>(ClientPacketId.ChatMessage, OnChatMessage);

// Register handlers for events from UI
uiManager.ConnectInterface.ConnectButtonPressed += Connect;
uiManager.ConnectInterface.DisconnectButtonPressed += () => Disconnect();
uiManager.ConnectInterface.ConnectButtonPressed += (address, port, username, autoConnect) => {
_autoConnect = autoConnect;
Connect(address, port, username);
};
uiManager.ConnectInterface.DisconnectButtonPressed += Disconnect;
uiManager.SettingsInterface.OnTeamRadioButtonChange += InternalChangeTeam;
uiManager.SettingsInterface.OnSkinIdChange += InternalChangeSkin;

Expand Down Expand Up @@ -326,6 +335,8 @@ public void Disconnect() {
/// Internal logic for disconnecting from the server.
/// </summary>
private void InternalDisconnect() {
_autoConnect = false;

_netClient.Disconnect();

// Let the player manager know we disconnected
Expand Down Expand Up @@ -511,7 +522,10 @@ private void OnClientConnect(LoginResponse loginResponse) {
private void OnHelloClient(HelloClient helloClient) {
Logger.Info("Received HelloClient from server");

_saveManager.SetSaveWithData(helloClient.CurrentSave);
// If this was not an auto-connect, we set save data. Otherwise, we know we already have the save data.
if (!_autoConnect) {
_saveManager.SetSaveWithData(helloClient.CurrentSave);
}

// Fill the player data dictionary with the info from the packet
foreach (var (id, username) in helloClient.ClientInfo) {
Expand Down
Loading

0 comments on commit 545af94

Please sign in to comment.