Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
- notify clients on game closing
- remove deprecated types support
- fix partial text printing
- show lost button players after showing player who wins the button
  • Loading branch information
VladimirKhil committed Mar 10, 2024
1 parent 720cb1e commit e5c97a2
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 85 deletions.
9 changes: 8 additions & 1 deletion src/SICore/SICore.Network/Nodes/Node.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Notions;
using SICore.Connections;
using SICore.Network.Clients;
using SICore.Network.Configuration;
using SICore.Network.Contracts;
using SIData;
Expand Down Expand Up @@ -108,7 +109,7 @@ protected async void Connection_ConnectionClosed(IConnection connection, bool wi

if (clientName != null)
{
var m = new Message(string.Join(Message.ArgsSeparator, SystemMessages.Disconnect, clientName, (withError ? "+" : "-")), "", NetworkConstants.GameName);
var m = new Message(string.Join(Message.ArgsSeparator, SystemMessages.Disconnect, clientName, withError ? "+" : "-"), "", NetworkConstants.GameName);
await ProcessOutgoingMessageAsync(m);
}
}
Expand Down Expand Up @@ -373,6 +374,12 @@ await connection.SendMessageAsync(
}
}

public async Task NotifyGameCloseAsync()
{
var m = new Message(SystemMessages.GameClosed, NetworkConstants.GameName, NetworkConstants.Everybody);
await ProcessOutgoingMessageAsync(m);
}

/// <summary>
/// Disposes the node.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/SICore/SICore.Network/SystemMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public static class SystemMessages
/// </summary>
public const string Disconnect = "DISCONNECT";

/// <summary>
/// Game has been closed by server.
/// </summary>
public const string GameClosed = "GAME_CLOSED";

/// <summary>
/// Отказ в подключении
/// </summary>
Expand Down
5 changes: 2 additions & 3 deletions src/SICore/SICore/Clients/Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2566,9 +2566,8 @@ private void DropPlayerIndex(int playerIndex)
DropCurrentStaker();
}

if (ClientData.Question != null &&
((ClientData.Question.TypeName ?? ClientData.Type?.Name) == QuestionTypes.Auction
|| (ClientData.Question.TypeName ?? ClientData.Type?.Name) == QuestionTypes.Stake)
if (ClientData.Question != null
&& ClientData.Question.TypeName == QuestionTypes.Stake
&& ClientData.Order.Length > 0)
{
DropPlayerFromStakes(playerIndex);
Expand Down
14 changes: 6 additions & 8 deletions src/SICore/SICore/Clients/Game/GameActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using SIData;
using SIPackages;
using SIPackages.Core;
using SIStorage.Service.Contract.Models;
using System.Text;
using R = SICore.Properties.Resources;

Expand Down Expand Up @@ -63,7 +62,7 @@ internal void UserMessage(MessageTypes messageType, string text, GameRole? perso
? ReplicCodes.System.ToString()
: messageType == MessageTypes.Special
? ReplicCodes.Special.ToString()
: (personRole == GameRole.Player
: (personRole == GameRole.Player && personIndex.HasValue
? ReplicCodes.Player + personIndex.Value.ToString()
: ReplicCodes.Showman.ToString());

Expand Down Expand Up @@ -220,15 +219,14 @@ internal void InformRoundContent(string person = NetworkConstants.Everybody)

switch (contentType)
{
case AtomTypes.Image:
case AtomTypes.Audio:
case AtomTypes.AudioNew:
case AtomTypes.Video:
case AtomTypes.Html:
case ContentTypes.Image:
case ContentTypes.Audio:
case ContentTypes.Video:
case ContentTypes.Html:
{
if (!contentItem.IsRef) // External link
{
if (contentType == AtomTypes.Html)
if (contentType == ContentTypes.Html)
{
continue;
}
Expand Down
9 changes: 4 additions & 5 deletions src/SICore/SICore/Clients/Game/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ internal int ChooserIndex
/// </summary>
internal Question? Question { get; set; }

/// <summary>
/// Currently playing question type.
/// </summary>
internal QuestionType? Type { get; set; }

private NumberSet? _catInfo = null;

public NumberSet? CatInfo
Expand Down Expand Up @@ -561,6 +556,10 @@ internal void EndUpdatePersons()

public bool MediaOk { get; internal set; }

public int InitialPartialTextLength { get; internal set; }

public int PartialIterationCounter { get; internal set; }

public int TextLength { get; internal set; }

/// <summary>
Expand Down
101 changes: 50 additions & 51 deletions src/SICore/SICore/Clients/Game/GameLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ private void Engine_Round(Round round)
_data.AnswererIndex = -1;
_data.QuestionPlayState.Clear();
_data.StakerIndex = -1;
_data.Type = null;
_data.ThemeDeleters = null;

OnRound(round, 1);
Expand Down Expand Up @@ -463,6 +462,8 @@ internal void OnContentScreenText(string text, bool waitForFinish, TimeSpan dura
_gameActions.SendMessageWithArgs(Messages.ContentShape, ContentPlacements.Screen, 0, ContentTypes.Text, shape.EscapeNewLines());

_data.Text = text;
_data.InitialPartialTextLength = 0;
_data.PartialIterationCounter = 0;
_data.TextLength = 0;
ScheduleExecution(Tasks.PrintPartial, 1);
return;
Expand All @@ -485,7 +486,7 @@ internal void OnContentScreenText(string text, bool waitForFinish, TimeSpan dura
private bool IsPartial() =>
_data.Round != null
&& _data.Round.Type != RoundTypes.Final
&& (_data.Question.TypeName ?? _data.Type?.Name) == QuestionTypes.Simple
&& _data.Question?.TypeName == QuestionTypes.Simple
&& _data.Settings != null
&& !_data.Settings.AppSettings.FalseStart
&& _data.Settings.AppSettings.PartialText
Expand Down Expand Up @@ -787,7 +788,6 @@ private void Engine_NextQuestion()
_data.AnswererIndex = -1;
_data.QuestionPlayState.Clear();
_data.StakerIndex = -1;
_data.Type = null;
}

private void FinishRound(bool move = true)
Expand Down Expand Up @@ -1476,6 +1476,8 @@ public void ContinueQuestion()
// Resume question playing
if (_data.IsPartial)
{
_data.InitialPartialTextLength = _data.TextLength;
_data.PartialIterationCounter = 0;
ScheduleExecution(Tasks.PrintPartial, 5, force: true);
}
else
Expand Down Expand Up @@ -1881,7 +1883,7 @@ private void AskAnswerDeferred()
return;
}

ScheduleExecution(Tasks.AskAnswer, 7, force: true);
ScheduleExecution(Tasks.AskAnswer, 1, force: true);
}

private (bool, Tasks) ProcessStopReason(Tasks task, int arg)
Expand Down Expand Up @@ -1939,7 +1941,7 @@ private void AskAnswerDeferred()

if (stop)
{
ScheduleExecution(Tasks.AskAnswer, 7, force: true);
ScheduleExecution(Tasks.AskAnswer, 1, force: true);
}
break;

Expand Down Expand Up @@ -2689,8 +2691,7 @@ private void OnQuestionType(int arg)

if (arg == 3)
{
_data.Type = _data.Question.Type;
var typeName = _data.Question.TypeName ?? _data.Question.Type.Name;
var typeName = _data.Question?.TypeName;

// Only StakeAll type is supported in final for now
// This will be removed when full question type support will have been implemented
Expand All @@ -2704,21 +2705,18 @@ private void OnQuestionType(int arg)

switch (typeName)
{
case QuestionTypes.Auction:
case QuestionTypes.Stake:
_gameActions.ShowmanReplic(GetRandomString(LO[nameof(R.YouGetAuction)]));
s.Append(MakeCompatibleQuestionTypeName(typeName));
delay = 16;
break;

case QuestionTypes.Cat:
case QuestionTypes.BagCat:
case QuestionTypes.Secret:
case QuestionTypes.SecretPublicPrice:
case QuestionTypes.SecretNoQuestion:
var replic = new StringBuilder(LO[nameof(R.YouReceiveCat)]);

var selectionMode = _data.Question.Parameters?.FirstOrDefault(p => p.Key == QuestionParameterNames.SelectionMode);
var selectionMode = _data.Question?.Parameters?.FirstOrDefault(p => p.Key == QuestionParameterNames.SelectionMode);

if (selectionMode?.Value?.SimpleValue == StepParameterValues.SetAnswererSelect_Any)
{
Expand All @@ -2730,7 +2728,6 @@ private void OnQuestionType(int arg)
delay = 10;
break;

case QuestionTypes.Sponsored:
case QuestionTypes.NoRisk:
_gameActions.ShowmanReplic(LO[nameof(R.SponsoredQuestion)]);
s.Append(MakeCompatibleQuestionTypeName(typeName));
Expand All @@ -2746,7 +2743,7 @@ private void OnQuestionType(int arg)
break;

default:
OnUnsupportedQuestionType(typeName);
OnUnsupportedQuestionType(typeName ?? "");
return;
}

Expand All @@ -2761,27 +2758,16 @@ private void OnQuestionType(int arg)

private static string MakeCompatibleQuestionTypeName(string typeName) => typeName switch
{
QuestionTypes.Auction or QuestionTypes.Stake => QuestionTypes.Auction,
QuestionTypes.Cat or QuestionTypes.BagCat or QuestionTypes.Secret or QuestionTypes.SecretPublicPrice or QuestionTypes.SecretNoQuestion => QuestionTypes.Cat,
QuestionTypes.Sponsored or QuestionTypes.NoRisk => QuestionTypes.Sponsored,
QuestionTypes.Stake => QuestionTypes.Auction,
QuestionTypes.Secret or QuestionTypes.SecretPublicPrice or QuestionTypes.SecretNoQuestion => QuestionTypes.Cat,
QuestionTypes.NoRisk => QuestionTypes.Sponsored,
_ => typeName,
};

private void OnUnsupportedQuestionType(string typeName)
{
var sp = new StringBuilder(LO[nameof(R.UnknownType)]).Append(' ').Append(typeName);

// Obsolete
if (_data.Type != null && _data.Type.Params.Count > 0)
{
sp.Append(' ').Append(LO[nameof(R.WithParams)]);

foreach (var p in _data.Type.Params)
{
sp.Append(' ').Append(p);
}
}

_gameActions.SpecialReplic(sp.ToString());
_gameActions.SpecialReplic(LO[nameof(R.GameWillResume)]);
_gameActions.ShowmanReplic(LO[nameof(R.ManuallyPlayedQuestion)]);
Expand All @@ -2802,25 +2788,31 @@ private void PrintPartial()
return;
}

var printingLength = Math.Max(
1,
Math.Min(
text.Length - _data.TextLength,
(int)(_data.Settings.AppSettings.ReadingSpeed * PartialPrintFrequencyPerSecond)));
_data.PartialIterationCounter++;

// Align to next space position
while (_data.TextLength + printingLength + 1 < text.Length && !char.IsWhiteSpace(text[_data.TextLength + printingLength]))
var newTextLength = Math.Min(
_data.InitialPartialTextLength
+ (int)(_data.Settings.AppSettings.ReadingSpeed * PartialPrintFrequencyPerSecond * _data.PartialIterationCounter),
text.Length);

if (newTextLength > _data.TextLength)
{
printingLength++;
}
var printingLength = newTextLength - _data.TextLength;

var subText = text.Substring(_data.TextLength, printingLength);
// Align to next space position
while (_data.TextLength + printingLength + 1 < text.Length && !char.IsWhiteSpace(text[_data.TextLength + printingLength]))
{
printingLength++;
}

_gameActions.SendMessageWithArgs(Messages.ContentAppend, ContentPlacements.Screen, 0, ContentTypes.Text, subText.EscapeNewLines());
_gameActions.SendMessageWithArgs(Messages.Atom, Constants.PartialText, subText);
_gameActions.SystemReplic(subText);
var subText = text.Substring(_data.TextLength, printingLength);

_data.TextLength += printingLength;
_gameActions.SendMessageWithArgs(Messages.ContentAppend, ContentPlacements.Screen, 0, ContentTypes.Text, subText.EscapeNewLines());
_gameActions.SendMessageWithArgs(Messages.Atom, Constants.PartialText, subText);
_gameActions.SystemReplic(subText);

_data.TextLength += printingLength;
}

if (_data.TextLength < text.Length)
{
Expand Down Expand Up @@ -2945,16 +2937,6 @@ internal bool PrepareForAskAnswer()

var index = ClientData.PendingAnswererIndicies.Count == 1 ? 0 : Random.Shared.Next(ClientData.PendingAnswererIndicies.Count);
ClientData.PendingAnswererIndex = ClientData.PendingAnswererIndicies[index];

for (var i = 0; i < ClientData.PendingAnswererIndicies.Count; i++)
{
if (i == index)
{
continue;
}

_gameActions.SendMessageWithArgs(Messages.WrongTry, i);
}
}

if (ClientData.PendingAnswererIndex < 0 || ClientData.PendingAnswererIndex >= ClientData.Players.Count)
Expand Down Expand Up @@ -3388,6 +3370,23 @@ private void AskAnswer()

_data.Answerer.Answer = "";

var buttonPressMode = ClientData.Settings.AppSettings.ButtonPressMode;

if (buttonPressMode == ButtonPressMode.RandomWithinInterval)
{
for (var i = 0; i < ClientData.PendingAnswererIndicies.Count; i++)
{
var playerIndex = ClientData.PendingAnswererIndicies[i];

if (playerIndex == ClientData.PendingAnswererIndex)
{
continue;
}

_gameActions.SendMessageWithArgs(Messages.WrongTry, playerIndex);
}
}

ScheduleExecution(Tasks.WaitAnswer, time1);
WaitFor(DecisionType.Answering, time1, _data.AnswererIndex);
}
Expand Down
Loading

0 comments on commit e5c97a2

Please sign in to comment.