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

Feature: Macro set to random number in range #859

Open
shawarden opened this issue Mar 10, 2024 · 2 comments
Open

Feature: Macro set to random number in range #859

shawarden opened this issue Mar 10, 2024 · 2 comments

Comments

@shawarden
Copy link

Would it be possible to have a rand(variable: str, minvalue: int, maxvalue: int) macro operation to set a variable to some integer between min and max?

Something like .../injection/macros/macros.py:

...
from random import randint
...

    def add_rand(self, variable: str, minvalue: int, maxvalue: int]):
        """Set a variable to a certain integer value between value1 and value2."""
        _type_check_variablename(variable)
        _type_check(minvalue, int, "value", 1)
        _type_check(maxvalue, int, "value", 1)

        async def task(_):
            value=randint(minvalue,maxvalue)
            resolved_value = _resolve(value)
            logger.debug('"%s" set to "%s"', variable, resolved_value)
            macro_variables[variable] = resolved_value

        self.tasks.append(task)
@shawarden
Copy link
Author

Alternatively, just give the wait() function a secondary value such that wait(100,200) waits for some value between 100 and 200.

There is already a line in parse.py that shows a wait with 2 parameters, "wait(1000).modify(Shift_L, repeat(2, k(a))).wait(10, 20).key(b)" which implies it should already be able to wait between 10 and 20ms but using that produces the error wait takes 1, not 2 parameters (type=value_error.macroparsing; symbol=wait(10,20).key(b))

@shawarden
Copy link
Author

I have edited the injection/macros/macro.py's add_wait function to accomplish this:

    def add_wait(self, time: Union[int, float], vary:Union[int, float]=0):
        """Wait from time to vary in milliseconds."""
        time = _type_check(time, [int, float], "wait", 1)
        vary = _type_check(vary, [int, float], "wait", 2)

        async def task(_):
            if vary > time:
                time_vary = random.randint(time, vary)
            else:
                time_vary = time

            await asyncio.sleep(_resolve(time_vary, [int, float]) / 1000)

        self.tasks.append(task)

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

No branches or pull requests

1 participant