Search in sources :

Example 1 with ActivityMgmtService

use of org.b3log.symphony.service.ActivityMgmtService in project symphony by b3log.

the class ChessGame method onMessage.

/**
 * Called when a message received from the browser.
 *
 * @param message message
 */
@OnMessage
public void onMessage(final String message) throws JSONException {
    JSONObject jsonObject = new JSONObject(message);
    final String player = jsonObject.optString("player");
    final String anti = getAntiPlayer(player);
    JSONObject sendText = new JSONObject();
    final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
    switch(jsonObject.optInt("type")) {
        case // 聊天
        1:
            LOGGER.debug(jsonObject.optString("message"));
            final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);
            sendText.put("type", 1);
            try {
                sendText.put("player", userQueryService.getUser(player).optString(User.USER_NAME));
            } catch (ServiceException e) {
                LOGGER.error("service not avaliable");
            }
            sendText.put("message", jsonObject.optString("message"));
            SESSIONS.get(anti).getAsyncRemote().sendText(sendText.toString());
            break;
        case // 落子
        2:
            ChessGame chessGame = chessPlaying.keySet().contains(player) ? chessPlaying.get(player) : chessPlaying.get(anti);
            int x = jsonObject.optInt("x");
            int y = jsonObject.optInt("y");
            int size = jsonObject.optInt("size");
            if (chessGame != null) {
                if (chessGame.getChess()[x / size][y / size] != 0) {
                    return;
                }
                boolean flag = false;
                if (player.equals(chessGame.getPlayer1())) {
                    if (chessGame.getStep() != 1) {
                        return;
                    } else {
                        sendText.put("color", "black");
                        chessGame.getChess()[x / size][y / size] = 1;
                        flag = chessGame.chessCheck(1);
                        chessGame.setStep(2);
                    }
                } else {
                    if (chessGame.getStep() != 2) {
                        return;
                    } else {
                        sendText.put("color", "white");
                        chessGame.getChess()[x / size][y / size] = 2;
                        flag = chessGame.chessCheck(2);
                        chessGame.setStep(1);
                    }
                }
                sendText.put("type", 2);
                sendText.put("player", player);
                sendText.put("posX", x);
                sendText.put("posY", y);
                sendText.put("chess", chessGame.getChess());
                sendText.put("step", chessGame.getStep());
                // 总有一次能正确移除,分开写只是为了好看,没有逻辑原因
                if (flag) {
                    sendText.put("result", "You Win");
                    chessPlaying.remove(player);
                }
                SESSIONS.get(player).getAsyncRemote().sendText(sendText.toString());
                if (flag) {
                    sendText.put("result", "You Lose");
                    chessPlaying.remove(anti);
                }
                SESSIONS.get(anti).getAsyncRemote().sendText(sendText.toString());
                if (flag) {
                    final ActivityMgmtService activityMgmtService = beanManager.getReference(ActivityMgmtService.class);
                    activityMgmtService.collectGobang(player, Pointtransfer.TRANSFER_SUM_C_ACTIVITY_GOBANG_START * 2);
                    SESSIONS.remove(player);
                    SESSIONS.remove(anti);
                }
            }
            break;
        case // 和棋
        7:
            if ("request".equals(jsonObject.optString("drawType"))) {
                sendText.put("type", 7);
                SESSIONS.get(anti).getAsyncRemote().sendText(sendText.toString());
            } else if ("yes".equals(jsonObject.optString("drawType"))) {
                sendText.put("type", 6);
                sendText.put("message", "【系统】:双方和棋,积分返还,游戏结束");
                chessPlaying.remove(player);
                chessPlaying.remove(anti);
                antiPlayer.remove(player);
                antiPlayer.remove(anti);
                final ActivityMgmtService activityMgmtService = beanManager.getReference(ActivityMgmtService.class);
                activityMgmtService.collectGobang(player, Pointtransfer.TRANSFER_SUM_C_ACTIVITY_GOBANG_START);
                activityMgmtService.collectGobang(anti, Pointtransfer.TRANSFER_SUM_C_ACTIVITY_GOBANG_START);
                SESSIONS.get(player).getAsyncRemote().sendText(sendText.toString());
                SESSIONS.get(anti).getAsyncRemote().sendText(sendText.toString());
                SESSIONS.remove(player);
                SESSIONS.remove(anti);
            } else if ("no".equals(jsonObject.optString("drawType"))) {
                sendText.put("type", 6);
                sendText.put("message", "【系统】:对手拒绝和棋,请继续下棋");
                SESSIONS.get(player).getAsyncRemote().sendText(sendText.toString());
            }
            break;
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) UserQueryService(org.b3log.symphony.service.UserQueryService) ActivityMgmtService(org.b3log.symphony.service.ActivityMgmtService) ServerEndpoint(javax.websocket.server.ServerEndpoint) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Example 2 with ActivityMgmtService

use of org.b3log.symphony.service.ActivityMgmtService in project symphony by b3log.

the class ChessGame method removeSession.

/**
 * Removes the specified session.
 *
 * @param session the specified session
 */
private void removeSession(final Session session) {
    for (String player : SESSIONS.keySet()) {
        if (session.equals(SESSIONS.get(player))) {
            if (getAntiPlayer(player) == null) {
                for (ChessGame chessGame : chessRandomWait) {
                    if (player.equals(chessGame.getPlayer1())) {
                        chessRandomWait.remove(chessGame);
                    }
                }
            } else {
                if (chessPlaying.get(player) != null) {
                    // 说明玩家1断开了链接
                    ChessGame chessGame = chessPlaying.get(player);
                    chessGame.setPlayState1(false);
                    if (!chessGame.isPlayState2()) {
                        chessPlaying.remove(player);
                        antiPlayer.remove(player);
                        // 由于玩家2先退出,补偿玩家1的积分
                        final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
                        final ActivityMgmtService activityMgmtService = beanManager.getReference(ActivityMgmtService.class);
                        activityMgmtService.collectGobang(chessGame.getPlayer1(), Pointtransfer.TRANSFER_SUM_C_ACTIVITY_GOBANG_START);
                    } else {
                        JSONObject sendText = new JSONObject();
                        sendText.put("type", 6);
                        sendText.put("message", "【系统】:对手离开了棋局");
                        SESSIONS.get(chessGame.getPlayer2()).getAsyncRemote().sendText(sendText.toString());
                    }
                } else if (chessPlaying.get(getAntiPlayer(player)) != null) {
                    // 说明玩家2断开了链接
                    String player1 = getAntiPlayer(player);
                    ChessGame chessGame = chessPlaying.get(player1);
                    chessGame.setPlayState2(false);
                    if (!chessGame.isPlayState1()) {
                        chessPlaying.remove(player1);
                        antiPlayer.remove(player1);
                        // 由于玩家1先退出,补偿玩家2的积分
                        final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
                        final ActivityMgmtService activityMgmtService = beanManager.getReference(ActivityMgmtService.class);
                        activityMgmtService.collectGobang(chessGame.getPlayer2(), Pointtransfer.TRANSFER_SUM_C_ACTIVITY_GOBANG_START);
                    } else {
                        JSONObject sendText = new JSONObject();
                        sendText.put("type", 6);
                        sendText.put("message", "【系统】:对手离开了棋局");
                        SESSIONS.get(chessGame.getPlayer1()).getAsyncRemote().sendText(sendText.toString());
                    }
                }
            }
            SESSIONS.remove(player);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) ActivityMgmtService(org.b3log.symphony.service.ActivityMgmtService) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Example 3 with ActivityMgmtService

use of org.b3log.symphony.service.ActivityMgmtService in project symphony by b3log.

the class ChessGame method onConnect.

// 等待指定用户的棋局(暂不实现)
/**
 * Called when the socket connection with the browser is established.
 *
 * @param session session
 */
@OnOpen
public void onConnect(final Session session) {
    final JSONObject user = (JSONObject) Channels.getHttpSessionAttribute(session, User.USER);
    if (null == user) {
        return;
    }
    final String userId = user.optString(Keys.OBJECT_ID);
    final String userName = user.optString(User.USER_NAME);
    boolean playing = false;
    LOGGER.debug("new connection from " + userName);
    if (SESSIONS.containsKey(userId)) {
        JSONObject sendText = new JSONObject();
        sendText.put("type", 6);
        sendText.put("message", "【系统】:您已在匹配队列中,请勿开始多个游戏,如需打开新的窗口,请先关闭原窗口再开始");
        session.getAsyncRemote().sendText(sendText.toString());
        return;
    } else {
        SESSIONS.put(userId, session);
    }
    for (String temp : chessPlaying.keySet()) {
        ChessGame chessGame = chessPlaying.get(temp);
        if (userId.equals(chessGame.getPlayer1())) {
            // 玩家1返回战局
            recoverGame(userId, userName, chessGame.getPlayer2(), chessGame);
            chessGame.setPlayState1(true);
            playing = true;
        } else if (userId.equals(chessGame.getPlayer2())) {
            // 玩家2返回战局
            recoverGame(userId, userName, chessGame.getPlayer1(), chessGame);
            chessGame.setPlayState2(true);
            playing = true;
        }
    }
    if (playing) {
        return;
    } else {
        ChessGame chessGame = null;
        JSONObject sendText = new JSONObject();
        do {
            chessGame = chessRandomWait.poll();
        } while (chessRandomWait.size() > 0 && SESSIONS.get(chessGame.getPlayer1()) == null);
        if (chessGame == null) {
            chessGame = new ChessGame(userId, userName);
            chessRandomWait.add(chessGame);
            sendText.put("type", 3);
            sendText.put("playerName", userName);
            sendText.put("message", "【系统】:请等待另一名玩家加入游戏");
            session.getAsyncRemote().sendText(sendText.toString());
        } else if (userId.equals(chessGame.getPlayer1())) {
            // 仍然在匹配队列中
            // 重新入队
            chessRandomWait.add(chessGame);
            sendText.put("type", 3);
            sendText.put("playerName", userName);
            sendText.put("message", "【系统】:请等待另一名玩家加入游戏");
            session.getAsyncRemote().sendText(sendText.toString());
        } else {
            final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
            chessGame.setPlayer2(userId);
            chessGame.setName2(userName);
            chessGame.setPlayState2(true);
            chessGame.setStep(1);
            chessPlaying.put(chessGame.getPlayer1(), chessGame);
            antiPlayer.put(chessGame.getPlayer1(), chessGame.getPlayer2());
            final ActivityMgmtService activityMgmtService = beanManager.getReference(ActivityMgmtService.class);
            sendText.put("type", 4);
            // 针对开局玩家的消息
            sendText.put("message", "【系统】:玩家 [" + userName + "] 已加入,游戏开始,请落子");
            sendText.put("player", chessGame.getPlayer1());
            SESSIONS.get(chessGame.getPlayer1()).getAsyncRemote().sendText(sendText.toString());
            // 针对参与玩家的消息
            sendText.put("message", "游戏开始~!您正在与 [" + chessGame.getName1() + "] 对战");
            sendText.put("player", chessGame.getPlayer2());
            session.getAsyncRemote().sendText(sendText.toString());
            JSONObject r1 = activityMgmtService.startGobang(chessGame.getPlayer1());
            JSONObject r2 = activityMgmtService.startGobang(chessGame.getPlayer2());
        }
    }
}
Also used : JSONObject(org.json.JSONObject) ActivityMgmtService(org.b3log.symphony.service.ActivityMgmtService) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Aggregations

LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)3 ActivityMgmtService (org.b3log.symphony.service.ActivityMgmtService)3 JSONObject (org.json.JSONObject)3 ServerEndpoint (javax.websocket.server.ServerEndpoint)1 ServiceException (org.b3log.latke.service.ServiceException)1 UserQueryService (org.b3log.symphony.service.UserQueryService)1