hanf continuation
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Timers;
|
||||
using GTANetworkAPI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using ReallifeGamemode.Database.Entities;
|
||||
using ReallifeGamemode.Database.Models;
|
||||
using ReallifeGamemode.Server.Extensions;
|
||||
@@ -47,8 +48,18 @@ namespace ReallifeGamemode.Server.Managers
|
||||
/// </summary>
|
||||
private const string _manufacturerAnim = "manufacturJoint";
|
||||
|
||||
/// <summary>
|
||||
/// Data-Key, ob der Spieler grade eine Pflanze anpflanzt
|
||||
/// </summary>
|
||||
private const string PLAYER_CURRENTLY_PLANTING_DATA_KEY = "isPlantingCannabis";
|
||||
|
||||
/// <summary>
|
||||
/// Timer der den Status des Verarbeiters zurücksetzt
|
||||
/// </summary>
|
||||
private static Timer _manufacturerDoneTimer = new Timer(TimeSpan.FromSeconds(10).TotalMilliseconds);
|
||||
|
||||
private static List<CannabisData> _currentCannabisData = new List<CannabisData>();
|
||||
|
||||
static HanfManager()
|
||||
{
|
||||
_manufacturerDoneTimer.Elapsed += ManufacturerDoneTimerCallback;
|
||||
@@ -72,11 +83,29 @@ namespace ReallifeGamemode.Server.Managers
|
||||
colShape = NAPI.ColShape.CreateSphereColShape(buyPoint, 20.0f);
|
||||
colShape.OnEntityEnterColShape += OnSeedBuyRangeColShapeEnter;
|
||||
}
|
||||
|
||||
UpdateHanfWorldData(new DatabaseContext());
|
||||
|
||||
Timer updateHanfDataTimer = new Timer(TimeSpan.FromMinutes(1).TotalMilliseconds);
|
||||
updateHanfDataTimer.Elapsed += UpdateHanfDataTimer_Elapsed;
|
||||
updateHanfDataTimer.Start();
|
||||
}
|
||||
|
||||
private static void UpdateHanfDataTimer_Elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
using var dbContext = new DatabaseContext();
|
||||
UpdateHanfWorldData(dbContext);
|
||||
}
|
||||
|
||||
private static void OnSeedBuyRangeColShapeEnter(ColShape colShape, Player player)
|
||||
{
|
||||
ChatService.SendMessage(player, $"Fremder sagt: Pssst.. Willst paar Samen erwerben?");
|
||||
var user = player.GetUser();
|
||||
if (user.Faction.StateOwned)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ChatService.SendMessage(player, $"Fremder sagt: Pssst.. Willst du Samen kaufen?");
|
||||
}
|
||||
|
||||
internal static bool IsPlayerNearSeedBuyPoint(Player player)
|
||||
@@ -89,6 +118,95 @@ namespace ReallifeGamemode.Server.Managers
|
||||
return _jointManufacturerPoint.DistanceTo(player.Position) <= 2.5f;
|
||||
}
|
||||
|
||||
internal static void StartCannabisPlanting(Player player)
|
||||
{
|
||||
player.ToggleInventory(InventoryToggleOption.HIDE);
|
||||
|
||||
if (!player.IsAlive())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var dbContext = new DatabaseContext();
|
||||
var user = player.GetUser(dbContext);
|
||||
if (user.Faction?.StateOwned ?? false)
|
||||
{
|
||||
player.SendNotification("~r~Du darfst keine Hanfsamen einfplanzen");
|
||||
return;
|
||||
}
|
||||
|
||||
if (player.HasData(PLAYER_CURRENTLY_PLANTING_DATA_KEY) && player.GetData<bool>(PLAYER_CURRENTLY_PLANTING_DATA_KEY))
|
||||
{
|
||||
player.SendNotification("~r~Du pflanzt aktuell schon einen Hanfsamen ein");
|
||||
return;
|
||||
}
|
||||
|
||||
player.SetData(PLAYER_CURRENTLY_PLANTING_DATA_KEY, true);
|
||||
|
||||
player.TriggerEvent("SERVER:Hanf_StartPlanting");
|
||||
}
|
||||
|
||||
[RemoteEvent("CLIENT:Hanf_PlantHanf")]
|
||||
public void HanfManagerPlantHanf(Player player, float x, float y, float z)
|
||||
{
|
||||
player.ResetData(PLAYER_CURRENTLY_PLANTING_DATA_KEY);
|
||||
|
||||
IItem cannabisSeedItem = InventoryManager.GetItem<CannabisSeeds>();
|
||||
UserItem userCannabisSeedsItem = InventoryManager.UserHasThisItem(player, cannabisSeedItem.Id);
|
||||
if (userCannabisSeedsItem == null)
|
||||
{
|
||||
player.SendNotification("~r~Du hast keine Samen mehr");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.LogInformation("Player {0} planted a cannabis plant at x: {1}, y: {2}, z: {3}", player.Name, x, y, z);
|
||||
|
||||
using var dbContext = new DatabaseContext();
|
||||
var user = player.GetUser(dbContext);
|
||||
|
||||
InventoryManager.RemoveUserItem(user, userCannabisSeedsItem, 1);
|
||||
|
||||
CannabisPlant newPlant = new CannabisPlant()
|
||||
{
|
||||
X = x,
|
||||
Y = y,
|
||||
Z = z,
|
||||
PlantedBy = user,
|
||||
PlantDate = DateTime.Now,
|
||||
};
|
||||
|
||||
dbContext.CannabisPlants.Add(newPlant);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
UpdateHanfWorldData(dbContext);
|
||||
}
|
||||
|
||||
public static void UpdateHanfWorldData(DatabaseContext dbContext)
|
||||
{
|
||||
var activePlants = dbContext.CannabisPlants.Where(p => !p.Harvested).Select(p => new CannabisData()
|
||||
{
|
||||
Id = p.Id,
|
||||
Time = p.PlantDate,
|
||||
X = p.X,
|
||||
Y = p.Y,
|
||||
Z = p.Z
|
||||
}).ToList();
|
||||
|
||||
_currentCannabisData = activePlants;
|
||||
|
||||
NAPI.Pools.GetAllPlayers().ForEach(p =>
|
||||
{
|
||||
UpdateHanfForPlayer(p, activePlants);
|
||||
});
|
||||
}
|
||||
|
||||
public async static void UpdateHanfForPlayer(Player player, List<CannabisData> cannabisData = null)
|
||||
{
|
||||
cannabisData ??= _currentCannabisData;
|
||||
var x = await NAPI.Task.WaitForMainThread();
|
||||
player.TriggerEvent("SERVER:Hanf_UpdateHanfData", JsonConvert.SerializeObject(cannabisData));
|
||||
}
|
||||
|
||||
[RemoteEvent("CLIENT:Hanf_BuySeeds")]
|
||||
public void HanfManagerBuySeeds(Player player, int amount)
|
||||
{
|
||||
@@ -129,7 +247,7 @@ namespace ReallifeGamemode.Server.Managers
|
||||
|
||||
internal static void BuildJointsFromCannabis(Player player)
|
||||
{
|
||||
if(player.HasAnimation(_manufacturerAnim) || _manufacturerCurrentlyUsed)
|
||||
if (player.HasAnimation(_manufacturerAnim) || _manufacturerCurrentlyUsed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user