input-remapper icon indicating copy to clipboard operation
input-remapper copied to clipboard

Feature: Macro set to random number in range

Open shawarden opened this issue 11 months ago • 3 comments

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 avatar Mar 10 '24 08:03 shawarden

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 avatar Mar 29 '24 01:03 shawarden

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)

shawarden avatar Mar 29 '24 06:03 shawarden

Additional edit of macro.py to allow wait to accept variables defined by set(foo,#)

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

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

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

        self.tasks.append(task)

shawarden avatar Jul 09 '24 00:07 shawarden