hanf vielleicht fertig

This commit is contained in:
hydrant
2021-05-27 21:06:20 +02:00
parent 211269d03a
commit 60f55e0eeb
20 changed files with 2996 additions and 56 deletions

View File

@@ -9,7 +9,7 @@ namespace ReallifeGamemode.Server
{
public abstract class BaseScript : Script
{
protected readonly ILogger logger;
protected static ILogger logger { get; private set; }
public BaseScript()
{

View File

@@ -47,6 +47,14 @@ namespace ReallifeGamemode.Server.Events
return;
}
if(player.HasData("IsCarryingPlant") || player.GetData<bool>("IsCarryingPlant"))
{
var currentModel = player.GetData<int>("HoldingCannabisPlant");
player.AddAttachment("CannabisPlantInHand" + currentModel, true);
player.ResetData("IsCarryingPlant");
}
using var dbContext = new DatabaseContext();
User user = player.GetUser(dbContext);

View File

@@ -41,6 +41,14 @@ namespace ReallifeGamemode.Server.Events
return;
}
if (player.HasData("IsCarryingPlant") || player.GetData<bool>("IsCarryingPlant"))
{
var currentModel = player.GetData<int>("HoldingCannabisPlant");
player.AddAttachment("CannabisPlantInHand" + currentModel, true);
player.ResetData("IsCarryingPlant");
}
var logEntry = new LoginLogoutLogEntry()
{
LoginLogout = false,

View File

@@ -21,6 +21,8 @@ using ReallifeGamemode.Server.Types;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Server.WeaponDeal;
using ReallifeGamemode.Server.Log;
using ReallifeGamemode.Server.Inventory.Items;
using Microsoft.EntityFrameworkCore;
using ReallifeGamemode.Server.Wanted;
/**
@@ -217,9 +219,61 @@ namespace ReallifeGamemode.Server.Events
[RemoteEvent("keyPress:E")]
public void KeyPressE(Player player)
{
if (!player.IsLoggedIn() || player.GetData<bool>("isDead") || player.IsInVehicle) return;
if (!player.IsLoggedIn() || player.GetData<bool>("isDead")) return;
var user = player.GetUser();
if (player.IsInVehicle
&& player.VehicleSeat == 0
&& player.Vehicle.Model == (uint)VehicleHash.Riot
&& (user.FactionId == 1 || user.FactionId == 3)
&& player.Position.DistanceTo(HanfManager.ASSERVATENKAMMER_POSITION) <= 3
/*&& player.IsDuty()*/)
{
using var dbContext = new DatabaseContext();
GTANetworkAPI.Vehicle veh = player.Vehicle;
ServerVehicle serverVehicle = veh.GetServerVehicle(dbContext);
List<VehicleItem> items = InventoryManager.GetVehicleItems(veh);
if (items.Count() == 0)
{
player.SendNotification("Der Riot ist leer");
return;
}
IIllegalItem cannabisItem = InventoryManager.GetItem<Cannabis>();
IIllegalItem cannabisSeedItem = InventoryManager.GetItem<CannabisSeeds>();
VehicleItem vehicleCannabisItem = items.Where(i => i.ItemId == cannabisItem.Id).FirstOrDefault();
VehicleItem vehicleCannabisSeedItem = items.Where(i => i.ItemId == cannabisSeedItem.Id).FirstOrDefault();
int cannabisAmount = vehicleCannabisItem?.Amount ?? 0;
int cannabisSeedAmount = vehicleCannabisSeedItem?.Amount ?? 0;
InventoryManager.RemoveVehicleItem(serverVehicle, vehicleCannabisItem, cannabisAmount, player);
InventoryManager.RemoveVehicleItem(serverVehicle, vehicleCannabisSeedItem, cannabisSeedAmount, player);
var price = cannabisSeedAmount * cannabisSeedItem.PriceForConfiscation + cannabisAmount * cannabisItem.PriceForConfiscation;
logger.LogInformation("Player {0} unloaded the riot truck with {1} cannabis and {2} cannabis seed and the faction got {3}", player.Name, cannabisAmount, cannabisSeedAmount, price);
var factions = dbContext.Factions.Include(f => f.BankAccount).Where(f => f.Id == 1 || f.Id == 3);
foreach (var faction in factions)
{
faction.BankAccount.Balance += price;
}
player.SendNotification("~g~Der Riot wurde erfolgreich entladen");
dbContext.SaveChanges();
return;
}
if (player.IsInVehicle)
{
return;
}
if (player.HasData("nearATM"))
{
ATMManager.ShowAtmUi(player, player.GetData<int>("nearATM"));
@@ -245,6 +299,7 @@ namespace ReallifeGamemode.Server.Events
JailPoint nearestjailPoint = PositionManager.jailPoints.Find(s => s.Position.DistanceTo(player.Position) <= 1.5);
bool isNearCannabisSeedBuyPoint = HanfManager.IsPlayerNearSeedBuyPoint(player);
bool isNearJointManufacturerPoint = HanfManager.IsPlayerNearJointManufacturer(player);
bool isNearCannabisSellPoint = HanfManager.IsPlayerNearCannabisSellPoint(player);
if (user?.FactionId != null)
{
@@ -766,7 +821,7 @@ namespace ReallifeGamemode.Server.Events
if (isNearCannabisSeedBuyPoint)
{
player.TriggerEvent("SERVER:Hanf_BuySeed", HanfManager.SEED_PRICE);
HanfManager.ShowPlayerBuySeedMenu(player);
return;
}
@@ -775,6 +830,39 @@ namespace ReallifeGamemode.Server.Events
HanfManager.BuildJointsFromCannabis(player);
return;
}
if (isNearCannabisSellPoint)
{
HanfManager.PlayerSellCannabis(player);
return;
}
if (player.HasData("IsCarryingPlant") || player.GetData<bool>("IsCarryingPlant") && (user.FactionId == 3 || user.FactionId == 1) && player.IsDuty())
{
using var dbContext = new DatabaseContext();
FactionVehicle riot = dbContext.FactionVehicles.Where(f => f.Model == VehicleHash.Riot).ToList().Where(f => f.GetOwners().Contains(3)).FirstOrDefault();
if (riot == null)
{
return;
}
GTANetworkAPI.Vehicle riotVehicle = VehicleManager.GetVehicleFromServerVehicle(riot);
if (riotVehicle.Position.DistanceTo(player.Position) > 3)
{
return;
}
var currentModel = player.GetData<int>("HoldingCannabisPlant");
player.AddAttachment("CannabisPlantInHand" + currentModel, true);
player.ResetData("IsCarryingPlant");
var cannabisItem = InventoryManager.GetItem<Cannabis>();
var cannabisSeedsItem = InventoryManager.GetItem<CannabisSeeds>();
int itemIdToGive = currentModel == 3 ? cannabisItem.Id : cannabisSeedsItem.Id;
int amountToGive = currentModel == 3 ? new Random().Next(4, 10) + 1 : 1;
InventoryManager.AddItemToVehicleInventory(riotVehicle, itemIdToGive, amountToGive);
}
}
[RemoteEvent("keyPress:I")]

View File

@@ -1,17 +1,21 @@
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.Types;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Server.WeaponDeal;
using ReallifeGamemode.Server.Log;
namespace ReallifeGamemode.Server.Events
{
public class Vehicle : Script
{
private static readonly ILogger logger = LogManager.GetLogger<Vehicle>();
[RemoteEvent("VehicleMenu_ToggleEngine")]
public void VehicleMenuToggleEngineEvent(Player player)
{
@@ -271,6 +275,8 @@ namespace ReallifeGamemode.Server.Events
sV.PositionZ = pos.Z;
sV.Heading = v.Heading;
logger.LogInformation("Player {0} parked the car {1} at x:{2}, y:{3}, z:{4}", player.Name, sV.Id, pos.X, pos.Y, pos.Z);
player.SendNotification("~g~Das Fahrzeug wurde geparkt.");

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace ReallifeGamemode.Server.Inventory.Interfaces
{
interface IIllegalItem : IItem
{
int PriceForConfiscation { get; }
}
}

View File

@@ -9,7 +9,7 @@ using ReallifeGamemode.Server.Inventory.Interfaces;
namespace ReallifeGamemode.Server.Inventory.Items
{
public class Cannabis : DropItem
public class Cannabis : DropItem, IIllegalItem
{
public override int Id => 108;
@@ -24,5 +24,9 @@ namespace ReallifeGamemode.Server.Inventory.Items
public override uint Object => 3076948544;
public override int Price => 0;
public override bool Legal => false;
public int PriceForConfiscation { get; } = 5;
}
}

View File

@@ -5,11 +5,12 @@ using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Inventory.Interfaces;
using ReallifeGamemode.Server.Managers;
namespace ReallifeGamemode.Server.Inventory.Items
{
public class CannabisSeeds : UseItem
public class CannabisSeeds : UseItem, IIllegalItem
{
public override int Id { get; } = 109;
public override string Name { get; } = "Cannabis Samen";
@@ -21,14 +22,15 @@ namespace ReallifeGamemode.Server.Inventory.Items
public override uint Object { get; }
public override bool RemoveWhenUsed { get; } = false;
public int PriceForConfiscation { get; } = 10;
public override bool Use(Player player, User user, DatabaseContext databaseContext)
{
if(!player.IsInVehicle)
if (!player.IsInVehicle)
{
HanfManager.StartCannabisPlanting(player);
}
return true;
}
}

View File

@@ -5,12 +5,16 @@ using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Inventory.Interfaces;
using ReallifeGamemode.Server.Util;
namespace ReallifeGamemode.Server.Inventory.Items
{
public class Joint : UseItem
public class Joint : UseItem, IIllegalItem
{
private static Dictionary<int, DateTime> lastJointUse = new Dictionary<int, DateTime>();
private static readonly TimeSpan _jointCooldown = TimeSpan.FromMinutes(10);
public override int Id { get; } = 110;
public override string Name { get; } = "Joint";
public override string Description { get; } = "stay high bis zum tod";
@@ -21,10 +25,33 @@ namespace ReallifeGamemode.Server.Inventory.Items
public override bool Legal => false;
public override bool RemoveWhenUsed => true;
public int PriceForConfiscation { get; } = 25;
public override bool Use(Player player, User user, DatabaseContext databaseContext)
{
if (!CanUserUseJoint(user))
{
player.TriggerEvent("Error", $"Versuche es nach {-1 * (int)((DateTime.Now - lastJointUse[user.Id]) - _jointCooldown).TotalSeconds} Sekunden erneut.");
return false;
}
player.SyncAnimation("jointUse");
player.ToggleInventory(InventoryToggleOption.HIDE);
int armorToSet = Math.Min(player.Armor + 25, 100);
player.SafeSetArmor(armorToSet);
lastJointUse[user.Id] = DateTime.Now;
return true;
}
private bool CanUserUseJoint(User user)
{
if(!lastJointUse.ContainsKey(user.Id))
{
return true;
}
return DateTime.Now - lastJointUse[user.Id] > _jointCooldown;
}
}
}

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Timers;
using GTANetworkAPI;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ReallifeGamemode.Database.Entities;
@@ -12,6 +13,7 @@ using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Inventory.Interfaces;
using ReallifeGamemode.Server.Inventory.Items;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Types;
using ReallifeGamemode.Server.Util;
namespace ReallifeGamemode.Server.Managers
@@ -23,10 +25,28 @@ namespace ReallifeGamemode.Server.Managers
/// </summary>
private readonly static List<Vector3> _seedsBuyPoints = new List<Vector3>();
private readonly static List<Vector3> _cannabisSellPoints = new List<Vector3>();
/// <summary>
/// Aktueller Samen-Preis
/// </summary>
public static int SEED_PRICE = 50;
public static int SEED_PRICE = 0;
private const int SEED_PRICE_MIN = 40;
private const int SEED_PRICE_MAX = 75;
/// <summary>
/// Aktueller Hanf-Verkaufspreis
/// </summary>
public static int CANNABIS_PRICE = 0;
private const int CANNABIS_PRICE_MIN = 50;
private const int CANNABIS_PRICE_MAX = 125;
/// <summary>
/// Wie viele Samen ein Spieler pro Tag maximal kaufen darf
/// </summary>
private const int MAX_SEEDS_PER_DAY = 50;
/// <summary>
/// Wie viele Joints man aus einem Cannabis bekommt
@@ -72,6 +92,8 @@ namespace ReallifeGamemode.Server.Managers
private static List<CannabisData> _currentCannabisData = new List<CannabisData>();
public static readonly Vector3 ASSERVATENKAMMER_POSITION = new Vector3(-5.45, -670.03, 32.33);
static HanfManager()
{
_manufacturerDoneTimer.Elapsed += ManufacturerDoneTimerCallback;
@@ -82,6 +104,12 @@ namespace ReallifeGamemode.Server.Managers
/// </summary>
public static void Load()
{
var priceRandom = new Random();
SEED_PRICE = priceRandom.Next(SEED_PRICE_MIN, SEED_PRICE_MAX + 1);
CANNABIS_PRICE = priceRandom.Next(CANNABIS_PRICE_MIN, CANNABIS_PRICE_MAX + 1);
logger.LogInformation("Generated hanf prices: seed = {0}, cannabis = {1}", SEED_PRICE, CANNABIS_PRICE);
_seedsBuyPoints.Add(new Vector3(-30.21876, -585.3222, 17.917326));
_seedsBuyPoints.Add(new Vector3(-680.89386, -634.6783, 25.29923));
_seedsBuyPoints.Add(new Vector3(-1310.743, -608.9064, 29.382874));
@@ -92,17 +120,58 @@ namespace ReallifeGamemode.Server.Managers
foreach (Vector3 buyPoint in _seedsBuyPoints)
{
colShape = NAPI.ColShape.CreateSphereColShape(buyPoint, 20.0f);
colShape = NAPI.ColShape.CreateSphereColShape(buyPoint, 10.0f);
colShape.OnEntityEnterColShape += OnSeedBuyRangeColShapeEnter;
}
UpdateHanfWorldData(new DatabaseContext());
_cannabisSellPoints.Add(new Vector3(2220.04, 5614.24, 54.72));
_cannabisSellPoints.Add(new Vector3(201.77, 2442.06, 60.45));
_cannabisSellPoints.Add(new Vector3(155.87, -3103.26, 7.03));
foreach (Vector3 sellPoint in _cannabisSellPoints)
{
colShape = NAPI.ColShape.CreateSphereColShape(sellPoint, 10.0f);
colShape.OnEntityEnterColShape += OnCannabisSellRangeColShapeEnter;
}
NAPI.Marker.CreateMarker(GTANetworkAPI.MarkerType.VerticalCylinder, ASSERVATENKAMMER_POSITION.Subtract(new Vector3(0, 0, 3.0)), new Vector3(), new Vector3(), 3.0f, Colors.White);
NAPI.TextLabel.CreateTextLabel("Asservatenkammer~n~Drücke ~y~E, um den ~y~Riot~s~ zu entladen", ASSERVATENKAMMER_POSITION, 10.0f, 10.0f, 0, Colors.White);
using var dbContext = new DatabaseContext();
HarvestRottenPlants(dbContext);
UpdateHanfWorldData(dbContext);
Timer updateHanfDataTimer = new Timer(TimeSpan.FromMinutes(1).TotalMilliseconds);
updateHanfDataTimer.Elapsed += UpdateHanfDataTimer_Elapsed;
updateHanfDataTimer.Start();
}
private static void OnCannabisSellRangeColShapeEnter(ColShape colShape, Player player)
{
if (!player.IsLoggedIn())
{
return;
}
var user = player.GetUser();
if (user?.Faction?.StateOwned == true)
{
return;
}
ChatService.SendMessage(player, $"Fremder sagt: Pssst.. Hier kannst du dein Brokkoli loswerden.");
}
private static void HarvestRottenPlants(DatabaseContext dbContext)
{
var rottenPlants = dbContext.CannabisPlants.Where(p => EF.Functions.DateDiffHour(p.PlantDate, DateTime.Now) > MAX_PLANT_TIME.TotalHours);
foreach (var plant in rottenPlants)
{
plant.Harvested = true;
}
dbContext.SaveChanges();
}
private static void UpdateHanfDataTimer_Elapsed(object sender, ElapsedEventArgs e)
{
using var dbContext = new DatabaseContext();
@@ -148,7 +217,7 @@ namespace ReallifeGamemode.Server.Managers
var user = player.GetUser(dbContext);
if (user.Faction?.StateOwned ?? false)
{
player.SendNotification("~r~Du darfst keine Hanfsamen einfplanzen");
player.SendNotification("~r~Du darfst keine Hanfsamen einpflanzen");
return;
}
@@ -220,7 +289,7 @@ namespace ReallifeGamemode.Server.Managers
public async static void UpdateHanfForPlayer(Player player, List<CannabisData> cannabisData = null)
{
cannabisData ??= _currentCannabisData;
var x = await NAPI.Task.WaitForMainThread();
await NAPI.Task.WaitForMainThread();
player.TriggerEvent("SERVER:Hanf_UpdateHanfData", JsonConvert.SerializeObject(cannabisData));
}
@@ -235,6 +304,12 @@ namespace ReallifeGamemode.Server.Managers
using var dbContext = new DatabaseContext();
var user = player.GetUser(dbContext);
if (amount > GetAmountOfCannabisSeedsPlayerCanBuyToday(user))
{
player.SendNotification("~r~Du kannst heute nicht mehr so viele Samen kaufen");
return;
}
IItem seedItem = InventoryManager.GetItem<CannabisSeeds>();
var newAmount = seedItem.Gewicht * amount;
@@ -254,6 +329,16 @@ namespace ReallifeGamemode.Server.Managers
logger.LogInformation("Player {0} bought {1} cannabis seeds for {2} dollars (price per seed: {3})", player.Name, amount, price, SEED_PRICE); // <-- WICHTIG LOGS
if (user.LastTimeBoughtCannabisSeeds == null || user.LastTimeBoughtCannabisSeeds.Value.Date != DateTime.Now.Date)
{
user.CannabisSeedsBoughtToday = amount;
}
else
{
user.CannabisSeedsBoughtToday += amount;
}
user.LastTimeBoughtCannabisSeeds = DateTime.Now;
user.Handmoney -= price;
dbContext.SaveChanges();
@@ -263,7 +348,7 @@ namespace ReallifeGamemode.Server.Managers
}
[RemoteEvent("CLIENT:Hanf_HarvestHanf")]
public void HanfManagerHarvestHanf(Player player, long hanfId)
public async void HanfManagerHarvestHanf(Player player, long hanfId)
{
if (!player.IsLoggedIn())
{
@@ -273,15 +358,10 @@ namespace ReallifeGamemode.Server.Managers
using var dbContext = new DatabaseContext();
User user = player.GetUser(dbContext);
CannabisPlant plant = dbContext.CannabisPlants.Find(hanfId);
CannabisPlant plant = dbContext.CannabisPlants.Include(p => p.PlantedBy).Where(p => p.Id == hanfId).FirstOrDefault();
if (plant == null)
{
logger.LogError("Player {0} tried to harvest cannabis plant {1} but it was not found in database", player.Name, hanfId);
}
if (user.Faction?.StateOwned == true && !(user.FactionId == 3 && player.IsDuty()))
{
player.SendNotification("~r~Du kannst kein Hanf ernten");
return;
}
@@ -290,10 +370,32 @@ namespace ReallifeGamemode.Server.Managers
return;
}
plant.Harvested = true;
if (user.FactionId != 3) // Zivi / Gangmember erntet ab
if (player.IsAdminDuty() && player.IsTSupport() && user.IsAdmin(AdminLevel.ADMIN))
{
player.SendNotification($"Du hast die Hanf-Pflanze von ~y~{plant.PlantedBy.Name}~s~ entfernt");
plant.Harvested = true;
dbContext.SaveChanges();
UpdateHanfWorldData(dbContext);
return;
}
if (user.Faction?.StateOwned == true)
{
if (!((user.FactionId == 1 || user.FactionId == 3) && player.IsDuty()))
{
player.SendNotification("~r~Du kannst kein Hanf ernten");
return;
}
}
//player.SyncAnimation(new[] { "harvestPlantEnter", "harvestPlant", "harvestPlantExit" }.ToList());
//player.SyncAnimation("harvestPlant");
//player.AddAttachment("shovel", false);
if (user.FactionId != 3 && user.FactionId != 1) // Zivi / Gangmember erntet ab
{
player.SyncAnimation("harvestPlant");
plant.Harvested = true;
bool isPlantRotten = DateTime.Now - plant.PlantDate > MAX_PLANT_TIME;
if (isPlantRotten)
{
@@ -305,7 +407,7 @@ namespace ReallifeGamemode.Server.Managers
if (!isPlantReadyToHarvest) // Wenn die Pflanze noch nicht ausgewachsen ist
{
bool getSeedBack = _random.Next(0, 2) == 1; // 50% Chance
if(getSeedBack)
if (getSeedBack)
{
IItem cannabisSeedItem = InventoryManager.GetItem<CannabisSeeds>();
InventoryManager.AddItemToInventory(player, cannabisSeedItem.Id, 1);
@@ -340,15 +442,32 @@ namespace ReallifeGamemode.Server.Managers
}
}
}
else // FIB erntet ab
else // FIB / LSPD erntet ab
{
if (!(player.HasData("IsCarryingPlant") || player.GetData<bool>("IsCarryingPlant")))
{
player.SetData("IsCarryingPlant", true);
player.SendNotification($"Du hast eine Pflanze von ~y~{plant.PlantedBy.Name}~s~ ausgegraben");
player.SyncAnimation("harvestPlant");
plant.Harvested = true;
var modelToGet = await player.TriggerProcedure("SERVER:Hanf_GetModelToGivePlayer", plant.Id);
player.SetData("HoldingCannabisPlant", modelToGet);
player.AddAttachment("CannabisPlantInHand" + modelToGet, false);
}
}
dbContext.SaveChanges();
UpdateHanfWorldData(dbContext);
}
internal static bool IsPlayerNearCannabisSellPoint(Player player)
{
return _cannabisSellPoints.Any(p => p.DistanceTo(player.Position) <= 3);
}
internal static void BuildJointsFromCannabis(Player player)
{
if (player.HasAnimation(_manufacturerAnim) || _manufacturerCurrentlyUsed)
@@ -396,5 +515,105 @@ namespace ReallifeGamemode.Server.Managers
_manufacturerDoneTimer.Stop();
_manufacturerCurrentlyUsed = false;
}
[RemoteEvent("CLIENT:Hanf_FinishDiggingAnimation")]
public void HanfManagerFinishDiggingAnimation(Player player)
{
player.AddAttachment("shovel", true);
}
private static int GetAmountOfCannabisSeedsPlayerCanBuyToday(User user)
{
if (user.LastTimeBoughtCannabisSeeds == null)
{
return MAX_SEEDS_PER_DAY;
}
if (user.LastTimeBoughtCannabisSeeds.Value.Date != DateTime.Now.Date)
{
return MAX_SEEDS_PER_DAY;
}
return MAX_SEEDS_PER_DAY - user.CannabisSeedsBoughtToday;
}
internal static void ShowPlayerBuySeedMenu(Player player)
{
var user = player.GetUser();
if (user.Faction?.StateOwned == true)
{
return;
}
var seedsUserCanBuy = GetAmountOfCannabisSeedsPlayerCanBuyToday(user);
if (seedsUserCanBuy == 0)
{
player.SendNotification("~r~Du kannst heute keine Samen mehr kaufen");
return;
}
player.TriggerEvent("SERVER:Hanf_BuySeed", seedsUserCanBuy, SEED_PRICE);
}
internal static void PlayerSellCannabis(Player player)
{
if (!player.IsLoggedIn())
{
return;
}
using var dbContext = new DatabaseContext();
var user = player.GetUser(dbContext);
if (user.Faction?.StateOwned == true)
{
return;
}
List<UserItem> items = InventoryManager.GetUserItems(player, dbContext);
IItem cannabisItem = InventoryManager.GetItem<Cannabis>();
var cannabisAmount = items.Where(i => i.ItemId == cannabisItem.Id).FirstOrDefault()?.Amount ?? 0;
if (cannabisAmount == 0)
{
player.SendNotification("~r~Du hast kein Cannabis dabei");
return;
}
player.TriggerEvent("SERVER:Hanf_SellCannabisMenu", cannabisAmount, CANNABIS_PRICE);
}
[RemoteEvent("CLIENT:Hanf_SellCannabis")]
public void HanfManagerSellCannabis(Player player, int amount)
{
if (!player.IsLoggedIn())
{
return;
}
using var dbContext = new DatabaseContext();
var user = player.GetUser(dbContext);
List<UserItem> items = InventoryManager.GetUserItems(player, dbContext);
IItem cannabisItem = InventoryManager.GetItem<Cannabis>();
var cannabisUserItem = items.Where(i => i.ItemId == cannabisItem.Id).FirstOrDefault();
var cannabisAmount = cannabisUserItem?.Amount ?? 0;
if (cannabisAmount < amount)
{
player.SendNotification("~r~Du hast nicht so viel Cannabis dabei");
return;
}
var price = amount * CANNABIS_PRICE;
user.Handmoney += price;
InventoryManager.RemoveUserItem(user, cannabisUserItem, amount);
logger.LogInformation("Player {0} sold {1} cannabis to the server for {2} dollars", player.Name, amount, price);
player.SendNotification($"Du hast ~g~{amount} Hanfblüten~s~ für ~g~{price.ToMoneyString()}~s~ verkauft");
}
}
}

View File

@@ -18,6 +18,7 @@ using ReallifeGamemode.Server.Core.API;
using ReallifeGamemode.Server.Factions.Medic;
using Microsoft.Extensions.Logging;
using ReallifeGamemode.Server.Log;
using ReallifeGamemode.Server.Inventory.Interfaces;
/**
* @overview Life of German Reallife - Managers Interaction (InteractionManager.cs)
@@ -758,6 +759,150 @@ namespace ReallifeGamemode.Server.Managers
}
}
[RemoteEvent("CLIENT:InteractionMenu_LSPD_FriskUser")]
public void InteractionMenuLspdFriskUser(Player player, string name)
{
if (!player.IsLoggedIn())
{
return;
}
Player target = PlayerService.GetPlayerByNameOrId(name);
if (!target.IsLoggedIn())
{
ChatService.PlayerNotFound(player);
return;
}
using var dbContext = new DatabaseContext();
var user = player.GetUser(dbContext);
if ((user.FactionId != 1 && user.FactionId != 3) || !player.IsDuty())
{
return;
}
if (player.Position.DistanceTo(target.Position) > 5)
{
player.SendNotification("~r~Der Spieler ist nicht in deiner Nähe");
return;
}
var targetUser = target.GetUser(dbContext);
var targetItems = InventoryManager.GetUserItems(target, dbContext);
List<string> illItemsList = new List<string>();
bool illegalItemsFound = false;
var price = 0;
foreach (var targetItem in targetItems)
{
IItem item = InventoryManager.GetItemById(targetItem.ItemId);
if (!item.Legal)
{
illItemsList.Add($"{targetItem.Amount}x {item.Name}");
InventoryManager.RemoveUserItem(targetUser, targetItem, targetItem.Amount);
illegalItemsFound = true;
price += ((IIllegalItem)item).PriceForConfiscation * targetItem.Amount;
logger.LogInformation("Player {0} confiscated the illegal item {1} ({2}, amount: {3}) from player {4}", player.Name, item.Name, item.Id, targetItem.Amount, target.Name);
}
}
if (!illegalItemsFound)
{
player.SendNotification("~g~Der Spieler hat keine illegalen Gegenstände dabei");
return;
}
logger.LogInformation("executive factions got {0} dollars from the confiscation", price);
var factions = dbContext.Factions.Include(f => f.BankAccount).Where(f => f.Id == 1 || f.Id == 3);
foreach (var faction in factions)
{
faction.BankAccount.Balance += price;
}
dbContext.SaveChanges();
string illItemsStr = "~y~" + string.Join("~s~,~y~ ", illItemsList) + "~s~";
ChatService.SendMessage(player, $"Du hast ~y~{target.Name}~s~ folgende Gegenstände abgenommen: {illItemsStr}");
ChatService.SendMessage(target, $"~y~{player.Name}~s~ hat die folgende Gegenstände abgenommen: {illItemsStr}");
}
[RemoteEvent("CLIENT:InteractionMenu_LSPD_FriskVehicle")]
public void InteractionMenuLspdFriskVehicle(Player player)
{
if (!player.IsLoggedIn())
{
return;
}
using var dbContext = new DatabaseContext();
var user = player.GetUser(dbContext);
if ((user.FactionId != 1 && user.FactionId != 3) || !player.IsDuty())
{
return;
}
var vehicle = NAPI.Pools.GetAllVehicles().Where(v => v.Position.DistanceTo(player.Position) <= 5).OrderBy(v => v.Position.DistanceTo(player.Position)).FirstOrDefault();
if (vehicle == null)
{
player.SendNotification("~r~Es befindet sich kein Fahrzeug in deiner Nähe");
return;
}
ServerVehicle serverVehicle = vehicle.GetServerVehicle(dbContext);
if (serverVehicle == null)
{
player.SendNotification("~r~Dieses Fahrzeug kann nicht durchsucht werden");
return;
}
if (VehicleStreaming.GetLockState(vehicle) || serverVehicle.Locked)
{
player.SendNotification("~r~Dieses Fahrzeug ist abgeschlossen");
return;
}
var targetItems = InventoryManager.GetVehicleItems(vehicle);
List<string> illItemsList = new List<string>();
bool illegalItemsFound = false;
var price = 0;
foreach (var targetItem in targetItems)
{
IItem item = InventoryManager.GetItemById(targetItem.ItemId);
if (!item.Legal)
{
illItemsList.Add($"{targetItem.Amount}x {item.Name}");
InventoryManager.RemoveVehicleItem(serverVehicle, targetItem, targetItem.Amount, null);
illegalItemsFound = true;
price += ((IIllegalItem)item).PriceForConfiscation * targetItem.Amount;
logger.LogInformation("Player {0} confiscated the illegal item {1} ({2}, amount: {3}) from vehicle {4}", player.Name, item.Name, item.Id, targetItem.Amount, serverVehicle.Id);
}
}
if (!illegalItemsFound)
{
player.SendNotification("~g~Im Kofferraum sind keine illegalen Gegenstände");
return;
}
logger.LogInformation("executive factions got {0} dollars from the confiscation", price);
var factions = dbContext.Factions.Include(f => f.BankAccount).Where(f => f.Id == 1 || f.Id == 3);
foreach (var faction in factions)
{
faction.BankAccount.Balance += price;
}
dbContext.SaveChanges();
string illItemsStr = "~y~" + string.Join("~s~,~y~ ", illItemsList) + "~s~";
ChatService.SendInRange(player.Position, 20, $"{player.Name} hat aus dem Kofferraum folgende Gegenstände beschlagnahmt: {illItemsStr}");
}
[RemoteEvent("CLIENT:InteractionMenu_Pay")]
public void InteractionMenu_Pay(Player player, string jsonNameOrId, string stringAmount)
{

View File

@@ -47,7 +47,7 @@ namespace ReallifeGamemode.Server.Managers
public int Amount;
public int ItemId;
public int Weight;
public Vehicle vehicle;
public ushort vehicle;
}
public static void SetBackpackItems(Player player)
@@ -75,9 +75,9 @@ namespace ReallifeGamemode.Server.Managers
player.SetSharedData("backpackItems", JsonConvert.SerializeObject(backpackItems[player].ToArray()));
}
internal static IItem GetItem<T>() where T : IItem
internal static T GetItem<T>() where T : IItem
{
return itemList.Where(i => i.GetType() == typeof(T)).First();
return (T)itemList.Where(i => i.GetType() == typeof(T)).First();
}
[RemoteEvent("CLIENT:getVehicleInventory")]
@@ -86,7 +86,9 @@ namespace ReallifeGamemode.Server.Managers
if (player.IsInVehicle)
{
player.TriggerEvent("Error", "Du kannst deinen Kofferraum gerade nicht öffnen.");
return;
}
Vehicle veh = NAPI.Pools.GetAllVehicles()
.ToList()
.Where(v => v.Position.DistanceTo(player.Position) <= 4)
@@ -131,7 +133,7 @@ namespace ReallifeGamemode.Server.Managers
Amount = i.Amount,
ItemId = i.ItemId,
Weight = GetVehicleInventoryWeight(veh),
vehicle = veh,
vehicle = veh.Handle.Value,
};
vehicleItems[player].Add(newItem);
}
@@ -197,6 +199,10 @@ namespace ReallifeGamemode.Server.Managers
public static void RemoveVehicleItem(ServerVehicle sVeh, VehicleItem item, int amount, Player player)
{
if (item == null)
{
return;
}
using (var dbContext = new DatabaseContext())
{
var vehicleItem = dbContext.VehicleItems.FirstOrDefault(i => i.Id == item.Id);
@@ -327,20 +333,29 @@ namespace ReallifeGamemode.Server.Managers
using (var context = new DatabaseContext())
{
User user = player.GetUser(context);
if (!vehicleItems.ContainsKey(player))
vehicleItems.Add(player, new List<InventoryItem>());
Vehicle veh = null;
if (vehicleItems[player].Count != 0)
veh = vehicleItems[player].FirstOrDefault().vehicle;
veh = new NetHandle(vehicleItems[player].FirstOrDefault().vehicle, EntityType.Vehicle).Entity<Vehicle>();
ServerVehicle serverVehicle = VehicleManager.GetServerVehicleFromVehicle(veh, context);
VehicleItem vehItem = context.VehicleItems.Where(v => v.VehicleId == serverVehicle.Id && v.ItemId == itemID).FirstOrDefault();
if (vehItem == null)
{
player.TriggerEvent("Error", "Transfer nicht möglich.");
return;
}
if (serverVehicle is FactionVehicle factionVehicle)
{
IItem item = GetItemById(itemID);
if (factionVehicle.Model == VehicleHash.Riot && user.Faction?.StateOwned == true)
{
player.TriggerEvent("Error", "Transfer nicht möglich.");
return;
}
}
RemoveVehicleItem(serverVehicle, vehItem, itemAmount, player);
SetVehicleItems(player);