public void UseChangeNickNameItem(GameSession session, CUseChangeNickNameItemReqMessage message) { var plr = session.Player; var item = plr.Inventory[message.ItemId]; var nickname = new NicknameHistoryDto { AccountId = (int)plr.Account.Id, Nickname = message.Nickname }; switch (item.ItemNumber) { case 4000001: // Permanent NickName Change nickname.ExpireDate = (long)(-1); break; case 4000002: // Remove NickName Change session.SendAsync(new SServerResultInfoAckMessage(ServerResult.FailedToRequestTask)); break; case 4000003: // 1 Day Nickname Change nickname.ExpireDate = DateTimeOffset.Now.AddDays(1).ToUnixTimeSeconds(); break; case 4000004: // 7 Day NickName Change nickname.ExpireDate = DateTimeOffset.Now.AddDays(7).ToUnixTimeSeconds(); break; case 4000005: // 30 Day NickName Change nickname.ExpireDate = DateTimeOffset.Now.AddDays(30).ToUnixTimeSeconds(); break; default: session.SendAsync(new SServerResultInfoAckMessage(ServerResult.FailedToRequestTask)); return; } using (var auth = AuthDatabase.Open()) auth.Insert(nickname); plr.Inventory.Remove(item); session.SendAsync(new SUseChangeNickItemAckMessage { Result = 0, Unk2 = 0, Unk3 = message.Nickname }); }
public async Task UseChangeNameItem(GameSession session, ItemUseChangeNickReqMessage message) { var plr = session.Player; var item = plr.Inventory[message.ItemId]; var ascii = Config.Instance.Game.NickRestrictions.AsciiOnly; if (!await AuthService.IsNickAvailableAsync(message.Nickname)) { await plr.Session.SendAsync(new ServerResultAckMessage(ServerResult.NicknameUnavailable)); } if (!Namecheck.IsNameValid(message.Nickname, true) || ascii && message.Nickname.Any(c => c > 127) || !ascii && message.Nickname.Any(c => c > 255)) { await session.SendAsync(new NickCheckAckMessage(true)); return; } var nickname = new NicknameHistoryDto { AccountId = (int)plr.Account.Id, OldName = plr.Account.Nickname, NewNickname = message.Nickname }; switch (item.ItemNumber) { case 4000001: //Perm Change nickname.ExpireDate = -1; if (await ChangeNickname(session.Player, nickname, false)) { plr.Inventory.RemoveOrDecrease(item); } break; default: await session.SendAsync(new ServerResultAckMessage(ServerResult.FailedToRequestTask)); return; } }
public static async Task <bool> ChangeNickname(Player plr, NicknameHistoryDto nicknameHistory, bool restore) { var toNickname = nicknameHistory.NewNickname; try { using (var db = AuthDatabase.Open()) { var account = (await DbUtil.FindAsync <AccountDto>(db, statement => statement .Where($"{nameof(AccountDto.Nickname):C} = @Nickname") .WithParameters(new { plr.Account.Nickname }))).FirstOrDefault(); if (account == null) { await plr.Session.SendAsync(new ServerResultAckMessage(ServerResult.FailedToRequestTask)); return(false); } if (restore) { var nicknameHistory1 = await DbUtil.FindAsync <NicknameHistoryDto>(db, statement => statement .Where($"{nameof(NicknameHistoryDto.AccountId):C} = @Id") .WithParameters(new { plr.Account.Id })); var firstchange = nicknameHistory1.FirstOrDefault(); if (firstchange == null) { await plr.Session.SendAsync(new ServerResultAckMessage(ServerResult.NicknameUnavailable)); return(false); } account.Nickname = firstchange.OldName; plr.Account.Nickname = firstchange.OldName; foreach (var history in nicknameHistory1) { await DbUtil.DeleteAsync(db, history); } await plr.Session.SendAsync(new ItemUseChangeNickCancelAckMessage(0)); } else { if (!await AuthService.IsNickAvailableAsync(toNickname)) { await plr.Session.SendAsync(new ServerResultAckMessage(ServerResult.NicknameUnavailable)); return(false); } account.Nickname = toNickname; plr.Account.Nickname = toNickname; DbUtil.Insert(db, nicknameHistory); await plr.Session.SendAsync(new ItemUseChangeNickAckMessage { Result = 0, Unk2 = 0, Unk3 = toNickname }); } DbUtil.Update(db, account); return(true); } } catch (Exception e) { Logger.Error(e.ToString()); await plr.Session.SendAsync(new ServerResultAckMessage(ServerResult.FailedToRequestTask)); return(false); } }