Skip to content

Commit

Permalink
Switch to sending labels instead of indices when using answer options…
Browse files Browse the repository at this point in the history
…; fully support stake steps
  • Loading branch information
VladimirKhil committed Nov 18, 2023
1 parent f8f9808 commit cdb4468
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 249 deletions.
18 changes: 7 additions & 11 deletions src/SICore/SICore/Clients/Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,10 +1164,10 @@ private void OnStake(Message message, string[] args)
}
else if (ClientData.StakeType == StakeMode.Sum)
{
var minimum = ClientData.Stake != -1 ? ClientData.Stake + 100 : ClientData.CurPriceRight + 100;
var minimum = ClientData.Stake != -1 ? ClientData.Stake + ClientData.StakeStep : ClientData.CurPriceRight + ClientData.StakeStep;

// TODO: optimize
while (minimum % 100 != 0)
while (minimum % ClientData.StakeStep != 0)
{
minimum++;
}
Expand All @@ -1178,7 +1178,7 @@ private void OnStake(Message message, string[] args)
return;
}

if (stakeSum < minimum || stakeSum > ClientData.ActivePlayer.Sum || stakeSum % 100 != 0)
if (stakeSum < minimum || stakeSum > ClientData.ActivePlayer.Sum || stakeSum % ClientData.StakeStep != 0)
{
ClientData.StakeType = null;
return;
Expand Down Expand Up @@ -1986,9 +1986,7 @@ private void OnAnswer(Message message, string[] args)
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();
ClientData.Answerer.Answer = rightLabel;
}
else
{
Expand All @@ -1999,12 +1997,10 @@ private void OnAnswer(Message message, string[] args)
else if (ClientData.QuestionPlayState.AnswerOptions != null)
{
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)];
var wrongOptions = ClientData.QuestionPlayState.AnswerOptions.Where(o => o.Label != rightLabel && !ClientData.QuestionPlayState.UsedAnswerOptions.Contains(o.Label)).ToArray();
var wrong = wrongOptions[Random.Shared.Next(wrongOptions.Length)];

ClientData.Answerer.Answer = wrongIndex.ToString();
ClientData.Answerer.Answer = wrong.Label;
}
else
{
Expand Down
11 changes: 11 additions & 0 deletions src/SICore/SICore/Clients/Game/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,19 @@ public int StakerIndex
/// - пас
/// - ва-банк
/// </summary>
[Obsolete]
internal bool[] StakeVariants { get; set; } = new bool[4];

/// <summary>
/// Possible stake types.
/// </summary>
internal StakeTypes StakeTypes { get; set; }

/// <summary>
/// Minimum stake step value in current round.
/// </summary>
internal int StakeStep { get; set; } = 100;

/// <summary>
/// Тип ставки
/// </summary>
Expand Down
54 changes: 37 additions & 17 deletions src/SICore/SICore/Clients/Game/GameLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using SICore.Clients;
using SICore.Clients.Game;
using SICore.Contracts;
using SICore.Models;
using SICore.Results;
using SICore.Utils;
using SIData;
Expand Down Expand Up @@ -71,11 +72,6 @@ public sealed class GameLogic : Logic<GameData>
/// </summary>
private int _maxRoundPrice = 1;

/// <summary>
/// Minimum stake step value in current round.
/// </summary>
private int _stakeStep = 100;

public object? UserState { get; set; }

private readonly GameActions _gameActions;
Expand Down Expand Up @@ -277,7 +273,8 @@ private void SetRoundPrices(Round round)
}

_minRoundPrice = Math.Max(1, _minRoundPrice);
_stakeStep = (int)Math.Pow(10, Math.Floor(Math.Log10(_minRoundPrice))); // Maximum power of 10 <= _minRoundPrice
// Uncomment after client update
// ClientData.StakeStep = (int)Math.Pow(10, Math.Floor(Math.Log10(_minRoundPrice))); // Maximum power of 10 <= _minRoundPrice
}

private void InitThemes(Theme[] themes, bool willPlayAllThemes, bool isFirstPlay)
Expand Down Expand Up @@ -1346,10 +1343,16 @@ private bool OnDecisionAnswerValidating()

_gameActions.SendMessage(s.ToString());

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

if (answerIndex > -1)
{
_gameActions.SendMessageWithArgs(Messages.ContentState, ContentPlacements.Screen, answerIndex + 1, ItemState.Wrong);
}

_data.QuestionPlayState.UsedAnswerOptions.Add(_data.Answerer.Answer);
}

_data.Answerer.Sum -= _data.CurPriceWrong;
Expand Down Expand Up @@ -1401,7 +1404,7 @@ public void ContinueQuestion()

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

if (oneOptionLeft)
{
Expand Down Expand Up @@ -1526,7 +1529,9 @@ private bool OnDecisionAnswering()

if (_data.QuestionPlayState.AnswerOptions != null)
{
if (int.TryParse(_data.Answerer.Answer, out var answerIndex))
var answerIndex = Array.FindIndex(_data.QuestionPlayState.AnswerOptions, o => o.Label == _data.Answerer.Answer);

if (answerIndex > -1)
{
_gameActions.SendMessageWithArgs(Messages.ContentState, ContentPlacements.Screen, answerIndex + 1, ItemState.Active);
}
Expand Down Expand Up @@ -3199,9 +3204,8 @@ private void AskRight()
_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.AnswerIsRight = _data.Answerer.Answer == rightLabel;
_data.Answerer.AnswerIsRightFactor = 1.0;
_data.ShowmanDecision = true;

Expand Down Expand Up @@ -3685,9 +3689,18 @@ private void AskStake(bool canDetectNextStakerGuard)
return;
}

// TODO: enum StakeMode, StakeVariants -> HashSet<StakeMode> ?
var minimumStake = (_data.Stake != -1 ? _data.Stake : cost) + ClientData.StakeStep;
var minimumStakeAligned = (int)Math.Ceiling((double)minimumStake / ClientData.StakeStep) * ClientData.StakeStep;

_data.StakeTypes = StakeTypes.AllIn | (_data.StakerIndex == -1 ? StakeTypes.Nominal : StakeTypes.Pass);

if (!_data.AllIn && playerMoney >= minimumStakeAligned)
{
_data.StakeTypes |= StakeTypes.Stake;
}

_data.StakeVariants[0] = _data.StakerIndex == -1;
_data.StakeVariants[1] = !_data.AllIn && playerMoney != cost && playerMoney > _data.Stake + _stakeStep;
_data.StakeVariants[1] = !_data.AllIn && playerMoney != cost && playerMoney > _data.Stake + ClientData.StakeStep;
_data.StakeVariants[2] = !_data.StakeVariants[0];
_data.StakeVariants[3] = true;

Expand All @@ -3702,22 +3715,25 @@ private void AskStake(bool canDetectNextStakerGuard)
_data.IsOralNow = _data.IsOral && _data.ActivePlayer.IsHuman;

var stakeMsg = new MessageBuilder(Messages.Stake);
var stakeMsg2 = new MessageBuilder(Messages.Stake2);

for (var i = 0; i < _data.StakeVariants.Length; i++)
{
stakeMsg.Add(_data.StakeVariants[i] ? '+' : '-');
}

var minimumStake = (_data.Stake != -1 ? _data.Stake : cost) + _stakeStep;
var minimumStakeAligned = (int)Math.Ceiling((double)minimumStake / _stakeStep) * _stakeStep;
stakeMsg2.Add(_data.StakeTypes);

stakeMsg.Add(minimumStakeAligned);
stakeMsg2.Add(minimumStakeAligned);
stakeMsg2.Add(ClientData.StakeStep);

var waitTime = _data.Settings.AppSettings.TimeSettings.TimeForMakingStake * 10;

if (CanPlayerAct())
{
_gameActions.SendMessage(stakeMsg.Build(), _data.ActivePlayer.Name);
_gameActions.SendMessage(stakeMsg2.Build(), _data.ActivePlayer.Name);

if (!_data.ActivePlayer.IsConnected)
{
Expand All @@ -3730,6 +3746,10 @@ private void AskStake(bool canDetectNextStakerGuard)
stakeMsg.Add(_data.ActivePlayer.Sum); // Send maximum possible value to showman
stakeMsg.Add(_data.ActivePlayer.Name);
_gameActions.SendMessage(stakeMsg.Build(), _data.ShowMan.Name);

stakeMsg2.Add(_data.ActivePlayer.Sum); // Send maximum possible value to showman
stakeMsg2.Add(_data.ActivePlayer.Name);
_gameActions.SendMessage(stakeMsg2.Build(), _data.ShowMan.Name);
}

_data.StakeType = null;
Expand Down
4 changes: 2 additions & 2 deletions src/SICore/SICore/Clients/Game/QuestionPlayState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class QuestionPlayState
/// <summary>
/// Used answer options indicies.
/// </summary>
internal HashSet<int> UsedAnswerOptionsIndicies { get; } = new();
internal HashSet<string> UsedAnswerOptions { get; } = new();

/// <summary>
/// Has layout been set.
Expand All @@ -36,7 +36,7 @@ internal void Clear()
{
AnswererIndicies.Clear();
AnswerOptions = null;
UsedAnswerOptionsIndicies.Clear();
UsedAnswerOptions.Clear();
LayoutShown = false;
AnswerOptionsShown = false;
}
Expand Down
41 changes: 19 additions & 22 deletions src/SICore/SICore/Clients/Other/AI/DirPoint.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
using System;
namespace SICore;

namespace SICore
/// <summary>
/// Граница отрезка с указанием расположения отрезка относительно точки
/// </summary>
internal sealed class DirPoint
{
/// <summary>
/// Граница отрезка с указанием расположения отрезка относительно точки
/// </summary>
internal sealed class DirPoint
public DirPoint(double value, bool direction)
{
public DirPoint(double value, bool direction)
{
Direction = direction;
Value = direction ? (int)Math.Ceiling(value / 100) * 100 : (int)Math.Floor(value / 100) * 100;
}
Direction = direction;
Value = direction ? (int)Math.Ceiling(value / 100) * 100 : (int)Math.Floor(value / 100) * 100;
}

/// <summary>
/// Значение точки
/// </summary>
public int Value { get; }
/// <summary>
/// Значение точки
/// </summary>
public int Value { get; }

/// <summary>
/// true, если отрезок находится правее точки
/// false, если отрезок находится левее точки
/// </summary>
public bool Direction { get; }
/// <summary>
/// true, если отрезок находится правее точки
/// false, если отрезок находится левее точки
/// </summary>
public bool Direction { get; }

public new string ToString() => $"{Value} {Direction}";
}
public new string ToString() => $"{Value} {Direction}";
}
Loading

0 comments on commit cdb4468

Please sign in to comment.