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

Randomize collect interval (#18) and improve code readability #41

Merged
merged 11 commits into from
Nov 4, 2023
Merged

Conversation

maxisoft
Copy link
Owner

What?

This pull request adds a new feature that randomizes the collect interval using a normal distribution with a mean of 30 minutes and a standard deviation of 7 minutes.

Why?

This feature resolves #18 by adding more randomness and flexibility to the collect interval. It also makes the code more efficient and readable by refactoring and simplifying some methods.

How?

This pull request implements the following changes:

  • Add a new option to the ASFFreeGamesOptions class called RandomizeRecheckInterval, which is a nullable bool that indicates whether to randomize the collect interval or not.
  • Modify the ASFFreeGamesPlugin class to use the RandomizeRecheckInterval option to determine the value of the RandomizeIntervalSwitch property, which is used to multiply the standard deviation of the normal distribution used to generate the random delay in the GetRandomizedTimerDelay method.
  • Extract an interface called IASFFreeGamesPlugin from the ASFFreeGamesPlugin class, which contains the members that are used by the CollectIntervalManager class.
  • Move the timer and random delay logic from the ASFFreeGamesPlugin class to the CollectIntervalManager class, which encapsulates the functionality of managing the collect interval.

Testing?

To test this feature, you can run the unit tests in RandomUtilsTests.cs and check if they pass. You can also run the plugin with different values of RandomizeRecheckInterval and observe how it affects the collect interval.

Screenshots (optional)

N/A

Anything Else?

N/A

This commit adds a new feature that resolves #18 by randomizing the collect interval using a normal distribution with a mean of 30 minutes and a standard deviation of 7 minutes. The random number is clamped between 11 minutes and 1 hour.

The commit also refactors the ResetTimer method to take a Func<Timer?> parameter instead of a Timer parameter, and adds a summary and remarks for the GetRandomizedTimerDelay method.

The commit uses the NextGaussian method from the newly created RandomUtils class to generate normally distributed random numbers.
This commit improves the flexibility and randomness of the collect interval by refactoring the GetRandomizedTimerDelay method and adding four parameters to it: meanSeconds, stdSeconds, minSeconds, and maxSeconds. These parameters allow the caller to specify the mean, standard deviation, minimum, and maximum values of the normal distribution used to generate the random delay.

The commit also updates the documentation of the method to reflect the new parameters and their units. It adds a seealso section to link to an external source that explains how to implement the NextGaussian method in C#.

The commit also modifies the StartTimerIfNeeded method to use the GetRandomizedTimerDelay method with different parameters for the initial and regular delays. This adds more randomness to the first collect operation, which will happen at a random time within 1 second and 5 minutes after starting the plugin.
…ty of the ASFFreeGamesPlugin class instead of CancellationTokenSourceLazy
This commit adds a new option to the ASFFreeGamesOptions class called RandomizeRecheckInterval, which is a nullable bool that indicates whether to randomize the collect interval or not. The default value is true, which means the collect interval will be randomized by default.

The commit also modifies the ASFFreeGamesPlugin class to use the RandomizeRecheckInterval option to determine the value of the RandomizeIntervalSwitch property, which is used to multiply the standard deviation of the normal distribution used to generate the random delay in the GetRandomizedTimerDelay method. If the option is false or null, then the random delay will be equal to the mean value.

The commit also optimizes the performance of the GetRandomizedTimerDelay method by checking if the standard deviation parameter is zero before calling the Random.NextGaussian method. If it is zero, then the random number will be equal to the mean parameter, and there is no need to generate a random number from a normal distribution. This can save some computation time and resources.
…lectIntervalManager

This commit extracts an interface called IASFFreeGamesPlugin from the ASFFreeGamesPlugin class, which contains the members that are used by the CollectIntervalManager class. This interface is implemented by the ASFFreeGamesPlugin class and passed as a parameter to the constructor of the CollectIntervalManager class. This way, the CollectIntervalManager class can access the plugin's options and methods without depending on its concrete implementation.

This commit also moves the timer and random delay logic from the ASFFreeGamesPlugin class to the CollectIntervalManager class, which encapsulates the functionality of managing the collect interval. The CollectIntervalManager class has methods to start, stop, and reset the timer with a random initial and regular delay. The timer's callback is the CollectGamesOnClock method of the plugin.

This commit refactors and simplifies the code by separating concerns.
…mesPlugin

This commit extracts an interface called ICollectIntervalManager from the CollectIntervalManager class, which defines the contract for managing the collect interval for the ASFFreeGamesPlugin. The interface contains the public and internal methods and properties of the CollectIntervalManager class, and is documented using XML comments.

The commit also modifies the ASFFreeGamesPlugin class to use the ICollectIntervalManager interface instead of the CollectIntervalManager class as a field. The plugin's constructor creates an instance of the CollectIntervalManager class and passes it as an argument to the ICollectIntervalManager field. This way, the plugin can access the collect interval manager's functionality without depending on its concrete implementation.

This commit improves the code quality by following the dependency inversion principle, which states that high-level modules should not depend on low-level modules, but both should depend on abstractions. This makes the code more loosely coupled and easier to test and maintain.
This commit increases the sample size for the normal distribution tests in RandomUtilsTests.cs. The increased sample size reduces the margin of error and makes the tests more reliable and consistent.
@maxisoft maxisoft self-assigned this Oct 29, 2023
@maxisoft maxisoft marked this pull request as draft October 29, 2023 18:13
This commit adds some checks and methods to ensure that the PluginContext is valid and initialized before using it. It also adds a cancellationToken parameter to some of the methods that use the PluginContext cancellation token. This should resolve the issue #42 (#42) that was reported.
This commit makes the following changes:

- Change the PluginContext from a readonly record struct to a sealed record class
- Add a CancellationTokenChanger struct that implements IDisposable and temporarily changes the cancellation token of the PluginContext instance
- Add a TemporaryChangeCancellationToken method that creates an instance of the CancellationTokenChanger struct
- Change the SaveOptions method to return a string and use the TemporaryChangeCancellationToken method
- Change the CollectGamesOnClock method to use the TemporaryChangeCancellationToken method
- Add XML documentation comments to the PluginContext and its members
This commit resolves the CS8603 error: "Possible null reference return" in the ASFFreeGamesPlugin.cs file. The error was caused by the possibility of returning a null value from the Context property. The fix involves returning a default empty invalid value to ensure the _context.Value is not null.
@maxisoft maxisoft merged commit 0615926 into main Nov 4, 2023
22 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NullReferenceException in PluginContext.get_CancellationToken() on dev branch Randomize the collect interval
1 participant