Skip to content

Commit

Permalink
#162 SIGame: fix error while opening help
Browse files Browse the repository at this point in the history
Fix other issues too.
  • Loading branch information
VladimirKhil committed Dec 2, 2023
1 parent 072cc6d commit 3b3ee31
Show file tree
Hide file tree
Showing 25 changed files with 186 additions and 118 deletions.
28 changes: 14 additions & 14 deletions deploy/SIGame.Setup/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@
<Component Guid="{1627896d-65da-47fa-9802-a3cfe67cb0aa}">
<File KeyPath="yes" Source="..\dlls\$(var.Platform)\api-ms-win-core-winrt-l1-1-0.dll" />
</Component>
<Component Guid="{4687DB8D-2AA3-46AD-8B85-A8469390E198}">
<File KeyPath="yes" Source="$(var.PublishFolder)runtimes\win-$(var.Platform)\native\WebView2Loader.dll" />
</Component>
<Component Guid="{B6AFAEC8-30A0-4BD9-836A-920D1190DAF9}">
<File KeyPath="yes" Source="$(var.PublishFolder)aspnetcorev2_inprocess.dll" />
</Component>
<Component Guid="{9E128CD4-80CB-4268-832B-C2D07E36E03D}">
<File KeyPath="yes" Source="$(var.PublishFolder)appsettings.json" />
</Component>
<Component Guid="{84D30ED4-930D-436F-8287-A8FDB01860FA}">
<File KeyPath="yes" Source="$(var.PublishFolder)appsettings.Production.json" />
</Component>
<Component Guid="{4687DB8D-2AA3-46AD-8B85-A8469390E198}">
<File KeyPath="yes" Source="$(var.PublishFolder)runtimes\win-$(var.Platform)\native\WebView2Loader.dll" />
</Component>
<Component Guid="{B6AFAEC8-30A0-4BD9-836A-920D1190DAF9}">
<File KeyPath="yes" Source="$(var.PublishFolder)aspnetcorev2_inprocess.dll" />
</Component>
<Component Guid="{9E128CD4-80CB-4268-832B-C2D07E36E03D}">
<File KeyPath="yes" Source="$(var.PublishFolder)appsettings.json" />
</Component>
<Component Guid="{84D30ED4-930D-436F-8287-A8FDB01860FA}">
<File KeyPath="yes" Source="$(var.PublishFolder)appsettings.Production.json" />
</Component>
<Component Guid="{10301C79-C85C-4FD5-94D8-A23533BE7D79}">
<File KeyPath="yes" Source="$(var.PublishFolder)Help_ru.xps" />
<File KeyPath="yes" Source="$(var.PublishFolder)Help_ru.pdf" />
</Component>
<Component Guid="{ADDF321D-6B06-412B-8096-3911FF966799}">
<File KeyPath="yes" Source="$(var.PublishFolder)Help_en.xps" />
<File KeyPath="yes" Source="$(var.PublishFolder)Help_en.pdf" />
</Component>
<Component Id="RegistryPath" Guid="{b8ad70bb-c94c-4e11-af70-c6eaf427fb1c}">
<RegistryValue
Expand Down
7 changes: 6 additions & 1 deletion src/Common/SI.GameServer.Client/GameServerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public async Task OpenAsync(string userName, CancellationToken cancellationToken
{
options.AccessTokenProvider = () => Task.FromResult<string?>(Convert.ToBase64String(Encoding.UTF8.GetBytes(userName)));
})
.WithAutomaticReconnect()
.WithAutomaticReconnect(new ReconnectPolicy())
.AddMessagePackProtocol()
.Build();

Expand Down Expand Up @@ -387,4 +387,9 @@ private void OnUI(Action action)
}

private void MessageHandler_HttpSendProgress(object? sender, HttpProgressEventArgs e) => UploadProgress?.Invoke(e.ProgressPercentage);

private sealed class ReconnectPolicy : IRetryPolicy
{
public TimeSpan? NextRetryDelay(RetryContext retryContext) => TimeSpan.FromSeconds(5);
}
}
103 changes: 77 additions & 26 deletions src/Common/SI.GameServer.Client/SIHostClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public sealed class SIHostClient : IGameClient
private readonly HubConnection _connection;
private readonly SIHostClientOptions _options;

private JoinGameRequest? _joinGameRequest;

public event Action<Message>? IncomingMessage;

public event Func<Exception?, Task>? Reconnecting;
Expand All @@ -28,22 +30,8 @@ public SIHostClient(HubConnection connection, SIHostClientOptions options)
_connection = connection;
_options = options;

_connection.Reconnecting += async e =>
{
if (Reconnecting != null)
{
await Reconnecting(e);
}
};

_connection.Reconnected += async s =>
{
if (Reconnected != null)
{
await Reconnected(s);
}
};

_connection.Reconnecting += OnReconnecting;
_connection.Reconnected += OnReconnected;
_connection.Closed += OnConnectionClosedAsync;

_connection.On<Message>("Receive", (message) => IncomingMessage?.Invoke(message));
Expand All @@ -56,6 +44,22 @@ public SIHostClient(HubConnection connection, SIHostClientOptions options)
});
}

private async Task OnReconnecting(Exception? ex)
{
if (Reconnecting != null)
{
await Reconnecting(ex);
}
}

private async Task OnReconnected(string? connectionId)
{
if (Reconnected != null)
{
await Reconnected(connectionId);
}
}

public static async Task<SIHostClient> CreateAsync(SIHostClientOptions options, CancellationToken cancellationToken)
{
var serviceUri = options.ServiceUri?.ToString() ?? "";
Expand All @@ -67,7 +71,7 @@ public static async Task<SIHostClient> CreateAsync(SIHostClientOptions options,

var connection = new HubConnectionBuilder()
.WithUrl($"{serviceUri}sihost")
.WithAutomaticReconnect()
.WithAutomaticReconnect(new ReconnectPolicy())
.AddMessagePackProtocol()
.Build();

Expand All @@ -78,23 +82,70 @@ public static async Task<SIHostClient> CreateAsync(SIHostClientOptions options,
return new SIHostClient(connection, options);
}

public Task<JoinGameResponse> JoinGameAsync(
public async Task<JoinGameResponse> JoinGameAsync(
JoinGameRequest joinGameRequest,
CancellationToken cancellationToken = default) =>
_connection.InvokeAsync<JoinGameResponse>("JoinGame", joinGameRequest, cancellationToken);
CancellationToken cancellationToken = default)
{
var response = await _connection.InvokeAsync<JoinGameResponse>("JoinGame", joinGameRequest, cancellationToken);

if (response.IsSuccess)
{
_joinGameRequest = joinGameRequest;
}

return response;
}

public Task SendMessageAsync(Message message, CancellationToken cancellationToken = default) =>
_connection.InvokeAsync("SendMessage", message, cancellationToken);
_connection.SendAsync("SendMessage", message, cancellationToken);

public Task LeaveGameAsync(CancellationToken cancellationToken = default) =>
_connection.InvokeAsync("LeaveGame", cancellationToken);
_connection.SendAsync("LeaveGame", cancellationToken);

private Task OnConnectionClosedAsync(Exception? exc)
private async Task OnConnectionClosedAsync(Exception? exc)
{
// TODO: recreate connection and retry
if (await TryReconnectAsync())
{
return;
}

if (Closed != null)
{
await Closed(exc);
}
}

private async Task<bool> TryReconnectAsync()
{
if (_joinGameRequest == null)
{
return false;
}

return Closed != null ? Closed(exc) : Task.CompletedTask;
try
{
await Task.Delay(5000);
await _connection.StartAsync();
await JoinGameAsync(_joinGameRequest);
return true;
}
catch
{
return false;
}
}

public async ValueTask DisposeAsync()
{
_connection.Reconnecting -= OnReconnecting;
_connection.Reconnected -= OnReconnected;
_connection.Closed -= OnConnectionClosedAsync;

await _connection.DisposeAsync();
}

public ValueTask DisposeAsync() => _connection.DisposeAsync();
private sealed class ReconnectPolicy : IRetryPolicy
{
public TimeSpan? NextRetryDelay(RetryContext retryContext) => TimeSpan.FromSeconds(5);
}
}
2 changes: 1 addition & 1 deletion src/Common/SIUI.ViewModel/TableInfoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public bool Enabled
private double _volume = 0.5;

/// <summary>
/// Sound volume level.
/// Sound volume level (from 0 to 1).
/// </summary>
public double Volume
{
Expand Down
28 changes: 21 additions & 7 deletions src/Common/SIUI/Behaviors/MediaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ namespace SIUI.Behaviors;

public static class MediaController
{
/// <summary>
/// Current volume level.
/// </summary>
public static double Volume { get; private set; } = 0.5;

static MediaController()
{
if (waveOutGetVolume(IntPtr.Zero, out uint dwVolume) == 0)
{
// Setting default volume level from mixer (dividing it by half because default volume is 0.5)
Volume = ((double)Math.Max(dwVolume >> 16, dwVolume & 0x0000FFFF)) / 0xFFFF / 2;
}
}

public static void UpdateVolume(double factor)
{
Volume *= factor;
_ = waveOutSetVolume(IntPtr.Zero, (uint)(0xFFFF * 2 * Volume));
}

private static readonly DependencyPropertyDescriptor SourceDescriptor =
DependencyPropertyDescriptor.FromProperty(MediaElement.SourceProperty, typeof(MediaElement));

Expand All @@ -29,12 +49,7 @@ public static void OnLoadHandlerChanged(DependencyObject d, DependencyPropertyCh
}

mediaElement.LoadedBehavior = MediaState.Manual;

if (waveOutGetVolume(IntPtr.Zero, out uint dwVolume) == 0)
{
// Setting default volume level from mixer (dividing it by half because default volume is 0.5)
mediaElement.Volume = ((double)Math.Max(dwVolume >> 16, dwVolume & 0x0000FFFF)) / 0xFFFF / 2;
}
mediaElement.Volume = Volume;

void loaded(object? sender, EventArgs e2)
{
Expand Down Expand Up @@ -113,7 +128,6 @@ void resumeHandler()
void volumeChangedHandler(double volume)
{
mediaElement.Volume *= volume;
waveOutSetVolume(IntPtr.Zero, (uint)(0xFFFF * 2 * mediaElement.Volume));
}

tableInfo.MediaSeek += seekHandler;
Expand Down
2 changes: 2 additions & 0 deletions src/SICore/SICore/Clients/Game/GameLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,9 +1632,11 @@ override protected void ExecuteTask(int taskId, int arg)

if (stop)
{
_tasksHistory.AddLogEntry("Execution stopped");
return;
}

_tasksHistory.AddLogEntry($"Execution proceed with task {newTask}");
task = newTask;
}

Expand Down
6 changes: 5 additions & 1 deletion src/SICore/SICore/Clients/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public Player(Client client, Account personData, bool isHost, ILocalizer localiz
{
ClientData.PlayerDataExtensions.PressGameButton = new CustomCommand(arg =>
{
_viewerActions.SendMessage(Messages.I);
var tryStartTime = ClientData.PlayerDataExtensions.TryStartTime;
var pressDuration = tryStartTime.HasValue ? (int)DateTimeOffset.UtcNow.Subtract(tryStartTime.Value).TotalMilliseconds : -1;

_viewerActions.SendMessageWithArgs(Messages.I, pressDuration);
DisableGameButton(false);
ReleaseGameButton();
}) { CanBeExecuted = true };
Expand Down Expand Up @@ -282,6 +285,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams
case Messages.Try:
ClientData.PlayerDataExtensions.Pass.CanBeExecuted = true;
ClientData.PlayerDataExtensions.Apellate.CanBeExecuted = false;
ClientData.PlayerDataExtensions.TryStartTime = DateTimeOffset.UtcNow;
break;

case Messages.YouTry:
Expand Down
2 changes: 1 addition & 1 deletion src/SICore/SICore/Clients/Player/PlayerComputerLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private int GetTimePercentage(int timerIndex)
return (int)(100 * (now - timer.StartTime).TotalMilliseconds / (timer.EndTime - timer.StartTime).TotalMilliseconds);
}

private void PressButton() => _viewerActions.PressGameButton();
private void PressButton() => _data.PlayerDataExtensions.PressGameButton.Execute(null);

private void AnswerRight() => _viewerActions.SendMessage(Messages.IsRight, "+");

Expand Down
5 changes: 5 additions & 0 deletions src/SICore/SICore/Clients/Player/PlayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public bool MyTry
set { _myTry = value; OnPropertyChanged(); }
}

/// <summary>
/// Defines time stamp when game buttons havebeen activated.
/// </summary>
public DateTimeOffset? TryStartTime { get; set; }

public event Action? PressButton;

public event PropertyChangedEventHandler? PropertyChanged;
Expand Down
5 changes: 0 additions & 5 deletions src/SICore/SICore/Clients/Viewer/ViewerActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,4 @@ public void SendMessage(params string[] args) =>

public void SendMessageWithArgs(params object[] args) =>
Client.SendMessage(string.Join(Message.ArgsSeparator, args), receiver: NetworkConstants.GameName);

/// <summary>
/// Жмёт на игровую кнопку
/// </summary>
internal void PressGameButton() => SendMessage(Messages.I);
}
Binary file added src/SIGame/SIGame.ViewModel/Help_en.pdf
Binary file not shown.
Binary file removed src/SIGame/SIGame.ViewModel/Help_en.xps
Binary file not shown.
Binary file added src/SIGame/SIGame.ViewModel/Help_ru.pdf
Binary file not shown.
Binary file removed src/SIGame/SIGame.ViewModel/Help_ru.xps
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public abstract class PlatformManager : IUIThreadExecutor

public IServiceProvider? ServiceProvider { get; set; }

public abstract double Volume { get; }

protected PlatformManager()
{
Instance = this;
Expand Down Expand Up @@ -62,4 +64,6 @@ protected PlatformManager()
public abstract void ShowDialogWindow(object dataContext, Action onClose);

public abstract void CloseDialogWindow();

public abstract void UpdateVolume(double factor);
}
4 changes: 2 additions & 2 deletions src/SIGame/SIGame.ViewModel/SIGame.ViewModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
</ItemGroup>

<ItemGroup>
<None Update="Help_en.xps">
<None Update="Help_en.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Help_ru.xps">
<None Update="Help_ru.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions src/SIGame/SIGame.ViewModel/ViewModel/GameCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ private static void OpenLogs_Executed(object? arg)

private static void Comment_Executed(object? arg)
{
var commentUri = Uri.EscapeDataString(Resources.FeedbackLink);

try
{
Browser.Open(commentUri);
Browser.Open(Resources.FeedbackLink);
}
catch (Exception exc)
{
Expand Down
12 changes: 11 additions & 1 deletion src/SIGame/SIGame.ViewModel/ViewModel/GameViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,18 @@ public string? Ad

public UserSettings UserSettings { get; }

/// <summary>
/// Sound volume level.
/// </summary>
public double Volume
{
get => TInfo.Volume * 100;
set { TInfo.Volume = Math.Max(1, value) / 100; }
set
{
var currentValue = TInfo.Volume;
TInfo.Volume = Math.Min(100, Math.Max(1, value)) / 100;
PlatformManager.Instance.UpdateVolume(TInfo.Volume / currentValue);
}
}

/// <summary>
Expand Down Expand Up @@ -157,6 +165,8 @@ public GameViewModel(
Host.Ad += Host_Ad;
Host.IsPausedChanged += Host_IsPausedChanged;

TInfo.Volume = PlatformManager.Instance.Volume;

UserSettings = userSettings;

ChangePauseInGame = new CustomCommand(ChangePauseInGame_Executed) { CanBeExecuted = false };
Expand Down
Loading

0 comments on commit 3b3ee31

Please sign in to comment.