Exemple #1
0
def describe_position(git_status):
    res = []
    if git_status.ahead != 0:
        up = ui.Symbol("↑", "+")
        n_commits = commit_string(git_status.ahead)
        ahead_desc = "{}{} {}".format(up.as_string, git_status.ahead,
                                      n_commits)
        res += [ui.blue, ahead_desc, ui.reset]
    if git_status.behind != 0:
        down = ui.Symbol("↓", "-")
        n_commits = commit_string(git_status.behind)
        behind_desc = "{}{} {}".format(down.as_string, git_status.behind,
                                       n_commits)
        res += [ui.blue, behind_desc, ui.reset]
    return res
Exemple #2
0
def run(port,
        max_players,
        timeout,
        mode,
        max_matches=False,
        ui_en=False,
        exit_handler=False):
    """Description: This function is responsible for running the server.  
    Arguments:  
    * port: The port used by the server  
    * max_players: The maximum amount of concurrent players  
    * timeout: (In Seconds) The time to wait before issuing the STOP action  
    * mode: The flavor of pommerman  
    * max_matches: The maximum amount of concurrent matches (If not defined this \
is set to int(max_players/4))
    * ui_en: If True, UI is enabled else UI is disabled  
    * exit_handler: If True, the exit handler is set else the exit handler \
isn't set"""
    netpipe, rnetpipe = multiprocessing.Pipe()
    netqueue = multiprocessing.Queue()
    subprocess_net = multiprocessing.Process(target=network.thread,
                                             args=(rnetpipe, netqueue, port,
                                                   max_players, mode, timeout),
                                             daemon=True)
    subprocess_net.start()
    if not max_matches:
        max_matches = int(max_players / 4)
    if exit_handler:
        signal.signal(signal.SIGINT, _exit_handler(subprocess_net))
    if ui_en:
        ui.info(ui.yellow, constants.Strings.server_ready.value, ui.white,
                ui.Symbol("✔", ":)"))
    while True:
        netpipe.send([constants.SubprocessCommands.get_players.value])
        concurrent_list, num_players, num_matches = netpipe.recv()
        if int(num_matches) < max_matches:
            for x in list(concurrent_list["room"].keys()):
                i = concurrent_list["room"][x]
                if len(i) >= 4:
                    MATCH_SUBPROCESS.append(_create_match(i, netqueue, mode))
                    del concurrent_list["room"][x]
            if len(concurrent_list["noroom"]) >= 4:
                e = random.sample(
                    concurrent_list["noroom"],
                    (int(len(concurrent_list["noroom"]) / 4) * 4))
                for group in range(int(len(concurrent_list["noroom"]) / 4)):
                    MATCH_SUBPROCESS.append(
                        _create_match(e[group * 4:(group + 1) * 4], netqueue,
                                      mode))
                    for player in e[group * 4:(group + 1) * 4]:
                        del concurrent_list["noroom"][
                            concurrent_list["noroom"].index(player)]
            netpipe.send([
                constants.SubprocessCommands.update_cc.value, concurrent_list
            ])
        if ui_en:
            ui.info("\033[2K\r",
                    ui.white,
                    constants.Strings.server_players.value,
                    ui.yellow,
                    "[",
                    num_players,
                    "/",
                    max_players,
                    "]",
                    ui.white,
                    constants.Strings.server_matches.value,
                    ui.yellow,
                    "[",
                    num_matches,
                    "/",
                    max_matches,
                    "]",
                    end="")
        for process in tuple(MATCH_SUBPROCESS):
            if not process.is_alive():
                MATCH_SUBPROCESS.remove(process)
        time.sleep(2)