Skip to content

Commit

Permalink
Continue to implement SImulator Web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirKhil committed May 11, 2024
1 parent 7adf1c6 commit 81af7c5
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal interface ISelectionStrategy
/// <summary>
/// Moves the game forward.
/// </summary>
void MoveNext() { }
void MoveNext() { } // TODO: can this method return selected question so the selection callback could be removed?

/// <summary>
/// Can the game move backwards.
Expand Down
10 changes: 10 additions & 0 deletions src/Common/Utils.Wpf/Behaviors/WebView2Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private static void WebView2_Unloaded(object sender, RoutedEventArgs e)
if (webView2.DataContext is IWebInterop webInterop)
{
webInterop.SendJsonMessage -= coreWebView.PostWebMessageAsJson;
webView2.WebMessageReceived -= WebView2_WebMessageReceived;
}
}
}
Expand All @@ -96,11 +97,20 @@ private static async void UpdateWebView2Environment(WebView2 webView2)
if (webView2.DataContext is IWebInterop webInterop)
{
webInterop.SendJsonMessage += webView2.CoreWebView2.PostWebMessageAsJson;
webView2.WebMessageReceived += WebView2_WebMessageReceived;
}
}
catch (Exception exc)
{
Trace.TraceError(exc.ToString());
}
}

private static void WebView2_WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
{
if (sender is WebView2 webView2 && webView2.DataContext is IWebInterop webInterop)
{
webInterop.OnMessage(e.WebMessageAsJson);
}
}
}
2 changes: 2 additions & 0 deletions src/Common/Utils/Web/IWebInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public interface IWebInterop
{
event Action<string> SendJsonMessage;

void OnMessage(string webMessageAsJson) { }
}
4 changes: 2 additions & 2 deletions src/SICore/SICore/Clients/Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ private void OnToggle(Message message, string[] args)
{
if (ClientData.TableInformStage > 1)
{
_gameActions.InformRoundThemes(play: false);
_gameActions.InformRoundThemes();
_gameActions.InformTable();
}
},
Expand Down Expand Up @@ -1339,7 +1339,7 @@ private void OnInfo(string person)
{
if (ClientData.TableInformStage > 0)
{
_gameActions.InformRoundThemes(person, false);
_gameActions.InformRoundThemes(person);

if (ClientData.TableInformStage > 1)
{
Expand Down
9 changes: 7 additions & 2 deletions src/SICore/SICore/Clients/Game/GameActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using SICore.Clients;
using SICore.Contracts;
using SICore.Extensions;
using SICore.Models;
using SICore.Network;
using SICore.Network.Clients;
using SICore.Utils;
using SIData;
using SIPackages;
using SIPackages.Core;
Expand Down Expand Up @@ -161,15 +163,18 @@ public void InformStage(string person = NetworkConstants.Everybody, string? name
public void InformStageInfo(string person, int stageIndex) =>
SendMessageToWithArgs(person, Messages.StageInfo, _gameData.Stage.ToString(), _gameData.Round?.Name ?? "", stageIndex);

internal void InformRoundThemes(string person = NetworkConstants.Everybody, bool play = true)
internal void InformRoundThemes(string person = NetworkConstants.Everybody, ThemesPlayMode playMode = ThemesPlayMode.None)
{
var msg = new StringBuilder(Messages.RoundThemes)
.Append(Message.ArgsSeparatorChar)
.Append(play ? '+' : '-')
.Append(playMode != ThemesPlayMode.None ? '+' : '-')
.Append(Message.ArgsSeparatorChar)
.Append(string.Join(Message.ArgsSeparator, _gameData.TInfo.RoundInfo.Select(info => info.Name)));

SendMessage(msg.ToString(), person);

var messageBuilder = new MessageBuilder(Messages.RoundThemes2, playMode).AddRange(_gameData.TInfo.RoundInfo.Select(info => info.Name));
SendMessage(messageBuilder.ToString(), person);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/SICore/SICore/Clients/Game/GameLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private void SetRoundPrices(Round round)
ClientData.StakeStep = (int)Math.Pow(10, Math.Floor(Math.Log10(_minRoundPrice))); // Maximum power of 10 <= _minRoundPrice
}

internal void InitThemes(IEnumerable<Theme> themes, bool willPlayAllThemes, bool isFirstPlay)
internal void InitThemes(IEnumerable<Theme> themes, bool willPlayAllThemes, bool isFirstPlay, ThemesPlayMode playMode)
{
_data.TInfo.RoundInfo.Clear();

Expand All @@ -312,7 +312,7 @@ internal void InitThemes(IEnumerable<Theme> themes, bool willPlayAllThemes, bool

_data.TableInformStageLock.WithLock(() =>
{
_gameActions.InformRoundThemes();
_gameActions.InformRoundThemes(playMode: playMode);
_data.TableInformStage = 1;
},
5000);
Expand Down
4 changes: 2 additions & 2 deletions src/SICore/SICore/Clients/Game/PlayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void OnRoundThemes(IReadOnlyList<Theme> themes, IRoundTableController tab
}

// _gameData.TInfo.RoundInfo is initialized here
GameLogic?.InitThemes(themes, false, true);
GameLogic?.InitThemes(themes, false, true, Models.ThemesPlayMode.OneByOne);

// Filling initial questions table
_gameData.ThemeInfoShown.Clear();
Expand Down Expand Up @@ -116,7 +116,7 @@ public void OnFinalThemes(IReadOnlyList<Theme> themes, bool willPlayAllThemes, b

_gameData.AnnounceAnswer = true; // initialization

GameLogic?.InitThemes(themes, willPlayAllThemes, isFirstPlay);
GameLogic?.InitThemes(themes, willPlayAllThemes, isFirstPlay, Models.ThemesPlayMode.AllTogether);
_gameData.ThemeDeleters = new ThemeDeletersEnumerator(_gameData.Players, _gameData.TInfo.RoundInfo.Count(t => t.Name != null));
_gameData.ThemeDeleters.Reset(true);
GameLogic?.ScheduleExecution(Tasks.MoveNext, 30 + Random.Shared.Next(10));
Expand Down
6 changes: 6 additions & 0 deletions src/SICore/SICore/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ public static class Messages
[IdempotencyRequired]
public const string RoundThemes = "ROUNDTHEMES";

/// <summary>
/// Round themes names.
/// </summary>
[IdempotencyRequired]
public const string RoundThemes2 = "ROUND_THEMES2";

/// <summary>
/// Gives move to player.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/SICore/SICore/Models/ThemesPlayMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SICore.Models;

internal enum ThemesPlayMode
{
None,
OneByOne,
AllTogether
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ bool OnQuestionContent(
IReadOnlyCollection<ContentItem> content,
Func<ContentItem, string?> tryGetMediaUri,
string? textToShow);

void FinishQuestion() { }

void SetQuestionType(string typeName, string aliasName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void OnAskAnswer(string mode)

case StepParameterValues.AskAnswerMode_Direct:
GameViewModel.AskAnswerDirect();
GameViewModel.State = Model.QuestionState.Thinking;
GameViewModel.State = QuestionState.Thinking;
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,5 +736,7 @@ public bool OnQuestionContent(
return hasMedia;
}

public void SetQuestionType(string typeName, string aliasName) => SetText(aliasName);

public event PropertyChangedEventHandler? PropertyChanged;
}
Loading

0 comments on commit 81af7c5

Please sign in to comment.