Skip to content

Commit

Permalink
Added the option to terminate Wordler after a certain amount of time
Browse files Browse the repository at this point in the history
  • Loading branch information
vesk4000 committed Mar 16, 2022
1 parent 66026d9 commit 788e27e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Wordler": {
"commandName": "Project",
"commandLineArgs": "-t 4 -d 1/4000 -p \"2RLyQBWOCggxaoWsi2V1HDiCVJfpKCTR Vesk4000 xUiic4WkiQbcg5GQ\""
"commandLineArgs": "--tl 20"
},
"WSL": {
"commandName": "WSL2",
Expand Down
8 changes: 8 additions & 0 deletions Source/CLI/Commands/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class AppSettings : CommandSettings {
[DefaultValue("")]
public string Pastebin { get; set; }

[Description("Time (in seconds) after which to conclude the grading of words")]
[CommandOption("--tl|--timelimit|--time-limit")]
[DefaultValue(3_600_000)]
public int TimeLimit { get; set; }

public WordClues wordClues;
public PastebinAPI.User user;

Expand Down Expand Up @@ -146,6 +151,9 @@ public override ValidationResult Validate() {
}
}

if (TimeLimit < 0)
return ValidationResult.Error("Invalid time limit");

return ValidationResult.Success();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/CLI/Commands/SolveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
settings.wordClues,
Extensions.GetSolutionType(settings.SolutionName),
settings.Divide
));
), settings.TimeLimit);

settings.user.CreatePasteAsync
(
Expand Down
2 changes: 2 additions & 0 deletions Source/CLI/ITaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public void Poll(
out Type solution,
out bool hard
);

public void Terminate();
}
}
8 changes: 7 additions & 1 deletion Source/CLI/LiveTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Wordler
{
class LiveTask<TResult, TTask> where TTask : ITaskable<TResult>
{
public void Run(ITaskable<TResult> taskedObject, int fps = 10)
public void Run(ITaskable<TResult> taskedObject, int timeLimit, int fps = 10)
{
Console.Clear();

Expand Down Expand Up @@ -136,6 +136,12 @@ public void Run(ITaskable<TResult> taskedObject, int fps = 10)
if(partsTotal == partsDone)
break;

if(upTimer.Elapsed.TotalSeconds > timeLimit)
{
taskedObject.Terminate();
break;
}

Thread.Sleep(1000 / fps);
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Solving/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Solution {

protected WordClues wordClues;

private Thread solution;
public Thread solution;
private Thread cacher;

public abstract void GradeWords();
Expand All @@ -27,7 +27,7 @@ public Solution(Solver solver, List<string> gradeableWords, List<string> potenti
this.potentialComputerWords = potentialComputerWords;
this.wordClues = wordClues;
this.hard = hard;
solution = new Thread(new ThreadStart(GradeWords));
solution = new Thread(new ThreadStart(() => { try { GradeWords(); } catch (Exception ex) { } }));
solution.Start();
cacher = new Thread(new ThreadStart(ContinuouslyAddGradedWords));
cacher.Start();
Expand Down
13 changes: 12 additions & 1 deletion Source/Solving/Solver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ out bool _hard
++i;
}
}

}

public void AddWord(string word, double grade) {
Expand All @@ -128,5 +127,17 @@ public void AddWord(string word, double grade) {
Interlocked.Exchange(ref usingGradedWords, 0);
}
}

public void Terminate()
{
lock(usingGradedWords)
{
foreach (Solution sol in solutions)
{
sol.solution.Interrupt();
sol.solution.Join();
}
}
}
}
}
Binary file modified Wordler.exe
Binary file not shown.

0 comments on commit 788e27e

Please sign in to comment.