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

63 lines
1.9 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));
}
public void Update()
{
//NAPI.Util.ConsoleOutput("Updating Business: " + Name);
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);
//NAPI.Util.ConsoleOutput("[" + Name + "] GetOwner: " + (user?.Name ?? "null"));
return user;
}
}
public abstract void Load();
}
}