Exemple #1
0
def command(websocket):
    x = 0
    y = 0
    w = 0

    for buf in log_queue:
        websocket.send(buf)

    game_config = ConfigManager.instance("game")

    def get_game_options():
        return [
            ("field_id", [game_config.get_value("global", "field_id"),"A","B","Z"]),
            ("robot_id", [game_config.get_value("global", "robot_id"),"A","B"]),
            ("target goal color", [game_config.get_value("global", "target goal color"),"yellow","blue"]),
            ("gameplay status", [game_config.get_value("global", "gameplay status"),"disabled", "enabled"]),
        ]
    
    game_options = get_game_options()

    settings_packet = json.dumps(dict(
        action = "settings-packet",
        sliders = ConfigManager.as_list("imgrec"),
        options = game_options
    ))
    websocket.send(settings_packet)

    while not websocket.closed:
        websockets.add(websocket)

        gevent.sleep(0.01)

        msg = websocket.receive()

        if not msg:
            websockets.remove(websocket)
            logger.info("WebSocket connection presumably closed, %d left connected" % len(websockets))
            break

        response = json.loads(msg)
        action = response.pop("action", None)
        if not action:
            logger.info("Unknown action")
            continue


        if action == "gamepad":
            controls = response.pop("data")
            x = controls.pop("controller0.axis0", x) * 0.99
            y = controls.pop("controller0.axis1", y) * 0.99
            w = controls.pop("controller0.axis2", w) * 0.7

            # Kick the ball
            if controls.get("controller0.button7", None):
                gameplay.arduino.kick()

            # Toggle autonomy
            if controls.get("controller0.button4", None):
                gameplay.toggle()

            # Manual control of the robot
            if not gameplay.alive:
                gameplay.arduino.set_xyw(x,-y,-w)

        # TODO: slders
        elif action == "record_toggle":
            print("TOGGLING RECORDER")
            recorder.toggle()
        elif action == "record_enable":
            recorder.enable()
        elif action == "record_disable":
            recorder.disable()
        elif action == "set_settings":
            for k, v in response.items():
                ConfigManager.set_config_value(k, v)
            print(response.items())
        elif action == "set_options":
            for k, v in response.items():
                game_config.get_option("global", k).set_value(v)
                game_config.save()
            print(response.items())
        else:
            logger.error("Unhandled action: %s", action)
    websockets.remove(websocket)
    logger.info("WebSocket connection closed, %d left connected", len(websockets))
    return b""