Skip to content

Commit

Permalink
#152 Answer options support (final part)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirKhil committed Nov 12, 2023
1 parent 98488c4 commit 5f69fa5
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/Common/SIUI/Table.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@
<Setter Property="Foreground" Value="DarkBlue" />
<Setter Property="BorderBrush" Value="LightYellow" />
</DataTrigger>

<DataTrigger Binding="{Binding State}" Value="{x:Static vm:ItemState.Wrong}">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>

Expand Down
36 changes: 28 additions & 8 deletions src/SICore/SICore/Clients/Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1983,13 +1983,31 @@ private void OnAnswer(Message message, string[] args)
{
if (args[1] == MessageParams.Answer_Right)
{
ClientData.Answerer.Answer = args[2].Replace(Constants.AnswerPlaceholder, ClientData.Question.Right.FirstOrDefault() ?? "(...)");
ClientData.Answerer.AnswerIsWrong = false;
if (ClientData.QuestionPlayState.AnswerOptions != null)
{
var rightLabel = ClientData.Question.Right.FirstOrDefault();
var rightIndex = Array.FindIndex(ClientData.QuestionPlayState.AnswerOptions, o => o.Label == rightLabel);

ClientData.Answerer.Answer = rightIndex.ToString();
}
else
{
ClientData.Answerer.Answer = args[2].Replace(Constants.AnswerPlaceholder, ClientData.Question.Right.FirstOrDefault() ?? "(...)").GrowFirstLetter();
ClientData.Answerer.AnswerIsWrong = false;
}
}
else
else if (ClientData.QuestionPlayState.AnswerOptions != null)
{
// TODO: set wrong answer option when options are used
var rightLabel = ClientData.Question.Right.FirstOrDefault();
var rightIndex = Array.FindIndex(ClientData.QuestionPlayState.AnswerOptions, o => o.Label == rightLabel);

var leftIndicies = Enumerable.Range(0, ClientData.QuestionPlayState.AnswerOptions.Length).Except(ClientData.QuestionPlayState.UsedAnswerOptionsIndicies).Except(new int[] { rightIndex }).ToArray();
var wrongIndex = leftIndicies[Random.Shared.Next(leftIndicies.Length)];

ClientData.Answerer.Answer = wrongIndex.ToString();
}
else
{
ClientData.Answerer.AnswerIsWrong = true;

var restwrong = new List<string>();
Expand Down Expand Up @@ -2031,11 +2049,13 @@ private void OnAnswer(Message message, string[] args)

var wrongIndex = Random.Shared.Next(wrongCount);

ClientData.UsedWrongVersions.Add(restwrong[wrongIndex]);
ClientData.Answerer.Answer = args[2].Replace("#", restwrong[wrongIndex]);
}
if (ClientData.Round.Type != RoundTypes.Final)
{
ClientData.UsedWrongVersions.Add(restwrong[wrongIndex]);
}

ClientData.Answerer.Answer = ClientData.Answerer.Answer.GrowFirstLetter();
ClientData.Answerer.Answer = args[2].Replace("#", restwrong[wrongIndex]).GrowFirstLetter();
}
}
else
{
Expand Down
63 changes: 60 additions & 3 deletions src/SICore/SICore/Clients/Game/GameLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SIPackages.Core;
using SIPackages.Providers;
using SIUI.Model;
using SIUI.ViewModel;
using System.Text;
using System.Text.RegularExpressions;
using R = SICore.Properties.Resources;
Expand Down Expand Up @@ -1345,6 +1346,12 @@ private bool OnDecisionAnswerValidating()

_gameActions.SendMessage(s.ToString());

if (_data.QuestionPlayState.AnswerOptions != null && int.TryParse(_data.Answerer.Answer, out var answerIndex))
{
_gameActions.SendMessageWithArgs(Messages.ContentState, ContentPlacements.Screen, answerIndex + 1, ItemState.Wrong);
_data.QuestionPlayState.UsedAnswerOptionsIndicies.Add(answerIndex);
}

_data.Answerer.Sum -= _data.CurPriceWrong;
_data.Answerer.CanPress = false;
_gameActions.InformSums();
Expand Down Expand Up @@ -1392,6 +1399,17 @@ public void ContinueQuestion()
return;
}

if (_data.QuestionPlayState.AnswerOptions != null)
{
var oneOptionLeft = _data.QuestionPlayState.UsedAnswerOptionsIndicies.Count + 1 == _data.QuestionPlayState.AnswerOptions.Length;

if (oneOptionLeft)
{
ScheduleExecution(Tasks.WaitTry, 20, force: true);
return;
}
}

_data.PendingAnswererIndicies.Clear();

if (!ClientData.Settings.AppSettings.FalseStart)
Expand Down Expand Up @@ -1506,7 +1524,20 @@ private bool OnDecisionAnswering()

StopWaiting();

_gameActions.PlayerReplic(_data.AnswererIndex, _data.Answerer.Answer);
if (_data.QuestionPlayState.AnswerOptions != null)
{
if (int.TryParse(_data.Answerer.Answer, out var answerIndex))
{
_gameActions.SendMessageWithArgs(Messages.ContentState, ContentPlacements.Screen, answerIndex + 1, ItemState.Active);
}

ScheduleExecution(Tasks.AskRight, 15, force: true);
return true;
}
else
{
_gameActions.PlayerReplic(_data.AnswererIndex, _data.Answerer.Answer);
}

if (_data.IsOralNow)
{
Expand Down Expand Up @@ -3162,7 +3193,21 @@ private void AskRight()
{
_data.ShowmanDecision = false;

if (!_data.Answerer.IsHuman)
if (_data.QuestionPlayState.AnswerOptions != null)
{
_data.IsWaiting = true;
_data.Decision = DecisionType.AnswerValidating;

var rightLabel = ClientData.Question.Right.FirstOrDefault();
var rightIndex = Array.FindIndex(ClientData.QuestionPlayState.AnswerOptions, o => o.Label == rightLabel);

_data.Answerer.AnswerIsRight = _data.Answerer.Answer == rightIndex.ToString();
_data.Answerer.AnswerIsRightFactor = 1.0;
_data.ShowmanDecision = true;

OnDecision();
}
else if (!_data.Answerer.IsHuman)
{
_data.IsWaiting = true;
_data.Decision = DecisionType.AnswerValidating;
Expand Down Expand Up @@ -4405,7 +4450,19 @@ internal void OnComplexAnswer()

internal void OnRightAnswerOption(string rightOptionLabel)
{
_gameActions.SendMessageWithArgs(Messages.RightAnswer, ContentTypes.Text, rightOptionLabel);
var rightIndex = ClientData.QuestionPlayState.AnswerOptions != null
? Array.FindIndex(ClientData.QuestionPlayState.AnswerOptions, o => o.Label == rightOptionLabel)
: -1;

if (rightIndex > -1)
{
_gameActions.SendMessageWithArgs(Messages.RightAnswer, ContentTypes.Text, rightIndex);
}
else
{
var printedAnswer = $"{LO[nameof(R.RightAnswer)]}: {rightOptionLabel}";
_gameActions.ShowmanReplic(printedAnswer);
}

var answerTime = _data.Settings.AppSettings.TimeSettings.TimeForRightAnswer;
ScheduleExecution(Tasks.MoveNext, (answerTime == 0 ? 2 : answerTime) * 10);
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
Expand Up @@ -17,6 +17,11 @@ internal sealed class QuestionPlayState
/// </summary>
internal AnswerOption[]? AnswerOptions { get; set; }

/// <summary>
/// Used answer options indicies.
/// </summary>
internal HashSet<int> UsedAnswerOptionsIndicies { get; } = new();

/// <summary>
/// Has layout been set.
/// </summary>
Expand All @@ -31,6 +36,7 @@ internal void Clear()
{
AnswererIndicies.Clear();
AnswerOptions = null;
UsedAnswerOptionsIndicies.Clear();
LayoutShown = false;
AnswerOptionsShown = false;
}
Expand Down
10 changes: 9 additions & 1 deletion src/SICore/SICore/Clients/Player/PlayerHumanLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ public PlayerHumanLogic(
TInfo.SelectTheme.CanBeExecuted = false;
}

private void TInfo_AnswerSelected(ItemViewModel item) => ClientData.PlayerDataExtensions.SendAnswer.Execute(item.Label);
private void TInfo_AnswerSelected(ItemViewModel item)
{
var optionIndex = Array.IndexOf(TInfo.AnswerOptions.Options, item);

if (optionIndex > -1)
{
ClientData.PlayerDataExtensions.SendAnswer.Execute(optionIndex.ToString());
}
}

#region PlayerInterface Members

Expand Down
2 changes: 2 additions & 0 deletions src/SICore/SICore/Clients/Viewer/IViewerLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@ void OnAnswerOptions(bool questionHasScreenContent, IEnumerable<string> optionsT
void OnContent(string[] mparams) { }

void OnContentAppend(string[] mparams) { }

void OnContentState(string[] mparams) { }
}
4 changes: 4 additions & 0 deletions src/SICore/SICore/Clients/Viewer/Viewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,10 @@ await _client.Node.ConnectionsLock.WithLockAsync(() =>
_logic.OnContentAppend(mparams);
break;

case Messages.ContentState:
_logic.OnContentState(mparams);
break;

case Messages.Atom_Hint:
if (mparams.Length > 1)
{
Expand Down
27 changes: 26 additions & 1 deletion src/SICore/SICore/Clients/Viewer/ViewerHumanLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,26 @@ public void OnContentAppend(string[] mparams)
TInfo.TextLength += newTextLength;
}

public void OnContentState(string[] mparams)
{
if (mparams.Length < 4)
{
return;
}

var placement = mparams[1];

if (TInfo.LayoutMode == LayoutMode.AnswerOptions
&& placement == ContentPlacements.Screen
&& int.TryParse(mparams[2], out var layoutId)
&& layoutId > 0
&& layoutId <= TInfo.AnswerOptions.Options.Length
&& Enum.TryParse<ItemState>(mparams[3], out var state))
{
TInfo.AnswerOptions.Options[layoutId - 1].State = state;
}
}

private void OnScreenContent(IEnumerable<ContentInfo> contentInfo)
{
if (TInfo.TStage != TableStage.Answer && _data.Speaker != null && !_data.Speaker.IsShowman)
Expand Down Expand Up @@ -1056,9 +1076,14 @@ public void OnRightAnswer(string answer)
{
var options = TInfo.AnswerOptions.Options;

if (!int.TryParse(answer, out var answerIndex))
{
answerIndex = -1;
}

for (int i = 0; i < options.Length; i++)
{
if (options[i].Label == answer)
if (i == answerIndex)
{
options[i].State = ItemState.Right;
}
Expand Down
5 changes: 5 additions & 0 deletions src/SICore/SICore/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public static class Messages
/// </summary>
public const string ContentShape = "CONTENT_SHAPE";

/// <summary>
/// Updates content state.
/// </summary>
public const string ContentState = "CONTENT_STATE";

/// <summary>
/// Удаление темы
/// </summary>
Expand Down

0 comments on commit 5f69fa5

Please sign in to comment.