Skip to content
Snippets Groups Projects
  • Florian Schröder's avatar
    a9bda66c
    Refactor code and add comprehensive docstrings · a9bda66c
    Florian Schröder authored
    Implemented multiple code adjustments across various Python modules, including renaming variables for clarity and improving data type definition. Added comprehensive docstrings to all functions and classes to enhance code readability and maintainability. Corrected a bug in debug parameter where it incorrectly employed args.debug, replacing it with args.do_study for correct implementation.
    a9bda66c
    History
    Refactor code and add comprehensive docstrings
    Florian Schröder authored
    Implemented multiple code adjustments across various Python modules, including renaming variables for clarity and improving data type definition. Added comprehensive docstrings to all functions and classes to enhance code readability and maintainability. Corrected a bug in debug parameter where it incorrectly employed args.debug, replacing it with args.do_study for correct implementation.
__main__.py 3.66 KiB
import argparse
import time
from multiprocessing import Process

from cooperative_cuisine.utils import (
    url_and_port_arguments,
    disable_websocket_logging_arguments,
    add_list_of_manager_ids_arguments,
    add_study_arguments,
    add_gui_arguments,
)

USE_STUDY_SERVER = True


def start_game_server(cli_args):
    from cooperative_cuisine.game_server import main

    main(cli_args.game_url, cli_args.game_port, cli_args.manager_ids)


def start_study_server(cli_args):
    from cooperative_cuisine.study_server import main

    main(
        cli_args.study_url,
        cli_args.study_port,
        game_host=cli_args.game_url,
        game_port=cli_args.game_port,
        manager_ids=cli_args.manager_ids,
        study_config_path=cli_args.study_config,
    )


def start_pygame_gui(cli_args):
    from cooperative_cuisine.pygame_2d_vis.gui import main

    main(
        cli_args.study_url,
        cli_args.study_port,
        cli_args.game_url,
        cli_args.game_port,
        cli_args.manager_ids,
        CONNECT_WITH_STUDY_SERVER=USE_STUDY_SERVER,
        debug=cli_args.do_study,
    )


def main(cli_args=None):
    study_server = None

    parser = argparse.ArgumentParser(
        prog="Cooperative Cuisine",
        description="Game Engine Server + PyGameGUI: Starts overcooked game engine server and a PyGame 2D Visualization window in two processes.",
        epilog="For further information, see https://scs.pages.ub.uni-bielefeld.de/cocosy/overcooked-simulator/overcooked_simulator.html",
    )
    url_and_port_arguments(parser)
    disable_websocket_logging_arguments(parser)
    add_list_of_manager_ids_arguments(parser)
    add_study_arguments(parser)
    add_gui_arguments(parser)

    cli_args = parser.parse_args()

    game_server = None
    pygame_gui = None
    try:
        if USE_STUDY_SERVER:
            print("Start study server:")
            study_server = Process(target=start_study_server, args=(cli_args,))
            study_server.start()
            time.sleep(0.5)

        print("Start game engine:")
        game_server = Process(target=start_game_server, args=(cli_args,))
        game_server.start()
        time.sleep(0.5)

        print("Start PyGame GUI 1:")
        pygame_gui = Process(target=start_pygame_gui, args=(cli_args,))
        pygame_gui.start()

        if USE_STUDY_SERVER:
            pass
            # print("Start PyGame GUI 2:")
            # pygame_gui_2 = Process(target=start_pygame_gui, args=(cli_args,))
            # pygame_gui_2.start()
            # # #
            # print("Start PyGame GUI 3:")
            # pygame_gui_3 = Process(target=start_pygame_gui, args=(cli_args,))
            # pygame_gui_3.start()
            #
            # print("Start PyGame GUI 4:")
            # pygame_gui_4 = Process(target=start_pygame_gui, args=(cli_args,))
            # pygame_gui_4.start()
            # while (
            #     pygame_gui.is_alive()
            #     and pygame_gui_2.is_alive()
            #     and pygame_gui_3.is_alive()
            # ):
            #     time.sleep(1)

        while pygame_gui.is_alive():
            time.sleep(1)

    except KeyboardInterrupt:
        print("Received Keyboard interrupt")
    finally:
        if USE_STUDY_SERVER and study_server is not None and study_server.is_alive():
            print("Terminate study server")
            study_server.terminate()
        if game_server is not None and game_server.is_alive():
            print("Terminate game server")
            game_server.terminate()
        if pygame_gui is not None and pygame_gui.is_alive():
            print("Terminate pygame gui")
            pygame_gui.terminate()
        time.sleep(0.1)


if __name__ == "__main__":
    main()