Example #1
0
 @Deactivate
 void deactivate() {
   try {
     pongWebSocket.stop();
   } catch (Exception e) {
     e.printStackTrace();
     // ignore
   }
 }
Example #2
0
 @Activate
 void activate() {
   try {
     pongWebSocket = new PongWebSocketServer();
     pongWebSocket.start();
   } catch (UnknownHostException e) {
     System.err.println("Error starting Pong WebSocket server");
     e.printStackTrace();
   }
 }
Example #3
0
  @Override
  public void onAction(float reward, Tensor nextState) {
    if (reward != 0) {
      pongWebSocket.sendToAll("{ \"score\" : " + reward + " }");
    }

    float[] data = nextState.get();
    pongWebSocket.sendToAll(
        "{ "
            + "\"x\" : "
            + data[0]
            + ", "
            + "\"y\" : "
            + data[1]
            + ", "
            + "\"vx\" : "
            + data[2]
            + ", "
            + "\"vy\" : "
            + data[3]
            + ", "
            + "\"p\" : "
            + data[4]
            + ", "
            + "\"o\" : "
            + data[5]
            + "}");

    long t = System.currentTimeMillis();
    long sleep = interval - (t - timestamp);
    timestamp = t;

    // since the PongListeners are called synchronously, slow it down here
    if (sleep > 0) {
      try {
        Thread.sleep(sleep);
      } catch (InterruptedException e) {
      }
    }
  }