-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from pulcher/add_timeout_to_dad
Add timeout to dad joke command
- Loading branch information
Showing
19 changed files
with
344 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Magic8HeadService.Services | ||
{ | ||
public class CoolDownOptions | ||
{ | ||
public Dictionary<string, int> Options { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Magic8HeadService.Services | ||
{ | ||
public class CoolDownService | ||
{ | ||
private Dictionary<string, DateTime> CurrentCoolsDowns | ||
= new Dictionary<string, DateTime>(); | ||
private IOptions<CoolDownOptions> options; | ||
|
||
public CoolDownService(IOptions<CoolDownOptions> options) | ||
{ | ||
this.options = options; | ||
} | ||
|
||
public DateTime Execute(string commandString) | ||
{ | ||
var nextExecutionTime = DateTime.UtcNow.AddSeconds(options.Value.Options[commandString]); | ||
|
||
if (CurrentCoolsDowns.TryGetValue(commandString, out DateTime coolDownDateTime)) | ||
{ | ||
if (DateTime.UtcNow > coolDownDateTime) | ||
{ | ||
CurrentCoolsDowns[commandString] | ||
= nextExecutionTime; | ||
} | ||
else | ||
{ | ||
return coolDownDateTime; | ||
} | ||
} | ||
else | ||
{ | ||
CurrentCoolsDowns.Add(commandString, nextExecutionTime); | ||
} | ||
|
||
return nextExecutionTime; | ||
} | ||
|
||
public ReadOnlyDictionary<string, DateTime> GetAllCoolDowns() | ||
{ | ||
var readOnlyCurrentCurrentCoolDowns = new ReadOnlyDictionary<string, DateTime>(CurrentCoolsDowns); | ||
return readOnlyCurrentCurrentCoolDowns; | ||
} | ||
|
||
public DateTime GetCurrentCoolDown(string commandString) | ||
{ | ||
if (CurrentCoolsDowns.TryGetValue(commandString, out DateTime coolDownDateTime)) | ||
{ | ||
return coolDownDateTime; | ||
} | ||
|
||
return DateTime.MinValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.