179 lines
5.6 KiB
C#
179 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Timers;
|
|
using GTANetworkAPI;
|
|
using Microsoft.Extensions.Logging;
|
|
using ReallifeGamemode.Database.Entities;
|
|
using ReallifeGamemode.Database.Models;
|
|
using ReallifeGamemode.Server.Extensions;
|
|
using ReallifeGamemode.Server.Inventory.Interfaces;
|
|
using ReallifeGamemode.Server.Inventory.Items;
|
|
using ReallifeGamemode.Server.Services;
|
|
using ReallifeGamemode.Server.Util;
|
|
|
|
namespace ReallifeGamemode.Server.Managers
|
|
{
|
|
public class HanfManager : BaseScript
|
|
{
|
|
/// <summary>
|
|
/// Punkte zum Samen kaufen
|
|
/// </summary>
|
|
private readonly static List<Vector3> _seedsBuyPoints = new List<Vector3>();
|
|
|
|
/// <summary>
|
|
/// Aktueller Samen-Preis
|
|
/// </summary>
|
|
public static int SEED_PRICE = 50;
|
|
|
|
/// <summary>
|
|
/// Wie viele Joints man aus einem Cannabis bekommt
|
|
/// </summary>
|
|
public static int CANNABIS_TO_JOINT_RATIO = 3;
|
|
|
|
/// <summary>
|
|
/// Flag, ob der Verarbeiter aktuell benutzt wird
|
|
/// </summary>
|
|
private static bool _manufacturerCurrentlyUsed = false;
|
|
|
|
/// <summary>
|
|
/// Marihuana -> Joint
|
|
/// </summary>
|
|
private readonly static Vector3 _jointManufacturerPoint = new Vector3(-127.726105, 1921.5142, 197.31104);
|
|
|
|
/// <summary>
|
|
/// Animations-ID des Verarbeitens
|
|
/// </summary>
|
|
private const string _manufacturerAnim = "manufacturJoint";
|
|
|
|
private static Timer _manufacturerDoneTimer = new Timer(TimeSpan.FromSeconds(10).TotalMilliseconds);
|
|
|
|
static HanfManager()
|
|
{
|
|
_manufacturerDoneTimer.Elapsed += ManufacturerDoneTimerCallback;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ladefunktion zum
|
|
/// </summary>
|
|
public static void Load()
|
|
{
|
|
_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));
|
|
_seedsBuyPoints.Add(new Vector3(-758.6156, -1595.2686, 6.302392));
|
|
_seedsBuyPoints.Add(new Vector3(-441.0504, 1595.4435, 358.4195));
|
|
|
|
ColShape colShape = null;
|
|
|
|
foreach (Vector3 buyPoint in _seedsBuyPoints)
|
|
{
|
|
colShape = NAPI.ColShape.CreateSphereColShape(buyPoint, 20.0f);
|
|
colShape.OnEntityEnterColShape += OnSeedBuyRangeColShapeEnter;
|
|
}
|
|
}
|
|
|
|
private static void OnSeedBuyRangeColShapeEnter(ColShape colShape, Player player)
|
|
{
|
|
ChatService.SendMessage(player, $"Fremder sagt: Pssst.. Willst paar Samen erwerben?");
|
|
}
|
|
|
|
internal static bool IsPlayerNearSeedBuyPoint(Player player)
|
|
{
|
|
return _seedsBuyPoints.Any(p => player.Position.DistanceTo(p) <= 2.5f);
|
|
}
|
|
|
|
internal static bool IsPlayerNearJointManufacturer(Player player)
|
|
{
|
|
return _jointManufacturerPoint.DistanceTo(player.Position) <= 2.5f;
|
|
}
|
|
|
|
[RemoteEvent("CLIENT:Hanf_BuySeeds")]
|
|
public void HanfManagerBuySeeds(Player player, int amount)
|
|
{
|
|
if (!player.IsLoggedIn())
|
|
{
|
|
return;
|
|
}
|
|
|
|
using var dbContext = new DatabaseContext();
|
|
var user = player.GetUser(dbContext);
|
|
|
|
IItem seedItem = InventoryManager.GetItem<CannabisSeeds>();
|
|
var newAmount = seedItem.Gewicht * amount;
|
|
|
|
if (!InventoryManager.CanPlayerHoldMoreWeight(player, newAmount))
|
|
{
|
|
player.TriggerEvent("~r~So viele Samen passen nicht mehr in deine Hosentasche");
|
|
return;
|
|
}
|
|
|
|
var price = amount * SEED_PRICE;
|
|
|
|
if (user.Handmoney < price)
|
|
{
|
|
player.SendNotification("~r~Du hast nicht genug Geld dafür");
|
|
return;
|
|
}
|
|
|
|
logger.LogInformation("Player {0} bought {1} cannabis seeds for {2} dollars (price per seed: {3})", player.Name, amount, price, SEED_PRICE); // <-- WICHTIG LOGS
|
|
|
|
user.Handmoney -= price;
|
|
dbContext.SaveChanges();
|
|
|
|
InventoryManager.AddItemToInventory(player, seedItem.Id, amount);
|
|
|
|
player.SendNotification($"Du hast {amount} Hanfsamen gekauft");
|
|
}
|
|
|
|
internal static void BuildJointsFromCannabis(Player player)
|
|
{
|
|
if(player.HasAnimation(_manufacturerAnim) || _manufacturerCurrentlyUsed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
using var dbContext = new DatabaseContext();
|
|
var user = player.GetUser(dbContext);
|
|
var userItems = InventoryManager.GetUserItems(player, dbContext);
|
|
|
|
IItem cannabisItem = InventoryManager.GetItem<Cannabis>();
|
|
IItem jointItem = InventoryManager.GetItem<Joint>();
|
|
|
|
var addWeight = jointItem.Gewicht * CANNABIS_TO_JOINT_RATIO - cannabisItem.Gewicht;
|
|
|
|
if (!InventoryManager.CanPlayerHoldMoreWeight(player, addWeight))
|
|
{
|
|
player.SendNotification("~r~Für die Verarbeitung hast du nicht genug Platz im Rucksack");
|
|
return;
|
|
}
|
|
|
|
UserItem userCannabisItem = userItems.Where(i => i.ItemId == cannabisItem.Id).FirstOrDefault();
|
|
if (userCannabisItem == null)
|
|
{
|
|
player.SendNotification("~r~Du hast kein Cannabis dabei");
|
|
return;
|
|
}
|
|
|
|
_manufacturerCurrentlyUsed = true;
|
|
_manufacturerDoneTimer.Start();
|
|
|
|
player.SyncAnimation(_manufacturerAnim);
|
|
|
|
InventoryManager.RemoveUserItem(user, userCannabisItem, 1);
|
|
InventoryManager.AddItemToInventory(player, jointItem.Id, CANNABIS_TO_JOINT_RATIO);
|
|
|
|
player.SendNotification($"Du hast 1 Cannabis in {CANNABIS_TO_JOINT_RATIO} Joints verarbeitet");
|
|
|
|
NAPI.ClientEvent.TriggerClientEventInRange(player.Position, 100.0f, "SERVER:Hanf_PlayManufacturerAnim", _manufacturerAnim);
|
|
}
|
|
|
|
private static void ManufacturerDoneTimerCallback(object sender, EventArgs args)
|
|
{
|
|
_manufacturerDoneTimer.Stop();
|
|
_manufacturerCurrentlyUsed = false;
|
|
}
|
|
}
|
|
}
|