(message, websocket)
Parse the messages recieved from the clients
Parse the messages recieved from the clients
Parse the messages recieved from the clients
async def message_parse(message, websocket):
    """Parse the messages recieved from the clients"""
    if message["intent"] is constants.NetworkCommands.check.value:
        await websocket.send(
            rapidjson.dumps({
                "intent":
                constants.NetworkCommands.status_ok.value,
                "players":
                len(PLAYER_WS),
                "matches":
                len(MATCH_PROCESS)
            }))
    elif message["intent"] is constants.NetworkCommands.match_act.value:
        if message["turn_id"] == MATCH_PROCESS[message["match_id"]]["turn_id"]:
            # Note: The statements below assign the action to the respective players
            MATCH_PROCESS[message["match_id"]]["act"][
                MATCH_PROCESS[message["match_id"]]["players"].index(
                    message["player_id"])] = message["act"]
            MATCH_PROCESS[message["match_id"]]["recv"][MATCH_PROCESS[message[
                "match_id"]]["players"].index(message["player_id"])] = True
    elif message["intent"] is constants.NetworkCommands.replay.value:
        try:
            with open(
                    os.path.join(
                        os.path.join(os.getcwd(), "matches"),
                        str(message["replay_id"]) + ".json"), 'r') as f:
                # Note: Registry expression match comes after as it's an expensive operation as compared to file I/O
                if re.fullmatch("^[a-z0-9-]*$",
                                message["replay_id"]) is not None:
                    f = rapidjson.load(f)
                    await websocket.send(
                        gzip.compress(
                            bytes(
                                rapidjson.dumps([
                                    constants.NetworkCommands.status_ok.value, f
                                ]), "utf8")))
                else:
                    await websocket.send(
                        gzip.compress(
                            bytes(
                                rapidjson.dumps([
                                    constants.NetworkCommands.status_fail.value
                                ]), "utf8")))
        except:
            await websocket.send(
                gzip.compress(
                    bytes(
                        rapidjson.dumps(
                            [constants.NetworkCommands.status_fail.value]),
                        "utf8")))
    elif message["intent"] in [
            constants.NetworkCommands.match.value,
            constants.NetworkCommands.room.value
    ]:
        if len(PLAYER_WS) >= MAX_PLAYERS:
            await websocket.send(
                rapidjson.dumps({
                    "intent":
                    constants.NetworkCommands.status_full.value
                }))
            return
        uuid_ = str(uuid.uuid4())
        while uuid_ in PLAYER_WS:
            uuid_ = str(uuid.uuid4())
        PLAYER_WS[uuid_] = {"ws": websocket}
        if message["intent"] is constants.NetworkCommands.match.value:
            CONCURRENTLY_LOOKING["noroom"].append(uuid_)
            PLAYER_WS[uuid_]["noroom"] = True
        elif message["intent"] is constants.NetworkCommands.room.value:
            if message["room"] in CONCURRENTLY_LOOKING["room"]:
                if len(CONCURRENTLY_LOOKING["room"][message["room"]]) <= 4:
                    CONCURRENTLY_LOOKING["room"][message["room"]].append(uuid_)
                else:
                    await websocket.send(
                        rapidjson.dumps({
                            "intent":
                            constants.NetworkCommands.status_full.value
                        }))
                    return
            else:
                CONCURRENTLY_LOOKING["room"][message["room"]] = [uuid_]
            PLAYER_WS[uuid_]["noroom"] = False
            PLAYER_WS[uuid_]["room"] = str(message["room"])
        await websocket.send(
            rapidjson.dumps({
                "intent":
                constants.NetworkCommands.status_reg.value,
                "player_id":
                uuid_,
                "mode":
                MODE
            }))
async def message_parse ( message , websocket ) : if message [ "intent" ] is constants . NetworkCommands . check . value : await websocket . send ( rapidjson . dumps ( { "intent" : constants . NetworkCommands . status_ok . value , "players" : len ( PLAYER_WS ) , "matches" : len ( MATCH_PROCESS ) } ) ) elif message [ "intent" ] is constants . NetworkCommands . match_act . value : if message [ "turn_id" ] == MATCH_PROCESS [ message [ "match_id" ] ] [ "turn_id" ] : # Note: The statements below assign the action to the respective players MATCH_PROCESS [ message [ "match_id" ] ] [ "act" ] [ MATCH_PROCESS [ message [ "match_id" ] ] [ "players" ] . index ( message [ "player_id" ] ) ] = message [ "act" ] MATCH_PROCESS [ message [ "match_id" ] ] [ "recv" ] [ MATCH_PROCESS [ message [ "match_id" ] ] [ "players" ] . index ( message [ "player_id" ] ) ] = True elif message [ "intent" ] is constants . NetworkCommands . replay . value : try : with open ( os . path . join ( os . path . join ( os . getcwd ( ) , "matches" ) , str ( message [ "replay_id" ] ) + ".json" ) , 'r' ) as f : # Note: Registry expression match comes after as it's an expensive operation as compared to file I/O if re . fullmatch ( "^[a-z0-9-]*$" , message [ "replay_id" ] ) is not None : f = rapidjson . load ( f ) await websocket . send ( gzip . compress ( bytes ( rapidjson . dumps ( [ constants . NetworkCommands . status_ok . value , f ] ) , "utf8" ) ) ) else : await websocket . send ( gzip . compress ( bytes ( rapidjson . dumps ( [ constants . NetworkCommands . status_fail . value ] ) , "utf8" ) ) ) except : await websocket . send ( gzip . compress ( bytes ( rapidjson . dumps ( [ constants . NetworkCommands . status_fail . value ] ) , "utf8" ) ) ) elif message [ "intent" ] in [ constants . NetworkCommands . match . value , constants . NetworkCommands . room . value ] : if len ( PLAYER_WS ) >= MAX_PLAYERS : await websocket . send ( rapidjson . dumps ( { "intent" : constants . NetworkCommands . status_full . value } ) ) return uuid_ = str ( uuid . uuid4 ( ) ) while uuid_ in PLAYER_WS : uuid_ = str ( uuid . uuid4 ( ) ) PLAYER_WS [ uuid_ ] = { "ws" : websocket } if message [ "intent" ] is constants . NetworkCommands . match . value : CONCURRENTLY_LOOKING [ "noroom" ] . append ( uuid_ ) PLAYER_WS [ uuid_ ] [ "noroom" ] = True elif message [ "intent" ] is constants . NetworkCommands . room . value : if message [ "room" ] in CONCURRENTLY_LOOKING [ "room" ] : if len ( CONCURRENTLY_LOOKING [ "room" ] [ message [ "room" ] ] ) <= 4 : CONCURRENTLY_LOOKING [ "room" ] [ message [ "room" ] ] . append ( uuid_ ) else : await websocket . send ( rapidjson . dumps ( { "intent" : constants . NetworkCommands . status_full . value } ) ) return else : CONCURRENTLY_LOOKING [ "room" ] [ message [ "room" ] ] = [ uuid_ ] PLAYER_WS [ uuid_ ] [ "noroom" ] = False PLAYER_WS [ uuid_ ] [ "room" ] = str ( message [ "room" ] ) await websocket . send ( rapidjson . dumps ( { "intent" : constants . NetworkCommands . status_reg . value , "player_id" : uuid_ , "mode" : MODE } ) )