private bool onUserExit(DataMessage message) { message.createReader(); int userId = message.readInt32(); message.closeReader(); users.Remove(userId); dialogs.Remove(userId); postUpdate(UiUpdates.UserRemoved, userId); return false; }
private bool onReceiveUserInfo(DataMessage message) { message.createReader(); int callback = message.readInt32(); int result = message.readInt32(); switch(result) { case Ids.SystemResults.SUCCESS: { int requestedUsedId = message.readInt32(); Dictionary<string, object> json = message.readJson(); message.closeReader(); ChatUser user; if (users.ContainsKey(requestedUsedId)) { user = users[requestedUsedId]; } else { user = new ChatUser(); users.Add(requestedUsedId, user); } if (!user.fromJson(json)) { Debug.LogError("ReceiveUserInfo : fail to parse user data for "+requestedUsedId.ToString()); user.setUnknown(requestedUsedId); } postUpdate(UiUpdates.UserUpdated, user); } break; case Ids.Actions.SimpleChat.RESULT_NO_USER_WITH_SUCH_ID: { int requestedUsedId = message.readInt32(); message.closeReader(); Debug.LogError("ReceiveUserInfo : requested User not found id: " + requestedUsedId.ToString()); } break; case Ids.SystemResults.INVALID_SESSION: message.closeReader(); Debug.LogError("ReceiveUserInfo : Internal error - Invalid session"); break; case Ids.SystemResults.INVALID_DATA: message.closeReader(); Debug.LogError("ReceiveUserInfo : Internal error - Invalid data"); break; case Ids.SystemResults.NO_GAME_WITH_SUCH_ID: message.closeReader(); Debug.LogError("ReceiveUserInfo : Internal error - no game with such id"); break; } return false; }
private bool onUserEnter(DataMessage message) { message.createReader(); Dictionary<string, object> json = message.readJson(); message.closeReader(); ChatUser u = new ChatUser(); if (u.fromJson(json)) { if (u.userId == systemController.getUserInfo().userId) { postUpdate(UiUpdates.GameIsReady, null); } else { if (!users.ContainsKey(u.userId)) { users.Add(u.userId, u); } postUpdate(UiUpdates.NewUser, u); } } return false; }
private bool onReceiveMessage(DataMessage message) { message.createReader(); int userId = message.readInt32(); string userMessage = message.readString(); message.closeReader(); ChatUser user = getUser(userId); if (user == null) { user = new ChatUser(); user.setUnknown(userId); users.Add(userId, user); requestUserInfo(userId); } ChatDialogMessage dialogMessage = new ChatDialogMessage(userMessage, user.userId, -1); if (dialogs.ContainsKey(userId)) { dialogs[userId].messages.Add(dialogMessage); } else { ChatDialogWithUser dialog = new ChatDialogWithUser(); dialog.withUser = user; dialog.messages.Add(dialogMessage); dialogs.Add(userId, dialog); } postUpdate(UiUpdates.ReceivedMessage, dialogMessage); return false; }
private bool onReceivePlayersList(DataMessage message) { message.createReader(); int callback = message.readInt32(); if (callback != myCallback) { Debug.LogError("receive users list, but invalid Callback"); } Dictionary<string, object> json = message.readJson(); message.closeReader(); object[] usersJson = (object[])json["users"]; for (int i = 0; i < usersJson.Length; i++) { ChatUser u = new ChatUser(); if (u.fromJson((Dictionary<string, object>)usersJson[i])) { users[u.userId] = u; } } postUpdate(UiUpdates.UpdateUsersList, null); sendUserReady(); return false; }
private bool errorsHandler(DataMessage message) { message.createReader(); int result = message.readInt32(); message.closeReader(); if (!checkErrorResult(result)) { uiController.getMessageBox().showMessage("Internal error!"); } return true; }
private bool onMessageSenedResult(DataMessage message) { message.createReader(); int result = message.readInt32(); int callback = message.readInt32(); message.closeReader(); if (result != Ids.SystemResults.SUCCESS) { Debug.LogError("Fail to send message for callback : " + callback.ToString()); } else { if (callback != myCallback) { Debug.LogError("receive send message result, but invalid Callback"); } } postUpdate(UiUpdates.MessageSended, callback); return false; }
private bool onRegistrationComplete(DataMessage message) { RunnableDelegate runnable; if (callbacks.TryGetValue(SystemCallbacks.Registration,out runnable) && message!=null && message.Data != null) { callbacks.Remove(SystemCallbacks.Registration); message.createReader(); int result = message.readInt32(); message.closeReader(); switch (result) { case Ids.UserManagerResults.INVALID_DATA: Handler.getInstance().postAction(runnable, RegistrationResults.InvalidData); break; case Ids.UserManagerResults.REGISTER_SUCH_USER_EXIST: Handler.getInstance().postAction(runnable, RegistrationResults.UserExist); break; case Ids.UserManagerResults.SUCCESS: Handler.getInstance().postAction(runnable, RegistrationResults.Success); break; case Ids.UserManagerResults.REGISTER_UNKNOWN_TYPE: case Ids.UserManagerResults.INTERNAL_ERROR: Handler.getInstance().postAction(runnable, RegistrationResults.IntentalServerError); break; default: Handler.getInstance().postAction(runnable, RegistrationResults.IntentalServerError); break; } state = SystemControllerStates.Default; } return true; }
private bool onLoginComplete(DataMessage message) { RunnableDelegate runnable; if (callbacks.TryGetValue(SystemCallbacks.Authorization, out runnable) && message != null && message.Data != null) { message.createReader(); int result = message.readInt32(); callbacks.Remove(SystemCallbacks.Authorization); switch (result) { case Ids.UserManagerResults.INVALID_DATA: message.closeReader(); Handler.getInstance().postAction(runnable, LoginResults.InvalidData); break; case Ids.UserManagerResults.LOGIN_OR_PASSWORD_INVALID: message.closeReader(); Handler.getInstance().postAction(runnable, LoginResults.InvalidLoginOrPassword); break; case Ids.UserManagerResults.SUCCESS: { int clientId = message.readInt32(); string session = message.Session; message.closeReader(); userInfo.session = session; userInfo.userId = clientId; Handler.getInstance().postAction(runnable, LoginResults.Success); } break; case Ids.UserManagerResults.REGISTER_UNKNOWN_TYPE: case Ids.UserManagerResults.INTERNAL_ERROR: message.closeReader(); Handler.getInstance().postAction(runnable, LoginResults.IntentalServerError); break; default: message.closeReader(); Handler.getInstance().postAction(runnable, LoginResults.IntentalServerError); break; } state = SystemControllerStates.Default; } else { UnityEngine.Debug.LogError(GetType().Name + ", onLoginComplete : can`t get callback or empty message = " + message); } return true; }
private bool onGameStarted(DataMessage message) { if (state != SystemControllerStates.WaitGameStart) throw new InvalidOperationException("Invalid SystemController state, waiting WaitGameStart, but have : " + state); userInfo.session = message.Session; if (message !=null && message.Data != null) { message.createReader(); int gameId = message.readInt32(); message.closeReader(); currentGame.GameId = gameId; state = SystemControllerStates.InGame; currentGame.startGame(); } return true; }
private bool onGameRequestComplete(DataMessage message) { RunnableDelegate runnable; if (callbacks.TryGetValue(SystemCallbacks.RequestGame, out runnable) && message != null && message.Data != null) { message.createReader(); int result = message.readInt32(); message.closeReader(); callbacks.Remove(SystemCallbacks.RequestGame); switch (result) { case Ids.SystemResults.SUCCESS: currentGame.prepareGame(); Handler.getInstance().postAction(runnable, RequestGameResults.Success); state = SystemControllerStates.WaitGameStart; break; case Ids.SystemResults.GAME_IS_UNAVALABLE_NOW: Handler.getInstance().postAction(runnable, RequestGameResults.GameIsUnavalble); state = SystemControllerStates.Default; break; case Ids.SystemResults.INVALID_DATA: case Ids.SystemResults.INVALID_SESSION: Handler.getInstance().postAction(runnable, RequestGameResults.InvalidData); state = SystemControllerStates.Default; break; case Ids.SystemResults.INTERNAL_ERROR: Handler.getInstance().postAction(runnable, RequestGameResults.IntentalServerError); state = SystemControllerStates.Default; break; default: Handler.getInstance().postAction(runnable, RequestGameResults.IntentalServerError); state = SystemControllerStates.Default; break; } } else { UnityEngine.Debug.LogError(GetType().Name + ", onGameRequestComplete : can`t get callback or empty message = " + message); } return true; }
private bool onGameFinished(DataMessage message) { if (state != SystemControllerStates.InGame) throw new InvalidOperationException("Invalid SystemController state, waiting InGame), but have : " + state); if (message != null && message.Data != null) { message.createReader(); int gameId = message.readInt32(); int result = message.readInt32(); Dictionary<string, object> data = message.readJson(); message.closeReader(); if (gameId == currentGame.GameId) { state = SystemControllerStates.Default; currentGame.finishGame(result, data); } else { } } return true; }