[BUG-58] Fix waffenschrank

This commit is contained in:
hydrant
2021-05-08 00:27:20 +02:00
parent 53e81e6fdd
commit 459ce10a64
2 changed files with 95 additions and 85 deletions

View File

@@ -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<UpdateCharacterWeapon>();
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<WeaponHash>().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")]