Files
reallife-gamemode/Server/Business/BusinessBase.cs

75 lines
2.3 KiB
C#

using GTANetworkAPI;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Entities;
using reallife_gamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace reallife_gamemode.Server.Business
{
public abstract class BusinessBase : IBankAccountOwner
{
private TextLabel _informationLabel;
public abstract int Id { get; }
public abstract string Name { get; }
public abstract Vector3 Position { get; }
public IBankAccount GetBankAccount(DatabaseContext databaseContext = null)
{
if (databaseContext == null)
{
using (databaseContext = new DatabaseContext())
{
return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == Id);
}
}
else
{
return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == Id);
}
}
public void Setup()
{
_informationLabel = NAPI.TextLabel.CreateTextLabel(Name, Position, 20.0f, 1.3f, 0, new Color(255, 255, 255));
if (GetBankAccount() == null)
{
NAPI.Util.ConsoleOutput("Creating Bank Account for Business: " + Name);
using (var dbContext = new DatabaseContext())
{
dbContext.BusinessBankAccounts.Add(new BusinessBankAccount()
{
BusinessId = Id,
Balance = 0
});
dbContext.SaveChanges();
}
}
}
public void Update()
{
User owner = GetOwner();
string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: " + (GetBankAccount()?.Balance ?? 0);
_informationLabel.Text = infoText;
}
public User GetOwner()
{
using (var dbContext = new DatabaseContext())
{
User user = dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
return user;
}
}
public abstract void Load();
}
}