Skip to content

Commit

Permalink
Send screen content type sequence to clients for answer options layout
Browse files Browse the repository at this point in the history
That allows the clients to organize their screen space wisely
  • Loading branch information
VladimirKhil committed Apr 20, 2024
1 parent c1a1895 commit bfe71e1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Common/SIEngine.Core/IQuestionEnginePlayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public interface IQuestionEnginePlayHandler
/// Sets answer options.
/// </summary>
/// <param name="answerOptions">Answer options.</param>
bool OnAnswerOptions(AnswerOption[] answerOptions);
/// <param name="screenContentSequence">Screen content sequence.</param>
bool OnAnswerOptions(AnswerOption[] answerOptions, IReadOnlyList<ContentItem[]> screenContentSequence);

/// <summary>
/// Shows question content.
Expand Down
36 changes: 35 additions & 1 deletion src/Common/SIEngine.Core/QuestionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,42 @@ public bool PlayNext()
continue;
}

var allTypes = new List<ContentItem[]>();

if (_question.Parameters != null)
{
foreach (var param in _question.Parameters)
{
if (param.Value.ContentValue != null)
{
var types = new List<ContentItem>();

foreach (var contentItem in param.Value.ContentValue)
{
if (contentItem.Placement != ContentPlacements.Screen)
{
continue;
}

types.Add(contentItem);

if (contentItem.WaitForFinish)
{
allTypes.Add(types.ToArray());
types.Clear();
}
}

if (types.Count > 0)
{
allTypes.Add(types.ToArray());
}
}
}
}

_isAnswerTypeSelect = true;
var setAnswerOptions = _playHandler.OnAnswerOptions(answerOptions.ToArray());
var setAnswerOptions = _playHandler.OnAnswerOptions(answerOptions.ToArray(), allTypes);
_stepIndex++;

if (setAnswerOptions)
Expand Down
18 changes: 15 additions & 3 deletions src/SICore/SICore/Clients/Game/GameLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4642,12 +4642,24 @@ internal void AcceptQuestion()
ScheduleExecution(Tasks.MoveNext, 20, 1);
}

internal void OnAnswerOptions(bool questionHasScreenContent, AnswerOption[] answerOptions)
internal void OnAnswerOptions()
{
var messageBuilder = new MessageBuilder(Messages.Layout).Add(MessageParams.Layout_AnswerOptions).Add(questionHasScreenContent ? '+' : '-');
messageBuilder.AddRange(answerOptions.Select(o => o.Content.Type));
var screenContentSequence = _data.QuestionPlayState.ScreenContentSequence;
var answerOptions = _data.QuestionPlayState.AnswerOptions;

if (answerOptions == null || screenContentSequence == null)
{
return;
}

var messageBuilder = new MessageBuilder(Messages.Layout)
.Add(MessageParams.Layout_AnswerOptions)
.Add(string.Join("|", screenContentSequence.Select(group => string.Join("+", group.Select(serializeContentItem)))))
.AddRange(answerOptions.Select(o => o.Content.Type));

_gameActions.SendMessage(messageBuilder.ToString());

static string serializeContentItem(ContentItem ci) => $"{ci.Type}{(ci.Type == ContentTypes.Text ? "." + ci.Value.Length : "")}";
}

internal void ShowAnswerOptions(Action? continuation)
Expand Down
11 changes: 6 additions & 5 deletions src/SICore/SICore/Clients/Game/QuestionPlayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ internal sealed class QuestionPlayHandler : IQuestionEnginePlayHandler

private GameData? GameData => GameLogic?.ClientData;

public bool OnAnswerOptions(AnswerOption[] answerOptions)
public bool OnAnswerOptions(AnswerOption[] answerOptions, IReadOnlyList<ContentItem[]> screenContentSequence)
{
if (GameLogic == null || GameData == null)
{
return false;
}

GameData.QuestionPlayState.AnswerOptions = answerOptions;
GameData.QuestionPlayState.ScreenContentSequence = screenContentSequence;
return false;
}

Expand All @@ -37,7 +38,7 @@ public void OnAskAnswer(string mode)

if (GameData.QuestionPlayState.AnswerOptions != null && !GameData.QuestionPlayState.LayoutShown)
{
GameLogic.OnAnswerOptions(false, GameData.QuestionPlayState.AnswerOptions);
GameLogic.OnAnswerOptions();
GameData.QuestionPlayState.LayoutShown = true;
}

Expand Down Expand Up @@ -93,7 +94,7 @@ public bool OnButtonPressStart()
// TODO: merge somehow with GameLogic.AskToPress() and OnAskAnswer() for buttons
if (GameData.QuestionPlayState.AnswerOptions != null && !GameData.QuestionPlayState.LayoutShown)
{
GameLogic.OnAnswerOptions(false, GameData.QuestionPlayState.AnswerOptions);
GameLogic.OnAnswerOptions();
GameData.QuestionPlayState.LayoutShown = true;
}

Expand All @@ -116,9 +117,9 @@ public void OnContentStart(IEnumerable<ContentItem> contentItems)
return;
}

if (contentItems.Any(item => item.Placement == ContentPlacements.Screen) && GameData.QuestionPlayState.AnswerOptions != null && !GameData.QuestionPlayState.LayoutShown)
if (GameData.QuestionPlayState.AnswerOptions != null && !GameData.QuestionPlayState.LayoutShown)
{
GameLogic.OnAnswerOptions(true, GameData.QuestionPlayState.AnswerOptions);
GameLogic.OnAnswerOptions();
GameData.QuestionPlayState.LayoutShown = true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/SICore/SICore/Clients/Game/QuestionPlayState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SIEngine.Core;
using SIPackages;

namespace SICore.Clients.Game;

Expand Down Expand Up @@ -32,6 +33,11 @@ internal sealed class QuestionPlayState
/// </summary>
internal bool AnswerOptionsShown { get; set; }

/// <summary>
/// Screen content sequence.
/// </summary>
internal IReadOnlyList<ContentItem[]>? ScreenContentSequence { get; set; }

internal void Clear()
{
AnswererIndicies.Clear();
Expand Down

0 comments on commit bfe71e1

Please sign in to comment.