From 7277555de82b58f2564741d25b65151d5eda24e5 Mon Sep 17 00:00:00 2001 From: hydrant Date: Mon, 7 Jun 2021 10:21:45 +0200 Subject: [PATCH] Hanf Changes - 50 Plants maximal - Pro gepflanzter Pflanze einen Samen kaufen - Cops 5 Sekunden rausrupfen --- ReallifeGamemode.Client/util/animationSync.ts | 1 + .../Managers/HanfManager.cs | 197 ++++++++++++++++-- 2 files changed, 177 insertions(+), 21 deletions(-) diff --git a/ReallifeGamemode.Client/util/animationSync.ts b/ReallifeGamemode.Client/util/animationSync.ts index 02f51191..f0a8652e 100644 --- a/ReallifeGamemode.Client/util/animationSync.ts +++ b/ReallifeGamemode.Client/util/animationSync.ts @@ -53,6 +53,7 @@ export default function animationSync() { animationSyncData.register("manufacturJoint", "anim@mp_snowball", "pickup_snowball", 1000 * 10, false, 1, false); //animationSyncData.register("harvestPlantEnter", "amb@world_human_gardener_plant@female@enter", "enter_female", 1000 * 3.5, false, 1, false); animationSyncData.register("harvestPlant", "amb@world_human_gardener_plant@male@base", "base", 1000 * 10, false, 1, false, "Hanf_FinishDiggingAnimation"); + animationSyncData.register("harvestPlantCop", "amb@world_human_gardener_plant@male@base", "base", 1000 * 5, false, 1, false, "Hanf_FinishDiggingAnimation"); //animationSyncData.register("harvestPlantExit", "amb@world_human_gardener_plant@female@exit", "exit_female", 1000 * 3.5, false, 1, false, "Hanf_FinishDiggingAnimation"); animationSyncData.register("jointUse", "amb@world_human_smoking_pot@male@base", "base", 1000 * 10, false, 1, false); }); diff --git a/ReallifeGamemode.Server/Managers/HanfManager.cs b/ReallifeGamemode.Server/Managers/HanfManager.cs index 7eeaa57f..9a363910 100644 --- a/ReallifeGamemode.Server/Managers/HanfManager.cs +++ b/ReallifeGamemode.Server/Managers/HanfManager.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -28,19 +28,21 @@ namespace ReallifeGamemode.Server.Managers private readonly static List _cannabisSellPoints = new List(); /// - /// Aktueller Samen-Preis + /// Minimum Preis für Samen /// - public static int SEED_PRICE = 0; - private const int SEED_PRICE_MIN = 175; + /// + /// Maximum Preis für Samen + /// private const int SEED_PRICE_MAX = 225; /// - /// Aktueller Hanf-Verkaufspreis + /// Minimum Preis für Cannabis /// - public static int CANNABIS_PRICE = 0; - private const int CANNABIS_PRICE_MIN = 80; + /// + /// Maximum Preis für Cannabis + /// private const int CANNABIS_PRICE_MAX = 140; /// @@ -48,6 +50,11 @@ namespace ReallifeGamemode.Server.Managers /// private const int MAX_SEEDS_PER_DAY = 50; + /// + /// Wie viele Pflanzen ein Spieler gleichzeitig anpflanen darf + /// + private const int MAX_CONCURRENT_PLANTS = 5; + /// /// Wie viel Cannabis man für einen Joint braucht /// @@ -73,6 +80,11 @@ namespace ReallifeGamemode.Server.Managers /// private const string PLAYER_CURRENTLY_PLANTING_DATA_KEY = "isPlantingCannabis"; + /// + /// Data-Key, bei welchem Hanf-NPC der Spieler grade steht + /// + private const string CURRENT_HANF_NPC_DATA_KEY = "CurrentHanfNpc"; + /// /// Ab welcher Zeit Pflanzen verwelken /// @@ -85,6 +97,8 @@ namespace ReallifeGamemode.Server.Managers /// private static Timer _manufacturerDoneTimer = new Timer(TimeSpan.FromSeconds(10).TotalMilliseconds); + public static List HanfNpcs { get; private set; } = new List(); + /// /// Zufallsgenerator für weibliche Pflanze und erhaltenes Cannabis /// @@ -103,12 +117,6 @@ namespace ReallifeGamemode.Server.Managers /// 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)); @@ -117,10 +125,21 @@ namespace ReallifeGamemode.Server.Managers ColShape colShape = null; + int npcId = 0; + foreach (Vector3 buyPoint in _seedsBuyPoints) { - colShape = NAPI.ColShape.CreateSphereColShape(buyPoint, 10.0f); + colShape = NAPI.ColShape.CreateSphereColShape(buyPoint, 8.0f); + HanfNpcs.Add(new HanfNpc() + { + ColShape = colShape, + Position = buyPoint, + Id = ++npcId, + Type = HanfNpcType.BUY_SEEDS, + }); colShape.OnEntityEnterColShape += OnSeedBuyRangeColShapeEnter; + colShape.OnEntityEnterColShape += OnHanfNpcColShapeEnter; + colShape.OnEntityExitColShape += OnHanfNpcColShapeExit; } _cannabisSellPoints.Add(new Vector3(2220.04, 5614.24, 54.72)); @@ -129,10 +148,21 @@ namespace ReallifeGamemode.Server.Managers foreach (Vector3 sellPoint in _cannabisSellPoints) { - colShape = NAPI.ColShape.CreateSphereColShape(sellPoint, 10.0f); + colShape = NAPI.ColShape.CreateSphereColShape(sellPoint, 8.0f); + HanfNpcs.Add(new HanfNpc() + { + ColShape = colShape, + Position = sellPoint, + Id = ++npcId, + Type = HanfNpcType.SELL_CANNABIS + }); colShape.OnEntityEnterColShape += OnCannabisSellRangeColShapeEnter; + colShape.OnEntityEnterColShape += OnHanfNpcColShapeEnter; + colShape.OnEntityExitColShape += OnHanfNpcColShapeExit; } + RegeneratePrices(); + 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~s~, um den ~y~Riot~s~ zu entladen", ASSERVATENKAMMER_POSITION, 10.0f, 10.0f, 0, Colors.White); @@ -145,6 +175,72 @@ namespace ReallifeGamemode.Server.Managers updateHanfDataTimer.Start(); } + private static void RegeneratePrices() + { + foreach (HanfNpc hanfNpc in HanfNpcs) + { + hanfNpc.Price = hanfNpc.Type switch + { + HanfNpcType.BUY_SEEDS => GetRandomSeedPrice(), + HanfNpcType.SELL_CANNABIS => GetRandomCannabisPrice(), + _ => int.MaxValue + }; + } + + string generatedPricesStr = string.Join(", ", HanfNpcs.Select(h => + { + return $"{h.Id} ({h.Type}) {h.Price} dollars"; + })); + logger.LogInformation("Generated prices: {0}", generatedPricesStr); + } + + private static HanfNpc GetHanfNpcFromColShape(ColShape colShape) + { + return HanfNpcs.Where(h => h.ColShape.Handle.Value == colShape.Handle.Value).FirstOrDefault(); + } + + private static HanfNpc GetHanfNpcWherePlayerIsStanding(Player client) + { + if (!client.HasData(CURRENT_HANF_NPC_DATA_KEY)) + { + return null; + } + + int npcId = client.GetData(CURRENT_HANF_NPC_DATA_KEY); + return GetHanfNpcFromId(npcId); + } + + public static HanfNpc GetHanfNpcFromId(int id) + { + return HanfNpcs.Where(h => h.Id == id).FirstOrDefault(); + } + + private static void OnHanfNpcColShapeExit(ColShape colShape, Player client) + { + client.ResetData(CURRENT_HANF_NPC_DATA_KEY); + } + + private static void OnHanfNpcColShapeEnter(ColShape colShape, Player client) + { + HanfNpc hanfNpc = GetHanfNpcFromColShape(colShape); + if (hanfNpc != null) + { + client.SetData(CURRENT_HANF_NPC_DATA_KEY, hanfNpc.Id); + } + } + + private static int GetRandomSeedPrice() + { + return _random.Next(SEED_PRICE_MIN, SEED_PRICE_MAX + 1); + } + + private static int GetRandomCannabisPrice() + { + return _random.Next(CANNABIS_PRICE_MIN, CANNABIS_PRICE_MAX + 1); + } + + + private static void OnCannabisSellRangeColShapeEnter(ColShape colShape, Player player) { if (!player.IsLoggedIn()) @@ -229,6 +325,12 @@ namespace ReallifeGamemode.Server.Managers return; } + if (dbContext.CannabisPlants.Where(p => p.PlantedById == user.Id && !p.Harvested).Count() >= MAX_CONCURRENT_PLANTS) + { + player.SendNotification($"~r~Du kannst nur {MAX_CONCURRENT_PLANTS} Pflanzen gleichzeitig anpflanzen"); + return; + } + player.SetData(PLAYER_CURRENTLY_PLANTING_DATA_KEY, true); player.TriggerEvent("SERVER:Hanf_StartPlanting"); @@ -252,6 +354,11 @@ namespace ReallifeGamemode.Server.Managers using var dbContext = new DatabaseContext(); var user = player.GetUser(dbContext); + if (user.CannabisSeedsBoughtToday > 0) + { + user.CannabisSeedsBoughtToday -= 1; + } + InventoryManager.RemoveUserItem(user, userCannabisSeedsItem, 1); CannabisPlant newPlant = new CannabisPlant() @@ -349,7 +456,14 @@ namespace ReallifeGamemode.Server.Managers return; } - var price = amount * SEED_PRICE; + HanfNpc hanfNpc = GetHanfNpcWherePlayerIsStanding(player); + if (hanfNpc == null) + { + player.SendNotification("~r~Du bist nicht mehr beim Dealer"); + return; + } + + var price = amount * hanfNpc.Price; if (user.Handmoney < price) { @@ -357,7 +471,7 @@ namespace ReallifeGamemode.Server.Managers return; } - logger.LogInformation("Player {0} bought {1} cannabis seeds for {2} dollars (price per seed: {3})", player.Name, amount, price, SEED_PRICE); // <-- WICHTIG LOGS + logger.LogInformation("Player {0} bought {1} cannabis seeds for {2} dollars (price per seed: {3})", player.Name, amount, price, hanfNpc.Price); // <-- WICHTIG LOGS if (user.LastTimeBoughtCannabisSeeds == null || user.LastTimeBoughtCannabisSeeds.Value.Date != DateTime.Now.Date) { @@ -428,6 +542,7 @@ namespace ReallifeGamemode.Server.Managers logger.LogInformation("Player {0} harvested the cannabis plant {1} from {2}", player.Name, plant.Id, plant.PlantedBy.Name); player.SyncAnimation("harvestPlant"); plant.Harvested = true; + bool isPlantRotten = DateTime.Now - plant.PlantDate > MAX_PLANT_TIME; if (isPlantRotten) { @@ -483,7 +598,7 @@ namespace ReallifeGamemode.Server.Managers player.SendNotification($"Du hast eine Pflanze von ~y~{plant.PlantedBy.Name}~s~ ausgegraben"); - player.SyncAnimation("harvestPlant"); + player.SyncAnimation("harvestPlantCop"); plant.Harvested = true; var modelToGet = GetModelFromPlant(plant); @@ -598,7 +713,14 @@ namespace ReallifeGamemode.Server.Managers return; } - player.TriggerEvent("SERVER:Hanf_BuySeed", seedsUserCanBuy, SEED_PRICE); + HanfNpc hanfNpc = GetHanfNpcWherePlayerIsStanding(player); + if (hanfNpc == null) + { + player.SendNotification("~r~Du bist nicht bei einem Dealer"); + return; + } + + player.TriggerEvent("SERVER:Hanf_BuySeed", seedsUserCanBuy, hanfNpc.Price); } internal static void PlayerSellCannabis(Player player) @@ -626,7 +748,14 @@ namespace ReallifeGamemode.Server.Managers return; } - player.TriggerEvent("SERVER:Hanf_SellCannabisMenu", cannabisAmount, CANNABIS_PRICE); + HanfNpc hanfNpc = GetHanfNpcWherePlayerIsStanding(player); + if (hanfNpc == null) + { + player.SendNotification("~r~Du bist nicht bei einem Dealer"); + return; + } + + player.TriggerEvent("SERVER:Hanf_SellCannabisMenu", cannabisAmount, hanfNpc.Price); } [RemoteEvent("CLIENT:Hanf_SellCannabis")] @@ -651,7 +780,14 @@ namespace ReallifeGamemode.Server.Managers return; } - var price = amount * CANNABIS_PRICE; + HanfNpc hanfNpc = GetHanfNpcWherePlayerIsStanding(player); + if (hanfNpc == null) + { + player.SendNotification("~r~Du bist nicht mehr bei einem Dealer"); + return; + } + + var price = amount * hanfNpc.Price; user.Handmoney += price; InventoryManager.RemoveUserItem(user, cannabisUserItem, amount); dbContext.SaveChanges(); @@ -663,4 +799,23 @@ namespace ReallifeGamemode.Server.Managers player.SendNotification($"Du hast ~g~{amount} Hanfblüten~s~ für ~g~{price.ToMoneyString()}~s~ verkauft"); } } + + public class HanfNpc + { + public int Id { get; set; } + + public HanfNpcType Type { get; set; } + + public int Price { get; set; } + + public ColShape ColShape { get; set; } + + public Vector3 Position { get; set; } + } + + public enum HanfNpcType + { + BUY_SEEDS, + SELL_CANNABIS + } }