public void sendMessage(ChatUser toUser, String textMessage)
    {
        ChatDialogMessage dialogMessage = new ChatDialogMessage(textMessage, systemController.getUserInfo().userId, -1);
        if (dialogs.ContainsKey(toUser.userId))
        {
            dialogs[toUser.userId].messages.Add(dialogMessage);
        }
        else
        {
            ChatDialogWithUser dialog = new ChatDialogWithUser();
            dialog.withUser = toUser;
            dialog.messages.Add(dialogMessage);
            dialogs.Add(toUser.userId, dialog);
        }

        DataMessage message = new DataMessage(Ids.Services.GAMES, Ids.Actions.SimpleChat.SEND_MESSAGE, systemController.getUserInfo().session);
        message.createWriter()
            .writerInt32(GameId)
            .writerInt32(toUser.userId)
            .writerInt32(myCallback)
            .writerString(textMessage)
            .closeWriter();

        connection.addMessageToSend(message);
    }
 public void requestUsersList()
 {
     connection.registerDataListener(Ids.Services.GAMES, Ids.Actions.SimpleChat.REQUEST_USERS_LIST, errorsHandler);
     DataMessage message = new DataMessage(Ids.Services.GAMES, Ids.Actions.SimpleChat.REQUEST_USERS_LIST, systemController.getUserInfo().session);
     message.createWriter().writerInt32(GameId).writerInt32(myCallback).closeWriter();
     connection.addMessageToSend(message);
 }
 private void requestUserInfo(int userId)
 {
     DataMessage message = new DataMessage(Ids.Services.GAMES, Ids.Actions.SimpleChat.REQUEST_USER_INFO, systemController.getUserInfo().session);
     message.createWriter().writerInt32(GameId).writerInt32(userId).writerInt32(myCallback).closeWriter();
     connection.addMessageToSend(message);
 }
 public void sendUserReady()
 {
     DataMessage message = new DataMessage(Ids.Services.GAMES, Ids.Actions.SimpleChat.NEW_USER, systemController.getUserInfo().session);
     message.createWriter().writerInt32(GameId).closeWriter();
     connection.addMessageToSend(message);
 }
    public void requestGame(long gameId, BaseGameController controller, RunnableDelegate callback)
    {
        if (state != SystemControllerStates.Default)
            throw new InvalidOperationException("Invalid SystemController state, waiting Default, but have : " + state);

        if (connection.isConnected()) {
            DataMessage message = new DataMessage(Ids.Services.GAME_RESLOVER, Ids.Actions.GameResolver.START_GAME_REQUEST, userInfo.session);
            message.createWriter();
            message.writerLong(gameId).closeWriter();

            state = SystemControllerStates.RequestGame;
            callbacks[SystemCallbacks.RequestGame] = callback;

            currentGame = controller;

            connection.registerDataListener(Ids.Services.GAME_RESLOVER, Ids.Actions.GameResolver.START_GAME_REQUEST, onGameRequestComplete);
            connection.registerDataListener(Ids.Services.GAME_RESLOVER, Ids.Actions.GameResolver.GAME_STARTED, onGameStarted);
            connection.registerDataListener(Ids.Services.GAME_RESLOVER, Ids.Actions.GameResolver.GAME_FINISHED, onGameFinished);
            connection.addMessageToSend(message);
        } else {
            Handler.getInstance().postAction(callback, RequestGameResults.ConnectionError);
        }
    }
    public void registerPlayer(RunnableDelegate callback)
    {
        if (state != SystemControllerStates.Default)
            throw new InvalidOperationException("Invalid SystemController state, waiting Default, but have : " + state);

        if (connection.isConnected())
        {
            DataMessage message = new DataMessage(Ids.Services.CLIENTS, Ids.Actions.Clients.REGISTER_NEW, null);
            JSONObject json = JSONBuilder.create()
               .with("type", Ids.Actions.Authorizations.BY_LOGIN_AND_PASSWORD)
               .with("id", userInfo.userLogin)
               .with("password", userInfo.userPassword).getJson();

            message.createWriter().writeJson(json).closeWriter();

            state = SystemControllerStates.RegisterUser;
            callbacks[SystemCallbacks.Registration] = callback;
            connection.registerDataListener(message, onRegistrationComplete);
            connection.addMessageToSend(message);
        }
        else
        {
            Handler.getInstance().postAction(callback, RegistrationResults.ConnectionError);
        }
    }