public async UniTaskVoid OpenStorage(long connectionId, IPlayerCharacterData playerCharacter, StorageId storageId) { #if UNITY_STANDALONE && !CLIENT_BUILD if (!CanAccessStorage(playerCharacter, storageId)) { GameInstance.ServerGameMessageHandlers.SendGameMessage(connectionId, UITextKeys.UI_ERROR_CANNOT_ACCESS_STORAGE); return; } // Store storage usage states if (!usingStorageClients.ContainsKey(storageId)) { usingStorageClients.TryAdd(storageId, new HashSet <long>()); } usingStorageClients[storageId].Add(connectionId); usingStorageIds.TryRemove(connectionId, out _); usingStorageIds.TryAdd(connectionId, storageId); // Load storage items from database ReadStorageItemsReq req = new ReadStorageItemsReq(); req.StorageType = (EStorageType)storageId.storageType; req.StorageOwnerId = storageId.storageOwnerId; ReadStorageItemsResp resp = await DbServiceClient.ReadStorageItemsAsync(req); List <CharacterItem> storageItems = DatabaseServiceUtils.MakeListFromRepeatedByteString <CharacterItem>(resp.StorageCharacterItems); SetStorageItems(storageId, storageItems); // Notify storage items to client uint storageObjectId; Storage storage = GetStorage(storageId, out storageObjectId); GameInstance.ServerGameMessageHandlers.NotifyStorageOpened(connectionId, storageId.storageType, storageId.storageOwnerId, storageObjectId, storage.weightLimit, storage.slotLimit); storageItems.FillEmptySlots(storage.slotLimit > 0, storage.slotLimit); GameInstance.ServerGameMessageHandlers.NotifyStorageItems(connectionId, storageItems); #endif }
private async UniTaskVoid HandleRequestCharactersRoutine(LiteNetLibMessageHandler messageHandler) { long connectionId = messageHandler.connectionId; RequestCharactersMessage message = messageHandler.ReadMessage <RequestCharactersMessage>(); ResponseCharactersMessage.Error error = ResponseCharactersMessage.Error.None; List <PlayerCharacterData> characters = null; CentralUserPeerInfo userPeerInfo; if (!userPeers.TryGetValue(connectionId, out userPeerInfo)) { error = ResponseCharactersMessage.Error.NotLoggedin; } else { CharactersResp charactersResp = await DbServiceClient.ReadCharactersAsync(new ReadCharactersReq() { UserId = userPeerInfo.userId }); characters = DatabaseServiceUtils.MakeListFromRepeatedByteString <PlayerCharacterData>(charactersResp.List); } ResponseCharactersMessage responseMessage = new ResponseCharactersMessage(); responseMessage.ackId = message.ackId; responseMessage.responseCode = error == ResponseCharactersMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error; responseMessage.error = error; responseMessage.characters = characters; ServerSendResponse(connectionId, MMOMessageTypes.ResponseCharacters, responseMessage); }
public async UniTask <DecreaseStorageItemsResult> DecreaseStorageItems(StorageId storageId, int dataId, short amount) { #if UNITY_STANDALONE && !CLIENT_BUILD Storage storge = GetStorage(storageId, out _); DecreaseStorageItemsReq req = new DecreaseStorageItemsReq(); req.StorageType = (EStorageType)storageId.storageType; req.StorageOwnerId = storageId.storageOwnerId; req.WeightLimit = storge.weightLimit; req.SlotLimit = storge.slotLimit; req.DataId = dataId; req.Amount = amount; DecreaseStorageItemsResp resp = await DbServiceClient.DecreaseStorageItemsAsync(req); if (UITextKeys.NONE != (UITextKeys)resp.Error) { // Error ocurring, storage may overwhelming let's it drop items to ground return(new DecreaseStorageItemsResult()); } SetStorageItems(storageId, DatabaseServiceUtils.MakeListFromRepeatedByteString <CharacterItem>(resp.StorageCharacterItems)); NotifyStorageItemsUpdated(storageId.storageType, storageId.storageOwnerId); Dictionary <int, short> decreasedItems = new Dictionary <int, short>(); foreach (ItemIndexAmountMap entry in resp.DecreasedItems) { decreasedItems.Add(entry.Index, (short)entry.Amount); } return(new DecreaseStorageItemsResult() { IsSuccess = true, DecreasedItems = decreasedItems, }); #else return(new DecreaseStorageItemsResult()); #endif }
protected async UniTaskVoid HandleRequestCharacters( RequestHandlerData requestHandler, EmptyMessage request, RequestProceedResultDelegate <ResponseCharactersMessage> result) { long connectionId = requestHandler.ConnectionId; ResponseCharactersMessage.Error error = ResponseCharactersMessage.Error.None; List <PlayerCharacterData> characters = null; CentralUserPeerInfo userPeerInfo; if (!userPeers.TryGetValue(connectionId, out userPeerInfo)) { error = ResponseCharactersMessage.Error.NotLoggedin; } else { CharactersResp charactersResp = await DbServiceClient.ReadCharactersAsync(new ReadCharactersReq() { UserId = userPeerInfo.userId }); characters = DatabaseServiceUtils.MakeListFromRepeatedByteString <PlayerCharacterData>(charactersResp.List); } // Response result.Invoke( error == ResponseCharactersMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error, new ResponseCharactersMessage() { error = error, characters = characters, }); }
protected async UniTaskVoid HandleRequestCharacters( RequestHandlerData requestHandler, EmptyMessage request, RequestProceedResultDelegate <ResponseCharactersMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD long connectionId = requestHandler.ConnectionId; UITextKeys message = UITextKeys.NONE; List <PlayerCharacterData> characters = null; CentralUserPeerInfo userPeerInfo; if (!userPeers.TryGetValue(connectionId, out userPeerInfo)) { message = UITextKeys.UI_ERROR_NOT_LOGGED_IN; } else { CharactersResp charactersResp = await DbServiceClient.ReadCharactersAsync(new ReadCharactersReq() { UserId = userPeerInfo.userId }); characters = DatabaseServiceUtils.MakeListFromRepeatedByteString <PlayerCharacterData>(charactersResp.List); } // Response result.Invoke( message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error, new ResponseCharactersMessage() { message = message, characters = characters, }); #endif }
public async UniTaskVoid HandleRequestMoveItemFromStorage(RequestHandlerData requestHandler, RequestMoveItemFromStorageMessage request, RequestProceedResultDelegate <ResponseMoveItemFromStorageMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD StorageId storageId = new StorageId(request.storageType, request.storageOwnerId); IPlayerCharacterData playerCharacter; if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter)) { result.Invoke(AckResponseCode.Error, new ResponseMoveItemFromStorageMessage() { message = UITextKeys.UI_ERROR_NOT_LOGGED_IN, }); return; } if (!GameInstance.ServerStorageHandlers.CanAccessStorage(playerCharacter, storageId)) { result.Invoke(AckResponseCode.Error, new ResponseMoveItemFromStorageMessage() { message = UITextKeys.UI_ERROR_CANNOT_ACCESS_STORAGE, }); return; } Storage storage = GameInstance.ServerStorageHandlers.GetStorage(storageId, out _); MoveItemFromStorageReq req = new MoveItemFromStorageReq(); req.StorageType = (EStorageType)request.storageType; req.StorageOwnerId = request.storageOwnerId; req.CharacterId = playerCharacter.Id; req.WeightLimit = storage.weightLimit; req.SlotLimit = storage.slotLimit; req.StorageItemIndex = request.storageItemIndex; req.StorageItemAmount = request.storageItemAmount; req.InventoryItemIndex = request.inventoryItemIndex; MoveItemFromStorageResp resp = await DbServiceClient.MoveItemFromStorageAsync(req); UITextKeys message = (UITextKeys)resp.Error; if (message != UITextKeys.NONE) { result.Invoke(AckResponseCode.Error, new ResponseMoveItemFromStorageMessage() { message = message, }); return; } playerCharacter.NonEquipItems = DatabaseServiceUtils.MakeListFromRepeatedByteString <CharacterItem>(resp.InventoryItemItems); GameInstance.ServerStorageHandlers.SetStorageItems(storageId, DatabaseServiceUtils.MakeListFromRepeatedByteString <CharacterItem>(resp.StorageCharacterItems)); GameInstance.ServerStorageHandlers.NotifyStorageItemsUpdated(request.storageType, request.storageOwnerId); // Success result.Invoke(AckResponseCode.Success, new ResponseMoveItemFromStorageMessage()); #endif }
public async UniTask <bool> SendMail(Mail mail) { #if UNITY_STANDALONE && !CLIENT_BUILD SendMailResp resp = await DbServiceClient.SendMailAsync(new SendMailReq() { Mail = DatabaseServiceUtils.ToByteString(mail), }); if (resp.Error == 0) { return(true); } #endif return(false); }
public async UniTaskVoid HandleRequestAcceptGuildInvitation(RequestHandlerData requestHandler, RequestAcceptGuildInvitationMessage request, RequestProceedResultDelegate <ResponseAcceptGuildInvitationMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD await UniTask.Yield(); BasePlayerCharacterEntity playerCharacter; if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter)) { result.Invoke(AckResponseCode.Error, new ResponseAcceptGuildInvitationMessage() { message = UITextKeys.UI_ERROR_NOT_LOGGED_IN, }); return; } ValidateGuildRequestResult validateResult = GameInstance.ServerGuildHandlers.CanAcceptGuildInvitation(request.guildId, playerCharacter); if (!validateResult.IsSuccess) { result.Invoke(AckResponseCode.Error, new ResponseAcceptGuildInvitationMessage() { message = validateResult.GameMessage, }); return; } playerCharacter.GuildId = request.guildId; validateResult.Guild.AddMember(playerCharacter); GameInstance.ServerGuildHandlers.SetGuild(request.guildId, validateResult.Guild); GameInstance.ServerGuildHandlers.RemoveGuildInvitation(request.guildId, playerCharacter.Id); // Save to database _ = DbServiceClient.UpdateCharacterGuildAsync(new UpdateCharacterGuildReq() { SocialCharacterData = DatabaseServiceUtils.ToByteString(SocialCharacterData.Create(playerCharacter)), GuildId = request.guildId, GuildRole = validateResult.Guild.GetMemberRole(playerCharacter.Id) }); // Broadcast via chat server if (ChatNetworkManager.IsClientConnected) { ChatNetworkManager.SendAddSocialMember(null, MMOMessageTypes.UpdateGuildMember, request.guildId, playerCharacter.Id, playerCharacter.CharacterName, playerCharacter.DataId, playerCharacter.Level); } GameInstance.ServerGameMessageHandlers.SendSetGuildData(requestHandler.ConnectionId, validateResult.Guild); GameInstance.ServerGameMessageHandlers.SendAddGuildMembersToOne(requestHandler.ConnectionId, validateResult.Guild); GameInstance.ServerGameMessageHandlers.SendAddGuildMembersToMembers(validateResult.Guild, playerCharacter.Id, playerCharacter.CharacterName, playerCharacter.DataId, playerCharacter.Level); result.Invoke(AckResponseCode.Success, new ResponseAcceptGuildInvitationMessage() { message = UITextKeys.UI_GUILD_INVITATION_ACCEPTED, }); #endif }
public async UniTaskVoid HandleRequestCreateParty(RequestHandlerData requestHandler, RequestCreatePartyMessage request, RequestProceedResultDelegate <ResponseCreatePartyMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD await UniTask.Yield(); IPlayerCharacterData playerCharacter; if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter)) { result.Invoke(AckResponseCode.Error, new ResponseCreatePartyMessage() { message = UITextKeys.UI_ERROR_NOT_LOGGED_IN, }); return; } ValidatePartyRequestResult validateResult = playerCharacter.CanCreateParty(); if (!validateResult.IsSuccess) { result.Invoke(AckResponseCode.Error, new ResponseCreatePartyMessage() { message = validateResult.GameMessage, }); return; } PartyResp createPartyResp = await DbServiceClient.CreatePartyAsync(new CreatePartyReq() { LeaderCharacterId = playerCharacter.Id, ShareExp = request.shareExp, ShareItem = request.shareItem }); PartyData party = DatabaseServiceUtils.FromByteString <PartyData>(createPartyResp.PartyData); GameInstance.ServerPartyHandlers.SetParty(party.id, party); playerCharacter.PartyId = party.id; // Broadcast via chat server if (ChatNetworkManager.IsClientConnected) { ChatNetworkManager.SendCreateParty(null, MMOMessageTypes.UpdateParty, party.id, party.shareExp, party.shareItem, playerCharacter.Id); ChatNetworkManager.SendAddSocialMember(null, MMOMessageTypes.UpdatePartyMember, party.id, playerCharacter.Id, playerCharacter.CharacterName, playerCharacter.DataId, playerCharacter.Level); } GameInstance.ServerGameMessageHandlers.SendSetPartyData(requestHandler.ConnectionId, party); GameInstance.ServerGameMessageHandlers.SendAddPartyMembersToOne(requestHandler.ConnectionId, party); result.Invoke(AckResponseCode.Success, new ResponseCreatePartyMessage()); #endif }
public async UniTask <bool> IncreaseStorageItems(StorageId storageId, CharacterItem addingItem) { #if UNITY_STANDALONE && !CLIENT_BUILD Storage storge = GetStorage(storageId, out _); IncreaseStorageItemsReq req = new IncreaseStorageItemsReq(); req.StorageType = (EStorageType)storageId.storageType; req.StorageOwnerId = storageId.storageOwnerId; req.WeightLimit = storge.weightLimit; req.SlotLimit = storge.slotLimit; req.Item = DatabaseServiceUtils.ToByteString(addingItem); IncreaseStorageItemsResp resp = await DbServiceClient.IncreaseStorageItemsAsync(req); if (UITextKeys.NONE != (UITextKeys)resp.Error) { // Error ocurring, storage may overwhelming let's it drop items to ground return(false); } SetStorageItems(storageId, DatabaseServiceUtils.MakeListFromRepeatedByteString <CharacterItem>(resp.StorageCharacterItems)); NotifyStorageItemsUpdated(storageId.storageType, storageId.storageOwnerId); return(true); #else return(false); #endif }
public async UniTaskVoid HandleRequestCreateGuild(RequestHandlerData requestHandler, RequestCreateGuildMessage request, RequestProceedResultDelegate <ResponseCreateGuildMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD await UniTask.Yield(); IPlayerCharacterData playerCharacter; if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter)) { result.Invoke(AckResponseCode.Error, new ResponseCreateGuildMessage() { message = UITextKeys.UI_ERROR_NOT_LOGGED_IN, }); return; } ValidateGuildRequestResult validateResult = playerCharacter.CanCreateGuild(request.guildName); if (!validateResult.IsSuccess) { result.Invoke(AckResponseCode.Error, new ResponseCreateGuildMessage() { message = validateResult.GameMessage, }); return; } FindGuildNameResp findGuildNameResp = await DbServiceClient.FindGuildNameAsync(new FindGuildNameReq() { GuildName = request.guildName, }); if (findGuildNameResp.FoundAmount > 0) { result.Invoke(AckResponseCode.Error, new ResponseCreateGuildMessage() { message = UITextKeys.UI_ERROR_GUILD_NAME_EXISTED, }); return; } GuildResp createGuildResp = await DbServiceClient.CreateGuildAsync(new CreateGuildReq() { LeaderCharacterId = playerCharacter.Id, GuildName = request.guildName, }); GuildData guild = DatabaseServiceUtils.FromByteString <GuildData>(createGuildResp.GuildData); GameInstance.Singleton.SocialSystemSetting.DecreaseCreateGuildResource(playerCharacter); GameInstance.ServerGuildHandlers.SetGuild(guild.id, guild); playerCharacter.GuildId = guild.id; playerCharacter.GuildRole = guild.GetMemberRole(playerCharacter.Id); playerCharacter.SharedGuildExp = 0; if (playerCharacter is BasePlayerCharacterEntity) { // Sync guild name to client (playerCharacter as BasePlayerCharacterEntity).GuildName = request.guildName; } // Broadcast via chat server if (ChatNetworkManager.IsClientConnected) { ChatNetworkManager.SendCreateGuild(null, MMOMessageTypes.UpdateGuild, guild.id, request.guildName, playerCharacter.Id); ChatNetworkManager.SendAddSocialMember(null, MMOMessageTypes.UpdateGuildMember, guild.id, playerCharacter.Id, playerCharacter.CharacterName, playerCharacter.DataId, playerCharacter.Level); } GameInstance.ServerGameMessageHandlers.SendSetGuildData(requestHandler.ConnectionId, guild); GameInstance.ServerGameMessageHandlers.SendAddGuildMembersToOne(requestHandler.ConnectionId, guild); result.Invoke(AckResponseCode.Success, new ResponseCreateGuildMessage()); #endif }
public async UniTaskVoid HandleRequestSendMail(RequestHandlerData requestHandler, RequestSendMailMessage request, RequestProceedResultDelegate <ResponseSendMailMessage> result) { #if UNITY_STANDALONE && !CLIENT_BUILD IPlayerCharacterData playerCharacter; if (GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter)) { // Validate gold if (request.gold < 0) { request.gold = 0; } if (playerCharacter.Gold >= request.gold) { playerCharacter.Gold -= request.gold; } else { result.Invoke(AckResponseCode.Error, new ResponseSendMailMessage() { message = UITextKeys.UI_ERROR_NOT_ENOUGH_GOLD, }); return; } // Find receiver GetUserIdByCharacterNameResp userIdResp = await DbServiceClient.GetUserIdByCharacterNameAsync(new GetUserIdByCharacterNameReq() { CharacterName = request.receiverName, }); string receiverId = userIdResp.UserId; if (string.IsNullOrEmpty(receiverId)) { result.Invoke(AckResponseCode.Error, new ResponseSendMailMessage() { message = UITextKeys.UI_ERROR_MAIL_SEND_NO_RECEIVER, }); return; } Mail mail = new Mail() { SenderId = playerCharacter.UserId, SenderName = playerCharacter.CharacterName, ReceiverId = receiverId, Title = request.title, Content = request.content, Gold = request.gold, }; SendMailResp resp = await DbServiceClient.SendMailAsync(new SendMailReq() { Mail = DatabaseServiceUtils.ToByteString(mail), }); UITextKeys message = (UITextKeys)resp.Error; result.Invoke( message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error, new ResponseSendMailMessage() { message = message, }); } else { result.Invoke(AckResponseCode.Error, new ResponseSendMailMessage() { message = UITextKeys.UI_ERROR_SERVICE_NOT_AVAILABLE, }); } #endif }