samen kaufen und hanf -> joint
This commit is contained in:
178
ReallifeGamemode.Server/Managers/HanfManager.cs
Normal file
178
ReallifeGamemode.Server/Managers/HanfManager.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,6 +75,11 @@ namespace ReallifeGamemode.Server.Managers
|
||||
player.SetSharedData("backpackItems", JsonConvert.SerializeObject(backpackItems[player].ToArray()));
|
||||
}
|
||||
|
||||
internal static IItem GetItem<T>() where T : IItem
|
||||
{
|
||||
return itemList.Where(i => i.GetType() == typeof(T)).First();
|
||||
}
|
||||
|
||||
[RemoteEvent("CLIENT:getVehicleInventory")]
|
||||
public static void SetVehicleItems(Player player)
|
||||
{
|
||||
@@ -134,6 +139,11 @@ namespace ReallifeGamemode.Server.Managers
|
||||
player.SetSharedData("vehicleItems", JsonConvert.SerializeObject(vehicleItems[player].ToArray()));
|
||||
}
|
||||
|
||||
public static bool CanPlayerHoldMoreWeight(Player player, int moreWeight)
|
||||
{
|
||||
return GetUserInventoryWeight(player) + moreWeight <= MAX_USER_INVENTORY;
|
||||
}
|
||||
|
||||
public static void LoadItems()
|
||||
{
|
||||
itemList = new List<IItem>();
|
||||
@@ -179,12 +189,8 @@ namespace ReallifeGamemode.Server.Managers
|
||||
if (userItem.Amount == 0)
|
||||
{
|
||||
dbContext.Remove(userItem);
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
Player player = user.Player;
|
||||
|
||||
List<UserItem> itemList = GetUserItems(player);
|
||||
if (itemList == null) return;
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -216,16 +222,29 @@ namespace ReallifeGamemode.Server.Managers
|
||||
return new DatabaseContext().VehicleItems.ToList().FindAll(i => i.GetVehicle().GetVehicle() == vehicle);
|
||||
}
|
||||
|
||||
public static List<UserItem> GetUserItems(Player player)
|
||||
public static List<UserItem> GetUserItems(Player player, DatabaseContext dbContext = null)
|
||||
{
|
||||
var user = player.GetUser();
|
||||
|
||||
using (var context = new DatabaseContext())
|
||||
if (dbContext == null)
|
||||
{
|
||||
return context.UserItems.ToList().FindAll(i => i.UserId == user.Id);
|
||||
using (dbContext = new DatabaseContext())
|
||||
{
|
||||
return GetUserItemsInternal(player, dbContext);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetUserItemsInternal(player, dbContext);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<UserItem> GetUserItemsInternal(Player player, DatabaseContext dbContext)
|
||||
{
|
||||
var user = player.GetUser(dbContext);
|
||||
|
||||
return dbContext.UserItems.ToList().FindAll(i => i.UserId == user.Id);
|
||||
}
|
||||
|
||||
|
||||
public static int GetUserInventoryWeight(Player player)
|
||||
{
|
||||
var user = player.GetUser();
|
||||
|
||||
Reference in New Issue
Block a user