public Friend AddOrUpdate(ulong friendId, Player plr, FriendState playerState, FriendState friendState) { using (var db = GameDatabase.Open()) { if (_friends.TryGetValue(friendId, out var friend)) { var dbFriend = DbUtil.Find <PlayerFriendDto>(db, statement => statement .Where( $"{nameof(PlayerFriendDto.PlayerId):C} = @PlayerId AND {nameof(PlayerFriendDto.FriendId):C} = @FriendId") .WithParameters(new { PlayerId = Player.Account.Id, FriendId = friendId })).FirstOrDefault(); if (dbFriend == null) { dbFriend = DbUtil.Find <PlayerFriendDto>(db, statement => statement .Where( $"{nameof(PlayerFriendDto.PlayerId):C} = @PlayerId AND {nameof(PlayerFriendDto.FriendId):C} = @FriendId") .WithParameters(new { PlayerId = friendId, FriendId = Player.Account.Id })).FirstOrDefault(); } if (dbFriend == null) { throw new Exception("db and server are de-synced!"); } dbFriend.PlayerState = (int)playerState; dbFriend.FriendState = (int)friendState; DbUtil.Update(db, dbFriend); friend.PlayerState = playerState; friend.FriendState = friendState; return(friend); } var newDbFriend = new PlayerFriendDto() { PlayerId = (int)Player.Account.Id, FriendId = (int)friendId, PlayerState = (int)playerState, FriendState = (int)friendState }; DbUtil.Insert(db, newDbFriend); var newFriend = new Friend(Player.Account.Id, friendId, playerState, friendState); if (!_friends.TryAdd(newFriend.FriendId, newFriend)) { throw new ArgumentException("Player is already added", nameof(Player.Account.Id)); } if (plr != null) { var newPlrFriend = new Friend(friendId, Player.Account.Id, friendState, playerState); if (!plr.FriendManager._friends.TryAdd(Player.Account.Id, newPlrFriend)) { throw new ArgumentException("Player is already added", nameof(newFriend.FriendId)); } } return(newFriend); } }
internal void Save(IDbConnection db) { if (!_deniesToRemove.IsEmpty) { var idsToRemove = new StringBuilder(); var firstRun = true; Deny denyToRemove; while (_deniesToRemove.TryPop(out denyToRemove)) { if (firstRun) { firstRun = false; } else { idsToRemove.Append(','); } idsToRemove.Append(denyToRemove.Id); } DbUtil.BulkDelete <PlayerDenyDto>(db, statement => statement .Where($"{nameof(PlayerDenyDto.Id):C} IN ({idsToRemove})")); } foreach (var deny in _denies.Values.Where(deny => !deny.ExistsInDatabase)) { DbUtil.Insert(db, new PlayerDenyDto { Id = deny.Id, PlayerId = (int)Player.Account.Id, DenyPlayerId = (int)deny.DenyId }); deny.ExistsInDatabase = true; } }
internal void Save(IDbConnection db) { if (!_charactersToDelete.IsEmpty) { var idsToRemove = new StringBuilder(); var firstRun = true; while (_charactersToDelete.TryPop(out var charToDelete)) { if (firstRun) { firstRun = false; } else { idsToRemove.Append(','); } idsToRemove.Append(charToDelete.Id); } DbUtil.BulkDelete <PlayerCharacterDto>(db, statement => statement .Where($"{nameof(PlayerCharacterDto.Id):C} IN ({idsToRemove})")); } foreach (var @char in _characters.Values) { if ([email protected]) { var charDto = new PlayerCharacterDto { Id = @char.Id, PlayerId = (int)Player.Account.Id, Slot = @char.Slot, Gender = (byte)@char.Gender, }; SetDtoItems(@char, charDto); DbUtil.Insert(db, charDto); @char.ExistsInDatabase = true; } else { if ([email protected]) { continue; } var charDto = new PlayerCharacterDto { Id = @char.Id, PlayerId = (int)Player.Account.Id, Slot = @char.Slot, Gender = (byte)@char.Gender, }; SetDtoItems(@char, charDto); DbUtil.Update(db, charDto); @char.NeedsToSave = false; } } }
internal void Save(IDbConnection db) { foreach (var pair in _settings) { var name = pair.Key; var setting = pair.Value; if (!setting.ExistsInDatabase) { DbUtil.Insert(db, new PlayerSettingDto { PlayerId = (int)Player.Account.Id, Setting = name, Value = GetString(name, setting.Data) }); setting.ExistsInDatabase = true; } else { if (!setting.NeedsToSave) { continue; } var dto = new PlayerSettingDto { PlayerId = (int)Player.Account.Id, Setting = name, Value = GetString(name, setting.Data) }; DbUtil.BulkUpdate(db, dto, statement => statement .Where( $"{nameof(PlayerSettingDto.PlayerId):C} = @PlayerId AND {nameof(PlayerSettingDto.Setting):C} = @Setting") .WithParameters(new { PlayerId = (int)Player.Account.Id, Setting = name })); setting.NeedsToSave = false; } } }
internal void Save(IDbConnection db) { if (Player.Room == null) { var ExpireItems = (from it in _items where it.Value.ExpireDate == 0 select it.Value).ToList(); foreach (var it in ExpireItems) { Remove(it); } } if (!_itemsToDelete.IsEmpty) { var idsToRemove = new StringBuilder(); var firstRun = true; PlayerItem itemToDelete; while (_itemsToDelete.TryPop(out itemToDelete)) { if (firstRun) { firstRun = false; } else { idsToRemove.Append(','); } idsToRemove.Append(itemToDelete.Id); } DbUtil.BulkDelete <PlayerItemDto>(db, statement => statement .Where($"{nameof(PlayerItemDto.Id):C} IN ({idsToRemove})")); } foreach (var item in _items.Values) { var rawEffects = item.Effects.ToList(); var dtoEffects = ""; try { dtoEffects = string.Join(",", rawEffects); } catch (Exception ex) { dtoEffects = "0"; } if (!item.ExistsInDatabase) { DbUtil.Insert(db, new PlayerItemDto { Id = (int)item.Id, PlayerId = (int)Player.Account.Id, ShopItemInfoId = item.GetShopItemInfo().Id, ShopPriceId = item.GetShopItemInfo().PriceGroup.GetPrice(item.PeriodType, item.Period).Id, DaysLeft = item.Period, Period = item.Period, Effects = dtoEffects, Color = item.Color, PurchaseDate = item.PurchaseDate.ToUnixTimeSeconds(), Durability = item.Durability, Count = (int)item.Count, EnchantMP = 0, EnchantLvl = 0 }); item.ExistsInDatabase = true; } else { if (!item.NeedsToSave) { continue; } DbUtil.Update(db, new PlayerItemDto { Id = (int)item.Id, PlayerId = (int)Player.Account.Id, ShopItemInfoId = item.GetShopItemInfo().Id, ShopPriceId = item.GetShopPrice().Id, Period = item.Period, DaysLeft = item.DaysLeft, Effects = dtoEffects, Color = item.Color, PurchaseDate = item.PurchaseDate.ToUnixTimeSeconds(), Durability = item.Durability, Count = (int)item.Count, EnchantLvl = item.EnchantLvl, EnchantMP = item.EnchantMP }); item.NeedsToSave = false; } } }
private static void FillShop() { if (!Config.Instance.NoobMode) { return; } using (var db = GameDatabase.Open()) { if (!DbUtil.Find <ShopVersionDto>(db).Any()) { var version = new ShopVersionDto { Version = DateTimeOffset.UtcNow.ToString("yyyyMMddHHmmss") }; DbUtil.Insert(db, version); } if (DbUtil.Find <ShopEffectGroupDto>(db).Any() || DbUtil.Find <ShopEffectDto>(db).Any() || DbUtil.Find <ShopPriceGroupDto>(db).Any() || DbUtil.Find <ShopPriceDto>(db).Any() || DbUtil.Find <ShopItemDto>(db).Any() || DbUtil.Find <ShopItemInfoDto>(db).Any()) { return; } Log.Information("NoobMode: Filling the shop with items"); using (var transaction = DbUtil.BeginTransaction(db)) { var effects = new Dictionary <string, Tuple <uint[], uint> > // effectids, groupid/effect { { "None", Tuple.Create(Array.Empty <uint>(), (uint)0) }, { "Shooting Weapon Defense (Head) +5%", Tuple.Create(new uint[] { 1100313003, 1100315003, 1100317003 }, (uint)1100315002) }, { "SP+6", Tuple.Create(new uint[] { 1101301006 }, (uint)1101301006) }, { "Attack+1%", Tuple.Create(new uint[] { 1299600001 }, (uint)1299600001) }, { "Attack+3%", Tuple.Create(new uint[] { 1299600002 }, (uint)1299600002) }, { "Attack+5%", Tuple.Create(new uint[] { 1299600003 }, (uint)1299600003) }, { "Attack+8%", Tuple.Create(new uint[] { 1299600005 }, (uint)1299600005) }, { "Attack+10%", Tuple.Create(new uint[] { 1299600006 }, (uint)1299600006) }, { "Defense+5%", Tuple.Create(new uint[] { 1103302004 }, (uint)1103302004) }, { "HP+4", Tuple.Create(new uint[] { 1105300004 }, (uint)1105300004) }, { "HP+30", Tuple.Create(new uint[] { 1999300011 }, (uint)1999300011) }, { "HP+15", Tuple.Create(new uint[] { 1999300009 }, (uint)1999300009) }, { "SP+40", Tuple.Create(new uint[] { 1300301012 }, (uint)1300301012) }, { "HP+20 & SP+20", Tuple.Create(new uint[] { 1999300010, 1999301011 }, (uint)30001) }, { "HP+25 & SP+25", Tuple.Create(new uint[] { 1999300012, 1999301013 }, (uint)30003) } }; #region Effects foreach (var pair in effects.ToArray()) { var effectGroup = new ShopEffectGroupDto { Name = pair.Key, Effect = pair.Value.Item2 }; DbUtil.Insert(db, effectGroup, statement => statement.AttachToTransaction(transaction)); effects[pair.Key] = Tuple.Create(pair.Value.Item1, (uint)effectGroup.Id); foreach (var effect in pair.Value.Item1) { DbUtil.Insert(db, new ShopEffectDto { EffectGroupId = effectGroup.Id, Effect = effect }, statement => statement.AttachToTransaction(transaction)); } } #endregion #region Price var priceGroup = new ShopPriceGroupDto { Name = "PEN", PriceType = (byte)ItemPriceType.PEN }; var priceGroup2 = new ShopPriceGroupDto { Name = "Item", PriceType = (byte)ItemPriceType.Premium }; var priceGroup3 = new ShopPriceGroupDto { Name = "AP", PriceType = (byte)ItemPriceType.AP }; DbUtil.Insert(db, priceGroup); DbUtil.Insert(db, priceGroup2); DbUtil.Insert(db, priceGroup3); var price_none_perm = new ShopPriceDto { PriceGroupId = priceGroup.Id, PeriodType = (byte)ItemPeriodType.None, IsRefundable = true, Durability = 2400, IsEnabled = true, Price = 1 }; var price_unit_one = new ShopPriceDto { PriceGroupId = priceGroup2.Id, Durability = 1, PeriodType = (byte)ItemPeriodType.Units, IsRefundable = true, Period = 1, IsEnabled = true, Price = 1000 }; var price_unit_two = new ShopPriceDto { PriceGroupId = priceGroup2.Id, Durability = 1, PeriodType = (byte)ItemPeriodType.Units, IsRefundable = true, Period = 2, IsEnabled = true, Price = 2000 }; var price_unit_five = new ShopPriceDto { PriceGroupId = priceGroup2.Id, Durability = 1, PeriodType = (byte)ItemPeriodType.Units, IsRefundable = true, Period = 5, IsEnabled = true, Price = 4500 }; var price_unit_ten = new ShopPriceDto { PriceGroupId = priceGroup2.Id, Durability = 1, PeriodType = (byte)ItemPeriodType.Units, IsRefundable = true, Period = 10, IsEnabled = true, Price = 8000 }; var price_ap_perm = new ShopPriceDto { PriceGroupId = priceGroup3.Id, PeriodType = (byte)ItemPeriodType.None, IsRefundable = true, Durability = 2400, IsEnabled = true, Price = 1 }; DbUtil.Insert(db, price_none_perm); DbUtil.Insert(db, price_unit_one); DbUtil.Insert(db, price_unit_two); DbUtil.Insert(db, price_unit_five); DbUtil.Insert(db, price_unit_ten); DbUtil.Insert(db, price_ap_perm); #endregion #region Items var items = GameServer.Instance.ResourceCache.GetItems().Values.ToArray(); for (var i = 0; i < items.Length; ++i) { var item = items[i]; var effectToUse = effects["None"]; byte mainTab = 0; byte subTab = 0; bool enabled = true; switch (item.ItemNumber.Category) { case ItemCategory.Card: case ItemCategory.Coupon: mainTab = 4; subTab = 6; break; case ItemCategory.EsperChip: mainTab = 4; subTab = 2; break; case ItemCategory.Boost: switch ((BoostCategory)item.ItemNumber.SubCategory) { case BoostCategory.Pen: mainTab = 4; subTab = 4; break; case BoostCategory.Exp: mainTab = 4; subTab = 5; break; case BoostCategory.Mp: mainTab = 4; subTab = 3; break; case BoostCategory.Unique: mainTab = 4; subTab = 5; break; } break; case ItemCategory.OneTimeUse: switch ((OneTimeUseCategory)item.ItemNumber.SubCategory) { case OneTimeUseCategory.Namechange: mainTab = 4; subTab = 0; break; case OneTimeUseCategory.Stat: mainTab = 4; subTab = 0; break; case OneTimeUseCategory.Capsule: // Clothes + weps mainTab = 1; subTab = 0; break; case OneTimeUseCategory.FumbiCapsule: mainTab = 1; subTab = 4; break; case OneTimeUseCategory.Event: mainTab = 4; subTab = 0; break; case OneTimeUseCategory.Set: mainTab = 1; subTab = 7; break; default: continue; } break; case ItemCategory.Weapon: effectToUse = effects["Attack+1%"]; mainTab = 2; switch ((WeaponCategory)item.ItemNumber.SubCategory) { case WeaponCategory.Melee: subTab = 1; break; case WeaponCategory.RifleGun: subTab = 2; break; case WeaponCategory.HeavyGun: subTab = 4; break; case WeaponCategory.Sniper: subTab = 5; break; case WeaponCategory.Sentry: subTab = 6; break; case WeaponCategory.Bomb: subTab = 7; break; case WeaponCategory.Mind: subTab = 6; break; } break; case ItemCategory.Skill: mainTab = 2; subTab = 8; if (item.ItemNumber.SubCategory == 0 && item.ItemNumber.Number == 0) // half hp mastery { effectToUse = effects["HP+15"]; } if (item.ItemNumber.SubCategory == 0 && item.ItemNumber.Number == 1) // hp mastery { effectToUse = effects["HP+30"]; } if (item.ItemNumber.SubCategory == 0 && item.ItemNumber.Number == 2) // sp mastery { effectToUse = effects["SP+40"]; } if (item.ItemNumber.SubCategory == 0 && item.ItemNumber.Number == 3) // dual mastery { effectToUse = effects["HP+20 & SP+20"]; } if (item.ItemNumber.SubCategory == 0 && item.ItemNumber.Number == 5 ) // dual mastery - returner { effectToUse = effects["HP+20 & SP+20"]; } if (item.ItemNumber.SubCategory == 0 && item.ItemNumber.Number == 7 ) // unique dual mastery { effectToUse = effects["HP+25 & SP+25"]; } break; case ItemCategory.Costume: mainTab = 3; subTab = (byte)(item.ItemNumber.SubCategory + 2); switch ((CostumeSlot)item.ItemNumber.SubCategory) { case CostumeSlot.Hair: effectToUse = effects["Shooting Weapon Defense (Head) +5%"]; break; case CostumeSlot.Face: effectToUse = effects["SP+6"]; break; case CostumeSlot.Shirt: effectToUse = effects["Attack+5%"]; break; case CostumeSlot.Pants: effectToUse = effects["Defense+5%"]; break; case CostumeSlot.Gloves: effectToUse = effects["HP+4"]; break; case CostumeSlot.Shoes: effectToUse = effects["HP+4"]; break; case CostumeSlot.Accessory: effectToUse = effects["SP+6"]; break; case CostumeSlot.Pet: effectToUse = effects["SP+6"]; break; } break; default: effectToUse = effects["None"]; mainTab = 4; subTab = 6; break; } var shopItem = new ShopItemDto { Id = item.ItemNumber, RequiredGender = (byte)item.Gender, RequiredLicense = (byte)item.License, IsDestroyable = true, MainTab = mainTab, SubTab = subTab, Colors = (byte)item.Colors }; DbUtil.Insert(db, shopItem, statement => statement.AttachToTransaction(transaction)); var shopItemInfo = new ShopItemInfoDto { ShopItemId = shopItem.Id, PriceGroupId = priceGroup.Id, EffectGroupId = (int)effectToUse.Item2, ShopInfoType = enabled ? (byte)1 : (byte)0, }; var shopItemInfo_onetimeuse = new ShopItemInfoDto { ShopItemId = shopItem.Id, PriceGroupId = priceGroup2.Id, EffectGroupId = (int)effectToUse.Item2, ShopInfoType = enabled ? (byte)1 : (byte)0, }; if (item.ItemNumber.Category == ItemCategory.Costume || item.ItemNumber.Category == ItemCategory.Weapon || item.ItemNumber.Category == ItemCategory.Skill || item.ItemNumber.Category == ItemCategory.EsperChip) { DbUtil.Insert(db, shopItemInfo, statement => statement.AttachToTransaction(transaction)); } else { DbUtil.Insert(db, shopItemInfo_onetimeuse, statement => statement.AttachToTransaction(transaction)); } Log.Information($"[{i}/{items.Length}] {item.ItemNumber}: {item.Name} | Colors: {item.Colors}"); } #endregion var itemInfos = DbUtil.Find <ShopItemInfoDto>(db, statement => statement .Include <ShopItemDto>(join => join.LeftOuterJoin()) .AttachToTransaction(transaction)); var caps = GameServer.Instance.ResourceCache._loader.GetWorkingCapsules(); foreach (var item in items.Where(x => x.ItemNumber.Category == ItemCategory.OneTimeUse)) { var itemInfo = itemInfos.FirstOrDefault(x => x.ShopItemId == item.ItemNumber); if (itemInfo != null) { if (!caps.Contains(item.ItemNumber)) { itemInfo.ShopInfoType = 0; DbUtil.Update(db, itemInfo, statement => statement.AttachToTransaction(transaction)); } } } try { transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }