Skip to content

Commit 03498e8

Browse files
committed
Moved stuff a bit and finished implementing client side creation of game state
1 parent 3db6ce2 commit 03498e8

4 files changed

Lines changed: 54 additions & 55 deletions

File tree

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using HarmonyLib;
22
using Il2CppMicrosoft.AspNetCore.SignalR.Client;
3-
using PolyMod.ViewModels;
3+
using PolyMod.Multiplayer.ViewModels;
44
using Polytopia.Data;
55
using PolytopiaBackendBase;
66
using PolytopiaBackendBase.Common;
@@ -9,9 +9,9 @@
99
using UnityEngine;
1010
using Newtonsoft.Json;
1111

12-
namespace PolyMod.Managers;
12+
namespace PolyMod.Multiplayer;
1313

14-
public static class Multiplayer
14+
public static class Client
1515
{
1616
internal const string DEFAULT_SERVER_URL = "https://dev.polydystopia.xyz";
1717
internal const string LOCAL_SERVER_URL = "http://localhost:5051/";
@@ -24,13 +24,13 @@ public static class Multiplayer
2424

2525
internal static void Init()
2626
{
27-
Harmony.CreateAndPatchAll(typeof(Multiplayer));
27+
Harmony.CreateAndPatchAll(typeof(Client));
2828
BuildConfig buildConfig = BuildConfigHelper.GetSelectedBuildConfig();
2929
buildConfig.buildServerURL = BuildServerURL.Custom;
3030
buildConfig.customServerURL = LOCAL_SERVER_URL;
3131

32-
Plugin.logger.LogInfo($"Server URL set to: {Plugin.config.backendUrl}");
33-
Plugin.logger.LogInfo("GLD patches applied");
32+
Plugin.logger.LogInfo($"Multiplayer> Server URL set to: {Plugin.config.backendUrl}");
33+
Plugin.logger.LogInfo("Multiplayer> GLD patches applied");
3434
}
3535

3636
[HarmonyPostfix]
@@ -187,35 +187,44 @@ private static void Deserialize_Postfix(GameState __instance, BinaryReader __0)
187187

188188
[HarmonyPrefix]
189189
[HarmonyPatch(typeof(BackendAdapter), nameof(BackendAdapter.StartLobbyGame))]
190-
private static bool BackendAdapter_StartLobbyGame(
190+
private static bool BackendAdapter_StartLobbyGame_Modded(
191191
ref Il2CppSystem.Threading.Tasks.Task<ServerResponse<LobbyGameViewModel>> __result,
192192
BackendAdapter __instance,
193193
StartLobbyBindingModel model)
194194
{
195-
Plugin.logger.LogInfo("BackendAdapter_StartLobbyGame");
196-
_ = HandleStartLobbyGameAsync(__instance, model);
197-
return true;
195+
Plugin.logger.LogInfo("Multiplayer> BackendAdapter_StartLobbyGame_Modded");
196+
var taskCompletionSource = new Il2CppSystem.Threading.Tasks.TaskCompletionSource<ServerResponse<LobbyGameViewModel>>();
197+
198+
_ = HandleStartLobbyGameModded(taskCompletionSource, __instance, model);
199+
200+
__result = taskCompletionSource.Task;
201+
202+
return false;
198203
}
199204

200-
private static async Task HandleStartLobbyGameAsync(BackendAdapter instance, StartLobbyBindingModel model)
205+
private static async System.Threading.Tasks.Task HandleStartLobbyGameModded(
206+
Il2CppSystem.Threading.Tasks.TaskCompletionSource<ServerResponse<LobbyGameViewModel>> tcs,
207+
BackendAdapter instance,
208+
StartLobbyBindingModel model)
201209
{
202210
try
203211
{
204212
var lobbyResponse = await PolytopiaBackendAdapter.Instance.GetLobby(new GetLobbyBindingModel
205213
{
206214
LobbyId = model.LobbyId
207215
});
208-
Plugin.logger.LogInfo($"Lobby processed {lobbyResponse.Success}");
216+
217+
Plugin.logger.LogInfo($"Multiplayer> Lobby processed {lobbyResponse.Success}");
209218
LobbyGameViewModel lobbyGameViewModel = lobbyResponse.Data;
210-
Plugin.logger.LogInfo("Lobby received");
219+
Plugin.logger.LogInfo("Multiplayer> Lobby received");
211220

212221
(byte[] serializedGameState, string gameSettingsJson) = CreateMultiplayerGame(
213222
lobbyGameViewModel,
214223
VersionManager.GameVersion,
215224
VersionManager.GameLogicDataVersion
216225
);
217226

218-
Plugin.logger.LogInfo("Game data created");
227+
Plugin.logger.LogInfo("Multiplayer> GameState and Settiings created");
219228

220229
var setupGameDataViewModel = new SetupGameDataViewModel
221230
{
@@ -226,48 +235,40 @@ private static async Task HandleStartLobbyGameAsync(BackendAdapter instance, Sta
226235

227236
var setupData = System.Text.Json.JsonSerializer.Serialize(setupGameDataViewModel);
228237

229-
var serverResponse = await instance.HubConnection.InvokeAsync<ServerResponse<BoolResponseViewModel>>(
230-
"SetupGameData",
238+
var serverResponse = await instance.HubConnection.InvokeAsync<ServerResponse<LobbyGameViewModel>>(
239+
"StartLobbyGameModded",
231240
setupData,
232241
Il2CppSystem.Threading.CancellationToken.None
233242
);
234-
235-
Plugin.logger.LogInfo("Setup complete: " + serverResponse.Success);
243+
Plugin.logger.LogInfo("Multiplayer> Invoked StartLobbyGameModded");
244+
tcs.SetResult(serverResponse);
236245
}
237246
catch (Exception ex)
238247
{
239-
Plugin.logger.LogInfo("Error: " + ex.Message);
248+
Plugin.logger.LogError("Multiplayer> Error during HandleStartLobbyGameModded: " + ex.Message);
249+
tcs.SetException(new Il2CppSystem.Exception(ex.Message));
240250
}
241251
}
242252

243253
public static (byte[] serializedGameState, string gameSettingsJson) CreateMultiplayerGame(LobbyGameViewModel lobby,
244254
int gameVersion, int gameLogicVersion)
245255
{
246-
Console.Write(1);
247-
Console.Write(lobby == null);
248256
var lobbyMapSize = lobby.MapSize;
249-
Console.Write(11);
250257
var settings = new GameSettings();
251-
Console.Write(111);
252258
settings.ApplyLobbySettings(lobby);
253-
Console.Write(111);
254259
if (settings.LiveGamePreset)
255260
{
256261
settings.SetLiveModePreset();
257262
}
258-
Console.Write(3);
259263
foreach (var participatorViewModel in lobby.Participators)
260264
{
261-
Console.Write(4);
262-
if (participatorViewModel.SelectedTribe == 0) participatorViewModel.SelectedTribe = 2; //TODO: Remove later
263-
264265
var humanPlayer = new PlayerData
265266
{
266267
type = PlayerDataType.LocalUser,
267268
state = PlayerDataFriendshipState.Accepted,
268269
knownTribe = true,
269270
tribe = (TribeType)participatorViewModel.SelectedTribe,
270-
tribeMix = (TribeType)participatorViewModel.SelectedTribe, //?
271+
tribeMix = (TribeType)participatorViewModel.SelectedTribe,
271272
skinType = (SkinType)participatorViewModel.SelectedTribeSkin,
272273
defaultName = participatorViewModel.GetNameInternal()
273274
};
@@ -277,9 +278,8 @@ public static (byte[] serializedGameState, string gameSettingsJson) CreateMultip
277278
humanPlayer.profile.avatarState = avatarState;
278279

279280
settings.AddPlayer(humanPlayer);
280-
Console.Write(5);
281281
}
282-
Console.Write(6);
282+
283283
foreach (var botDifficulty in lobby.Bots)
284284
{
285285
var botGuid = Il2CppSystem.Guid.NewGuid();
@@ -294,7 +294,7 @@ public static (byte[] serializedGameState, string gameSettingsJson) CreateMultip
294294
};
295295
;
296296
botPlayer.botDifficulty = (BotDifficulty)botDifficulty;
297-
botPlayer.skinType = SkinType.Default; //TODO
297+
botPlayer.skinType = SkinType.Default;
298298
botPlayer.defaultName = "Bot" + botGuid;
299299
botPlayer.profile.id = botGuid;
300300

@@ -330,7 +330,7 @@ public static (byte[] serializedGameState, string gameSettingsJson) CreateMultip
330330
skinType = player.skinType
331331
};
332332
gameState.PlayerStates.Add(playerState);
333-
Plugin.logger.LogInfo($"Created player: {playerState}");
333+
Plugin.logger.LogInfo($"Multiplayer> Created player: {playerState}");
334334
}
335335
else
336336
{
@@ -341,35 +341,34 @@ public static (byte[] serializedGameState, string gameSettingsJson) CreateMultip
341341

342342
GameStateUtils.SetPlayerColors(gameState);
343343
GameStateUtils.AddNaturePlayer(gameState);
344-
Plugin.logger.LogInfo("Creating world...");
344+
345+
Plugin.logger.LogInfo("Multiplayer> Creating world...");
346+
345347
ushort num = (ushort)Math.Max(lobbyMapSize,
346348
(int)MapDataExtensions.GetMinimumMapSize(gameState.PlayerCount));
347349
gameState.Map = new MapData(num, num);
348350
MapGeneratorSettings generatorSettings = settings.GetMapGeneratorSettings();
349351
new MapGenerator().Generate(gameState, generatorSettings);
350-
Plugin.logger.LogInfo($"Creating initial state for {gameState.PlayerCount} players...");
351352

352-
foreach (PlayerState playerState3 in gameState.PlayerStates)
353+
Plugin.logger.LogInfo($"Multiplayer> Creating initial state for {gameState.PlayerCount} players...");
354+
355+
foreach (PlayerState player in gameState.PlayerStates)
353356
{
354-
foreach (PlayerState playerState4 in gameState.PlayerStates)
355-
playerState3.aggressions[playerState4.Id] = 0;
356-
if (playerState3.Id != byte.MaxValue)
357+
foreach (PlayerState otherPlayer in gameState.PlayerStates)
358+
player.aggressions[otherPlayer.Id] = 0;
359+
360+
if (player.Id != byte.MaxValue && gameState.GameLogicData.TryGetData(player.tribe, out TribeData tribeData))
357361
{
358-
playerState3.Currency = 55;
359-
TribeData data3;
360-
UnitData data4;
361-
if (gameState.GameLogicData.TryGetData(playerState3.tribe, out data3) &&
362-
gameState.GameLogicData.TryGetData(data3.startingUnit.type, out data4))
363-
{
364-
TileData tile = gameState.Map.GetTile(playerState3.startTile);
365-
UnitState unitState = ActionUtils.TrainUnitScored(gameState, playerState3, tile, data4);
366-
unitState.attacked = false;
367-
unitState.moved = false;
368-
}
362+
player.Currency = tribeData.startingStars;
363+
TileData tile = gameState.Map.GetTile(player.startTile);
364+
UnitState unitState = ActionUtils.TrainUnitScored(gameState, player, tile, tribeData.startingUnit);
365+
unitState.attacked = false;
366+
unitState.moved = false;
369367
}
370368
}
371369

372-
Plugin.logger.LogInfo("Session created successfully");
370+
Plugin.logger.LogInfo("Multiplayer> Session created successfully");
371+
373372
gameState.CommandStack.Add((CommandBase)new StartMatchCommand((byte)1));
374373

375374
var serializedGameState = SerializationHelpers.ToByteArray(gameState, gameState.Version);

src/ViewModels/IMonoServerResponseData.cs renamed to src/Multiplayer/ViewModels/IMonoServerResponseData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace PolyMod.ViewModels;
1+
namespace PolyMod.Multiplayer.ViewModels;
22

33
public interface IMonoServerResponseData
44
{

src/ViewModels/SetupGameDataViewModel.cs renamed to src/Multiplayer/ViewModels/SetupGameDataViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
namespace PolyMod.ViewModels;
2+
namespace PolyMod.Multiplayer.ViewModels;
33
public class SetupGameDataViewModel : IMonoServerResponseData
44
{
55
public string lobbyId { get; set; }

src/Plugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal record PolyConfig(
2525
bool autoUpdate = true,
2626
bool updatePrerelease = false,
2727
bool allowUnsafeIndexes = false,
28-
string backendUrl = Multiplayer.DEFAULT_SERVER_URL,
28+
string backendUrl = Multiplayer.Client.DEFAULT_SERVER_URL,
2929
string overrideDeviceId = ""
3030
);
3131

@@ -134,7 +134,7 @@ public override void Load()
134134
Hub.Init();
135135

136136
Main.Init();
137-
Multiplayer.Init();
137+
Multiplayer.Client.Init();
138138
}
139139

140140
/// <summary>

0 commit comments

Comments
 (0)