Files
reallife-gamemode/ReallifeGamemode.Server/Bank/bank.cs
2021-07-05 11:33:04 +02:00

243 lines
8.8 KiB
C#

/**
* @overview Life of German Reallife - Managers Interaction (InteractionManager.cs)
* @author MichaPlays
* @copyright (c) 2008 - 2021 Life of German
*/
using GTANetworkAPI;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Finance;
using ReallifeGamemode.Server.Log;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Services;
using System;
namespace ReallifeGamemode.Server.Bank
{
class Bank : Script
{
private static TextLabel informationLabel;
private static TextLabel factionInformationLabel;
private static Marker marker;
private static Marker factionMarker;
private static ColShape _colShape;
private static ColShape _factioncolShape;
public static Vector3 Position { get; }
private static readonly ILogger logger = LogManager.GetLogger<Bank>();
public static void Setup()
{
informationLabel = NAPI.TextLabel.CreateTextLabel("Bank", new Vector3(246.48, 223.01, 106.28), 20.0f, 1.3f, 0, new Color(255, 255, 255));
marker = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, new Vector3(246.48, 223.01, 105.28), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255));
factionInformationLabel = NAPI.TextLabel.CreateTextLabel("Fraktionskasse", new Vector3(251.61, 221.32, 106.28), 20.0f, 1.3f, 0, new Color(255, 255, 255));
factionMarker = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, new Vector3(251.61, 221.32, 105.28), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255));
_colShape = NAPI.ColShape.CreateSphereColShape(new Vector3(246.48, 223.01, 105.28), 2f);
_colShape.OnEntityEnterColShape += EntityEnterBankColShape;
_colShape.OnEntityExitColShape += EntityExitBankColShape;
_factioncolShape = NAPI.ColShape.CreateSphereColShape(new Vector3(251.61, 221.32, 105.28), 2f);
_factioncolShape.OnEntityEnterColShape += EntityEnterFactionBankColShape;
_factioncolShape.OnEntityExitColShape += EntityExitFactionBankColShape;
NAPI.Blip.CreateBlip(207, new Vector3(231.35, 214.98, 106.27), 1.0f, 2, "Bank", shortRange: true);
}
private static void EntityEnterBankColShape(ColShape colShape, Player client)
{
if (client.IsInVehicle || !client.IsLoggedIn()) return;
client.TriggerEvent("showBankMenu");
}
private static void EntityExitBankColShape(ColShape colShape, Player client)
{
client.TriggerEvent("removeBankmenu");
}
private static void EntityEnterFactionBankColShape(ColShape colShape, Player client)
{
if (client.IsInVehicle || !client.IsLoggedIn() || client.GetUser().FactionId is null) return;
User user = client.GetUser();
if (user.FactionLeader == true)
{
client.TriggerEvent("showFactionBankMenuLeader", user.Faction.BankAccount.Balance.ToString());
return;
}
client.TriggerEvent("showFactionBankMenu", user.Faction.BankAccount.Balance.ToString());
}
private static void EntityExitFactionBankColShape(ColShape colShape, Player client)
{
client.TriggerEvent("removeFactionBankmenu");
}
[RemoteEvent("CLIENT:payIn")]
public void bank_payIn(Player player, string stringAmount)
{
int amount = Int32.Parse(stringAmount);
using (var dbContext = new DatabaseContext())
{
if (amount <= 0 || amount > player.GetUser(dbContext).Handmoney)
{
NotificationService.SendErrorNotification(player, "Dieser Betrag ist ungültig");
return;
}
else
{
logger.LogInformation("Player {0} did a deposit of {1} dollars at the bank", player.Name, amount);
player.SendNotification($"~w~Du hast $~g~{amount} ~w~eingezahlt.");
player.GetUser(dbContext).Handmoney -= amount;
player.GetUser(dbContext).BankAccount.Balance += amount;
dbContext.SaveChanges();
}
}
}
[RemoteEvent("CLIENT:payOut")]
public void bank_payOut(Player player, string stringAmount)
{
int amount = Int32.Parse(stringAmount);
using (var dbContext = new DatabaseContext())
{
if (amount <= 0 || amount > player.GetUser(dbContext).BankAccount.Balance)
{
NotificationService.SendErrorNotification(player, "Dieser Betrag ist ungültig");
return;
}
else
{
logger.LogInformation("Player {0} did a withdraw of {1} dollars at the bank", player.Name, amount);
player.SendNotification($"~w~Du hast $~g~{amount} ~w~abgehoben.");
player.GetUser(dbContext).Handmoney += amount;
player.GetUser(dbContext).BankAccount.Balance -= amount;
dbContext.SaveChanges();
}
}
}
[RemoteEvent("CLIENT:payment")]
public void bank_payment(Player player, string jsonNameOrId, string stringAmount)
{
string nameOrId = (string)JsonConvert.DeserializeObject(jsonNameOrId);
int amount = Int32.Parse(stringAmount);
Player target = PlayerService.GetPlayerByNameOrId(nameOrId);
if (!target.IsLoggedIn())
{
NotificationService.SendErrorNotification(player, "Dieser Spieler ist nicht online");
return;
}
using (var dbContext = new DatabaseContext())
{
User user = player.GetUser(dbContext);
if (user.PlayedMinutes < 180)
{
NotificationService.SendErrorNotification(player, "Du kannst Geld erst ab drei Spielstunden vergeben");
return;
}
User targetUser = target.GetUser(dbContext);
if (user == targetUser)
{
NotificationService.SendErrorNotification(player, "Du kannst dir selber kein Geld überweisen");
return;
}
else if (amount <= 0 || (int)(amount * 1.05) > user.BankAccount.Balance)
{
NotificationService.SendErrorNotification(player, "Dieser Betrag kann nicht überwiesen werden");
return;
}
else
{
logger.LogInformation("Player {0} did a transfer of {1} dollars to {2}", user.Name, amount, targetUser.Name);
player.SendNotification($"~w~Du hast {target.Name} $~g~{amount} ~w~Überwiesen.");
target.SendNotification($"~w~{player.Name} hat dir $~g~{amount} ~w~Überwiesen.");
user.BankAccount.Balance -= (int)(amount * 1.05);
targetUser.otheramount += amount;
dbContext.SaveChanges();
}
}
}
[RemoteEvent("CLIENT:factionPayIn")]
public void bank_factionPayIn(Player player, string stringAmount)
{
int amount = Int32.Parse(stringAmount);
using (var dbContext = new DatabaseContext())
{
User user = player.GetUser(dbContext);
if (user.FactionId == null)
{
return;
}
if (amount <= 0 || amount > user.BankAccount.Balance)
{
NotificationService.SendErrorNotification(player, "Dieser Betrag ist ungültig");
return;
}
else
{
logger.LogInformation("Player {0} did a faction payin of {1} dollars to faction {2}", user.Name, amount, user.Faction.Id);
player.SendNotification($"~w~Du hast $~g~{amount}~w~ in die Fraktionskasse eingezahlt.");
user.BankAccount.Balance -= amount;
user.Faction.BankAccount.Balance += amount;
dbContext.SaveChanges();
}
}
}
[RemoteEvent("CLIENT:factionPayment")]
public void bank_factionPayment(Player player, string jsonNameOrId, string stringAmount)
{
string nameOrId = (string)JsonConvert.DeserializeObject(jsonNameOrId);
int amount = Int32.Parse(stringAmount);
Player target = PlayerService.GetPlayerByNameOrId(nameOrId);
if (!target.IsLoggedIn())
{
NotificationService.SendErrorNotification(player, "Dieser Spieler ist nicht online");
return;
}
using (var dbContext = new DatabaseContext())
{
User user = player.GetUser(dbContext);
User targetUser = target.GetUser(dbContext);
if (amount <= 0 || (int)(amount * 1.05) > user.Faction.BankAccount.Balance)
{
NotificationService.SendErrorNotification(player, "Dieser Betrag kann nicht überwiesen werden");
return;
}
else
{
logger.LogInformation("Player {0} did a faction transfer of {1} dollars from faction {2} to {3}", user.Name, amount, user.Faction.Id, target.Name);
player.SendNotification($"Du hast ~g~{amount.ToMoneyString()}~s~ an ~y~{targetUser.Name}~s~ überwiesen");
target.SendNotification($"Dir wurden ~g~{amount.ToMoneyString()}~s~ von ~y~{user.Name}~s~ überwiesen");
user.Faction.BankAccount.Balance -= (int)(amount * 1.05);
targetUser.otheramount += amount;
dbContext.SaveChanges();
}
}
}
}
}