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

@@ -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");
}
}
}