Files
reallife-gamemode/ReallifeGamemode.Server/Finance/Economy.cs
2020-03-01 17:39:54 +01:00

233 lines
6.5 KiB
C#

/***
@overview Life of German - Economy
@author kookroach
@copyright (c) 2008 - 2019 Life of German
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GTANetworkAPI;
using Microsoft.EntityFrameworkCore;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Services;
namespace ReallifeGamemode.Server.Finance
{
public class Economy
{
public static Dictionary<int, Paycheck> Paychecks { get; set; } = new Dictionary<int, Paycheck>();
public static (int, float, float) GetEconomyClass(Player client, int wage)
{
int bankAccount = client.GetUser().GetBankAccount().Balance;
float financialHelp = -(float)Math.Pow(1.0005, -bankAccount) * -1000;
float financialInterest = 1 - (float)Math.Pow(1.00006, -wage) * 1;
if (financialInterest >= 0.7)
financialInterest = 0.7f;
return (bankAccount, financialHelp, financialInterest);
}
public static int GetVehicleTaxation(Player client)
{
int vehicleTaxation = 0;
using (var dbContext = new DatabaseContext())
{
User user = client.GetUser(dbContext);
foreach (VehicleHash vehHash in dbContext.UserVehicles.Where(u => u.UserId == user.Id).Select(u => u.Model))
{
int type = NAPI.Vehicle.GetVehicleClass(vehHash);
switch (type)
{
/*
0 Compacts
1 Sedans
2 SUVs
3 Coupes
4 Muscle
5 Sports
6 Sports
7 Super
8 Motorcycles
9 Off-Road
10 Industrial
11 Utility
12 Vans
13 Cycles
14 Boats
15 Helicopters
16 Planes
17 Service
18 Emergency
19 Military
20 Commercial
21 Trains
*/
case 0:
vehicleTaxation += 250;
break;
case 1:
vehicleTaxation += 350;
break;
case 2:
vehicleTaxation += 500;
break;
case 3:
vehicleTaxation += 400;
break;
case 4:
vehicleTaxation += 300;
break;
case 5:
vehicleTaxation += 800;
break;
case 6:
vehicleTaxation += 950;
break;
case 7:
vehicleTaxation += 0;
break;
case 8:
vehicleTaxation += 400;
break;
case 9:
vehicleTaxation += 500;
break;
case 10:
vehicleTaxation += 300;
break;
case 11:
vehicleTaxation += 0;
break;
case 12:
vehicleTaxation += 350;
break;
case 13:
vehicleTaxation += 0;
break;
case 14:
vehicleTaxation += 500;
break;
case 15:
vehicleTaxation += 650;
break;
case 16:
vehicleTaxation += 750;
break;
case 17:
vehicleTaxation += 0;
break;
case 18:
vehicleTaxation += 0;
break;
case 19:
vehicleTaxation += 0;
break;
case 20:
vehicleTaxation += 400;
break;
case 21:
vehicleTaxation += 69696969;
break;
}
}
}
return vehicleTaxation;
}
public static float GetPropertyTaxation(Player client)
{
float propertyTaxation = 0;
User user = client.GetUser();
if (user.HouseId != null)
{
propertyTaxation += user.House.Price * 0.005f;
}
return propertyTaxation;
}
public static void SetPaycheck(Player client, int wage)
{
(int bankAccount, float financialHelp, float financialInterest) = GetEconomyClass(client, wage);
float propertyTax = GetPropertyTaxation(client);
int vehicleTaxation = GetVehicleTaxation(client);
int rentalFees = GetRentalFees(client);
int amount = wage - (int)(wage * financialInterest) - vehicleTaxation - (int)propertyTax + (int)financialHelp - rentalFees;
Paycheck paycheck = new Paycheck(financialHelp, financialInterest, vehicleTaxation, propertyTax, wage, amount, rentalFees);
User user = client.GetUser();
Paychecks[user.Id] = paycheck;
ReleasePayDay(client, paycheck);
}
private static int GetRentalFees(Player client)
{
using (var dbContext = new DatabaseContext())
{
User user = client.GetUser();
return dbContext.HouseRentals.Where(h => h.UserId == user.Id).Include(h => h.House).Sum(h => h.House.RentalFee);
}
}
public static void ReleasePayDay(Player client, Paycheck paycheck)
{
using (var dbContext = new DatabaseContext())
{
client.GetUser().GetBankAccount(dbContext).Balance += paycheck.Amount;
client.GetUser(dbContext).Wage = 0;
dbContext.SaveChanges();
}
if (paycheck.Amount > 0)
ChatService.SendMessage(client, "~g~[PAYDAY]~s~ Dein Stundenumsatz beträgt + $~g~" + paycheck.Amount + "~s~.");
else
ChatService.SendMessage(client, "~g~[PAYDAY]~s~ Dein Stundenumsatz beträgt - $~r~" + paycheck.Amount + "~s~.");
}
public static void PaydayTimer()
{
System.Timers.Timer timer = new System.Timers.Timer(60000);
timer.Start();
timer.Elapsed += Timer_Elapsed;
}
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
foreach (var player in NAPI.Pools.GetAllPlayers())
{
User user = player.GetUser();
if (player.IsLoggedIn())
{
if (user.PaydayTimer <= 0)
{
Economy.SetPaycheck(player, user.Wage);
using (var dbContext = new DatabaseContext())
{
player.GetUser(dbContext).PaydayTimer = 60;
dbContext.SaveChanges();
}
return;
}
if (user.PaydayTimer > 0)
{
using (var dbContext = new DatabaseContext())
{
player.GetUser(dbContext).PaydayTimer -= 1;
dbContext.SaveChanges();
}
}
}
}
}
}
}