Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Deal or No Deal" Game #203

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/DevChatter.Bot.Core/Automation/AutomatedActionFactory.cs
Original file line number Diff line number Diff line change
@@ -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 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)
{
switch (gameState)
{
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;
}
}
}
}
}
49 changes: 49 additions & 0 deletions src/DevChatter.Bot.Core/Automation/ChoseStartingBoxesAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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)
{
_dealNoDealGame = dealNoDealGame;
_chatClient = chatClient;
_clock = new SystemClock();
_chatClient.SendMessage($"everyone has {DealNoDealGame.SECONDS_TO_CHOOSE_BOXES} seconds to choose a box!");
SetNextRunTime();
}

public string Name { get; } = nameof(ChoseStartingBoxesAction);
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.PickingBoxes;
_dealNoDealGame.SetActionForGameState(DealNoDealGameState.PickingBoxes);
}

private void SetNextRunTime()
{
_nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOOSE_BOXES);
}
}
}
7 changes: 4 additions & 3 deletions src/DevChatter.Bot.Core/Automation/DelayedMessageAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ namespace DevChatter.Bot.Core.Automation
{
public class DelayedMessageAction : IIntervalAction
{
private readonly string _message;
private readonly IChatClient _chatClient;
private DateTime _nextRunTime;
public TimeSpan DelayTimeSpan;

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;
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/DevChatter.Bot.Core/Automation/IAutomatedActionFactory.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
45 changes: 45 additions & 0 deletions src/DevChatter.Bot.Core/Automation/OfferDealAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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)
{
_dealNoDealGame = dealNoDealGame;
_chatClient = chatClient;
_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 ");
_chatClient.SendMessage($"You have {DealNoDealGame.SECONDS_TO_CHOOSE_BOXES} seconds or the deal will be automatically accepted");
SetNextRunTime();
}

public string Name { get; } = nameof(OfferDealAction);
public bool IsTimeToRun() => _clock.Now >= _nextRunTime;


public void Invoke()
{
_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_CHOOSE_BOXES);
}
}
}
41 changes: 41 additions & 0 deletions src/DevChatter.Bot.Core/Automation/PickBoxesAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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)
{
_dealNoDealGame = dealNoDealGame;
_chatClient = chatClient;
_clock = new SystemClock();
_dealNoDealGame.PrintBoxesRemaining();
SetNextRunTime();
}

public string Name { get; } = nameof(PickBoxesAction);
public bool IsTimeToRun() => _clock.Now >= _nextRunTime;

public void Invoke()
{
_chatClient.SendMessage("Picking a random Box. User did not respond");
_dealNoDealGame.PickRandomBox(_dealNoDealGame.MainPlayer.DisplayName);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indenting seems to be off here


private void SetNextRunTime()
{
_nextRunTime = _clock.Now.AddSeconds(DealNoDealGame.SECONDS_TO_CHOOSE_BOXES);
}
}
}
33 changes: 33 additions & 0 deletions src/DevChatter.Bot.Core/Games/DealNoDeal/Box.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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;
}
}
}
54 changes: 54 additions & 0 deletions src/DevChatter.Bot.Core/Games/DealNoDeal/DNDCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using DevChatter.Bot.Core.Commands;
using DevChatter.Bot.Core.Commands.Operations;
using DevChatter.Bot.Core.Data;
using DevChatter.Bot.Core.Data.Model;
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
{
public class DNDCommand : BaseCommand, IGameCommand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the C# convention for capitalization, see https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/capitalization-conventions#capitalization-rules-for-identifiers

Maybe even call it DealOrNoDealCommand?

{
private readonly DealNoDealGame _dealNoDealGame;
public IGame Game => _dealNoDealGame;

public DNDCommand(IRepository repository, DealNoDealGame dealNoDealGame, IChatClient chatClient) : base(repository, UserRole.Everyone)
{
_dealNoDealGame = dealNoDealGame;
_chatClient = chatClient;
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 .";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is quite long, split up into two lines?

}
private List<ICommandOperation> _operations;
private readonly IChatClient _chatClient;

public List<ICommandOperation> Operations => _operations ?? (_operations = new List<ICommandOperation>
{
new PickABoxOperation(_dealNoDealGame,_chatClient),
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we lose anything by removing this comment?

_dealNoDealGame.AttemptToStartGame(chatUser);
}
}
}

}
}
Loading