Files
reallife-gamemode/Server/Business/BusinessBase.cs
2018-11-22 22:19:34 +01:00

81 lines
2.5 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: ~g~" + (GetBankAccount()?.Balance ?? 0) + "$";
_informationLabel.Text = infoText;
}
public User GetOwner(DatabaseContext dbContext = null)
{
if(dbContext == null)
{
using (dbContext = new DatabaseContext())
{
return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
}
}
else
{
return dbContext.Users.FirstOrDefault(u => u.BusinessId == Id);
}
}
public abstract void Load();
}
}