Add Taximeter
This commit is contained in:
@@ -3,6 +3,14 @@ using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using GTANetworkAPI;
|
||||
using ReallifeGamemode.Server.Util;
|
||||
using ReallifeGamemode.Server.Services;
|
||||
using ReallifeGamemode.Server.Extensions;
|
||||
using System.Timers;
|
||||
using ReallifeGamemode.Database.Models;
|
||||
using ReallifeGamemode.Server.Managers;
|
||||
using ReallifeGamemode.Database.Entities;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace ReallifeGamemode.Server.Job
|
||||
{
|
||||
@@ -10,26 +18,109 @@ namespace ReallifeGamemode.Server.Job
|
||||
{
|
||||
public List<TaxiContract> TaxiContracts { get; set; } = new List<TaxiContract>();
|
||||
|
||||
private static readonly Dictionary<NetHandle, Vector3> lastPositions = new Dictionary<NetHandle, Vector3>();
|
||||
|
||||
public override int Id => 1;
|
||||
|
||||
public override string Name => "Taxifahrer";
|
||||
|
||||
public override bool NeedVehicleToStart => true;
|
||||
|
||||
|
||||
private static TaxiDriverJob _Instance;
|
||||
|
||||
public TaxiDriverJob()
|
||||
{
|
||||
JobStart += TaxiDriverJobJobStart;
|
||||
JobStop += TaxiDriverJobJobStop;
|
||||
}
|
||||
|
||||
public static TaxiDriverJob GetInstance()
|
||||
{
|
||||
if (_Instance == null)
|
||||
_Instance = new TaxiDriverJob();
|
||||
return _Instance;
|
||||
}
|
||||
private void TaxiDriverJobJobStart(Player player)
|
||||
{
|
||||
player.Vehicle.SetSharedData("vehicleTaxiLight", true);
|
||||
player.TriggerEvent("CLIENT:setFarePrice");
|
||||
}
|
||||
|
||||
private void TaxiDriverJobJobStop(Player player)
|
||||
{
|
||||
player.Vehicle.SetSharedData("vehicleTaxiLight", false);
|
||||
int Id = player.GetUser().Id;
|
||||
player.ResetData("DriverPrice");
|
||||
Vehicle v = player.Vehicle;
|
||||
|
||||
foreach (Player occupant in v.Occupants)
|
||||
{
|
||||
occupant.TriggerEvent("CLIENT:cancelFare");
|
||||
}
|
||||
}
|
||||
|
||||
[RemoteEvent("SERVER:failedFarePrice")]
|
||||
private void SrvEvent_failedFarePrice(Player player)
|
||||
{
|
||||
ChatService.ErrorMessage(player, "Bitte richtigen Wert eintragen");
|
||||
TaxiDriverJobJobStop(player);
|
||||
}
|
||||
|
||||
[RemoteEvent("SERVER:setFare")]
|
||||
private void SrvEvent_setFare(Player player, int amount)
|
||||
{
|
||||
player.SetData<int>("DriverPrice", amount);
|
||||
player.TriggerEvent("CLIENT:startFare");
|
||||
}
|
||||
|
||||
[RemoteEvent("SERVER:payFare")]
|
||||
private void SrvEvent_payFare(Player player, int amount, float km)
|
||||
{
|
||||
int id = player.GetUser().Id;
|
||||
player.SetData<float>("FareKm", km);
|
||||
int targetId = player.GetData<int>("Passager");
|
||||
if (targetId == 0) return;
|
||||
using (var dbContext = new DatabaseContext())
|
||||
{
|
||||
User target = dbContext.Users.Where(u => u.Id == targetId).FirstOrDefault();
|
||||
target.Handmoney -= amount;
|
||||
dbContext.SaveChanges();
|
||||
target.Player.TriggerEvent("SERVER:SET_HANDMONEY", target.Handmoney);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void StartTaxiTimer()
|
||||
{
|
||||
Timer timer = new Timer(1000);
|
||||
timer.Start();
|
||||
timer.Elapsed += UpdateFare;
|
||||
}
|
||||
|
||||
private static void UpdateFare(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
foreach(var player in GetPlayerInJob())
|
||||
{
|
||||
if (!player.HasData("hasPassager")) { player.SetData<bool>("hasPassager", false); continue; }
|
||||
int playerId = player.GetUser().Id;
|
||||
|
||||
Vehicle v = player.Vehicle;
|
||||
|
||||
Vector3 lastPosition = v.Position;
|
||||
if (lastPositions.ContainsKey(v.Handle)) lastPosition = lastPositions[v.Handle];
|
||||
lastPositions[v.Handle] = v.Position;
|
||||
|
||||
double distance = lastPosition.DistanceTo(v.Position) / 1000.0;
|
||||
if (distance > 0.5) { ChatService.BroadcastAdmin($"Möglicher Cheater - {player.Name}", Database.AdminLevel.MAPPING); continue; }
|
||||
if (!player.GetData<bool>("hasPassager")) continue;
|
||||
foreach (Player occupant in v.Occupants)
|
||||
{
|
||||
occupant.TriggerEvent("CLIENT:updateFare", JsonConvert.SerializeObject(distance));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user