From 53e81e6fdd788bb23519d9e638a490e162ed19c0 Mon Sep 17 00:00:00 2001 From: hydrant Date: Sat, 8 May 2021 00:26:43 +0200 Subject: [PATCH 1/4] Add log project to server --- ReallifeGamemode.Server/ReallifeGamemode.Server.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/ReallifeGamemode.Server/ReallifeGamemode.Server.csproj b/ReallifeGamemode.Server/ReallifeGamemode.Server.csproj index 80049bd1..0bd45f54 100644 --- a/ReallifeGamemode.Server/ReallifeGamemode.Server.csproj +++ b/ReallifeGamemode.Server/ReallifeGamemode.Server.csproj @@ -42,6 +42,7 @@ + From 459ce10a6446298200b67bd8f12ffedcd30153ca Mon Sep 17 00:00:00 2001 From: hydrant Date: Sat, 8 May 2021 00:27:20 +0200 Subject: [PATCH 2/4] [BUG-58] Fix waffenschrank --- ReallifeGamemode.Client/Tuning/main.ts | 19 ++- .../Events/UpdateCharacterWeapon.cs | 161 +++++++++--------- 2 files changed, 95 insertions(+), 85 deletions(-) diff --git a/ReallifeGamemode.Client/Tuning/main.ts b/ReallifeGamemode.Client/Tuning/main.ts index c825708f..66414bb5 100644 --- a/ReallifeGamemode.Client/Tuning/main.ts +++ b/ReallifeGamemode.Client/Tuning/main.ts @@ -22,7 +22,7 @@ export default function tuning(globalData: IGlobalData) { const disableInput = [75, 278, 279, 280, 281, 23, 59, 60, 71, 72, 74]; - const carModTypes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 18, 22, -1, 25, 27, 28, 33, 34, 35, 38, 39, 40, 41, 42, 43, 44, 46, 48, 69]; + const carModTypes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 18, 22, -2, -1, 25, 27, 28, 33, 34, 35, 38, 39, 40, 41, 42, 43, 44, 46, 48, 69]; const carModSlotName = [ { Slot: 0, Name: "Spoiler", Price: 1000 }, @@ -43,7 +43,7 @@ export default function tuning(globalData: IGlobalData) { { Slot: 15, Name: "Federung", Price: 2000 }, { Slot: 18, Name: "Turbo", BasePercentage: 45, PriceIncreasePerLevel: 0 }, { Slot: 22, Name: "Licht", Price: 500 }, - { Slot: 23, Name: "Reifen", Price: 1000 }, + { Slot: -2, Name: "Reifen", Price: 1000 }, { Slot: -1, Name: "Lackierung", Price: 1000 }, { Slot: 25, Name: "Nummernschildhalter", Price: 500 }, { Slot: 27, Name: "Innenausstatung", Price: 500 }, @@ -207,6 +207,12 @@ export default function tuning(globalData: IGlobalData) { mainMenu.BindMenuToItem(colorMenu, colorItem); return; + } else if (modType == -2) { + var wheelsItem = new UIMenuItem("Reifen"); + var wheelsMenu = getWheelsMenu(); + + mainMenu.AddItem(wheelsItem); + mainMenu.BindMenuToItem(wheelsMenu, wheelsItem); } var num = localVehicle.getNumMods(modType); @@ -302,8 +308,6 @@ export default function tuning(globalData: IGlobalData) { }); }); - - mainMenu.ItemSelect.on((item: NativeUI.UIMenuItem, index: number) => { if (item === repairItem) { mp.events.callRemote("repairVehicle"); @@ -493,6 +497,11 @@ export default function tuning(globalData: IGlobalData) { } } + function getWheelsMenu(): NativeUI.Menu { + var wheelsMenu = new NativeUI.Menu("Reifen", "rollin on dem rims", new NativeUI.Point(0, screenRes.y / 3)); + return wheelsMenu; + } + function getColorMenu(): NativeUI.Menu { var currentColors = mp.players.local.vehicle.getColours(0, 0); @@ -591,6 +600,8 @@ export default function tuning(globalData: IGlobalData) { mp.events.callRemote("CLIENT:TuningManager_SetVehicleColor", false, color); } + + class VehicleModMenuItem extends UIMenuItem { public price: number; } diff --git a/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs b/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs index ea213d7b..741f6ae7 100644 --- a/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs +++ b/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs @@ -1,62 +1,32 @@ using System; using System.Linq; using GTANetworkAPI; +using Microsoft.Extensions.Logging; using ReallifeGamemode.Database.Entities; using ReallifeGamemode.Database.Models; using ReallifeGamemode.Server.Extensions; +using ReallifeGamemode.Server.Log; using ReallifeGamemode.Server.Managers; namespace ReallifeGamemode.Server.Events { public class UpdateCharacterWeapon : Script { - [RemoteEvent("updateWeaponSelection")] - public void UpdateWeaponSelection(Player client, string weaponModel, int slot) - { - if (weaponModel == "Keine") - { - client.RemoveAllWeapons(); - return; - } + private ILogger logger = LogManager.GetLogger(); - WeaponHash weaponHash = NAPI.Util.WeaponNameToModel(weaponModel); + private const int SLOT_PRIMARY = 2; + private const int SLOT_SECONDARY = 1; + private const int SLOT_MEELE = 3; + private const int SLOT_SPECIAL = 4; - if (slot == 1) - { - client.RemoveAllWeapons(); - client.GiveWeapon(weaponHash, 0); - } - if (slot == 2) - { - client.RemoveAllWeapons(); - client.GiveWeapon(weaponHash, 0); - } - if (slot == 3) - { - client.RemoveAllWeapons(); - client.GiveWeapon(weaponHash, 0); - } - if (slot == 4) - { - client.RemoveAllWeapons(); - client.GiveWeapon(weaponHash, 0); - } - if (slot == 5) - { - client.SafeSetArmor(100); - } - } - - [RemoteEvent("cancelWeaponSelection")] - public void CancelWeaponSelection(Player client) - { - client.RemoveAllWeapons(); - } [RemoteEvent("saveWeaponSelection")] public void SaveWeaponSelection(Player client, string primaryModel, string secondaryModel, string meleeModel, string specialModel, string armor) { - WeaponPoint nearestWeapon = PositionManager.WeaponPoints.Find(w => w.Position.DistanceTo(client.Position) <= 1.5 && w.FactionId == client.GetUser().FactionId); + using var dbContext = new DatabaseContext(); + User user = client.GetUser(dbContext); + + WeaponPoint nearestWeapon = PositionManager.WeaponPoints.Find(w => w.Position.DistanceTo(client.Position) <= 1.5 && w.FactionId == user.FactionId); if (nearestWeapon == null) { @@ -64,59 +34,88 @@ namespace ReallifeGamemode.Server.Events return; } - client.RemoveAllWeapons(); - if (!uint.TryParse(primaryModel, out uint primary)) + if (!string.IsNullOrEmpty(primaryModel)) { - if (primaryModel.Contains("mk2") && !primaryModel.Contains("_mk2")) primaryModel = primaryModel.Replace("mk2", "_mk2"); - primary = NAPI.Util.GetHashKey($"weapon_{primaryModel}"); - } - if (!uint.TryParse(secondaryModel, out uint secondary)) - { - if (secondaryModel.Contains("mk2") && !secondaryModel.Contains("_mk2")) secondaryModel = secondaryModel.Replace("mk2", "_mk2"); - secondary = NAPI.Util.GetHashKey($"weapon_{secondaryModel}"); - } - if (!uint.TryParse(meleeModel, out uint melee)) - { - if (meleeModel.Contains("mk2") && !meleeModel.Contains("_mk2")) meleeModel = meleeModel.Replace("mk2", "_mk2"); - melee = NAPI.Util.GetHashKey($"weapon_{meleeModel}"); + HandleSlot(user, client, 2, primaryModel, dbContext); } - client.GiveWeapon((WeaponHash)primary, 500); - client.GiveWeapon((WeaponHash)secondary, 250); - client.GiveWeapon((WeaponHash)melee, 1); - - if (!uint.TryParse(specialModel, out uint special)) + if (!string.IsNullOrEmpty(secondaryModel)) { - if (specialModel.Contains("mk2") && !specialModel.Contains("_mk2")) specialModel = specialModel.Replace("mk2", "_mk2"); - special = NAPI.Util.GetHashKey($"weapon_{specialModel}"); + HandleSlot(user, client, 1, secondaryModel, dbContext); } - client.GiveWeapon((WeaponHash)special, 30); + + if (!string.IsNullOrEmpty(meleeModel)) + { + HandleSlot(user, client, 3, meleeModel, dbContext); + } + + if (!string.IsNullOrEmpty(specialModel)) + { + HandleSlot(user, client, 4, specialModel, dbContext); + } + + dbContext.SaveChanges(); if (armor == "Schutzweste") { + logger.LogInformation("Player {0} took armor from weaponrack of faction {1}", user.Name, user.FactionId); client.SafeSetArmor(100); } + } - using (var context = new DatabaseContext()) + public void HandleSlot(User user, Player player, int slot, string selection, DatabaseContext context) + { + string weapon = GetRealWeaponModelName(selection); + + uint hashKey = NAPI.Util.GetHashKey(weapon); + + if (!Enum.GetValues(typeof(WeaponHash)).Cast().Contains((WeaponHash)hashKey)) { - User user = client.GetUser(); - FactionWeapon slot1 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == primaryModel).FirstOrDefault(); - if (slot1 != null) - slot1.Ammount -= 1; - FactionWeapon slot2 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == secondaryModel).FirstOrDefault(); - if (slot2 != null) - slot2.Ammount -= 1; - FactionWeapon slot3 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == meleeModel).FirstOrDefault(); - if (slot3 != null) - slot3.Ammount -= 1; - FactionWeapon slot4 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == specialModel).FirstOrDefault(); - if (slot4 != null) - slot4.Ammount -= 1; - FactionWeapon slot5 = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.WeaponModel == armor).FirstOrDefault(); - if (slot5 != null) - slot5.Ammount -= 1; - context.SaveChanges(); + logger.LogWarning("Player {0} tried to take invalid weapon {1} from weaponrack of faction {2}", user.Name, selection, user.FactionId); + return; } + + var slotWeapons = context.FactionWeapons.Where(w => w.FactionId == user.FactionId && w.SlotID == slot).ToList(); + + foreach (var slotWeaponHash in slotWeapons.Select(w => NAPI.Util.GetHashKey(GetRealWeaponModelName(w.WeaponModel)))) + { + player.RemoveWeapon((WeaponHash)slotWeaponHash); + } + + var chosenWeapon = slotWeapons.Where(w => w.WeaponModel == selection).FirstOrDefault(); + if (chosenWeapon == null) + { + logger.LogWarning("Player {0} tried to take not-existing weapon {1} from weaponrack of faction {2}", user.Name, selection, user.FactionId); + return; + } + + int ammo = GetSlotAmmoAmount(slot); + player.GiveWeapon((WeaponHash)hashKey, ammo); + player.SetWeaponAmmo((WeaponHash)hashKey, ammo); + + chosenWeapon.Ammount -= 1; + logger.LogInformation("Player {0} took weapon {1} from weaponrack of faction {2}", user.Name, selection, user.FactionId); + } + + private int GetSlotAmmoAmount(int slot) => slot switch + { + SLOT_PRIMARY => 500, + SLOT_SECONDARY => 250, + SLOT_MEELE => 1, + SLOT_SPECIAL => 20, + _ => 1 + }; + + private string GetRealWeaponModelName(string weapon) + { + string ret = "weapon_" + weapon.ToLower(); + + if (!ret.Contains("_mk2") && ret.Contains("mk2")) + { + ret = ret.Replace("mk2", "_mk2"); + } + + return ret; } [RemoteEvent("CLIENT:Ammunation_BuyWeapon")] From 5414a8513590878dab9c004d8dbdeda5f6e3f5b4 Mon Sep 17 00:00:00 2001 From: hydrant Date: Sat, 8 May 2021 00:31:19 +0200 Subject: [PATCH 3/4] ammunation logs --- ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs b/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs index 741f6ae7..86db7681 100644 --- a/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs +++ b/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs @@ -130,6 +130,7 @@ namespace ReallifeGamemode.Server.Events player.SendNotification("Du hast nicht genügend Geld bei dir"); return; } + logger.LogInformation("Player {0} bought a {1} in ammunation", user.Name, weaponmodel); user.Handmoney -= price; dbContext.SaveChanges(); player.GiveWeapon(NAPI.Util.WeaponNameToModel(weaponmodel), ammo); From 0d3cdee58c04cd695deec63c1c79abf3a083dae7 Mon Sep 17 00:00:00 2001 From: hydrant Date: Sat, 8 May 2021 00:32:06 +0200 Subject: [PATCH 4/4] Rename UpdateCharacterWeapon class --- .../Events/{UpdateCharacterWeapon.cs => Weapon.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename ReallifeGamemode.Server/Events/{UpdateCharacterWeapon.cs => Weapon.cs} (97%) diff --git a/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs b/ReallifeGamemode.Server/Events/Weapon.cs similarity index 97% rename from ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs rename to ReallifeGamemode.Server/Events/Weapon.cs index 86db7681..eb73287e 100644 --- a/ReallifeGamemode.Server/Events/UpdateCharacterWeapon.cs +++ b/ReallifeGamemode.Server/Events/Weapon.cs @@ -10,9 +10,9 @@ using ReallifeGamemode.Server.Managers; namespace ReallifeGamemode.Server.Events { - public class UpdateCharacterWeapon : Script + public class Weapon : Script { - private ILogger logger = LogManager.GetLogger(); + private ILogger logger = LogManager.GetLogger(); private const int SLOT_PRIMARY = 2; private const int SLOT_SECONDARY = 1;