Example usage for com.google.gson JsonObject JsonObject

List of usage examples for com.google.gson JsonObject JsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonObject JsonObject.

Prototype

JsonObject

Source Link

Usage

From source file:blockplus.exports.ContextRepresentation.java

License:Open Source License

@Override
public String toString() {
    final JsonObject data = new JsonObject();
    data.addProperty("color", this.context.side().toString());
    data.addProperty("isTerminal", this.context.isTerminal());
    data.add("board", this.encodeBoard());
    data.add("pieces", this.encodePieces());
    data.add("options", this.encodeOptions());
    return data.toString();
}

From source file:blockplus.exports.PiecesRepresentation.java

License:Open Source License

private static String _toJson() {
    final JsonArray pieces = new JsonArray();
    for (final Polyomino polyomino : Polyomino.set()) {
        final Iterable<IPosition> positions = polyomino.positions();
        if (positions.iterator().hasNext()) {
            final JsonArray data = new JsonArray();
            for (final IPosition position : positions) {
                final JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("y", position.row());
                jsonObject.addProperty("x", position.column());
                data.add(jsonObject);// w  w  w .  j ava 2 s . c o m
            }
            pieces.add(data);
        }
    }
    return pieces.toString();
}

From source file:blockplus.transport.BlockplusServer.java

License:Open Source License

public JsonObject games() {
    final JsonObject tables = new JsonObject();
    for (final IGame<Context> game : this.gameByOrdinal.values()) {
        if (game.isFull()) {
            boolean isAlive = false;
            for (final IClient client : game.getClients()) {
                final boolean isOpen = client.getEndpoint().isOpen();
                if (!isOpen)
                    this.disconnect(client.getEndpoint());
                isAlive = isAlive || isOpen;
            }//from  w w w.  j  av  a2 s .c o m
            if (!isAlive) {
                tables.addProperty("" + this.reset(game).getOrdinal(), this.reset(game).getClients().size());
            }
        } else {
            boolean isFullyAlive = true;
            boolean isFullyDead = true;
            for (final IClient client : game.getClients()) {
                final boolean isOpen = client.getEndpoint().isOpen();
                isFullyAlive = isFullyAlive && isOpen;
                isFullyDead = isFullyDead && !isOpen;
            }
            if (isFullyAlive) {
                tables.addProperty("" + game.getOrdinal(), game.getClients().size());
            } else if (isFullyDead) {
                tables.addProperty("" + this.reset(game).getOrdinal(), this.reset(game).getClients().size());
            }
        }
    }
    return tables;
}

From source file:blockplus.transport.BlockplusServerEvents.java

License:Open Source License

@Subscribe
@AllowConcurrentEvents/* ww w.ja  va  2 s . c  o m*/
public void onGameConnection(final IGameConnection gameConnection) {
    final IGame<Context> game = this.getServer().getGame(gameConnection.getOrdinal());
    if (!game.isFull()) {
        final IClient oldClient = this.getServer().getClientByEndpoint(gameConnection.getEndpoint());
        final IClient newClient = new Client(gameConnection.getEndpoint(), oldClient.getName(),
                game.getOrdinal());
        this.getServer().updateClients(newClient.getEndpoint(), newClient);

        final BlockplusGame newGame = (BlockplusGame) game.connect(newClient);

        final ImmutableList<IClient> clients = newGame.getClients();
        this.getServer().updateGame(newGame.getOrdinal(), newGame);

        final JsonObject gameInfo = new JsonObject();
        gameInfo.addProperty("id", newGame.getOrdinal());
        gameInfo.addProperty("players", clients.size());

        newClient.getEndpoint().emit("game", gameInfo.toString());

        final JsonObject playerInfo = new JsonObject();
        playerInfo.addProperty("name", newClient.getName());

        for (final IClient client : clients) {
            client.getEndpoint().emit("player", playerInfo.toString());
        }
        this.getServer().removeFromPatio(newClient.getEndpoint());
        for (final IEndPoint endPoint : this.getServer().getEndpointsInPatio()) {
            endPoint.emit("tables", this.getServer().games().toString());
        }
        if (newGame.isFull() && !newGame.isPaused())
            newGame.update();
    }
}

From source file:blockplus.transport.BlockplusServerEvents.java

License:Open Source License

@Subscribe
@AllowConcurrentEvents//  ww w  .  ja v a  2 s .c  o m
public void onNotification(final INotification notificationInterface) {
    final IClient client = this.getServer().getClientByEndpoint(notificationInterface.getEndpoint());
    final Integer game = client.getGame();
    final BlockplusGame blockplusGame = (BlockplusGame) this.getServer().getGame(game);
    final Colors from = Colors.valueOf(notificationInterface.getFrom());
    final Colors to = Colors.valueOf(notificationInterface.getTo());
    final String message = notificationInterface.getMessage();
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("message", message);
    jsonObject.addProperty("from", from.toString());
    jsonObject.addProperty("to", to.toString());
    final IClient toClient = blockplusGame.getPlayer(to);
    toClient.getEndpoint().emit("notification", jsonObject.toString());
}

From source file:blockplus.transport.events.Client.java

License:Open Source License

@Override
public String toString() {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("type", this.getClass().getSimpleName());
    final JsonObject data = new JsonObject();
    data.addProperty("name", this.getName());
    data.addProperty("game", this.getGame().toString());
    data.addProperty("io", this.getEndpoint().toString());
    jsonObject.add("data", data);
    return jsonObject.toString();
}

From source file:blockplus.transport.messages.Client.java

License:Open Source License

@Override
public JsonObject getData() {
    final JsonObject data = new JsonObject();
    data.addProperty("name", this.getName());
    return data;//from  www  .j a va 2s  .  c o m
}

From source file:blockplus.transport.messages.Client.java

License:Open Source License

@Override
public String toString() {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("type", this.getType());
    jsonObject.add("data", this.getData());
    return jsonObject.toString();
}

From source file:blockplus.transport.messages.GameConnection.java

License:Open Source License

@Override
public JsonObject getData() {
    final JsonObject data = new JsonObject();
    data.addProperty("ordinal", this.getOrdinal());
    return data;//from   w w w  .  ja v  a 2 s . c  om
}

From source file:blockplus.transport.messages.MoveSubmit.java

License:Open Source License

@Override
public JsonObject getData() {
    final JsonObject data = new JsonObject();
    data.add("positions", this.getPositions());
    return data;//  w  ww  . jav  a  2 s.  co  m
}