Example usage for javax.websocket Session getUserProperties

List of usage examples for javax.websocket Session getUserProperties

Introduction

In this page you can find the example usage for javax.websocket Session getUserProperties.

Prototype

Map<String, Object> getUserProperties();

Source Link

Usage

From source file:com.mycompany.wolf.Room.java

public void hunterKills(Session session, String killedPlayerId) {
    final String playerId = getPlayerId(session);
    if (HUNTER.equals(session.getUserProperties().get("role")) && !dead.contains(playerId)) {
        hunterKillings.put(playerId, new HunterKilling(killedPlayerId));
    }/*  w w w.  jav a 2s . c  o  m*/
}

From source file:com.mycompany.wolf.Room.java

public void witchPoison(Session session, String poisonedPlayerId) {
    final String playerId = getPlayerId(session);
    if (WITCH.equals(session.getUserProperties().get("role")) && !dead.contains(playerId)) {
        witchPoisonings.put(playerId, new WitchPoisoning(poisonedPlayerId));
    }/*from   w ww.ja  v  a 2s.c  o m*/
}

From source file:com.mycompany.wolf.Room.java

public void seerForecasts(Session session, String forecastedPlayerId) {
    final String playerId = getPlayerId(session);
    if (SEER.equals(session.getUserProperties().get("role")) && !dead.contains(playerId)
            && !seerForcastings.containsKey(playerId) //???
    ) {//  ww w  .  j  a  v a2s  .c  o  m
        seerForcastings.put(playerId, new SeerForcasting(forecastedPlayerId));
        Session forecastedSession = sessions.stream()
                .filter(s -> Objects.equals(getPlayerId(s), forecastedPlayerId)).findAny().orElse(null);
        Map<String, Object> forecastResp = ImmutableMap.of("code", "seerForecastResp", "properties",
                ImmutableMap.of("playerId", forecastedPlayerId, "role",
                        forecastedSession.getUserProperties().get("role")));
        String forecastRespJson = JsonUtils.toString(forecastResp);
        session.getAsyncRemote().sendText(forecastRespJson);
    }
}

From source file:com.mycompany.wolf.Room.java

public void prepare(String playerId, boolean flag) {
    Session session = sessions.stream().filter(s -> Objects.equals(getPlayerId(s), playerId)).findAny()
            .orElse(null);/*from w  ww.  jav  a  2s .com*/
    if (session != null) {
        session.getUserProperties().put("prepared", flag);
        Map<String, Object> m = ImmutableMap.of("code", "prepareResp", "properties",
                ImmutableMap.of("playerId", playerId, "prepare", Boolean.toString(flag)));
        String json = new Gson().toJson(m);
        sessions.stream().forEach(s -> {
            s.getAsyncRemote().sendText(json);
        });
        if (sessions.size() == availableCount()
                && sessions.stream().allMatch(s -> Boolean.TRUE.equals(isPrepared(s)))) {
            notifyCompeteRole();
        }
    }
}

From source file:com.mycompany.wolf.Room.java

private boolean contains(Collection<String> playerIds, String role) {
    return playerIds.stream().anyMatch(pid -> {
        Session session = sessions.stream().filter(s -> Objects.equals(getPlayerId(s), pid)).findAny()
                .orElse(null);/*  w  w w . j  a v a 2 s  .  co  m*/
        if (session != null) {
            return Objects.equals(session.getUserProperties().get("role"), role);
        }
        return false;
    });
}

From source file:io.hops.hopsworks.api.zeppelin.socket.NotebookServerImpl.java

private boolean isSessionAllowedToSwitchToWatcher(Session session) {
    String watcherSecurityKey = (String) session.getUserProperties().get(WatcherSecurityKey.HTTP_HEADER);
    return !(StringUtils.isBlank(watcherSecurityKey)
            || !watcherSecurityKey.equals(WatcherSecurityKey.getKey()));
}

From source file:org.damcode.web.c4webserver.Server.java

@OnOpen
public void onOpen(Session session, @PathParam("id") String gameId) throws IOException {
    printSysOut("new session: " + session.getId() + ", game id: " + gameId + "from ip: ");
    session.getUserProperties().put("gameid", gameId);
    session.getUserProperties().put("lobby", true);

    String connectedPlayers = "";
    int pCount = 0;
    for (Session s : session.getOpenSessions()) {
        if (!s.equals(session)) {
            String name = (String) s.getUserProperties().get("name");
            connectedPlayers += name + ", ";
            pCount++;/* w w  w.  ja  va  2  s.c  o m*/
        }
    }
    if (pCount > 0) {
        printSysOut("Connected Players: " + connectedPlayers);
        session.getBasicRemote()
                .sendText(Utils.assembleChatMessage("Server", connectedPlayers, "other players"));
    }

    for (GameController gc : waitingGames) {
        sendGameAvailMessage(session, gc.getId().toString(), gc.players.get(0));
    }
}

From source file:org.damcode.web.c4webserver.Server.java

@OnMessage
public void onMessage(MessageBean message, Session session, @PathParam("id") String gameId) {
    printSysOut("Current active games: " + activeGames.size());
    printSysOut("Current waiting games: " + waitingGames.size());
    String players = "";
    int pCount = 0;
    for (Session s : session.getOpenSessions()) {
        if (!s.equals(session)) {
            String name = (String) s.getUserProperties().get("name");
            players += name;//w w w.j  a  va 2  s  .  c o  m
            pCount++;
        }
    }
    printSysOut("Connected Players: " + players + " / " + pCount);

    try {

        processMessage(message, session);

    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.damcode.web.c4webserver.Server.java

private void processMessage(MessageBean m, Session s) throws IOException {
    Player p, o;/*from  w ww  . j av  a 2 s.  com*/
    GameController g;

    switch (m.getCommand()) {

    case MessageBean.MSG_CHAT:
        doChatMessage(m, s);
        break;

    case MessageBean.MSG_PASS_MOVE_TOKEN:
        getGame(s).passMoveToken(s);
        break;

    case MessageBean.MSG_MOVE_ACTION:
        int c = Integer.parseInt(m.getData());
        getGame(s).dropPiece(s, c);
        printSysOut("player move: " + m.getData());
        break;

    case MessageBean.MSG_START_GAME:
        if (isAlreadyPlaying(s)) {
            break;
        }
        GameController game = new GameController();
        game.addPlayer(s);
        s.getUserProperties().put("gameid", game.getId());
        waitingGames.add(game);
        game.gameStatus = GameController.GAME_WAITING;

        s.getBasicRemote().sendText(Utils.assembleChatMessage("Server",
                "ok, started game, waiting for players to join.", "server"));

        game.getPlayer(s).sendDataMessage(MessageBean.MSG_START_GAME, game.getId().toString());
        //sendDataMessage(s, , );
        printSysOut("start game: " + m.getData());
        break;

    case MessageBean.MSG_PLAYER_NAME:
        String name = m.getData();
        s.getUserProperties().put("name", name);
        printSysOut("user with name: " + name);
        for (Session ss : s.getOpenSessions()) {
            if (ss.equals(s))
                continue;
            ss.getBasicRemote()
                    .sendText(Utils.assembleChatMessage("Server", "Player join lobby: " + name, "server"));
        }
        break;

    case MessageBean.MSG_JOIN_GAME:
        if (isAlreadyPlaying(s)) {
            break;
        }

        printSysOut("joingame: " + m.getData());
        g = joinGame(s, m);
        if (g != null) {
            try {
                waitingGames.remove(g);
            } catch (Exception e) {
                printSysOut("JOIN FAILED");
            }
        } else {
            printSysOut("NO GAME MATCH FOUND: " + m.getData());
            Utils.sendDataMessage(MessageBean.MSG_PLAYER_QUIT, "Game has expired!", s);
        }
        break;

    case MessageBean.MSG_JOIN_LOBBY_CHAT:
        s.getUserProperties().put("lobby", Boolean.parseBoolean(m.getData()));
        break;

    case MessageBean.MSG_GAME_AVAILABLE:
        if (isAlreadyPlaying(s)) {
            break;
        }
        synchronized (waitingGames) {
            if (waitingGames.isEmpty()) {
                s.getBasicRemote().sendText(
                        Utils.assembleChatMessage("Server", "No games available, try starting one!", "server"));
            }
            for (GameController gc : waitingGames) {
                sendGameAvailMessage(s, gc.getId().toString(), gc.players.get(0));
            }
        }
        break;

    case MessageBean.MSG_PLAYER_READY:
        g = getGame(s);
        if (g == null || g.gameStatus == GameController.GAME_STARTED)
            break;
        printSysOut("gamestate = " + g.gameStatus);
        p = g.getPlayer(s);
        o = g.getOpponent(s);

        p.ready = true;

        if (g.readyCheck()) {
            p.sendDataMessage(MessageBean.MSG_PLAYER_READY, "rdy");
            o.sendDataMessage(MessageBean.MSG_PLAYER_READY, "rdy");
            g.start();
        }
        break;

    case MessageBean.MSG_PLAYER_QUIT:
        quitGame(s, MessageBean.MSG_PLAYER_QUIT);
        break;

    case MessageBean.MSG_PLAYER_SELECT:
        g = getGame(s);
        if (g == null)
            break;
        p = g.getPlayer(s);
        p.imageId = Integer.parseInt(m.getData());
        o = g.getOpponent(s);
        if (o != null) {
            printSysOut("SENT DISABLE MESSAGE: " + p.imageId + " to: " + o.session.getId());
            o.sendDataMessage(MessageBean.MSG_PLAYER_SELECT, m.getData());
        }
        break;

    default:
        break;

    }

}

From source file:org.damcode.web.c4webserver.Server.java

private GameController getGame(Session s) {
    return (GameController) s.getUserProperties().get("game");
}