Created
October 1, 2021 08:37
-
-
Save ajeetraina/e3f21f81b2cdc28c082ac91125d9d7eb to your computer and use it in GitHub Desktop.
Create New Game Function
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
class CreateNewGameFunctionBuilder(BaseFunctionBuilder): | |
def __init__(self): | |
super().__init__(command_name='create_new_game') | |
def register_command(self): | |
""" | |
Registers create_new_game redis gears fucntion to redis. | |
For each generate_new_game call creates a new HASH under game namespace: | |
GAME:[game_id] owner [user_id], secret [hash], private [bool], playercount [int] | |
Returns: | |
redis key [GAME:game_id] | |
Trigger example: | |
RG.TRIGGER create_new_game USER:123 1 secret123 | |
""" | |
def subcall(user, private=0, secret=""): | |
game_id = uuid.uuid4().hex | |
key = f"GAME:{game_id}" | |
execute("HSET", key, "owner", user, "secret", str(secret), "private", int(private), "playercount", 0) | |
execute("EXPIRE", key, SECONDS_IN_DAY) | |
return game_id | |
( | |
GB('CommandReader') | |
.map(lambda x: subcall(*x[1:])) | |
.register(trigger=self.command_name, mode='sync') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment