From 1ab633ea2691399a39d19472ea9f8a969c6f4577 Mon Sep 17 00:00:00 2001 From: VladimirKhil Date: Sat, 18 Nov 2023 14:34:32 +0100 Subject: [PATCH] https://github.com/VladimirKhil/SI/issues/161 Replace inheritance with aggregation in logic classes --- src/SICore/SICore/Clients/IPersonLogic.cs | 2 +- src/SICore/SICore/Clients/Player/Player.cs | 44 +++++----- .../Clients/Player/PlayerComputerLogic.cs | 51 +++++++++--- .../SICore/Clients/Player/PlayerHumanLogic.cs | 33 ++++---- src/SICore/SICore/Clients/Showman/Showman.cs | 37 ++++----- .../Clients/Showman/ShowmanComputerLogic.cs | 34 ++++---- .../Clients/Showman/ShowmanHumanLogic.cs | 19 ++--- .../SICore/Clients/Viewer/SimpleViewer.cs | 14 +--- src/SICore/SICore/Clients/Viewer/Viewer.cs | 8 +- .../Clients/Viewer/ViewerComputerLogic.cs | 33 +------- .../SICore/Clients/Viewer/ViewerHumanLogic.cs | 4 +- src/SICore/SICore/Models/TimerInfo.cs | 16 ++++ .../ViewModel/GameViewModel.cs | 2 +- .../SIGame/View/StudiaCommandPanel.xaml.cs | 82 ++++++++++--------- 14 files changed, 189 insertions(+), 190 deletions(-) create mode 100644 src/SICore/SICore/Models/TimerInfo.cs diff --git a/src/SICore/SICore/Clients/IPersonLogic.cs b/src/SICore/SICore/Clients/IPersonLogic.cs index 6c6f4c21..1448499d 100644 --- a/src/SICore/SICore/Clients/IPersonLogic.cs +++ b/src/SICore/SICore/Clients/IPersonLogic.cs @@ -3,7 +3,7 @@ /// /// Provides common behavior for player and showman. /// -public interface IPersonLogic : IViewerLogic +public interface IPersonLogic { void OnInitialized(); diff --git a/src/SICore/SICore/Clients/Player/Player.cs b/src/SICore/SICore/Clients/Player/Player.cs index b92d2d1a..26891777 100644 --- a/src/SICore/SICore/Clients/Player/Player.cs +++ b/src/SICore/SICore/Clients/Player/Player.cs @@ -11,7 +11,7 @@ namespace SICore; /// /// Represents a game player. /// -public sealed class Player : Viewer +public sealed class Player : Viewer { private readonly object _readyLock = new(); @@ -109,11 +109,6 @@ public Player(Client client, Account personData, bool isHost, ILocalizer localiz ClientData.AutoReadyChanged += ClientData_AutoReadyChanged; } - protected override IPlayerLogic CreateLogic(Account personData) => - personData.IsHuman ? - new PlayerHumanLogic(ClientData, null, _viewerActions, LO) : - new PlayerComputerLogic(ClientData, (ComputerAccount)personData, _viewerActions); - public override ValueTask DisposeAsync(bool disposing) { ClientData.AutoReadyChanged -= ClientData_AutoReadyChanged; @@ -131,6 +126,7 @@ private void ClientData_AutoReadyChanged() } var readyCommand = ((PersonAccount)ClientData.Me).BeReadyCommand; + if (ClientData.AutoReady && readyCommand != null) { readyCommand.Execute(null); @@ -152,7 +148,7 @@ private async void ReleaseGameButton() } } - private void Clear() => _logic.Clear(); + private void Clear() => _logic.PlayerLogic.Clear(); public override void Init() { @@ -166,7 +162,7 @@ public override void Init() { var readyCommand = personAccount.BeReadyCommand = new CustomCommand(arg => _viewerActions.SendMessage(Messages.Ready)); personAccount.BeUnReadyCommand = new CustomCommand(arg => _viewerActions.SendMessage(Messages.Ready, "-")); - _logic.OnInitialized(); + _logic.PlayerLogic.OnInitialized(); if (ClientData.AutoReady) { @@ -232,12 +228,12 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams if (mparams[1] == "1") { - _logic.ChooseQuest(); + _logic.PlayerLogic.ChooseQuest(); ClientData.Hint = LO[nameof(R.HintSelectQuestion)]; } else { - _logic.ChooseFinalTheme(); + _logic.PlayerLogic.ChooseFinalTheme(); ClientData.Hint = LO[nameof(R.HintSelectTheme)]; } @@ -259,7 +255,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams break; case Messages.Atom: // deprecated - _logic.OnPlayerAtom(mparams); + _logic.PlayerLogic.OnPlayerAtom(mparams); if (ClientData.QuestionType == QuestionTypes.Simple) { @@ -272,7 +268,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams break; case Messages.Content: - _logic.OnPlayerAtom(mparams); + _logic.PlayerLogic.OnPlayerAtom(mparams); if (ClientData.QuestionType == QuestionTypes.Simple) { @@ -293,7 +289,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams ClientData.PlayerDataExtensions.MyTry = true; _buttonDisabledByGame = false; EnableGameButton(); - _logic.StartThink(); + _logic.PlayerLogic.StartThink(); break; case Messages.EndTry: @@ -302,7 +298,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams if (mparams[1] == MessageParams.EndTry_All) { - _logic.EndThink(); + _logic.PlayerLogic.EndThink(); ClientData.PlayerDataExtensions.Apellate.CanBeExecuted = ClientData.PlayerDataExtensions.ApellationCount > 0; ClientData.PlayerDataExtensions.Pass.CanBeExecuted = false; @@ -311,7 +307,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams case Messages.Answer: ClientData.PersonDataExtensions.Answer = ""; - _logic.Answer(); + _logic.PlayerLogic.Answer(); break; case Messages.Cat: @@ -324,7 +320,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams ClientData.Hint = LO[nameof(R.HintSelectCatPlayer)]; - _logic.Cat(); + _logic.PlayerLogic.Cat(); break; case Messages.CatCost: @@ -336,7 +332,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams Stake = int.Parse(mparams[1]) }; - _logic.CatCost(); + _logic.PlayerLogic.CatCost(); break; case Messages.Stake: @@ -358,7 +354,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams Stake = int.Parse(mparams[5]) }; - _logic.Stake(); + _logic.PlayerLogic.Stake(); break; case Messages.Stake2: @@ -388,7 +384,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams Stake = minimumStake }; - _logic.Stake(); + _logic.PlayerLogic.Stake(); break; case Messages.FinalStake: @@ -404,7 +400,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams ClientData.DialogMode = DialogModes.FinalStake; ((PlayerAccount)ClientData.Me).IsDeciding = false; - _logic.FinalStake(); + _logic.PlayerLogic.FinalStake(); break; case Messages.Validation: @@ -430,7 +426,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams break; } - _logic.PersonAnswered(playerIndex, isRight); + _logic.PlayerLogic.PersonAnswered(playerIndex, isRight); break; case Messages.Report: @@ -450,7 +446,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams ClientData.PlayerDataExtensions.Report.Report = report.ToString(); ClientData.DialogMode = DialogModes.Report; ((PlayerAccount)ClientData.Me).IsDeciding = false; - _logic.Report(); + _logic.PlayerLogic.Report(); break; } } @@ -464,7 +460,7 @@ private void OnValidation(string[] mparams) { ClientData.PersonDataExtensions.ValidatorName = mparams[1]; ClientData.PersonDataExtensions.Answer = mparams[2]; - _logic.IsRight(mparams[3] == "+"); + _logic.PlayerLogic.IsRight(mparams[3] == "+"); _ = int.TryParse(mparams[4], out var rightAnswersCount); rightAnswersCount = Math.Min(rightAnswersCount, mparams.Length - 5); @@ -494,7 +490,7 @@ private void OnValidation2(string[] mparams) { ClientData.PersonDataExtensions.ValidatorName = mparams[1]; ClientData.PersonDataExtensions.Answer = mparams[2]; - _logic.IsRight(mparams[3] == "+"); + _logic.PlayerLogic.IsRight(mparams[3] == "+"); _ = int.TryParse(mparams[5], out var rightAnswersCount); rightAnswersCount = Math.Min(rightAnswersCount, mparams.Length - 6); diff --git a/src/SICore/SICore/Clients/Player/PlayerComputerLogic.cs b/src/SICore/SICore/Clients/Player/PlayerComputerLogic.cs index 2b67eb48..a3c3f799 100644 --- a/src/SICore/SICore/Clients/Player/PlayerComputerLogic.cs +++ b/src/SICore/SICore/Clients/Player/PlayerComputerLogic.cs @@ -1,5 +1,6 @@ using SICore.Clients.Player; using SICore.Extensions; +using SICore.Models; using SICore.Utils; using SIData; using SIPackages.Core; @@ -11,30 +12,42 @@ namespace SICore; /// /// Логика игрока-компьютера /// -internal sealed class PlayerComputerLogic : ViewerComputerLogic, IPlayerLogic +internal sealed class PlayerComputerLogic : IPlayerLogic { private ComputerAccount _account; - //private readonly ViewerActions _viewerActions; - //private readonly ViewerData _data; + private readonly ViewerActions _viewerActions; + private readonly ViewerData _data; + + private readonly TimerInfo[] _timersInfo; /// /// Создание логики /// /// Текущий клиент - public PlayerComputerLogic(ViewerData data, ComputerAccount account, ViewerActions viewerActions) - : base(data, viewerActions, account) + public PlayerComputerLogic(ViewerData data, ComputerAccount account, ViewerActions viewerActions, TimerInfo[] timerInfos) { _account = account; - //_viewerActions = viewerActions; - //_data = data; + _viewerActions = viewerActions; + _data = data; + _timersInfo = timerInfos; } - internal void ScheduleExecution(PlayerTasks task, double taskTime) => ScheduleExecution((int)task, 0, taskTime); - - protected override void ExecuteTask(int taskId, int arg) + private async void ScheduleExecution(PlayerTasks task, double taskTime) { - var task = (PlayerTasks)taskId; + await Task.Delay((int)taskTime * 100); + + try + { + ExecuteTask(task); + } + catch (Exception exc) + { + _data.SystemLog.AppendFormat("Execution error: {0}", exc.ToString()).AppendLine(); + } + } + private void ExecuteTask(PlayerTasks task) + { switch (task) { case PlayerTasks.Ready: @@ -86,6 +99,19 @@ protected override void ExecuteTask(int taskId, int arg) } } + private int GetTimePercentage(int timerIndex) + { + var now = DateTime.UtcNow; + var timer = _timersInfo[timerIndex]; + + if (!timer.IsEnabled) + { + return timer.PauseTime > -1 ? 100 * timer.PauseTime / timer.MaxTime : 0; + } + + return (int)(100 * (now - timer.StartTime).TotalMilliseconds / (timer.EndTime - timer.StartTime).TotalMilliseconds); + } + private void PressButton() => _viewerActions.PressGameButton(); private void AnswerRight() => _viewerActions.SendMessage(Messages.IsRight, "+"); @@ -911,9 +937,8 @@ internal static StakeMode MakeStake( } /// - /// Является ли ситуация критической + /// Checks if situation is critical. /// - /// private bool IsCritical() { int numQu; diff --git a/src/SICore/SICore/Clients/Player/PlayerHumanLogic.cs b/src/SICore/SICore/Clients/Player/PlayerHumanLogic.cs index daacfad1..d9018eaa 100644 --- a/src/SICore/SICore/Clients/Player/PlayerHumanLogic.cs +++ b/src/SICore/SICore/Clients/Player/PlayerHumanLogic.cs @@ -10,13 +10,13 @@ namespace SICore; /// /// Логика игрока-человека /// -internal sealed class PlayerHumanLogic : ViewerHumanLogic, IPlayerLogic +internal sealed class PlayerHumanLogic : IPlayerLogic, IDisposable { - //private readonly ViewerActions _viewerActions; - //private readonly ViewerData _data; - //private readonly ILocalizer _localizer; + private readonly ViewerActions _viewerActions; + private readonly ViewerData _data; + private readonly ILocalizer _localizer; - //public TableInfoViewModel TInfo { get; } + public TableInfoViewModel TInfo { get; } private readonly CancellationTokenSource _cancellationTokenSource = new(); @@ -25,12 +25,11 @@ public PlayerHumanLogic( TableInfoViewModel tableInfoViewModel, ViewerActions viewerActions, ILocalizer localizer) - : base(data, viewerActions, localizer) { - //_viewerActions = viewerActions; - //_data = data; - //_localizer = localizer; - //TInfo = tableInfoViewModel; + _viewerActions = viewerActions; + _data = data; + _localizer = localizer; + TInfo = tableInfoViewModel; TInfo.QuestionSelected += PlayerClient_QuestionSelected; TInfo.ThemeSelected += PlayerClient_ThemeSelected; @@ -40,7 +39,7 @@ public PlayerHumanLogic( TInfo.SelectTheme.CanBeExecuted = false; } - private void TInfo_AnswerSelected(ItemViewModel item) => ClientData.PlayerDataExtensions.SendAnswer.Execute(item.Label); + private void TInfo_AnswerSelected(ItemViewModel item) => _data.PlayerDataExtensions.SendAnswer.Execute(item.Label); #region PlayerInterface Members @@ -75,8 +74,8 @@ public void Answer() if (TInfo.LayoutMode == LayoutMode.Simple) { - ClientData.DialogMode = DialogModes.Answer; - ((PlayerAccount)ClientData.Me).IsDeciding = false; + _data.DialogMode = DialogModes.Answer; + ((PlayerAccount)_data.Me).IsDeciding = false; StartSendingVersion(_cancellationTokenSource.Token); } @@ -165,8 +164,8 @@ private async void Greet() { await Task.Delay(2000); - AddLog(string.Format(_viewerActions.LO[nameof(R.Hint)], _data.Host.GameButtonKey)); - AddLog(_viewerActions.LO[nameof(R.PressButton)] + Environment.NewLine); + _data.OnAddString(null, string.Format(_viewerActions.LO[nameof(R.Hint)], _data.Host.GameButtonKey), LogMode.Log); + _data.OnAddString(null, _viewerActions.LO[nameof(R.PressButton)] + Environment.NewLine, LogMode.Log); } catch (ObjectDisposedException) { @@ -245,11 +244,9 @@ public void OnPlayerAtom(string[] mparams) } - protected override ValueTask DisposeAsync(bool disposing) + public void Dispose() { _cancellationTokenSource.Cancel(); _cancellationTokenSource.Dispose(); - - return base.DisposeAsync(disposing); } } diff --git a/src/SICore/SICore/Clients/Showman/Showman.cs b/src/SICore/SICore/Clients/Showman/Showman.cs index fc6b3779..647f960b 100644 --- a/src/SICore/SICore/Clients/Showman/Showman.cs +++ b/src/SICore/SICore/Clients/Showman/Showman.cs @@ -9,7 +9,7 @@ namespace SICore; /// /// Defines a showman message processor. /// -public sealed class Showman : Viewer +public sealed class Showman : Viewer { private readonly object _readyLock = new(); @@ -118,11 +118,6 @@ public Showman(Client client, Account personData, bool isHost, ILocalizer locali }); } - protected override IShowmanLogic CreateLogic(Account personData) => - personData.IsHuman ? - new ShowmanHumanLogic(ClientData, null, _viewerActions, LO) : - new ShowmanComputerLogic(ClientData, _viewerActions, (ComputerAccount)personData); - private void ClientData_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == nameof(PersonData.AreAnswersShown)) @@ -136,7 +131,7 @@ private void Manage_Executed(object? arg) ClientData.DialogMode = DialogModes.Manage; } - private void ManageTable_Executed(object? arg) => _logic.ManageTable(); + private void ManageTable_Executed(object? arg) => _logic.ShowmanLogic.ManageTable(); void ClientData_AutoReadyChanged() { @@ -164,7 +159,7 @@ public override void Init() { var readyCommand = ((PersonAccount)ClientData.Me).BeReadyCommand = new CustomCommand(arg => _viewerActions.SendMessage(Messages.Ready)); ((PersonAccount)ClientData.Me).BeUnReadyCommand = new CustomCommand(arg => _viewerActions.SendMessage(Messages.Ready, "-")); - _logic.OnInitialized(); + _logic.ShowmanLogic.OnInitialized(); if (ClientData.AutoReady) { @@ -210,7 +205,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams ClientData.Hint = LO[nameof(R.HintSelectStarter)]; - _logic.StarterChoose(); + _logic.ShowmanLogic.StarterChoose(); break; #endregion @@ -231,7 +226,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams ClientData.Hint = LO[nameof(R.HintSelectStaker)]; - _logic.FirstStake(); + _logic.ShowmanLogic.FirstStake(); break; #endregion @@ -263,7 +258,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams ClientData.Hint = LO[nameof(R.HintThemeDeleter)]; - _logic.FirstDelete(); + _logic.ShowmanLogic.FirstDelete(); break; #endregion @@ -290,12 +285,12 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams if (mparams[1] == "1") { - _logic.ChooseQuest(); + _logic.ShowmanLogic.ChooseQuest(); ClientData.Hint = LO[nameof(R.HintSelectQuestion)]; } else { - _logic.ChooseFinalTheme(); + _logic.ShowmanLogic.ChooseFinalTheme(); ClientData.Hint = LO[nameof(R.HintSelectTheme)]; } @@ -312,7 +307,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams ClientData.Hint = LO[nameof(R.HintSelectCatPlayerForPlayer)]; - _logic.Cat(); + _logic.ShowmanLogic.Cat(); break; case Messages.CatCost: @@ -324,7 +319,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams Stake = int.Parse(mparams[1]) }; - _logic.CatCost(); + _logic.ShowmanLogic.CatCost(); break; case Messages.Stake: @@ -347,7 +342,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams PlayerName = mparams.Length >= 8 ? mparams[7] : null, }; - _logic.Stake(); + _logic.ShowmanLogic.Stake(); break; case Messages.Stake2: @@ -379,7 +374,7 @@ protected override async ValueTask OnSystemMessageReceivedAsync(string[] mparams PlayerName = mparams[5], }; - _logic.Stake(); + _logic.ShowmanLogic.Stake(); break; case Messages.Pause: @@ -405,7 +400,7 @@ private void OnPause(string[] mparams) if (!isPaused) { - _logic.ManageTable(false); + _logic.ShowmanLogic.ManageTable(false); } } @@ -436,7 +431,7 @@ private void OnValidation(string[] mparams) ClientData.Hint = LO[nameof(R.HintCheckAnswer)]; ClientData.DialogMode = DialogModes.AnswerValidation; ((PersonAccount)ClientData.Me).IsDeciding = false; - _logic.IsRight(); + _logic.ShowmanLogic.IsRight(); } private void OnValidation2(string[] mparams) @@ -468,8 +463,8 @@ private void OnValidation2(string[] mparams) ClientData.DialogMode = DialogModes.AnswerValidation; ((PersonAccount)ClientData.Me).IsDeciding = false; - _logic.IsRight(); + _logic.ShowmanLogic.IsRight(); } - private void ClearSelections(bool full = false) => _logic.ClearSelections(full); + private void ClearSelections(bool full = false) => _logic.ShowmanLogic.ClearSelections(full); } diff --git a/src/SICore/SICore/Clients/Showman/ShowmanComputerLogic.cs b/src/SICore/SICore/Clients/Showman/ShowmanComputerLogic.cs index be3818a9..9d315006 100644 --- a/src/SICore/SICore/Clients/Showman/ShowmanComputerLogic.cs +++ b/src/SICore/SICore/Clients/Showman/ShowmanComputerLogic.cs @@ -6,26 +6,35 @@ namespace SICore; /// /// Логика ведущего-компьютера /// -internal sealed class ShowmanComputerLogic : ViewerComputerLogic, IShowmanLogic +internal sealed class ShowmanComputerLogic : IShowmanLogic { - //private readonly ViewerActions _viewerActions; - //private readonly ViewerData _data; + private readonly ViewerActions _viewerActions; + private readonly ViewerData _data; public ShowmanComputerLogic(ViewerData data, ViewerActions viewerActions, ComputerAccount computerAccount) - : base(data, viewerActions, computerAccount) { - //_viewerActions = viewerActions; - //_data = data; + _viewerActions = viewerActions; + _data = data; } - internal void ScheduleExecution(ShowmanTasks task, double taskTime) => ScheduleExecution((int)task, 0, taskTime); + private async void ScheduleExecution(ShowmanTasks task, double taskTime) + { + await Task.Delay((int)taskTime * 100); + + try + { + ExecuteTask(task); + } + catch (Exception exc) + { + _data.SystemLog.AppendFormat("Execution error: {0}", exc.ToString()).AppendLine(); + } + } - protected override void ExecuteTask(int taskId, int arg) + private void ExecuteTask(ShowmanTasks task) { - var task = (ShowmanTasks)taskId; switch (task) { - case ShowmanTasks.Ready: Ready(); break; @@ -95,11 +104,6 @@ private void AnswerRight() public void FirstDelete() => ScheduleExecution(ShowmanTasks.AnswerNextToDelete, 10 + Random.Shared.Next(10)); - public void ChangeSum() - { - - } - public void OnInitialized() => ScheduleExecution(ShowmanTasks.Ready, 10); public void ClearSelections(bool full = false) diff --git a/src/SICore/SICore/Clients/Showman/ShowmanHumanLogic.cs b/src/SICore/SICore/Clients/Showman/ShowmanHumanLogic.cs index 464cea92..8f3100df 100644 --- a/src/SICore/SICore/Clients/Showman/ShowmanHumanLogic.cs +++ b/src/SICore/SICore/Clients/Showman/ShowmanHumanLogic.cs @@ -7,25 +7,24 @@ namespace SICore; /// /// Логика ведущего-человека /// -internal sealed class ShowmanHumanLogic : ViewerHumanLogic, IShowmanLogic +internal sealed class ShowmanHumanLogic : IShowmanLogic { - //private readonly ViewerActions _viewerActions; - //private readonly ViewerData _data; - //private readonly ILocalizer _localizer; + private readonly ViewerActions _viewerActions; + private readonly ViewerData _data; + private readonly ILocalizer _localizer; - //public TableInfoViewModel TInfo { get; } + public TableInfoViewModel TInfo { get; } public ShowmanHumanLogic( ViewerData data, TableInfoViewModel tableInfoViewModel, ViewerActions viewerActions, ILocalizer localizer) - : base(data, viewerActions, localizer) { - //_viewerActions = viewerActions; - //_data = data; - //_localizer = localizer; - //TInfo = tableInfoViewModel; + _viewerActions = viewerActions; + _data = data; + _localizer = localizer; + TInfo = tableInfoViewModel; TInfo.QuestionSelected += PlayerClient_QuestionSelected; TInfo.ThemeSelected += PlayerClient_ThemeSelected; diff --git a/src/SICore/SICore/Clients/Viewer/SimpleViewer.cs b/src/SICore/SICore/Clients/Viewer/SimpleViewer.cs index e5a21689..602783f8 100644 --- a/src/SICore/SICore/Clients/Viewer/SimpleViewer.cs +++ b/src/SICore/SICore/Clients/Viewer/SimpleViewer.cs @@ -4,7 +4,7 @@ namespace SICore; -public sealed class SimpleViewer : Viewer +public sealed class SimpleViewer : Viewer { /// /// Запуск клиента @@ -14,16 +14,4 @@ public SimpleViewer(Client client, Account personData, bool isHost, ILocalizer l { } - - protected override IViewerLogic CreateLogic(Account personData) - { - if (personData == null) - { - throw new ArgumentNullException(nameof(personData)); - } - - return personData.IsHuman ? - new ViewerHumanLogic(ClientData, _viewerActions, LO) : - new ViewerComputerLogic(ClientData, _viewerActions, (ComputerAccount)personData); - } } diff --git a/src/SICore/SICore/Clients/Viewer/Viewer.cs b/src/SICore/SICore/Clients/Viewer/Viewer.cs index 6d282f32..36c9d69e 100644 --- a/src/SICore/SICore/Clients/Viewer/Viewer.cs +++ b/src/SICore/SICore/Clients/Viewer/Viewer.cs @@ -20,8 +20,7 @@ namespace SICore; /// /// Implements a game viewer. /// -public abstract class Viewer : Actor, IViewerClient, INotifyPropertyChanged - where L : class, IViewerLogic +public abstract class Viewer : Actor, IViewerClient, INotifyPropertyChanged { private bool _ignoreAtoms = false; @@ -221,7 +220,10 @@ protected Viewer(Client client, Account personData, bool isHost, ILocalizer loca ClientData.Picture = personData.Picture; } - protected abstract L CreateLogic(Account personData); + protected IViewerLogic CreateLogic(Account personData) => + personData.IsHuman ? + new ViewerHumanLogic(ClientData, _viewerActions, LO) : + new ViewerComputerLogic(ClientData, _viewerActions, (ComputerAccount)personData); public void Move(object arg) => _viewerActions.SendMessageWithArgs(Messages.Move, arg); diff --git a/src/SICore/SICore/Clients/Viewer/ViewerComputerLogic.cs b/src/SICore/SICore/Clients/Viewer/ViewerComputerLogic.cs index 56adb410..44aa745b 100644 --- a/src/SICore/SICore/Clients/Viewer/ViewerComputerLogic.cs +++ b/src/SICore/SICore/Clients/Viewer/ViewerComputerLogic.cs @@ -1,4 +1,5 @@ -using SIData; +using SICore.Models; +using SIData; using SIPackages; using SIUI.ViewModel; @@ -17,43 +18,17 @@ internal class ViewerComputerLogic : Logic, IViewerLogic public IShowmanLogic ShowmanLogic { get; } - protected sealed class TimerInfo - { - public bool IsEnabled { get; set; } - - public bool IsUserEnabled { get; set; } = true; - - public DateTime StartTime { get; set; } - - public DateTime EndTime { get; set; } - public int MaxTime { get; set; } - - public int PauseTime { get; set; } = -1; - } private readonly TimerInfo[] _timersInfo = new TimerInfo[] { new TimerInfo(), new TimerInfo(), new TimerInfo() }; - protected int GetTimePercentage(int timerIndex) - { - var now = DateTime.UtcNow; - var timer = _timersInfo[timerIndex]; - - if (!timer.IsEnabled) - { - return timer.PauseTime > -1 ? 100 * timer.PauseTime / timer.MaxTime : 0; - } - - return (int)(100 * (now - timer.StartTime).TotalMilliseconds / (timer.EndTime - timer.StartTime).TotalMilliseconds); - } - internal ViewerComputerLogic(ViewerData data, ViewerActions viewerActions, ComputerAccount computerAccount) : base(data) { _viewerActions = viewerActions; - //PlayerLogic = new PlayerComputerLogic(data, computerAccount, viewerActions); - //ShowmanLogic = new ShowmanComputerLogic(data, viewerActions, computerAccount); + PlayerLogic = new PlayerComputerLogic(data, computerAccount, viewerActions, _timersInfo); + ShowmanLogic = new ShowmanComputerLogic(data, viewerActions, computerAccount); } public void ReceiveText(Message m) diff --git a/src/SICore/SICore/Clients/Viewer/ViewerHumanLogic.cs b/src/SICore/SICore/Clients/Viewer/ViewerHumanLogic.cs index 0d1f39ae..91651176 100644 --- a/src/SICore/SICore/Clients/Viewer/ViewerHumanLogic.cs +++ b/src/SICore/SICore/Clients/Viewer/ViewerHumanLogic.cs @@ -53,8 +53,8 @@ public ViewerHumanLogic(ViewerData data, ViewerActions viewerActions, ILocalizer TInfo.MediaLoad += TInfo_MediaLoad; TInfo.MediaLoadError += TInfo_MediaLoadError; - //PlayerLogic = new PlayerHumanLogic(data, TInfo, viewerActions, localizer); - //ShowmanLogic = new ShowmanHumanLogic(data, TInfo, viewerActions, localizer); + PlayerLogic = new PlayerHumanLogic(data, TInfo, viewerActions, localizer); + ShowmanLogic = new ShowmanHumanLogic(data, TInfo, viewerActions, localizer); _localFileManager.Error += LocalFileManager_Error; _localFileManagerTask = _localFileManager.StartAsync(_cancellation.Token); diff --git a/src/SICore/SICore/Models/TimerInfo.cs b/src/SICore/SICore/Models/TimerInfo.cs new file mode 100644 index 00000000..0a9b06d8 --- /dev/null +++ b/src/SICore/SICore/Models/TimerInfo.cs @@ -0,0 +1,16 @@ +namespace SICore.Models; + +internal sealed class TimerInfo +{ + public bool IsEnabled { get; set; } + + public bool IsUserEnabled { get; set; } = true; + + public DateTime StartTime { get; set; } + + public DateTime EndTime { get; set; } + + public int MaxTime { get; set; } + + public int PauseTime { get; set; } = -1; +} diff --git a/src/SIGame/SIGame.ViewModel/ViewModel/GameViewModel.cs b/src/SIGame/SIGame.ViewModel/ViewModel/GameViewModel.cs index 29d3d108..d3aba18d 100644 --- a/src/SIGame/SIGame.ViewModel/ViewModel/GameViewModel.cs +++ b/src/SIGame/SIGame.ViewModel/ViewModel/GameViewModel.cs @@ -313,7 +313,7 @@ private void Host_Switch(IViewerClient newHost) private void Cancel_Executed(object? arg) { - if (Host.MyLogic is IPersonLogic logic) + if (Host.MyLogic is IViewerLogic logic) { ((ViewerData)logic.Data).DialogMode = DialogModes.None; ((ViewerData)logic.Data).Hint = ""; diff --git a/src/SIGame/SIGame/View/StudiaCommandPanel.xaml.cs b/src/SIGame/SIGame/View/StudiaCommandPanel.xaml.cs index b743b2fa..9145dda0 100644 --- a/src/SIGame/SIGame/View/StudiaCommandPanel.xaml.cs +++ b/src/SIGame/SIGame/View/StudiaCommandPanel.xaml.cs @@ -5,63 +5,65 @@ using System.Windows.Controls; using System.Windows.Media.Animation; -namespace SIGame +namespace SIGame; + +/// +/// Логика взаимодействия для StudiaCommandPanel.xaml +/// +public partial class StudiaCommandPanel : UserControl { - /// - /// Логика взаимодействия для StudiaCommandPanel.xaml - /// - public partial class StudiaCommandPanel : UserControl + private readonly Storyboard _sb; + + public StudiaCommandPanel() { - private readonly Storyboard _sb; + InitializeComponent(); - public StudiaCommandPanel() - { - InitializeComponent(); + _sb = (Storyboard)Resources["gameSB"]; + _sb.Completed += Sb_Completed; - _sb = (Storyboard)Resources["gameSB"]; - _sb.Completed += Sb_Completed; + DataContextChanged += Studia_DataContextChanged; + } - DataContextChanged += Studia_DataContextChanged; - } + private void Sb_Completed(object? sender, EventArgs e) + { + gameBorder.Visibility = Visibility.Hidden; + } - private void Sb_Completed(object sender, EventArgs e) + private void RaiseButtonClick() + { + if (gameButton.IsEnabled) { - gameBorder.Visibility = Visibility.Hidden; + gameBorder.Visibility = Visibility.Visible; + BeginStoryboard(_sb); } + } - private void RaiseButtonClick() + private void Studia_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) + { + if (DataContext is not GameViewModel game) { - if (gameButton.IsEnabled) - { - gameBorder.Visibility = Visibility.Visible; - BeginStoryboard(_sb); - } + return; } - private void Studia_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) + if (game.Host.MyLogic is IViewerLogic logic) { - if (!(DataContext is GameViewModel game)) - { - return; - } + ((ViewerData)logic.Data).PlayerDataExtensions.PressButton += RaiseButtonClick; + } + } - if (game.Host.MyLogic is IPlayerLogic logic) - { - ((ViewerData)logic.Data).PlayerDataExtensions.PressButton += RaiseButtonClick; - } + public void OnMouseRightButtonDown() + { + if (((GameViewModel)DataContext)?.Host?.MyLogic is not IViewerLogic logic) + { + return; } - public void OnMouseRightButtonDown() + var pressCmd = ((ViewerData)logic.Data).PlayerDataExtensions.PressGameButton; + + if (pressCmd != null && pressCmd.CanBeExecuted) { - if (((GameViewModel)DataContext)?.Host?.MyLogic is IPlayerLogic logic) - { - var pressCmd = ((ViewerData)logic.Data).PlayerDataExtensions.PressGameButton; - if (pressCmd != null && pressCmd.CanBeExecuted) - { - RaiseButtonClick(); - pressCmd.Execute(null); - } - } + RaiseButtonClick(); + pressCmd.Execute(null); } } }