()
Handles other network-related function
Handles other network-related function
Handles other network - related function
async def program_loop():
    """Handles other network-related function"""
    global CONCURRENTLY_LOOKING
    while (True):
        try:
            for uuid_ in list(PLAYER_WS.keys()):
                i = PLAYER_WS[uuid_]
                if not i["ws"].open:
                    if i["noroom"] is True:
                        try:
                            del CONCURRENTLY_LOOKING["noroom"][CONCURRENTLY_LOOKING[
                                "noroom"].index(uuid_)]
                        except:
                            pass
                    elif i["noroom"] is False:
                        try:
                            del CONCURRENTLY_LOOKING["room"][i["room"]][
                                CONCURRENTLY_LOOKING["room"][i["room"]].index(
                                    uuid_)]
                        except:
                            pass
                    try:
                        del PLAYER_WS[uuid_]
                    except:
                        pass
            if PIPE_MAIN.poll():
                queue_msg = PIPE_MAIN.recv()
                if queue_msg[0] is constants.SubprocessCommands.get_players.value:
                    PIPE_MAIN.send(
                        [CONCURRENTLY_LOOKING,
                         len(PLAYER_WS),
                         len(MATCH_PROCESS)])
                elif queue_msg[0] is constants.SubprocessCommands.update_cc.value:
                    CONCURRENTLY_LOOKING = queue_msg[1]
            if not QUEUE_SUBPROC.empty():
                queue_msg = QUEUE_SUBPROC.get()
                MATCH_PROCESS[queue_msg[2]] = {
                    "pipe": queue_msg[0],
                    "players": queue_msg[1],
                    "match_id": queue_msg[2],
                    "free": False,
                    "delete": False
             }
                for i in queue_msg[1]:
                    if i in PLAYER_WS:  # If the players didn't quits during matching
                        await PLAYER_WS[i]["ws"].send(
                            rapidjson.dumps({
                                "intent":
                                constants.NetworkCommands.match_start.value,
                                "match_id":
                                queue_msg[2]
                            }))
            for key in list(MATCH_PROCESS.keys()):
                value = MATCH_PROCESS[key]
                if value["pipe"].poll() and not value["free"]:
                    pipe_msg = value["pipe"].recv()
                    if pipe_msg[0] == constants.SubprocessCommands.match_next.value:
                        value["free"] = True
                        value["act"] = [0, 0, 0, 0]
                        value["recv"] = [False, False, False, False]
                        value["time"] = time.time()
                        value["turn_id"] = pipe_msg[1]
                        value["alive"] = pipe_msg[3]
                        for x, y in enumerate(value["players"]):
                            if y in list(PLAYER_WS.keys()):
                                try:
                                    await PLAYER_WS[y]["ws"].send(pipe_msg[2][x])
                                except:
                                    pass
                            elif y not in PLAYER_WS:
                                value["act"][x] = 5
                    if pipe_msg[0] is constants.SubprocessCommands.match_end.value:
                        value["delete"] = True
                        for x, y in enumerate(value["players"]):
                            if y in PLAYER_WS:
                                await PLAYER_WS[y]["ws"].send(
                                    rapidjson.dumps({
                                        "intent":
                                        constants.NetworkCommands.match_end.value,
                                        "reward":
                                        pipe_msg[1][x],
                                        "agent":
                                        10 + x
                                    }))
                if value["free"]:
                    if value["time"] + STOP_TIMEOUT < time.time(
                    ) or value["recv"].count(True) == value["alive"]:
                        value["pipe"].send(value["act"])
                        value["free"] = False
                if value["delete"]:
                    value["pipe"].send("END")
                    del MATCH_PROCESS[key]
        finally:
            time.sleep(0.0001)
async def program_loop ( ) : global CONCURRENTLY_LOOKING while ( True ) : try : for uuid_ in list ( PLAYER_WS . keys ( ) ) : i = PLAYER_WS [ uuid_ ] if not i [ "ws" ] . open : if i [ "noroom" ] is True : try : del CONCURRENTLY_LOOKING [ "noroom" ] [ CONCURRENTLY_LOOKING [ "noroom" ] . index ( uuid_ ) ] except : pass elif i [ "noroom" ] is False : try : del CONCURRENTLY_LOOKING [ "room" ] [ i [ "room" ] ] [ CONCURRENTLY_LOOKING [ "room" ] [ i [ "room" ] ] . index ( uuid_ ) ] except : pass try : del PLAYER_WS [ uuid_ ] except : pass if PIPE_MAIN . poll ( ) : queue_msg = PIPE_MAIN . recv ( ) if queue_msg [ 0 ] is constants . SubprocessCommands . get_players . value : PIPE_MAIN . send ( [ CONCURRENTLY_LOOKING , len ( PLAYER_WS ) , len ( MATCH_PROCESS ) ] ) elif queue_msg [ 0 ] is constants . SubprocessCommands . update_cc . value : CONCURRENTLY_LOOKING = queue_msg [ 1 ] if not QUEUE_SUBPROC . empty ( ) : queue_msg = QUEUE_SUBPROC . get ( ) MATCH_PROCESS [ queue_msg [ 2 ] ] = { "pipe" : queue_msg [ 0 ] , "players" : queue_msg [ 1 ] , "match_id" : queue_msg [ 2 ] , "free" : False , "delete" : False } for i in queue_msg [ 1 ] : if i in PLAYER_WS : # If the players didn't quits during matching await PLAYER_WS [ i ] [ "ws" ] . send ( rapidjson . dumps ( { "intent" : constants . NetworkCommands . match_start . value , "match_id" : queue_msg [ 2 ] } ) ) for key in list ( MATCH_PROCESS . keys ( ) ) : value = MATCH_PROCESS [ key ] if value [ "pipe" ] . poll ( ) and not value [ "free" ] : pipe_msg = value [ "pipe" ] . recv ( ) if pipe_msg [ 0 ] == constants . SubprocessCommands . match_next . value : value [ "free" ] = True value [ "act" ] = [ 0 , 0 , 0 , 0 ] value [ "recv" ] = [ False , False , False , False ] value [ "time" ] = time . time ( ) value [ "turn_id" ] = pipe_msg [ 1 ] value [ "alive" ] = pipe_msg [ 3 ] for x , y in enumerate ( value [ "players" ] ) : if y in list ( PLAYER_WS . keys ( ) ) : try : await PLAYER_WS [ y ] [ "ws" ] . send ( pipe_msg [ 2 ] [ x ] ) except : pass elif y not in PLAYER_WS : value [ "act" ] [ x ] = 5 if pipe_msg [ 0 ] is constants . SubprocessCommands . match_end . value : value [ "delete" ] = True for x , y in enumerate ( value [ "players" ] ) : if y in PLAYER_WS : await PLAYER_WS [ y ] [ "ws" ] . send ( rapidjson . dumps ( { "intent" : constants . NetworkCommands . match_end . value , "reward" : pipe_msg [ 1 ] [ x ] , "agent" : 10 + x } ) ) if value [ "free" ] : if value [ "time" ] + STOP_TIMEOUT < time . time ( ) or value [ "recv" ] . count ( True ) == value [ "alive" ] : value [ "pipe" ] . send ( value [ "act" ] ) value [ "free" ] = False if value [ "delete" ] : value [ "pipe" ] . send ( "END" ) del MATCH_PROCESS [ key ] finally : time . sleep ( 0.0001 )