Skip to content

Commit

Permalink
#45 sub 11 | Added sync chat command (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghpdohmen authored and kaenganxt committed Jan 24, 2020
1 parent fac2379 commit 7c16120
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/CSM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<Compile Include="Commands\Data\Internal\ConnectionResultCommand.cs" />
<Compile Include="Commands\Data\Internal\FinishTransactionCommand.cs" />
<Compile Include="Commands\Data\Internal\PlayerListCommand.cs" />
<Compile Include="Commands\Data\Internal\RequestWorldTransferCommand.cs" />
<Compile Include="Commands\Data\Internal\WorldTransferCommand.cs" />
<Compile Include="Commands\Data\Names\ChangeCityNameCommand.cs" />
<Compile Include="Commands\Data\Names\ChangeNameCommand.cs" />
Expand Down Expand Up @@ -191,6 +192,7 @@
<Compile Include="Commands\Handler\Internal\FinishTransactionHandler.cs" />
<Compile Include="Commands\Handler\Internal\PlayerListHandler.cs" />
<Compile Include="Commands\Handler\Internal\PlayerLocationHandler.cs" />
<Compile Include="Commands\Handler\Internal\RequestWorldTransferHandler.cs" />
<Compile Include="Commands\Handler\Internal\WorldTransferHandler.cs" />
<Compile Include="Commands\Handler\Names\ChangeCityNameHandler.cs" />
<Compile Include="Commands\Handler\Names\ChangeNameHandler.cs" />
Expand Down
14 changes: 14 additions & 0 deletions src/Commands/Data/Internal/RequestWorldTransferCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using ProtoBuf;

namespace CSM.Commands.Data.Internal
{
/// <summary>
/// Requests a world transfer/sync, only sent by clients
/// </summary>

[ProtoContract]
class RequestWorldTransferCommand : CommandBase
{

}
}
43 changes: 43 additions & 0 deletions src/Commands/Handler/Internal/RequestWorldTransferHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CSM.Commands.Data.Internal;
using CSM.Helpers;
using CSM.Networking;
using ColossalFramework.Threading;
using ColossalFramework.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using CSM.Networking.Status;

namespace CSM.Commands.Handler.Internal
{

class RequestWorldTransferHandler : CommandHandler<RequestWorldTransferCommand>
{
protected override void Handle(RequestWorldTransferCommand command)
{
//pause game
MultiplayerManager.Instance.GameBlocked = true;
SimulationManager.instance.SimulationPaused = true;
//saving world
SaveHelpers.SaveServerLevel();

new Thread(() =>
{
while (SaveHelpers.IsSaving())
{
Thread.Sleep(100);
}
//send the world to the client
Command.SendToClient(MultiplayerManager.Instance.CurrentServer.ConnectedPlayers[command.SenderId], new WorldTransferCommand
{
World = SaveHelpers.GetWorldFile()
});

MultiplayerManager.Instance.CurrentServer.ConnectedPlayers[command.SenderId].Status = ClientStatus.Loading;
}).Start();

}
}
}
14 changes: 13 additions & 1 deletion src/Panels/ChatLogPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,19 @@ public ChatLogPanel()
new ChatCommand("open-log", "Opens the multiplayer log.", (command) =>
{
Process.Start(Path.GetFullPath(".") + "/multiplayer-logs/log-current.txt");
})
}),
new ChatCommand("sync", "Redownloads the entire save", (command) => {
if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Client)
{
MultiplayerManager.Instance.CurrentClient.Status = Networking.Status.ClientStatus.Downloading;
PrintGameMessage("Requesting the save game from the server");
Command.SendToServer(new RequestWorldTransferCommand());
}
else {
PrintGameMessage("You are the server");
}
}
)
};
}

Expand Down

0 comments on commit 7c16120

Please sign in to comment.