Esempio n. 1
0
  private boolean preHandleEvent(GameEvent event, GameSession session) {
    AbstractRequestHandler handler = null;
    switch ((GameCommandType) event.getKey()) {
      case START_GAME_REQUEST:
        handler = new StartGameRequestHandler(event, session);
        break;
      case CLEAN_DRAW_REQUEST:
        handler = new CleanDrawRequestHandler(event, session);
        break;
      case SEND_DRAW_DATA_REQUEST:
        handler = new DrawDataRequestHandler(event, session);
        break;
      case CHAT_REQUEST:
        handler = new ChatRequestHandler(event, session);
        break;
      default:
        return false;
    }

    if (handler == null) return false;

    try {
      handler.handleRequest(event.getMessage());
    } catch (Exception e) {
      GameLog.error(event.getTargetSession(), e);
    }
    return true;
  }
Esempio n. 2
0
  @Override
  public void run() {

    logger.info("Thread " + threadHashKey + " is running...");

    while (stopFlag.intValue() == 0) {
      GameEvent event = null;
      try {
        event = queue.take();
        GameSession session = gameManager.findGameSessionById(event.getTargetSession());
        if (session == null) {
          GameLog.warn(
              event.getTargetSession(),
              "process event " + event.getKey() + " but session id not found ");
          continue;
        }

        boolean skipEvent = preHandleEvent(event, session);
        if (skipEvent) {
          continue;
        }

        com.orange.common.statemachine.State nextState =
            stateMachine.nextState(session.getCurrentState(), event, session);
        if (nextState == null) {
          GameLog.warn(
              event.getTargetSession(),
              "process event " + event.getKey() + " but next state not found");
          continue;
        }

        session.setCurrentState(nextState);

        /*
        GameSession session = gameManager.findGameSessionById(event.getTargetSession());
        if (session == null){
        	// no session available for this event?

        	GameLog.warn(event.getTargetSession(), "process event but session id not found ");
        	HandlerUtils.sendErrorResponse(event, GameResultCode.ERROR_NO_SESSION_ID);
        	continue;
        }

        if (session.getCurrentState().validateEvent(event, session) != 0){
        	// donnot send back request here since validate event will make it
        	// argument, where to send the response???
        	continue;
        }

         com.orange.common.statemachine.State nextState =
        	 stateMachine.nextState(session.getCurrentState(), event, session);

         if (nextState == null){
        	 // incorrect message event?
        	 HandlerUtils.sendErrorResponse(event, GameResultCode.ERROR_NEXT_STATE_NOT_FOUND);
        	 continue;
         }

         session.setCurrentState(nextState);
         */

      } catch (Exception e) {
        logger.error("catch exception while handle event, exception = " + e.toString(), e);
        // rem by Benson for refactor
        //				if (event != null){
        //					HandlerUtils.sendErrorResponse(event, GameResultCode.ERROR_SYSTEM_EXCEPTION);
        //				}
      }
    }

    logger.info("Thread " + threadHashKey + " is stop...");
  }