From 8bf88ce301c58ec146f2fe8adf7f8c0910e46200 Mon Sep 17 00:00:00 2001 From: "ace.aleks93" Date: Thu, 14 Jun 2018 18:08:25 +0200 Subject: [PATCH 01/17] Added a DealNoDeal Game --- .../Automation/AutomatedActionFactory.cs | 39 ++ .../Automation/ChoseStartingBoxesAction.cs | 48 +++ .../Automation/IAutomatedActionFactory.cs | 13 + .../Automation/OfferDealAction.cs | 44 +++ .../Automation/PickBoxesAction.cs | 40 +++ .../DevChatterBotCoreModule.cs | 4 + .../Games/DealNoDeal/Box.cs | 37 ++ .../Games/DealNoDeal/DNDCommand.cs | 59 +++ .../Games/DealNoDeal/DealNoDealGame.cs | 340 ++++++++++++++++++ .../Games/DealNoDeal/DealNoDealGameState.cs | 14 + .../Games/DealNoDeal/MakeADealOperation.cs | 66 ++++ .../Games/DealNoDeal/PickABoxOperation.cs | 88 +++++ .../Events/TwitchFollowerService.cs | 2 +- .../TwitchChatClient.cs | 7 +- 14 files changed, 797 insertions(+), 4 deletions(-) create mode 100644 src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs create mode 100644 src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs create mode 100644 src/DevChatter.Bot.Core/Automation/IAutomatedActionFactory.cs create mode 100644 src/DevChatter.Bot.Core/Automation/OfferDealAction.cs create mode 100644 src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs create mode 100644 src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs create mode 100644 src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs create mode 100644 src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs create mode 100644 src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGameState.cs create mode 100644 src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs create mode 100644 src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs diff --git a/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs b/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs new file mode 100644 index 00000000..c2cfa034 --- /dev/null +++ b/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DevChatter.Bot.Core.Games.DealNoDeal; +using DevChatter.Bot.Core.Systems.Chat; +using DevChatter.Bot.Core.Util; + +namespace DevChatter.Bot.Core.Automation +{ + public class AutomatedActionFactory : IAutomatedActionFactory + { + private readonly DealNoDealGame _dealNoDealGame; + private readonly IChatClient _chatClient; + + public AutomatedActionFactory(DealNoDealGame dealNoDealGame, IChatClient chatClient) + { + _dealNoDealGame = dealNoDealGame; + _chatClient = chatClient; + } + public IIntervalAction GetIntervalAction(DealNoDealGameState gameState) + { + if (gameState == DealNoDealGameState.CHOSING_STARTING_BOXES) + { + return new ChoseStartingBoxesAction(_dealNoDealGame,_chatClient,new SystemClock()); + } + if (gameState == DealNoDealGameState.PICKING_BOXES) + { + return new PickBoxesAction(_dealNoDealGame, _chatClient, new SystemClock()); + } + if (gameState == DealNoDealGameState.MAKING_A_DEAL) + { + return new OfferDealAction(_dealNoDealGame, _chatClient, new SystemClock()); + } + + return null; + } + + } +} diff --git a/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs b/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs new file mode 100644 index 00000000..7f0562a9 --- /dev/null +++ b/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DevChatter.Bot.Core.Data.Model; +using DevChatter.Bot.Core.Games.DealNoDeal; +using DevChatter.Bot.Core.Systems.Chat; +using DevChatter.Bot.Core.Util; + + +namespace DevChatter.Bot.Core.Automation +{ + public class ChoseStartingBoxesAction : IIntervalAction + { + private readonly IClock _clock; + private DateTime _nextRunTime; + private IChatClient _chatClient; + private DealNoDealGame _dealNoDealGame; + + + public ChoseStartingBoxesAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IClock clock) + { + _dealNoDealGame = dealNoDealGame; + _chatClient = chatClient; + _clock = clock; + _chatClient.SendMessage($"everyone has {DealNoDealGame.SECONDS_TO_CHOSE_BOXES} seconds to choose a box!"); + SetNextRunTime(); + } + + public bool IsTimeToRun() => _clock.Now >= _nextRunTime; + + public void Invoke() + { + _nextRunTime = _clock.Now.AddYears(1); + _chatClient.SendMessage("Finished picking starting boxes"); + + + _dealNoDealGame.EnsureMinPlayableBoxes(); + _chatClient.SendMessage("Game Started!"); + _dealNoDealGame.GameState = DealNoDealGameState.PICKING_BOXES; + _dealNoDealGame.SetActionForGameState(DealNoDealGameState.PICKING_BOXES); + } + + private void SetNextRunTime() + { + _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOSE_BOXES); + } + } +} diff --git a/src/DevChatter.Bot.Core/Automation/IAutomatedActionFactory.cs b/src/DevChatter.Bot.Core/Automation/IAutomatedActionFactory.cs new file mode 100644 index 00000000..94883b5c --- /dev/null +++ b/src/DevChatter.Bot.Core/Automation/IAutomatedActionFactory.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DevChatter.Bot.Core.Games.DealNoDeal; +using DevChatter.Bot.Core.Systems.Chat; + +namespace DevChatter.Bot.Core.Automation +{ + public interface IAutomatedActionFactory + { + IIntervalAction GetIntervalAction(DealNoDealGameState gameState); + } +} diff --git a/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs b/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs new file mode 100644 index 00000000..45681041 --- /dev/null +++ b/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DevChatter.Bot.Core.Games.DealNoDeal; +using DevChatter.Bot.Core.Systems.Chat; +using DevChatter.Bot.Core.Util; + +namespace DevChatter.Bot.Core.Automation +{ + public class OfferDealAction : IIntervalAction + { + private readonly IClock _clock; + private DateTime _nextRunTime; + private IChatClient _chatClient; + private DealNoDealGame _dealNoDealGame; + + + public OfferDealAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IClock clock) + { + _dealNoDealGame = dealNoDealGame; + _chatClient = chatClient; + _clock = clock; + _dealNoDealGame.PrintBoxesRemaining(); + _chatClient.SendMessage($"***DEAL OFFER: {dealNoDealGame.DealOffer} TOKENS!"); + _chatClient.SendMessage(" type \"!dnd accept\" or \"!dnd decline\" to accept or decline offer "); + _chatClient.SendMessage($"You have {DealNoDealGame.SECONDS_TO_CHOSE_BOXES} seconds or the deal will be automatically accepted"); + SetNextRunTime(); + } + + public bool IsTimeToRun() => _clock.Now >= _nextRunTime; + + + public void Invoke() + { + _chatClient.SendMessage($"Automatically accepting deal! {DealNoDealGame.SECONDS_TO_CHOSE_BOXES} seconds passed"); + _dealNoDealGame.AcceptDeal(_chatClient); + } + + private void SetNextRunTime() + { + _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOSE_BOXES); + } + } +} diff --git a/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs b/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs new file mode 100644 index 00000000..7e219da7 --- /dev/null +++ b/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DevChatter.Bot.Core.Games.DealNoDeal; +using DevChatter.Bot.Core.Systems.Chat; +using DevChatter.Bot.Core.Util; + +namespace DevChatter.Bot.Core.Automation +{ + public class PickBoxesAction : IIntervalAction + { + private readonly IClock _clock; + private DateTime _nextRunTime; + private IChatClient _chatClient; + private DealNoDealGame _dealNoDealGame; + + + public PickBoxesAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IClock clock) + { + _dealNoDealGame = dealNoDealGame; + _chatClient = chatClient; + _clock = clock; + _dealNoDealGame.PrintBoxesRemaining(); + SetNextRunTime(); + } + + public bool IsTimeToRun() => _clock.Now >= _nextRunTime; + + public void Invoke() + { + _chatClient.SendMessage("Picking a random Box. User did not respond"); + _dealNoDealGame.PickRandomBox(_dealNoDealGame._MainPlayer.DisplayName); + } + + private void SetNextRunTime() + { + _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOSE_BOXES); + } + } +} diff --git a/src/DevChatter.Bot.Core/DevChatterBotCoreModule.cs b/src/DevChatter.Bot.Core/DevChatterBotCoreModule.cs index cb1d3050..fa83cbda 100644 --- a/src/DevChatter.Bot.Core/DevChatterBotCoreModule.cs +++ b/src/DevChatter.Bot.Core/DevChatterBotCoreModule.cs @@ -5,6 +5,7 @@ using DevChatter.Bot.Core.Commands.Trackers; using DevChatter.Bot.Core.Data; using DevChatter.Bot.Core.Events; +using DevChatter.Bot.Core.Games.DealNoDeal; using DevChatter.Bot.Core.Games.Hangman; using DevChatter.Bot.Core.Games.Heist; using DevChatter.Bot.Core.Games.Quiz; @@ -43,6 +44,9 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().AsImplementedInterfaces().SingleInstance(); builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsImplementedInterfaces().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); + builder.RegisterType().AsSelf().SingleInstance(); builder.RegisterType().AsImplementedInterfaces().SingleInstance(); diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs new file mode 100644 index 00000000..5ac73e32 --- /dev/null +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DevChatter.Bot.Core.Games.DealNoDeal +{ + public class Box + { + public Box(int id, int value, string owner) + { + Id = id; + TokenValue = value; + Owner = owner; + } + public Box(int id, int value) + { + Id = id; + TokenValue = value; + } + + + public int Id { get; set; } + public int TokenValue { get; set; } + public string Owner { get; set; } + + public bool SetOwner(string ownerName) + { + if (Owner != null) + { + return false; + } + + Owner = ownerName; + return true; + } + } +} diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs new file mode 100644 index 00000000..e969a2da --- /dev/null +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs @@ -0,0 +1,59 @@ +using DevChatter.Bot.Core.Commands; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using DevChatter.Bot.Core.Commands.Operations; +using DevChatter.Bot.Core.Data; +using DevChatter.Bot.Core.Data.Model; +using DevChatter.Bot.Core.Events; +using DevChatter.Bot.Core.Events.Args; +using DevChatter.Bot.Core.Systems.Chat; + +namespace DevChatter.Bot.Core.Games.DealNoDeal +{ + public class DNDCommand : BaseCommand, IGameCommand + { + private readonly DealNoDealGame _dealNoDealGame; + public IGame Game => _dealNoDealGame; + + public DNDCommand(IRepository repository, DealNoDealGame dealNoDealGame, IChatClient chatClient, ICurrencyGenerator currencyGenerator) : base(repository, UserRole.Everyone) + { + _dealNoDealGame = dealNoDealGame; + _chatClient = chatClient; + _currencyGenerator = currencyGenerator; + HelpText = + "Use \"!dnd\" to start a game. Use \"!dnd pick x\" to pick/guess a box. Use \"!dnd accept\" or \"!dnd decline\" to accept or decline offers ."; + } + private List _operations; + private IChatClient _chatClient; + private ICurrencyGenerator _currencyGenerator; + + public List Operations => _operations ?? (_operations = new List + { + new PickABoxOperation(_dealNoDealGame,_chatClient,_currencyGenerator), + new MakeADealOperation(_dealNoDealGame,_chatClient) + }); + + protected override void HandleCommand(IChatClient chatClient, CommandReceivedEventArgs eventArgs) + { + string argumentOne = eventArgs?.Arguments?.FirstOrDefault(); + ChatUser chatUser = eventArgs?.ChatUser; + //dnd start dnd pick + var operationToUse = Operations.SingleOrDefault(x => x.ShouldExecute(argumentOne)); + if (operationToUse != null) + { + string resultMessage = operationToUse.TryToExecute(eventArgs); + } + else + { + if (string.IsNullOrWhiteSpace(argumentOne)) + { + // attempting to start a game + _dealNoDealGame.AttemptToStartGame(chatUser); + } + } + } + + } +} diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs new file mode 100644 index 00000000..5237b938 --- /dev/null +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs @@ -0,0 +1,340 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using DevChatter.Bot.Core.Automation; +using DevChatter.Bot.Core.Data; +using DevChatter.Bot.Core.Data.Model; +using DevChatter.Bot.Core.Events; +using DevChatter.Bot.Core.Systems.Chat; +using DevChatter.Bot.Core.Util; + +namespace DevChatter.Bot.Core.Games.DealNoDeal +{ + public class DealNoDealGame : IGame + { + public bool IsRunning { get; private set; } + + //Configuration + public const UserRole ROLE_REQUIRE_TO_START = UserRole.Everyone; + public const int SECONDS_TO_CHOSE_BOXES = 30; + public const int TOKENS_TO_PLAY = 25; + public const int MIN_PLAYABLE_BOXES = 5; + //Change these if you don't like the rewards + private static readonly List InitialPrices = new List() + { + //15 at the moment + 0,1,2,4,6,8,10,15,18,20,25,32,50,64,100 + }; + + public List StartingBoxes; + public List BoxesWithOwners; + + public DealNoDealGameState GameState = DealNoDealGameState.GAME_NOT_RUNNING; + private IIntervalAction ActionBeingPerformed; + + public ChatUser _MainPlayer; + public int DealOffer = 0; + + + public DealNoDealGame(IChatClient chatClient,ICurrencyGenerator currencyGenerator, IAutomatedActionSystem automatedActionSystem) + { + _chatClient = chatClient; + _automatedActionFactory = new AutomatedActionFactory(this, _chatClient); + _currencyGenerator = currencyGenerator; + _automatedActionSystem = automatedActionSystem; + } + + private IChatClient _chatClient; + private readonly ICurrencyGenerator _currencyGenerator; + private readonly IAutomatedActionSystem _automatedActionSystem; + private readonly AutomatedActionFactory _automatedActionFactory; + + + public void AttemptToStartGame(ChatUser chatUser) + { + + if (IsRunning) + { + SendGameAlreadyStartedMessage(_chatClient, chatUser); + return; + } + + if (!chatUser.IsInThisRoleOrHigher(ROLE_REQUIRE_TO_START)) + { + _chatClient.SendMessage( + $"You must be at least a {ROLE_REQUIRE_TO_START} to start a game, {chatUser.DisplayName}"); + return; + } + bool transactionComplete = ChargeTokensForStartingAGame(_chatClient, chatUser); + if (transactionComplete) + { + _MainPlayer = chatUser; + IsRunning = true; + BoxesWithOwners = new List(); + StartingBoxes = ShuffleBoxes(); + _chatClient.SendMessage( + $"{_MainPlayer.DisplayName} started a Deal or No Deal game! Please choose a box everyone by typing \"!dnd pick x\" ... Pick a box between numbers 1 and 15"); + + GameState = DealNoDealGameState.CHOSING_STARTING_BOXES; + SetActionForGameState(DealNoDealGameState.CHOSING_STARTING_BOXES); + } + else + { + _chatClient.SendMessage($"Not enough tokens to start a game, {chatUser}" ); + } + + } + + public void SetActionForGameState(DealNoDealGameState gameState) + { + if (gameState == DealNoDealGameState.GAME_NOT_RUNNING) + { + _automatedActionSystem.RemoveAction(ActionBeingPerformed); + return; + } + //Remove all Game-actions and start a new one + _automatedActionSystem.RemoveAction(ActionBeingPerformed); + ActionBeingPerformed = _automatedActionFactory.GetIntervalAction(gameState); + _automatedActionSystem.AddAction(ActionBeingPerformed); + } + + public void QuitGame(IChatClient chatClient) + { + IsRunning = false; + _MainPlayer = null; + DealOffer = 0; + GameState = DealNoDealGameState.GAME_NOT_RUNNING; + SetActionForGameState(DealNoDealGameState.GAME_NOT_RUNNING); + } + private void SendGameAlreadyStartedMessage(IChatClient chatClient, ChatUser chatUser) + { + chatClient.SendMessage( + $"There is already a {nameof(DealNoDealGame)} running by {_MainPlayer.DisplayName}, {chatUser.DisplayName}. Wait for the game to finish!"); + } + + private void SendGameNotStartedMessage(IChatClient chatClient, ChatUser chatUser) + { + chatClient.SendMessage($"There's no {nameof(DealNoDealGame)} running, {chatUser.DisplayName}."); + } + private bool ChargeTokensForStartingAGame(IChatClient chatClient, ChatUser chatUser) + { + chatClient.SendMessage($"{TOKENS_TO_PLAY} tokens are being charged for starting a game."); + return _currencyGenerator.RemoveCurrencyFrom(chatUser.DisplayName, TOKENS_TO_PLAY); + } + public void UpdateGameState() + { + if (CheckIfGameIsWon()) + { + return; + } + + //Note: if the box count is between 2 and totalcount - 2 + int totalPlayableBoxesCount = InitialPrices.Count - StartingBoxes.Count; + bool shouldOfferDeal = (BoxesWithOwners.Count < totalPlayableBoxesCount - 2) && + (BoxesWithOwners.Count != 1) && + (BoxesWithOwners.Count % 2 == 0); + if (shouldOfferDeal) + { + DealOffer = GetADeal(); + GameState = DealNoDealGameState.MAKING_A_DEAL; + SetActionForGameState(DealNoDealGameState.MAKING_A_DEAL); + } + else + { + //Restart timer + GameState = DealNoDealGameState.PICKING_BOXES; + SetActionForGameState(DealNoDealGameState.PICKING_BOXES); + } + } + + //Note: We can randomize this as we please + private int GetADeal() + { + //average + int Result = BoxesWithOwners.Sum(b => b.TokenValue) / BoxesWithOwners.Count; + + //randomize offer + Random r = new Random(); + bool shouldSubtract = r.Next(2) == 0; + int value = r.Next(0, 5); + //if the offer is 10. the offer would be subtracted or added either 5 or 15 + if (shouldSubtract) + { + Result -= value; + } + else + { + Result += value; + } + + //Ensure offer is > 0 + if (Result <= 0) + { + Result = 1; + } + return Result; + } + public void AcceptDeal(IChatClient chatClient) + { + _currencyGenerator.AddCurrencyTo(_MainPlayer.DisplayName, DealOffer); + chatClient.SendMessage($" {_MainPlayer.DisplayName} ACCEPTED THE OFFER!"); + chatClient.SendMessage($"GAME OVER! {_MainPlayer.DisplayName} recieved {DealOffer} tokens!"); + DealOffer = 0; + QuitGame(chatClient); + } + public void DeclineDeal(IChatClient chatClient) + { + chatClient.SendMessage($"{_MainPlayer.DisplayName} declined the Deal of {DealOffer} tokens!"); + DealOffer = 0; + GameState = DealNoDealGameState.PICKING_BOXES; + SetActionForGameState(DealNoDealGameState.PICKING_BOXES); + } + public bool CheckIfGameIsWon() + { + if (BoxesWithOwners.Count > 1) + { + return false; + } + else if (BoxesWithOwners.Count == 1) + { + int LastTokens = BoxesWithOwners.FirstOrDefault().TokenValue; + _chatClient.SendMessage($"GAME FINISHED!"); + _chatClient.SendMessage($"A LAST BOX WITH {LastTokens} TOKENS REMAINS!"); + _chatClient.SendMessage($"{_MainPlayer.DisplayName} recieved {LastTokens} tokens! "); + _currencyGenerator.AddCurrencyTo(_MainPlayer.DisplayName, LastTokens); + QuitGame(_chatClient); + return true; + } + + return false; + } + private List ShuffleBoxes() + { + List ShuffledBoxes = new List(); + List PricesToShfufle = new List(InitialPrices); + Random r = new Random(); + int TotalBoxesCount = InitialPrices.Count; + + for (int i = 0; i < TotalBoxesCount; i++) + { + //get a new random with a range of left indexes + int indexToRemoveAt = r.Next(TotalBoxesCount - i); + ShuffledBoxes.Add(new Box(i, PricesToShfufle[indexToRemoveAt])); + PricesToShfufle.RemoveAt(indexToRemoveAt); + } + return ShuffledBoxes; + } + public void PickRandomBox(string DisplayName) + { + Random randy = new Random(); + Box randomBox = BoxesWithOwners[randy.Next(BoxesWithOwners.Count)]; + string boxValue = PickABox(randomBox.Owner, DisplayName); + } + + public string PickABox(string namePicked, string DisplayName) + { + Box chosenBox = BoxesWithOwners.Find(b => b.Owner.ToLower() == namePicked.ToLower()); + if (chosenBox == null) + { + _chatClient.SendMessage("That user does not hold a box, please chose another one"); + return null; + } + else + { + //OpenBox + _chatClient.SendMessage($"{DisplayName}, opened a box with a value of {chosenBox.TokenValue}"); + BoxesWithOwners.Remove(chosenBox); + UpdateGameState(); + } + + return GetBoxValue(chosenBox.TokenValue); + } + public void PrintBoxesRemaining() + { + string allPrices = "Prices left: "; + + //Printe all the prices left in order + foreach (var boxPrice in InitialPrices) + { + if (BoxesWithOwners.SingleOrDefault(b => b.TokenValue == boxPrice) != null) + { + allPrices += $"[{boxPrice}]"; + } + } + _chatClient.SendMessage(allPrices); + + //print all randomized boxes remaining + string allBoxes = $"Boxes remaining: {BoxesWithOwners.Count}. choose from the following: "; + foreach (var box in BoxesWithOwners) + { + allBoxes += "[" + box.Owner + "] "; + } + allBoxes += $" type \"!dnd pick x\" "; + _chatClient.SendMessage(allBoxes); + } + public void EnsureMinPlayableBoxes() + { + if (StartingBoxes.Count <= 0) return; + if (BoxesWithOwners.Count > MIN_PLAYABLE_BOXES) return; + Random randy = new Random(); + for (int i = BoxesWithOwners.Count; i < 5; i++) + { + int index = randy.Next(StartingBoxes.Count); + Box box = StartingBoxes[index]; + BoxesWithOwners.Add(new Box(box.Id, box.TokenValue, "RandomChoice" + (i + 1))); + StartingBoxes.Remove(box); + } + } + public string GetBoxValue(int tokenValue) + { + /* + * 33% chance that you will get lied. + * The tokenvalue will either be between some values or the exact one + */ + string Result = ""; + Random randy = new Random(); + bool shouldLie = randy.Next(3) == 0; + int randomBoxValue = 0; + if (shouldLie) + { + //Lie to the user + randomBoxValue = BoxesWithOwners[randy.Next(BoxesWithOwners.Count)].TokenValue; + Result = GetAproximateValue(randomBoxValue); + } + else + { + //dont lie but tell him aproximately + Result = GetAproximateValue(tokenValue); + } + return Result; + } + + public string GetAproximateValue(int value) + { + //NOTE: This just spits out a value around -20 value +20 or the value + string Result = ""; + Random randy = new Random(); + + int r = randy.Next(10); + //every 4th value should be concrete + if (r % 4 == 0) + { + //return the exact one + return value.ToString(); + } + else + { + //return aproximate value + int lowerValue = value - r - randy.Next(10); + if (lowerValue < 0) lowerValue = 0; + + int higherValue = value + r + randy.Next(10); + Result = "Between " + (lowerValue).ToString() + " - " + (higherValue).ToString(); + } + + return Result; + + } + } +} diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGameState.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGameState.cs new file mode 100644 index 00000000..1ff797af --- /dev/null +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGameState.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DevChatter.Bot.Core.Games.DealNoDeal +{ + public enum DealNoDealGameState + { + GAME_NOT_RUNNING, + CHOSING_STARTING_BOXES, + PICKING_BOXES, + MAKING_A_DEAL, + } +} diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs new file mode 100644 index 00000000..a15d15f6 --- /dev/null +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DevChatter.Bot.Core.Commands.Operations; +using DevChatter.Bot.Core.Events; +using DevChatter.Bot.Core.Events.Args; +using DevChatter.Bot.Core.Systems.Chat; + +namespace DevChatter.Bot.Core.Games.DealNoDeal +{ + public class MakeADealOperation : BaseCommandOperation + { + private DealNoDealGame _dealNoDealGame; + private IChatClient _chatClient; + + public MakeADealOperation(DealNoDealGame dealNoDealGame, IChatClient chatClient) + { + _chatClient = chatClient; + _dealNoDealGame = dealNoDealGame; + } + + public override List OperandWords { get; } = new List() {"accept", "decline"}; + public override string HelpText { get; } = "Use \"!dnd accept\" or \"!dnd decline\" to accept or decline the offer."; + + public override string TryToExecute(CommandReceivedEventArgs eventArgs) + { + if (!_dealNoDealGame.IsRunning) + { + return null; + } + + bool isMainPlayer = _dealNoDealGame._MainPlayer != eventArgs.ChatUser; + string pick = eventArgs.Arguments[0]; + if (string.IsNullOrWhiteSpace(pick)) + { + _chatClient.SendMessage(HelpText); + return null; + } + if (_dealNoDealGame.GameState == DealNoDealGameState.MAKING_A_DEAL) + { + //Note: Only other players can choose a starting box + if (isMainPlayer) + { + + if (_dealNoDealGame.DealOffer == 0) + { + _chatClient.SendMessage("Something went wrong, the deal offer is 0"); + return ""; + } + + if (pick == "accept") + { + _dealNoDealGame.AcceptDeal(_chatClient); + return "Deal Accepted"; + } + else if(pick == "decline") + { + _dealNoDealGame.DeclineDeal(_chatClient); + return "Deal Declined"; + } + } + } + return ""; + } + } +} diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs new file mode 100644 index 00000000..598cd8e9 --- /dev/null +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs @@ -0,0 +1,88 @@ +using System.Collections.Generic; +using System.Linq; +using DevChatter.Bot.Core.Commands.Operations; +using DevChatter.Bot.Core.Events; +using DevChatter.Bot.Core.Events.Args; +using DevChatter.Bot.Core.Systems.Chat; + +namespace DevChatter.Bot.Core.Games.DealNoDeal +{ + /* + * Picking starting boxes and the MainPlayer choosing a box are the same operation, + * based on CHOSING_STARTING_BOXES or PICKING_BOXES the proper operations are performed + * the OperandWords are the same !dnd pick x + */ + public class PickABoxOperation : BaseCommandOperation + { + private DealNoDealGame _dealNoDealGame; + private IChatClient _chatClient; + private ICurrencyGenerator _currencyGenerator; + + + public PickABoxOperation(DealNoDealGame dealNoDealGame, IChatClient chatClient, + ICurrencyGenerator currencyGenerator) + { + _currencyGenerator = currencyGenerator; + _chatClient = chatClient; + _dealNoDealGame = dealNoDealGame; + } + + public override List OperandWords { get; } = new List(){"pick"}; + public override string HelpText { get; } = + "Use \"!dnd pick x\" to pick/guess a box."; + + public override string TryToExecute(CommandReceivedEventArgs eventArgs) + { + if (!_dealNoDealGame.IsRunning) + { + return ""; + } + + bool isMainPlayer = _dealNoDealGame._MainPlayer != eventArgs.ChatUser; + string namePicked = eventArgs.Arguments[1]; + if (string.IsNullOrWhiteSpace(namePicked)) + { + return ""; + } + + if (_dealNoDealGame.GameState == DealNoDealGameState.CHOSING_STARTING_BOXES) + { + //Note: Only other players can choose a starting box + if (!isMainPlayer) + { + bool alreadyPickedABox = _dealNoDealGame.BoxesWithOwners.Exists(b => b.Owner == eventArgs.ChatUser.DisplayName); + if (alreadyPickedABox) + { + _chatClient.SendDirectMessage(eventArgs.ChatUser.DisplayName, "You already picked a box"); + //return ""; + } + + Box chosenBox = _dealNoDealGame.StartingBoxes.Find(b => b.Id == int.Parse(namePicked)); + if (chosenBox == null) + { + _chatClient.SendDirectMessage(eventArgs.ChatUser.DisplayName,"That box number is not available, please chose a different number"); + return ""; + } + else + { + //AddBox + _chatClient.SendDirectMessage(eventArgs.ChatUser.DisplayName, $"Your box value is {_dealNoDealGame.GetBoxValue(chosenBox.TokenValue)}"); + chosenBox.SetOwner(eventArgs.ChatUser.DisplayName); + _dealNoDealGame.BoxesWithOwners.Add(chosenBox); + _dealNoDealGame.StartingBoxes.Remove(chosenBox); + } + } + } + if (_dealNoDealGame.GameState == DealNoDealGameState.PICKING_BOXES) + { + if (isMainPlayer) + { + //Pick a box + _dealNoDealGame.PickABox(namePicked, eventArgs.ChatUser.DisplayName); + } + } + return ""; + } + + } +} diff --git a/src/DevChatter.Bot.Infra.Twitch/Events/TwitchFollowerService.cs b/src/DevChatter.Bot.Infra.Twitch/Events/TwitchFollowerService.cs index 9c810555..6a2b3619 100644 --- a/src/DevChatter.Bot.Infra.Twitch/Events/TwitchFollowerService.cs +++ b/src/DevChatter.Bot.Infra.Twitch/Events/TwitchFollowerService.cs @@ -33,7 +33,7 @@ public TwitchFollowerService(ITwitchAPI twitchApi, TwitchClientSettings settings _followerService.SetChannelByChannelId(settings.TwitchChannelID); - _followerService.StartService().Wait(); + // _followerService.StartService().Wait(); _followerService.OnNewFollowersDetected += FollowerServiceOnOnNewFollowersDetected; } diff --git a/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs b/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs index 477eb99b..8851fc78 100644 --- a/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs +++ b/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs @@ -32,9 +32,9 @@ public TwitchChatClient(TwitchClientSettings settings, ITwitchAPI twitchApi, ILo _settings = settings; _twitchApi = twitchApi; _logger = logger; - var credentials = new ConnectionCredentials(settings.TwitchUsername, settings.TwitchBotOAuth); + var credentials = new ConnectionCredentials("aflamebot", "oauth:dv6wwmdfyy374c47r2p2s4vloz7pnc"); _twitchClient = new TwitchClient(); - _twitchClient.Initialize(credentials, channel:settings.TwitchChannel); + _twitchClient.Initialize(credentials, "aflamebot"); _twitchClient.OnChatCommandReceived += ChatCommandReceived; _twitchClient.OnNewSubscriber += NewSubscriber; _twitchClient.OnUserJoined += TwitchClientOnOnUserJoined; @@ -72,6 +72,7 @@ public async Task Connect() private void TwitchClientConnected(object sender, OnConnectedArgs onConnectedArgs) { + _logger.LogInformation($"{nameof(TwitchChatClient)} connected"); _twitchClient.OnConnected -= TwitchClientConnected; @@ -103,7 +104,7 @@ public void SendMessage(string message) { if (_isReady) { - _twitchClient.SendMessage(_settings.TwitchChannel, message); + _twitchClient.SendMessage("aflamebot", message); } } From 930af06d7031ab6eab09e6ee685fe6f2c9b49c64 Mon Sep 17 00:00:00 2001 From: "ace.aleks93" Date: Thu, 14 Jun 2018 18:17:48 +0200 Subject: [PATCH 02/17] reset changes of using my bot credentials back to normal --- .../Events/TwitchFollowerService.cs | 2 +- src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/DevChatter.Bot.Infra.Twitch/Events/TwitchFollowerService.cs b/src/DevChatter.Bot.Infra.Twitch/Events/TwitchFollowerService.cs index 6a2b3619..9c810555 100644 --- a/src/DevChatter.Bot.Infra.Twitch/Events/TwitchFollowerService.cs +++ b/src/DevChatter.Bot.Infra.Twitch/Events/TwitchFollowerService.cs @@ -33,7 +33,7 @@ public TwitchFollowerService(ITwitchAPI twitchApi, TwitchClientSettings settings _followerService.SetChannelByChannelId(settings.TwitchChannelID); - // _followerService.StartService().Wait(); + _followerService.StartService().Wait(); _followerService.OnNewFollowersDetected += FollowerServiceOnOnNewFollowersDetected; } diff --git a/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs b/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs index 4be8e8f1..48455d80 100644 --- a/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs +++ b/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs @@ -33,9 +33,9 @@ public TwitchChatClient(TwitchClientSettings settings, ITwitchAPI twitchApi, ILo _settings = settings; _twitchApi = twitchApi; _logger = logger; - var credentials = new ConnectionCredentials("aflamebot", "oauth:dv6wwmdfyy374c47r2p2s4vloz7pnc"); + var credentials = new ConnectionCredentials(settings.TwitchUsername, settings.TwitchBotOAuth); _twitchClient = new TwitchClient(); - _twitchClient.Initialize(credentials, "aflamebot"); + _twitchClient.Initialize(credentials, channel: settings.TwitchChannel); _twitchClient.OnChatCommandReceived += ChatCommandReceived; _twitchClient.OnNewSubscriber += NewSubscriber; _twitchClient.OnUserJoined += TwitchClientOnOnUserJoined; From 8dc398d8e166288c0a3e1287a1a936c192f07756 Mon Sep 17 00:00:00 2001 From: "ace.aleks93" Date: Thu, 14 Jun 2018 18:19:56 +0200 Subject: [PATCH 03/17] fixed a stupid bug where the bot joined my channel only --- src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs b/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs index 48455d80..14726310 100644 --- a/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs +++ b/src/DevChatter.Bot.Infra.Twitch/TwitchChatClient.cs @@ -105,7 +105,7 @@ public void SendMessage(string message) { if (_isReady) { - _twitchClient.SendMessage("aflamebot", message); + _twitchClient.SendMessage(_settings.TwitchChannel, message); } } From 3aacc746b7f8da6eb7ae0bcaf80773e9b836ab9d Mon Sep 17 00:00:00 2001 From: Aleksandar Date: Thu, 14 Jun 2018 19:33:01 +0200 Subject: [PATCH 04/17] Update PickABoxOperation.cs fixed user being able to pick boxes multiple times --- src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs index 598cd8e9..db49e8c2 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs @@ -54,7 +54,7 @@ public override string TryToExecute(CommandReceivedEventArgs eventArgs) if (alreadyPickedABox) { _chatClient.SendDirectMessage(eventArgs.ChatUser.DisplayName, "You already picked a box"); - //return ""; + return ""; } Box chosenBox = _dealNoDealGame.StartingBoxes.Find(b => b.Id == int.Parse(namePicked)); From c54d81b1a0c21cca0b2056c312aae727c3622bea Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Sat, 16 Jun 2018 16:19:21 -0400 Subject: [PATCH 05/17] Usings cleanup and quick rename --- .../Games/DealNoDeal/DealNoDealGame.cs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs index 5237b938..ef9dd1ae 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs @@ -1,13 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using DevChatter.Bot.Core.Automation; -using DevChatter.Bot.Core.Data; using DevChatter.Bot.Core.Data.Model; using DevChatter.Bot.Core.Events; using DevChatter.Bot.Core.Systems.Chat; -using DevChatter.Bot.Core.Util; +using System; +using System.Collections.Generic; +using System.Linq; namespace DevChatter.Bot.Core.Games.DealNoDeal { @@ -16,7 +13,7 @@ public class DealNoDealGame : IGame public bool IsRunning { get; private set; } //Configuration - public const UserRole ROLE_REQUIRE_TO_START = UserRole.Everyone; + public const UserRole ROLE_REQUIRED_TO_START = UserRole.Everyone; public const int SECONDS_TO_CHOSE_BOXES = 30; public const int TOKENS_TO_PLAY = 25; public const int MIN_PLAYABLE_BOXES = 5; @@ -60,10 +57,10 @@ public void AttemptToStartGame(ChatUser chatUser) return; } - if (!chatUser.IsInThisRoleOrHigher(ROLE_REQUIRE_TO_START)) + if (!chatUser.IsInThisRoleOrHigher(ROLE_REQUIRED_TO_START)) { _chatClient.SendMessage( - $"You must be at least a {ROLE_REQUIRE_TO_START} to start a game, {chatUser.DisplayName}"); + $"You must be at least a {ROLE_REQUIRED_TO_START} to start a game, {chatUser.DisplayName}"); return; } bool transactionComplete = ChargeTokensForStartingAGame(_chatClient, chatUser); From 3ce9409aa636dcea9514de4068c9b9885141b5b6 Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Sat, 16 Jun 2018 16:26:36 -0400 Subject: [PATCH 06/17] Simple clean-up, mostly naming conventions --- .../Automation/PickBoxesAction.cs | 2 +- .../Games/DealNoDeal/DealNoDealGame.cs | 120 +++++++++--------- .../Games/DealNoDeal/MakeADealOperation.cs | 2 +- .../Games/DealNoDeal/PickABoxOperation.cs | 2 +- 4 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs b/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs index 7e219da7..59fa5001 100644 --- a/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs +++ b/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs @@ -29,7 +29,7 @@ public PickBoxesAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IC public void Invoke() { _chatClient.SendMessage("Picking a random Box. User did not respond"); - _dealNoDealGame.PickRandomBox(_dealNoDealGame._MainPlayer.DisplayName); + _dealNoDealGame.PickRandomBox(_dealNoDealGame.MainPlayer.DisplayName); } private void SetNextRunTime() diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs index ef9dd1ae..efe220c5 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using DevChatter.Bot.Core.Extensions; namespace DevChatter.Bot.Core.Games.DealNoDeal { @@ -28,9 +29,9 @@ public class DealNoDealGame : IGame public List BoxesWithOwners; public DealNoDealGameState GameState = DealNoDealGameState.GAME_NOT_RUNNING; - private IIntervalAction ActionBeingPerformed; + private IIntervalAction _actionBeingPerformed; - public ChatUser _MainPlayer; + public ChatUser MainPlayer; public int DealOffer = 0; @@ -66,12 +67,12 @@ public void AttemptToStartGame(ChatUser chatUser) bool transactionComplete = ChargeTokensForStartingAGame(_chatClient, chatUser); if (transactionComplete) { - _MainPlayer = chatUser; + MainPlayer = chatUser; IsRunning = true; BoxesWithOwners = new List(); StartingBoxes = ShuffleBoxes(); _chatClient.SendMessage( - $"{_MainPlayer.DisplayName} started a Deal or No Deal game! Please choose a box everyone by typing \"!dnd pick x\" ... Pick a box between numbers 1 and 15"); + $"{MainPlayer.DisplayName} started a Deal or No Deal game! Please choose a box everyone by typing \"!dnd pick x\" ... Pick a box between numbers 1 and 15"); GameState = DealNoDealGameState.CHOSING_STARTING_BOXES; SetActionForGameState(DealNoDealGameState.CHOSING_STARTING_BOXES); @@ -87,19 +88,19 @@ public void SetActionForGameState(DealNoDealGameState gameState) { if (gameState == DealNoDealGameState.GAME_NOT_RUNNING) { - _automatedActionSystem.RemoveAction(ActionBeingPerformed); + _automatedActionSystem.RemoveAction(_actionBeingPerformed); return; } //Remove all Game-actions and start a new one - _automatedActionSystem.RemoveAction(ActionBeingPerformed); - ActionBeingPerformed = _automatedActionFactory.GetIntervalAction(gameState); - _automatedActionSystem.AddAction(ActionBeingPerformed); + _automatedActionSystem.RemoveAction(_actionBeingPerformed); + _actionBeingPerformed = _automatedActionFactory.GetIntervalAction(gameState); + _automatedActionSystem.AddAction(_actionBeingPerformed); } public void QuitGame(IChatClient chatClient) { IsRunning = false; - _MainPlayer = null; + MainPlayer = null; DealOffer = 0; GameState = DealNoDealGameState.GAME_NOT_RUNNING; SetActionForGameState(DealNoDealGameState.GAME_NOT_RUNNING); @@ -107,7 +108,7 @@ public void QuitGame(IChatClient chatClient) private void SendGameAlreadyStartedMessage(IChatClient chatClient, ChatUser chatUser) { chatClient.SendMessage( - $"There is already a {nameof(DealNoDealGame)} running by {_MainPlayer.DisplayName}, {chatUser.DisplayName}. Wait for the game to finish!"); + $"There is already a {nameof(DealNoDealGame)} running by {MainPlayer.DisplayName}, {chatUser.DisplayName}. Wait for the game to finish!"); } private void SendGameNotStartedMessage(IChatClient chatClient, ChatUser chatUser) @@ -174,79 +175,78 @@ private int GetADeal() } public void AcceptDeal(IChatClient chatClient) { - _currencyGenerator.AddCurrencyTo(_MainPlayer.DisplayName, DealOffer); - chatClient.SendMessage($" {_MainPlayer.DisplayName} ACCEPTED THE OFFER!"); - chatClient.SendMessage($"GAME OVER! {_MainPlayer.DisplayName} recieved {DealOffer} tokens!"); + _currencyGenerator.AddCurrencyTo(MainPlayer.DisplayName, DealOffer); + chatClient.SendMessage($" {MainPlayer.DisplayName} ACCEPTED THE OFFER!"); + chatClient.SendMessage($"GAME OVER! {MainPlayer.DisplayName} recieved {DealOffer} tokens!"); DealOffer = 0; QuitGame(chatClient); } public void DeclineDeal(IChatClient chatClient) { - chatClient.SendMessage($"{_MainPlayer.DisplayName} declined the Deal of {DealOffer} tokens!"); + chatClient.SendMessage($"{MainPlayer.DisplayName} declined the Deal of {DealOffer} tokens!"); DealOffer = 0; GameState = DealNoDealGameState.PICKING_BOXES; SetActionForGameState(DealNoDealGameState.PICKING_BOXES); } + public bool CheckIfGameIsWon() { - if (BoxesWithOwners.Count > 1) + if (BoxesWithOwners.Count != 1) { return false; } - else if (BoxesWithOwners.Count == 1) - { - int LastTokens = BoxesWithOwners.FirstOrDefault().TokenValue; - _chatClient.SendMessage($"GAME FINISHED!"); - _chatClient.SendMessage($"A LAST BOX WITH {LastTokens} TOKENS REMAINS!"); - _chatClient.SendMessage($"{_MainPlayer.DisplayName} recieved {LastTokens} tokens! "); - _currencyGenerator.AddCurrencyTo(_MainPlayer.DisplayName, LastTokens); - QuitGame(_chatClient); - return true; - } - return false; + int lastTokens = BoxesWithOwners.Single().TokenValue; + _chatClient.SendMessage($"GAME FINISHED!"); + _chatClient.SendMessage($"A LAST BOX WITH {lastTokens} TOKENS REMAINS!"); + _chatClient.SendMessage($"{MainPlayer.DisplayName} recieved {lastTokens} tokens! "); + _currencyGenerator.AddCurrencyTo(MainPlayer.DisplayName, lastTokens); + QuitGame(_chatClient); + return true; } + private List ShuffleBoxes() { - List ShuffledBoxes = new List(); - List PricesToShfufle = new List(InitialPrices); + List shuffledBoxes = new List(); + List pricesToShfufle = new List(InitialPrices); Random r = new Random(); - int TotalBoxesCount = InitialPrices.Count; + int totalBoxesCount = InitialPrices.Count; - for (int i = 0; i < TotalBoxesCount; i++) + for (int i = 0; i < totalBoxesCount; i++) { //get a new random with a range of left indexes - int indexToRemoveAt = r.Next(TotalBoxesCount - i); - ShuffledBoxes.Add(new Box(i, PricesToShfufle[indexToRemoveAt])); - PricesToShfufle.RemoveAt(indexToRemoveAt); + int indexToRemoveAt = r.Next(totalBoxesCount - i); + shuffledBoxes.Add(new Box(i, pricesToShfufle[indexToRemoveAt])); + pricesToShfufle.RemoveAt(indexToRemoveAt); } - return ShuffledBoxes; + return shuffledBoxes; } - public void PickRandomBox(string DisplayName) + + public void PickRandomBox(string displayName) { Random randy = new Random(); Box randomBox = BoxesWithOwners[randy.Next(BoxesWithOwners.Count)]; - string boxValue = PickABox(randomBox.Owner, DisplayName); + // TODO: Do something with this variable or don't store it here. + string boxValue = PickABox(randomBox.Owner, displayName); } - public string PickABox(string namePicked, string DisplayName) + public string PickABox(string namePicked, string displayName) { - Box chosenBox = BoxesWithOwners.Find(b => b.Owner.ToLower() == namePicked.ToLower()); + Box chosenBox = BoxesWithOwners.Find(b => b.Owner.EqualsIns(namePicked)); if (chosenBox == null) { _chatClient.SendMessage("That user does not hold a box, please chose another one"); return null; } - else - { - //OpenBox - _chatClient.SendMessage($"{DisplayName}, opened a box with a value of {chosenBox.TokenValue}"); - BoxesWithOwners.Remove(chosenBox); - UpdateGameState(); - } + + //OpenBox + _chatClient.SendMessage($"{displayName}, opened a box with a value of {chosenBox.TokenValue}"); + BoxesWithOwners.Remove(chosenBox); + UpdateGameState(); return GetBoxValue(chosenBox.TokenValue); } + public void PrintBoxesRemaining() { string allPrices = "Prices left: "; @@ -289,28 +289,27 @@ public string GetBoxValue(int tokenValue) * 33% chance that you will get lied. * The tokenvalue will either be between some values or the exact one */ - string Result = ""; + string result = ""; Random randy = new Random(); bool shouldLie = randy.Next(3) == 0; - int randomBoxValue = 0; if (shouldLie) { //Lie to the user - randomBoxValue = BoxesWithOwners[randy.Next(BoxesWithOwners.Count)].TokenValue; - Result = GetAproximateValue(randomBoxValue); + var randomBoxValue = BoxesWithOwners[randy.Next(BoxesWithOwners.Count)].TokenValue; + result = GetAproximateValue(randomBoxValue); } else { //dont lie but tell him aproximately - Result = GetAproximateValue(tokenValue); + result = GetAproximateValue(tokenValue); } - return Result; + return result; } public string GetAproximateValue(int value) { //NOTE: This just spits out a value around -20 value +20 or the value - string Result = ""; + string result = ""; Random randy = new Random(); int r = randy.Next(10); @@ -320,17 +319,18 @@ public string GetAproximateValue(int value) //return the exact one return value.ToString(); } - else - { - //return aproximate value - int lowerValue = value - r - randy.Next(10); - if (lowerValue < 0) lowerValue = 0; - int higherValue = value + r + randy.Next(10); - Result = "Between " + (lowerValue).ToString() + " - " + (higherValue).ToString(); + //return aproximate value + int lowerValue = value - r - randy.Next(10); + if (lowerValue < 0) + { + lowerValue = 0; } - return Result; + int higherValue = value + r + randy.Next(10); + result = "Between " + (lowerValue).ToString() + " - " + (higherValue).ToString(); + + return result; } } diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs index a15d15f6..ab083597 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs @@ -29,7 +29,7 @@ public override string TryToExecute(CommandReceivedEventArgs eventArgs) return null; } - bool isMainPlayer = _dealNoDealGame._MainPlayer != eventArgs.ChatUser; + bool isMainPlayer = _dealNoDealGame.MainPlayer != eventArgs.ChatUser; string pick = eventArgs.Arguments[0]; if (string.IsNullOrWhiteSpace(pick)) { diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs index db49e8c2..d72e954a 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs @@ -38,7 +38,7 @@ public override string TryToExecute(CommandReceivedEventArgs eventArgs) return ""; } - bool isMainPlayer = _dealNoDealGame._MainPlayer != eventArgs.ChatUser; + bool isMainPlayer = _dealNoDealGame.MainPlayer != eventArgs.ChatUser; string namePicked = eventArgs.Arguments[1]; if (string.IsNullOrWhiteSpace(namePicked)) { From 84f83b8c2cb4d122547d9567db655d9302b11877 Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Sat, 16 Jun 2018 16:29:07 -0400 Subject: [PATCH 07/17] Spacing clean up --- .../Games/DealNoDeal/DealNoDealGame.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs index efe220c5..b80085bf 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs @@ -27,15 +27,15 @@ public class DealNoDealGame : IGame public List StartingBoxes; public List BoxesWithOwners; - + public DealNoDealGameState GameState = DealNoDealGameState.GAME_NOT_RUNNING; private IIntervalAction _actionBeingPerformed; public ChatUser MainPlayer; public int DealOffer = 0; - - public DealNoDealGame(IChatClient chatClient,ICurrencyGenerator currencyGenerator, IAutomatedActionSystem automatedActionSystem) + + public DealNoDealGame(IChatClient chatClient, ICurrencyGenerator currencyGenerator, IAutomatedActionSystem automatedActionSystem) { _chatClient = chatClient; _automatedActionFactory = new AutomatedActionFactory(this, _chatClient); @@ -47,11 +47,11 @@ public DealNoDealGame(IChatClient chatClient,ICurrencyGenerator currencyGenerato private readonly ICurrencyGenerator _currencyGenerator; private readonly IAutomatedActionSystem _automatedActionSystem; private readonly AutomatedActionFactory _automatedActionFactory; - + public void AttemptToStartGame(ChatUser chatUser) { - + if (IsRunning) { SendGameAlreadyStartedMessage(_chatClient, chatUser); @@ -70,7 +70,7 @@ public void AttemptToStartGame(ChatUser chatUser) MainPlayer = chatUser; IsRunning = true; BoxesWithOwners = new List(); - StartingBoxes = ShuffleBoxes(); + StartingBoxes = ShuffleBoxes(); _chatClient.SendMessage( $"{MainPlayer.DisplayName} started a Deal or No Deal game! Please choose a box everyone by typing \"!dnd pick x\" ... Pick a box between numbers 1 and 15"); @@ -79,7 +79,7 @@ public void AttemptToStartGame(ChatUser chatUser) } else { - _chatClient.SendMessage($"Not enough tokens to start a game, {chatUser}" ); + _chatClient.SendMessage($"Not enough tokens to start a game, {chatUser}"); } } @@ -311,7 +311,7 @@ public string GetAproximateValue(int value) //NOTE: This just spits out a value around -20 value +20 or the value string result = ""; Random randy = new Random(); - + int r = randy.Next(10); //every 4th value should be concrete if (r % 4 == 0) From 309b3ea6e14eaf2257321b556591fce0d012b10c Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Sat, 16 Jun 2018 17:04:44 -0400 Subject: [PATCH 08/17] more clean up --- src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs index b80085bf..c179cab5 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs @@ -34,7 +34,6 @@ public class DealNoDealGame : IGame public ChatUser MainPlayer; public int DealOffer = 0; - public DealNoDealGame(IChatClient chatClient, ICurrencyGenerator currencyGenerator, IAutomatedActionSystem automatedActionSystem) { _chatClient = chatClient; @@ -43,7 +42,7 @@ public DealNoDealGame(IChatClient chatClient, ICurrencyGenerator currencyGenerat _automatedActionSystem = automatedActionSystem; } - private IChatClient _chatClient; + private readonly IChatClient _chatClient; private readonly ICurrencyGenerator _currencyGenerator; private readonly IAutomatedActionSystem _automatedActionSystem; private readonly AutomatedActionFactory _automatedActionFactory; From 01a69d1aa3e7d750b0faa5899a8701feec3be659 Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Sat, 16 Jun 2018 17:11:54 -0400 Subject: [PATCH 09/17] more naming and clean up --- .../Automation/ChoseStartingBoxesAction.cs | 4 ++-- .../Automation/OfferDealAction.cs | 6 ++--- .../Games/DealNoDeal/DealNoDealGame.cs | 24 ++++++++++++------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs b/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs index 7f0562a9..bbb69e56 100644 --- a/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs +++ b/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs @@ -22,7 +22,7 @@ public ChoseStartingBoxesAction(DealNoDealGame dealNoDealGame, IChatClient chatC _dealNoDealGame = dealNoDealGame; _chatClient = chatClient; _clock = clock; - _chatClient.SendMessage($"everyone has {DealNoDealGame.SECONDS_TO_CHOSE_BOXES} seconds to choose a box!"); + _chatClient.SendMessage($"everyone has {DealNoDealGame.SECONDS_TO_CHOOSE_BOXES} seconds to choose a box!"); SetNextRunTime(); } @@ -42,7 +42,7 @@ public void Invoke() private void SetNextRunTime() { - _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOSE_BOXES); + _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOOSE_BOXES); } } } diff --git a/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs b/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs index 45681041..6af5c918 100644 --- a/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs +++ b/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs @@ -23,7 +23,7 @@ public OfferDealAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IC _dealNoDealGame.PrintBoxesRemaining(); _chatClient.SendMessage($"***DEAL OFFER: {dealNoDealGame.DealOffer} TOKENS!"); _chatClient.SendMessage(" type \"!dnd accept\" or \"!dnd decline\" to accept or decline offer "); - _chatClient.SendMessage($"You have {DealNoDealGame.SECONDS_TO_CHOSE_BOXES} seconds or the deal will be automatically accepted"); + _chatClient.SendMessage($"You have {DealNoDealGame.SECONDS_TO_CHOOSE_BOXES} seconds or the deal will be automatically accepted"); SetNextRunTime(); } @@ -32,13 +32,13 @@ public OfferDealAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IC public void Invoke() { - _chatClient.SendMessage($"Automatically accepting deal! {DealNoDealGame.SECONDS_TO_CHOSE_BOXES} seconds passed"); + _chatClient.SendMessage($"Automatically accepting deal! {DealNoDealGame.SECONDS_TO_CHOOSE_BOXES} seconds passed"); _dealNoDealGame.AcceptDeal(_chatClient); } private void SetNextRunTime() { - _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOSE_BOXES); + _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOOSE_BOXES); } } } diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs index c179cab5..063c0d9d 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs @@ -1,11 +1,11 @@ using DevChatter.Bot.Core.Automation; using DevChatter.Bot.Core.Data.Model; using DevChatter.Bot.Core.Events; +using DevChatter.Bot.Core.Extensions; using DevChatter.Bot.Core.Systems.Chat; using System; using System.Collections.Generic; using System.Linq; -using DevChatter.Bot.Core.Extensions; namespace DevChatter.Bot.Core.Games.DealNoDeal { @@ -15,7 +15,7 @@ public class DealNoDealGame : IGame //Configuration public const UserRole ROLE_REQUIRED_TO_START = UserRole.Everyone; - public const int SECONDS_TO_CHOSE_BOXES = 30; + public const int SECONDS_TO_CHOOSE_BOXES = 30; public const int TOKENS_TO_PLAY = 25; public const int MIN_PLAYABLE_BOXES = 5; //Change these if you don't like the rewards @@ -104,6 +104,7 @@ public void QuitGame(IChatClient chatClient) GameState = DealNoDealGameState.GAME_NOT_RUNNING; SetActionForGameState(DealNoDealGameState.GAME_NOT_RUNNING); } + private void SendGameAlreadyStartedMessage(IChatClient chatClient, ChatUser chatUser) { chatClient.SendMessage( @@ -114,11 +115,13 @@ private void SendGameNotStartedMessage(IChatClient chatClient, ChatUser chatUser { chatClient.SendMessage($"There's no {nameof(DealNoDealGame)} running, {chatUser.DisplayName}."); } + private bool ChargeTokensForStartingAGame(IChatClient chatClient, ChatUser chatUser) { chatClient.SendMessage($"{TOKENS_TO_PLAY} tokens are being charged for starting a game."); return _currencyGenerator.RemoveCurrencyFrom(chatUser.DisplayName, TOKENS_TO_PLAY); } + public void UpdateGameState() { if (CheckIfGameIsWon()) @@ -149,7 +152,7 @@ public void UpdateGameState() private int GetADeal() { //average - int Result = BoxesWithOwners.Sum(b => b.TokenValue) / BoxesWithOwners.Count; + int result = BoxesWithOwners.Sum(b => b.TokenValue) / BoxesWithOwners.Count; //randomize offer Random r = new Random(); @@ -158,20 +161,21 @@ private int GetADeal() //if the offer is 10. the offer would be subtracted or added either 5 or 15 if (shouldSubtract) { - Result -= value; + result -= value; } else { - Result += value; + result += value; } //Ensure offer is > 0 - if (Result <= 0) + if (result <= 0) { - Result = 1; + result = 1; } - return Result; + return result; } + public void AcceptDeal(IChatClient chatClient) { _currencyGenerator.AddCurrencyTo(MainPlayer.DisplayName, DealOffer); @@ -180,6 +184,7 @@ public void AcceptDeal(IChatClient chatClient) DealOffer = 0; QuitGame(chatClient); } + public void DeclineDeal(IChatClient chatClient) { chatClient.SendMessage($"{MainPlayer.DisplayName} declined the Deal of {DealOffer} tokens!"); @@ -269,6 +274,7 @@ public void PrintBoxesRemaining() allBoxes += $" type \"!dnd pick x\" "; _chatClient.SendMessage(allBoxes); } + public void EnsureMinPlayableBoxes() { if (StartingBoxes.Count <= 0) return; @@ -282,6 +288,7 @@ public void EnsureMinPlayableBoxes() StartingBoxes.Remove(box); } } + public string GetBoxValue(int tokenValue) { /* @@ -330,7 +337,6 @@ public string GetAproximateValue(int value) result = "Between " + (lowerValue).ToString() + " - " + (higherValue).ToString(); return result; - } } } From 4c53053b61c0e4d92266e1b20691678a849598d6 Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Sat, 16 Jun 2018 17:40:36 -0400 Subject: [PATCH 10/17] more clean up --- src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs b/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs index 59fa5001..74e1c5ad 100644 --- a/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs +++ b/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs @@ -34,7 +34,7 @@ public void Invoke() private void SetNextRunTime() { - _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOSE_BOXES); + _nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOOSE_BOXES); } } } From c44cc2d9892eaaa7b7a60358f55e2d4396ee9cef Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Sat, 16 Jun 2018 21:21:14 -0400 Subject: [PATCH 11/17] More convention matching --- .../Automation/AutomatedActionFactory.cs | 6 ++--- .../Automation/ChoseStartingBoxesAction.cs | 4 +-- .../Games/DealNoDeal/Box.cs | 6 +---- .../Games/DealNoDeal/DNDCommand.cs | 15 ++++------- .../Games/DealNoDeal/DealNoDealGame.cs | 24 ++++++++--------- .../Games/DealNoDeal/DealNoDealGameState.cs | 12 +++------ .../Games/DealNoDeal/MakeADealOperation.cs | 14 +++++----- .../Games/DealNoDeal/PickABoxOperation.cs | 27 ++++++++----------- 8 files changed, 44 insertions(+), 64 deletions(-) diff --git a/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs b/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs index c2cfa034..60e2478a 100644 --- a/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs +++ b/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs @@ -19,15 +19,15 @@ public AutomatedActionFactory(DealNoDealGame dealNoDealGame, IChatClient chatCli } public IIntervalAction GetIntervalAction(DealNoDealGameState gameState) { - if (gameState == DealNoDealGameState.CHOSING_STARTING_BOXES) + if (gameState == DealNoDealGameState.ChosingStartingBoxes) { return new ChoseStartingBoxesAction(_dealNoDealGame,_chatClient,new SystemClock()); } - if (gameState == DealNoDealGameState.PICKING_BOXES) + if (gameState == DealNoDealGameState.PickingBoxes) { return new PickBoxesAction(_dealNoDealGame, _chatClient, new SystemClock()); } - if (gameState == DealNoDealGameState.MAKING_A_DEAL) + if (gameState == DealNoDealGameState.MakingADeal) { return new OfferDealAction(_dealNoDealGame, _chatClient, new SystemClock()); } diff --git a/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs b/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs index bbb69e56..e7b3a7ec 100644 --- a/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs +++ b/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs @@ -36,8 +36,8 @@ public void Invoke() _dealNoDealGame.EnsureMinPlayableBoxes(); _chatClient.SendMessage("Game Started!"); - _dealNoDealGame.GameState = DealNoDealGameState.PICKING_BOXES; - _dealNoDealGame.SetActionForGameState(DealNoDealGameState.PICKING_BOXES); + _dealNoDealGame.GameState = DealNoDealGameState.PickingBoxes; + _dealNoDealGame.SetActionForGameState(DealNoDealGameState.PickingBoxes); } private void SetNextRunTime() diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs index 5ac73e32..4bd489c7 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Text; - namespace DevChatter.Bot.Core.Games.DealNoDeal { public class Box @@ -12,13 +8,13 @@ public Box(int id, int value, string owner) TokenValue = value; Owner = owner; } + public Box(int id, int value) { Id = id; TokenValue = value; } - public int Id { get; set; } public int TokenValue { get; set; } public string Owner { get; set; } diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs index e969a2da..085dcc51 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs @@ -1,14 +1,11 @@ using DevChatter.Bot.Core.Commands; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using DevChatter.Bot.Core.Commands.Operations; using DevChatter.Bot.Core.Data; using DevChatter.Bot.Core.Data.Model; -using DevChatter.Bot.Core.Events; using DevChatter.Bot.Core.Events.Args; using DevChatter.Bot.Core.Systems.Chat; +using System.Collections.Generic; +using System.Linq; namespace DevChatter.Bot.Core.Games.DealNoDeal { @@ -17,21 +14,19 @@ public class DNDCommand : BaseCommand, IGameCommand private readonly DealNoDealGame _dealNoDealGame; public IGame Game => _dealNoDealGame; - public DNDCommand(IRepository repository, DealNoDealGame dealNoDealGame, IChatClient chatClient, ICurrencyGenerator currencyGenerator) : base(repository, UserRole.Everyone) + public DNDCommand(IRepository repository, DealNoDealGame dealNoDealGame, IChatClient chatClient) : base(repository, UserRole.Everyone) { _dealNoDealGame = dealNoDealGame; _chatClient = chatClient; - _currencyGenerator = currencyGenerator; HelpText = "Use \"!dnd\" to start a game. Use \"!dnd pick x\" to pick/guess a box. Use \"!dnd accept\" or \"!dnd decline\" to accept or decline offers ."; } private List _operations; - private IChatClient _chatClient; - private ICurrencyGenerator _currencyGenerator; + private readonly IChatClient _chatClient; public List Operations => _operations ?? (_operations = new List { - new PickABoxOperation(_dealNoDealGame,_chatClient,_currencyGenerator), + new PickABoxOperation(_dealNoDealGame,_chatClient), new MakeADealOperation(_dealNoDealGame,_chatClient) }); diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs index 063c0d9d..ea36b5ea 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs @@ -28,7 +28,7 @@ public class DealNoDealGame : IGame public List StartingBoxes; public List BoxesWithOwners; - public DealNoDealGameState GameState = DealNoDealGameState.GAME_NOT_RUNNING; + public DealNoDealGameState GameState = DealNoDealGameState.GameNotRunning; private IIntervalAction _actionBeingPerformed; public ChatUser MainPlayer; @@ -73,8 +73,8 @@ public void AttemptToStartGame(ChatUser chatUser) _chatClient.SendMessage( $"{MainPlayer.DisplayName} started a Deal or No Deal game! Please choose a box everyone by typing \"!dnd pick x\" ... Pick a box between numbers 1 and 15"); - GameState = DealNoDealGameState.CHOSING_STARTING_BOXES; - SetActionForGameState(DealNoDealGameState.CHOSING_STARTING_BOXES); + GameState = DealNoDealGameState.ChosingStartingBoxes; + SetActionForGameState(DealNoDealGameState.ChosingStartingBoxes); } else { @@ -85,7 +85,7 @@ public void AttemptToStartGame(ChatUser chatUser) public void SetActionForGameState(DealNoDealGameState gameState) { - if (gameState == DealNoDealGameState.GAME_NOT_RUNNING) + if (gameState == DealNoDealGameState.GameNotRunning) { _automatedActionSystem.RemoveAction(_actionBeingPerformed); return; @@ -101,8 +101,8 @@ public void QuitGame(IChatClient chatClient) IsRunning = false; MainPlayer = null; DealOffer = 0; - GameState = DealNoDealGameState.GAME_NOT_RUNNING; - SetActionForGameState(DealNoDealGameState.GAME_NOT_RUNNING); + GameState = DealNoDealGameState.GameNotRunning; + SetActionForGameState(DealNoDealGameState.GameNotRunning); } private void SendGameAlreadyStartedMessage(IChatClient chatClient, ChatUser chatUser) @@ -137,14 +137,14 @@ public void UpdateGameState() if (shouldOfferDeal) { DealOffer = GetADeal(); - GameState = DealNoDealGameState.MAKING_A_DEAL; - SetActionForGameState(DealNoDealGameState.MAKING_A_DEAL); + GameState = DealNoDealGameState.MakingADeal; + SetActionForGameState(DealNoDealGameState.MakingADeal); } else { //Restart timer - GameState = DealNoDealGameState.PICKING_BOXES; - SetActionForGameState(DealNoDealGameState.PICKING_BOXES); + GameState = DealNoDealGameState.PickingBoxes; + SetActionForGameState(DealNoDealGameState.PickingBoxes); } } @@ -189,8 +189,8 @@ public void DeclineDeal(IChatClient chatClient) { chatClient.SendMessage($"{MainPlayer.DisplayName} declined the Deal of {DealOffer} tokens!"); DealOffer = 0; - GameState = DealNoDealGameState.PICKING_BOXES; - SetActionForGameState(DealNoDealGameState.PICKING_BOXES); + GameState = DealNoDealGameState.PickingBoxes; + SetActionForGameState(DealNoDealGameState.PickingBoxes); } public bool CheckIfGameIsWon() diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGameState.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGameState.cs index 1ff797af..645a4176 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGameState.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGameState.cs @@ -1,14 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; - namespace DevChatter.Bot.Core.Games.DealNoDeal { public enum DealNoDealGameState { - GAME_NOT_RUNNING, - CHOSING_STARTING_BOXES, - PICKING_BOXES, - MAKING_A_DEAL, + GameNotRunning, + ChosingStartingBoxes, + PickingBoxes, + MakingADeal, } } diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs index ab083597..f4c00318 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/MakeADealOperation.cs @@ -1,17 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Text; using DevChatter.Bot.Core.Commands.Operations; -using DevChatter.Bot.Core.Events; using DevChatter.Bot.Core.Events.Args; using DevChatter.Bot.Core.Systems.Chat; +using System.Collections.Generic; namespace DevChatter.Bot.Core.Games.DealNoDeal { public class MakeADealOperation : BaseCommandOperation { - private DealNoDealGame _dealNoDealGame; - private IChatClient _chatClient; + private readonly DealNoDealGame _dealNoDealGame; + private readonly IChatClient _chatClient; public MakeADealOperation(DealNoDealGame dealNoDealGame, IChatClient chatClient) { @@ -36,7 +33,7 @@ public override string TryToExecute(CommandReceivedEventArgs eventArgs) _chatClient.SendMessage(HelpText); return null; } - if (_dealNoDealGame.GameState == DealNoDealGameState.MAKING_A_DEAL) + if (_dealNoDealGame.GameState == DealNoDealGameState.MakingADeal) { //Note: Only other players can choose a starting box if (isMainPlayer) @@ -53,7 +50,8 @@ public override string TryToExecute(CommandReceivedEventArgs eventArgs) _dealNoDealGame.AcceptDeal(_chatClient); return "Deal Accepted"; } - else if(pick == "decline") + + if(pick == "decline") { _dealNoDealGame.DeclineDeal(_chatClient); return "Deal Declined"; diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs index d72e954a..463ada65 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs @@ -1,28 +1,23 @@ -using System.Collections.Generic; -using System.Linq; using DevChatter.Bot.Core.Commands.Operations; -using DevChatter.Bot.Core.Events; using DevChatter.Bot.Core.Events.Args; using DevChatter.Bot.Core.Systems.Chat; +using System.Collections.Generic; namespace DevChatter.Bot.Core.Games.DealNoDeal { - /* - * Picking starting boxes and the MainPlayer choosing a box are the same operation, - * based on CHOSING_STARTING_BOXES or PICKING_BOXES the proper operations are performed - * the OperandWords are the same !dnd pick x - */ + /// + /// Picking starting boxes and the MainPlayer choosing a box are the same operation, + /// based on CHOSING_STARTING_BOXES or PICKING_BOXES the proper operations are performed + /// the OperandWords are the same !dnd pick x + /// public class PickABoxOperation : BaseCommandOperation { - private DealNoDealGame _dealNoDealGame; - private IChatClient _chatClient; - private ICurrencyGenerator _currencyGenerator; + private readonly DealNoDealGame _dealNoDealGame; + private readonly IChatClient _chatClient; - public PickABoxOperation(DealNoDealGame dealNoDealGame, IChatClient chatClient, - ICurrencyGenerator currencyGenerator) + public PickABoxOperation(DealNoDealGame dealNoDealGame, IChatClient chatClient) { - _currencyGenerator = currencyGenerator; _chatClient = chatClient; _dealNoDealGame = dealNoDealGame; } @@ -45,7 +40,7 @@ public override string TryToExecute(CommandReceivedEventArgs eventArgs) return ""; } - if (_dealNoDealGame.GameState == DealNoDealGameState.CHOSING_STARTING_BOXES) + if (_dealNoDealGame.GameState == DealNoDealGameState.ChosingStartingBoxes) { //Note: Only other players can choose a starting box if (!isMainPlayer) @@ -73,7 +68,7 @@ public override string TryToExecute(CommandReceivedEventArgs eventArgs) } } } - if (_dealNoDealGame.GameState == DealNoDealGameState.PICKING_BOXES) + if (_dealNoDealGame.GameState == DealNoDealGameState.PickingBoxes) { if (isMainPlayer) { From 8956b9fd7566e691131318bfef4358f026289a06 Mon Sep 17 00:00:00 2001 From: "ace.aleks93" Date: Sun, 17 Jun 2018 19:05:24 +0200 Subject: [PATCH 12/17] Moved SystemClock instantiation to constructor --- .../Automation/AutomatedActionFactory.cs | 6 +++--- .../Automation/ChoseStartingBoxesAction.cs | 4 ++-- src/DevChatter.Bot.Core/Automation/OfferDealAction.cs | 4 ++-- src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs b/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs index 60e2478a..b997c97c 100644 --- a/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs +++ b/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs @@ -21,15 +21,15 @@ public IIntervalAction GetIntervalAction(DealNoDealGameState gameState) { if (gameState == DealNoDealGameState.ChosingStartingBoxes) { - return new ChoseStartingBoxesAction(_dealNoDealGame,_chatClient,new SystemClock()); + return new ChoseStartingBoxesAction(_dealNoDealGame,_chatClient); } if (gameState == DealNoDealGameState.PickingBoxes) { - return new PickBoxesAction(_dealNoDealGame, _chatClient, new SystemClock()); + return new PickBoxesAction(_dealNoDealGame, _chatClient); } if (gameState == DealNoDealGameState.MakingADeal) { - return new OfferDealAction(_dealNoDealGame, _chatClient, new SystemClock()); + return new OfferDealAction(_dealNoDealGame, _chatClient); } return null; diff --git a/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs b/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs index e7b3a7ec..084ff548 100644 --- a/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs +++ b/src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs @@ -17,11 +17,11 @@ public class ChoseStartingBoxesAction : IIntervalAction private DealNoDealGame _dealNoDealGame; - public ChoseStartingBoxesAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IClock clock) + public ChoseStartingBoxesAction(DealNoDealGame dealNoDealGame, IChatClient chatClient) { _dealNoDealGame = dealNoDealGame; _chatClient = chatClient; - _clock = clock; + _clock = new SystemClock(); _chatClient.SendMessage($"everyone has {DealNoDealGame.SECONDS_TO_CHOOSE_BOXES} seconds to choose a box!"); SetNextRunTime(); } diff --git a/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs b/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs index 6af5c918..1ee198b6 100644 --- a/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs +++ b/src/DevChatter.Bot.Core/Automation/OfferDealAction.cs @@ -15,11 +15,11 @@ public class OfferDealAction : IIntervalAction private DealNoDealGame _dealNoDealGame; - public OfferDealAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IClock clock) + public OfferDealAction(DealNoDealGame dealNoDealGame, IChatClient chatClient) { _dealNoDealGame = dealNoDealGame; _chatClient = chatClient; - _clock = clock; + _clock = new SystemClock(); _dealNoDealGame.PrintBoxesRemaining(); _chatClient.SendMessage($"***DEAL OFFER: {dealNoDealGame.DealOffer} TOKENS!"); _chatClient.SendMessage(" type \"!dnd accept\" or \"!dnd decline\" to accept or decline offer "); diff --git a/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs b/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs index 74e1c5ad..930560ab 100644 --- a/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs +++ b/src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs @@ -15,11 +15,11 @@ public class PickBoxesAction : IIntervalAction private DealNoDealGame _dealNoDealGame; - public PickBoxesAction(DealNoDealGame dealNoDealGame, IChatClient chatClient, IClock clock) + public PickBoxesAction(DealNoDealGame dealNoDealGame, IChatClient chatClient) { _dealNoDealGame = dealNoDealGame; _chatClient = chatClient; - _clock = clock; + _clock = new SystemClock(); _dealNoDealGame.PrintBoxesRemaining(); SetNextRunTime(); } From aeab4d34e1bfd2de21d826ffc555f2ce2b995870 Mon Sep 17 00:00:00 2001 From: "ace.aleks93" Date: Sun, 17 Jun 2018 19:20:27 +0200 Subject: [PATCH 13/17] added a switch statement in AutomatedActionFactory --- .../Automation/AutomatedActionFactory.cs | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs b/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs index b997c97c..57b2ffd1 100644 --- a/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs +++ b/src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs @@ -17,23 +17,28 @@ public AutomatedActionFactory(DealNoDealGame dealNoDealGame, IChatClient chatCli _dealNoDealGame = dealNoDealGame; _chatClient = chatClient; } + public IIntervalAction GetIntervalAction(DealNoDealGameState gameState) { - if (gameState == DealNoDealGameState.ChosingStartingBoxes) - { - return new ChoseStartingBoxesAction(_dealNoDealGame,_chatClient); - } - if (gameState == DealNoDealGameState.PickingBoxes) + switch (gameState) { - return new PickBoxesAction(_dealNoDealGame, _chatClient); + case DealNoDealGameState.ChosingStartingBoxes: + { + return new ChoseStartingBoxesAction(_dealNoDealGame, _chatClient); + } + case DealNoDealGameState.PickingBoxes: + { + return new PickBoxesAction(_dealNoDealGame, _chatClient); + } + case DealNoDealGameState.MakingADeal: + { + return new OfferDealAction(_dealNoDealGame, _chatClient); + } + default: + { + return null; + } } - if (gameState == DealNoDealGameState.MakingADeal) - { - return new OfferDealAction(_dealNoDealGame, _chatClient); - } - - return null; } - } } From bca22a41b6f89aca22b329a6f4e5c824976e8474 Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Tue, 26 Jun 2018 15:22:36 -0400 Subject: [PATCH 14/17] no more Shfufles --- src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs index ea36b5ea..4d67316b 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/DealNoDealGame.cs @@ -212,7 +212,7 @@ public bool CheckIfGameIsWon() private List ShuffleBoxes() { List shuffledBoxes = new List(); - List pricesToShfufle = new List(InitialPrices); + List pricesToShuffle = new List(InitialPrices); Random r = new Random(); int totalBoxesCount = InitialPrices.Count; @@ -220,8 +220,8 @@ private List ShuffleBoxes() { //get a new random with a range of left indexes int indexToRemoveAt = r.Next(totalBoxesCount - i); - shuffledBoxes.Add(new Box(i, pricesToShfufle[indexToRemoveAt])); - pricesToShfufle.RemoveAt(indexToRemoveAt); + shuffledBoxes.Add(new Box(i, pricesToShuffle[indexToRemoveAt])); + pricesToShuffle.RemoveAt(indexToRemoveAt); } return shuffledBoxes; } From 70e0a15fe10abf9d6715d0481478ec1e5d712859 Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Tue, 26 Jun 2018 15:22:48 -0400 Subject: [PATCH 15/17] Save people from the "pick" typo --- src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs index 463ada65..c156d72e 100644 --- a/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs +++ b/src/DevChatter.Bot.Core/Games/DealNoDeal/PickABoxOperation.cs @@ -22,7 +22,7 @@ public PickABoxOperation(DealNoDealGame dealNoDealGame, IChatClient chatClient) _dealNoDealGame = dealNoDealGame; } - public override List OperandWords { get; } = new List(){"pick"}; + public override List OperandWords { get; } = new List(){ "pick", "pcik", "pikc", "ipck" }; public override string HelpText { get; } = "Use \"!dnd pick x\" to pick/guess a box."; From af8dfbbe6de92a9f94c2ec6f560f2aee2e549f19 Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Tue, 26 Jun 2018 15:23:00 -0400 Subject: [PATCH 16/17] Register the DNDCommand better --- src/DevChatter.Bot.Web/Startup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DevChatter.Bot.Web/Startup.cs b/src/DevChatter.Bot.Web/Startup.cs index a277a57e..a2c7adec 100644 --- a/src/DevChatter.Bot.Web/Startup.cs +++ b/src/DevChatter.Bot.Web/Startup.cs @@ -70,7 +70,7 @@ public void ConfigureServices(IServiceCollection services) IRepository repository = SetUpDatabase.SetUpRepository(fullConfig.DatabaseConnectionString); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(repository); From 7e7bfa141f48e5e59d9900aea4d7eea063575f42 Mon Sep 17 00:00:00 2001 From: Brendan Enrick <2243498+benrick@users.noreply.github.com> Date: Tue, 26 Jun 2018 16:06:30 -0400 Subject: [PATCH 17/17] Fix the automated messages --- .../Automation/DelayedMessageAction.cs | 7 ++++--- src/DevChatter.Bot.Web/AutomatedActionSystem.cs | 11 ++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/DevChatter.Bot.Core/Automation/DelayedMessageAction.cs b/src/DevChatter.Bot.Core/Automation/DelayedMessageAction.cs index f9562284..4ef8f382 100644 --- a/src/DevChatter.Bot.Core/Automation/DelayedMessageAction.cs +++ b/src/DevChatter.Bot.Core/Automation/DelayedMessageAction.cs @@ -5,7 +5,6 @@ namespace DevChatter.Bot.Core.Automation { public class DelayedMessageAction : IIntervalAction { - private readonly string _message; private readonly IChatClient _chatClient; private DateTime _nextRunTime; public TimeSpan DelayTimeSpan; @@ -13,18 +12,20 @@ public class DelayedMessageAction : IIntervalAction public DelayedMessageAction(int delayInSeconds, string message, IChatClient chatClient, string name) { DelayTimeSpan = TimeSpan.FromSeconds(delayInSeconds); - _message = message; + Message = message; _chatClient = chatClient; Name = name; _nextRunTime = DateTime.Now.AddSeconds(delayInSeconds); } public string Name { get; } + public string Message { get; set; } + public bool IsTimeToRun() => DateTime.Now > _nextRunTime; public void Invoke() { - _chatClient.SendMessage(_message); + _chatClient.SendMessage(Message); _nextRunTime = DateTime.MaxValue; } } diff --git a/src/DevChatter.Bot.Web/AutomatedActionSystem.cs b/src/DevChatter.Bot.Web/AutomatedActionSystem.cs index d6039a7c..c1fc582f 100644 --- a/src/DevChatter.Bot.Web/AutomatedActionSystem.cs +++ b/src/DevChatter.Bot.Web/AutomatedActionSystem.cs @@ -37,7 +37,8 @@ public void AddAction(IIntervalAction actionToAdd) RecurringJob.AddOrUpdate(action, Cron.MinuteInterval(currencyUpdate.IntervalInMinutes)); break; case DelayedMessageAction delayedMessageAction: - BackgroundJob.Schedule(action, delayedMessageAction.DelayTimeSpan); + BackgroundJob.Schedule(() => InvokeDelayedMessageAction(delayedMessageAction.Message), + delayedMessageAction.DelayTimeSpan); break; case OneTimeCallBackAction oneTimeCallBackAction: BackgroundJob.Schedule(action, oneTimeCallBackAction.DelayTimeSpan); @@ -49,6 +50,14 @@ public void AddAction(IIntervalAction actionToAdd) } } + public void InvokeDelayedMessageAction(string message) + { + foreach (IChatClient chatClient in _chatClients) + { + chatClient.SendMessage(message); + } + } + public void InvokeAutomatedMessage(string id) { var message = _repository.Single(DataItemPolicy.ById(Guid.Parse(id)));