Skip to content

Commit

Permalink
Support AskStake message; fix small errors in SIQuester
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirKhil committed Dec 30, 2024
1 parent 45cc84a commit baa3cc5
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 556 deletions.
3 changes: 1 addition & 2 deletions src/SICore/SICore/Clients/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
namespace SICore;

/// <summary>
/// Обработчик сообщений
/// Defines a message handler.
/// </summary>
/// <typeparam name="D">Тип данных клиента</typeparam>
/// <typeparam name="L">Тип логики клиента</typeparam>
public abstract class Actor<D> : IActor
where D : Data
{
Expand Down
26 changes: 0 additions & 26 deletions src/SICore/SICore/Clients/IPersonLogic.cs

This file was deleted.

4 changes: 0 additions & 4 deletions src/SICore/SICore/Clients/PersonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ namespace SICore;
/// </summary>
public sealed class PersonData : INotifyPropertyChanged
{
public ICommand SendCatCost { get; set; }

public ICommand SendFinalStake { get; set; }

private StakeInfo _stakeInfo = null;

public StakeInfo StakeInfo
Expand Down
88 changes: 3 additions & 85 deletions src/SICore/SICore/Clients/Player/Player.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using SICore.BusinessLogic;
using SICore.Models;
using SICore.Network.Clients;
using SIData;
using SIPackages.Core;
Expand All @@ -26,19 +25,7 @@ public sealed class Player : Viewer
/// <param name="data">Player game data.</param>
public Player(Client client, Account personData, bool isHost, IViewerLogic logic, ViewerActions viewerActions, ILocalizer localizer, ViewerData data)
: base(client, personData, isHost, logic, viewerActions, localizer, data)
{
ClientData.PersonDataExtensions.SendCatCost = new CustomCommand(arg =>
{
_viewerActions.SendMessageWithArgs(Messages.CatCost, ClientData.PersonDataExtensions.StakeInfo.Stake);
Clear();
});

ClientData.PersonDataExtensions.SendFinalStake = new CustomCommand(arg =>
{
_viewerActions.SendMessageWithArgs(Messages.FinalStake, ClientData.PersonDataExtensions.StakeInfo.Stake);
Clear();
});
}
{ }

private void Clear() => Logic.ClearSelections(true);

Expand Down Expand Up @@ -149,77 +136,8 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams
OnAskSelectPlayer(mparams);
break;

case Messages.AskStake: // Uncomment later
//OnAskStake(mparams);
//_logic.PlayerLogic.StakeNew();
break;

case Messages.CatCost:
ClientData.PersonDataExtensions.StakeInfo = new StakeInfo
{
Minimum = int.Parse(mparams[1]),
Maximum = int.Parse(mparams[2]),
Step = int.Parse(mparams[3]),
Stake = int.Parse(mparams[1])
};

Logic.PlayerLogic.CatCost();
break;

case Messages.Stake:
for (int i = 0; i < 4; i++)
{
ClientData.PersonDataExtensions.Var[i] = mparams[i + 1] == "+";
}

ClientData.PersonDataExtensions.StakeInfo = new StakeInfo
{
Minimum = int.Parse(mparams[5]),
Maximum = ((PlayerAccount)ClientData.Me).Sum,
Step = 100,
Stake = int.Parse(mparams[5])
};

Logic.PlayerLogic.Stake();
break;

case Messages.Stake2:
if (mparams.Length < 4
|| !Enum.TryParse<StakeTypes>(mparams[1], out var stakeTypes)
|| !int.TryParse(mparams[2], out var minimumStake)
|| !int.TryParse(mparams[3], out var step))
{
break;
}

ClientData.PersonDataExtensions.Var[0] = stakeTypes.HasFlag(StakeTypes.Nominal);
ClientData.PersonDataExtensions.Var[1] = stakeTypes.HasFlag(StakeTypes.Stake);
ClientData.PersonDataExtensions.Var[2] = stakeTypes.HasFlag(StakeTypes.Pass);
ClientData.PersonDataExtensions.Var[3] = stakeTypes.HasFlag(StakeTypes.AllIn);

ClientData.PersonDataExtensions.StakeInfo = new StakeInfo
{
Minimum = minimumStake,
Maximum = ((PlayerAccount)ClientData.Me).Sum,
Step = step,
Stake = minimumStake
};

Logic.PlayerLogic.Stake();
break;

case Messages.FinalStake:
ClientData.PersonDataExtensions.StakeInfo = new StakeInfo
{
Minimum = 1,
Maximum = ((PlayerAccount)ClientData.Me).Sum,
Step = 1,
Stake = 1
};

((PlayerAccount)ClientData.Me).IsDeciding = false;

Logic.PlayerLogic.FinalStake();
case Messages.AskStake:
OnAskStake(mparams);
break;

case Messages.Validation2:
Expand Down
151 changes: 3 additions & 148 deletions src/SICore/SICore/Clients/Player/PlayerComputerLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace SICore;
/// <summary>
/// Defines a player computer logic.
/// </summary>
internal sealed class PlayerComputerLogic : IPersonLogic
internal sealed class PlayerComputerLogic
{
private const int DefaultThemeQuestionCount = 5;

Expand Down Expand Up @@ -116,24 +116,12 @@ private void ExecuteTask(PlayerTasks task)
OnSelectPlayer();
break;

case PlayerTasks.CatCost:
CatCostTask();
break;

case PlayerTasks.ChooseFinal:
ChooseFinal();
break;

case PlayerTasks.FinalStake:
FinalStakeTask();
break;

case PlayerTasks.Stake:
StakeTask();
break;

case PlayerTasks.StakeNew:
StakeTaskNew();
MakeStake();
break;

case PlayerTasks.PressButton:
Expand Down Expand Up @@ -164,29 +152,6 @@ private int GetTimePercentage(int timerIndex)

private void AnswerWrong() => _viewerActions.SendMessage(Messages.IsRight, "-");

private void FinalStakeTask()
{
var myIndex2 = _data.Players.IndexOf((PlayerAccount)_data.Me);
var sums = _data.Players.Select(p => p.Sum).ToArray();

try
{
var stake = MakeFinalStake(sums, myIndex2, _account.Style);
_viewerActions.SendMessageWithArgs(Messages.FinalStake, stake);
}
catch (Exception exc)
{
_data.SystemLog.AppendFormat(
"Final stake calculation error: {0}\r\nParameter values:\r\n" +
"sums = {1}. myIndex = {2}. Style = {3}",
exc,
string.Join(", ", sums),
myIndex2,
_account.Style)
.AppendLine();
}
}

private void ChooseFinal()
{
try
Expand All @@ -203,7 +168,7 @@ private void ChooseFinal()
}
}

private void StakeTaskNew()
private void MakeStake()
{
var myIndex = _data.Players.IndexOf((PlayerAccount)_data.Me);
var isCritical = IsCritical();
Expand Down Expand Up @@ -321,101 +286,6 @@ private static StakeModes FromStakeMode(StakeMode stakeMode) =>
_ => StakeModes.Pass,
};

[Obsolete]
private void StakeTask()
{
var myIndex = _data.Players.IndexOf((PlayerAccount)_data.Me);
var isCritical = IsCritical();

try
{
int stakeSum = -1;

var stakeMode = MakeStake(
_data.QuestionIndex,
_data.Players.Select(p => p.Sum).ToArray(),
myIndex,
_data.LastStakerIndex,
_account.Style,
_data.PersonDataExtensions.Var,
_account.N1,
_account.N5,
_account.B1,
_account.B5,
isCritical,
_data.PersonDataExtensions.StakeInfo.Minimum,
_data.PersonDataExtensions.StakeInfo.Step,
out stakeSum);

var msg = new StringBuilder(Messages.Stake).Append(Message.ArgsSeparatorChar).Append((int)stakeMode);

if (stakeMode == StakeMode.Sum)
{
msg.Append(Message.ArgsSeparatorChar).Append(stakeSum);
}

_viewerActions.SendMessage(msg.ToString());
}
catch (Exception exc)
{
_data.SystemLog.AppendFormat(
"Ошибка при расчёте ставки компьютерного игрока. Описание ошибки: {0}\r\nЗначения параметров.\r\n" +
"this.data.choiceQuest = {1}. Sums = {2}. " +
"MyIndex = {3}.lastStakerNum = {4}. Style = {5} Vars = {6}:{7}:{8}:{9}. " +
"N1 = {10}, N5 = {11}, B1 = {12}, B5 = {13}, Critical = {14}. MinCost = {15}",
exc,
_data.QuestionIndex,
string.Join(", ", _data.Players.Select(p => p.Sum)),
myIndex,
_data.LastStakerIndex,
_account.Style,
_data.PersonDataExtensions.Var[0],
_data.PersonDataExtensions.Var[1],
_data.PersonDataExtensions.Var[2],
_data.PersonDataExtensions.Var[3],
_account.N1,
_account.N5,
_account.B1,
_account.B5,
isCritical,
_data.PersonDataExtensions.StakeInfo.Minimum)
.AppendLine();
}
}

private void CatCostTask()
{
try
{
var stakeInfo = _data.PersonDataExtensions.StakeInfo;
var optionCount = stakeInfo.Step == 0 ? 2 : (stakeInfo.Maximum - stakeInfo.Minimum) / stakeInfo.Step + 1;

var optionIndex = 0;

switch (_account.Style)
{
case PlayerStyle.Careful:
optionIndex = 0;
break;

case PlayerStyle.Normal:
optionIndex = Random.Shared.Next(optionCount);
break;

case PlayerStyle.Agressive:
optionIndex = optionCount - 1;
break;
}

var price = stakeInfo.Minimum + optionIndex * stakeInfo.Step;
_viewerActions.SendMessage(Messages.CatCost, price.ToString());
}
catch (Exception exc)
{
_data.SystemLog.AppendFormat("Secret question price selection error: {0}", exc).AppendLine();
}
}

private void OnSelectPlayer() => OnSecretQuestionAnswererSelect(Messages.SelectPlayer);

private void OnSecretQuestionAnswererSelect(string messageType)
Expand Down Expand Up @@ -1707,28 +1577,13 @@ public void Answer() => ScheduleExecution(PlayerTasks.Answer,

public void SelectPlayer() => ScheduleExecution(PlayerTasks.SelectPlayer, 10 + Random.Shared.Next(10));

/// <summary>
/// Необходимо сделать ставку
/// </summary>
public void Stake() => ScheduleExecution(PlayerTasks.Stake, 10 + Random.Shared.Next(10));

public void StakeNew() => ScheduleExecution(PlayerTasks.StakeNew, 10 + Random.Shared.Next(10));

/// <summary>
/// Необходимо выбрать финальную тему
/// </summary>
public void ChooseFinalTheme() => ScheduleExecution(PlayerTasks.ChooseFinal, 10 + Random.Shared.Next(10));

/// <summary>
/// Необходимо сделать финальную ставку
/// </summary>
public void FinalStake() => ScheduleExecution(PlayerTasks.FinalStake, 10 + Random.Shared.Next(20));

/// <summary>
/// Необходимо выбрать стоимость Вопроса с секретом
/// </summary>
public void CatCost() => ScheduleExecution(PlayerTasks.CatCost, 15);

public void IsRight(bool voteForRight) =>
ScheduleExecution(voteForRight ? PlayerTasks.AnswerRight : PlayerTasks.AnswerWrong, 10 + Random.Shared.Next(10));

Expand Down
3 changes: 0 additions & 3 deletions src/SICore/SICore/Clients/Player/PlayerTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ internal enum PlayerTasks
Ready,
Answer,
Choose,
CatCost,
Stake,
StakeNew,
ChooseFinal,
FinalStake,
AnswerRight,
AnswerWrong,
PressButton,
Expand Down
Loading

0 comments on commit baa3cc5

Please sign in to comment.