Continued business system

This commit is contained in:
hydrant
2018-11-18 22:21:02 +01:00
parent 52d8a74498
commit 53b9acab8b
8 changed files with 89 additions and 15 deletions

View File

@@ -1,23 +1,21 @@
using reallife_gamemode.Model;
using GTANetworkAPI;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
namespace reallife_gamemode.Server.Entities
namespace reallife_gamemode.Server.Business
{
public class Business : IBankAccountOwner
public abstract class BusinessBase : IBankAccountOwner
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public abstract int Id { get; }
public abstract string Name { get; }
public string Name { get; set; }
public abstract Vector3 Position { get; }
public IBankAccount GetBankAccount (DatabaseContext databaseContext = null)
public IBankAccount GetBankAccount(DatabaseContext databaseContext = null)
{
if (databaseContext == null)
{
@@ -31,5 +29,7 @@ namespace reallife_gamemode.Server.Entities
return databaseContext.BusinessBankAccounts.FirstOrDefault(u => u.BusinessId == this.Id);
}
}
public abstract void Load();
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using GTANetworkAPI;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Util;
namespace reallife_gamemode.Server.Business
{
class TestBusiness : BusinessBase
{
public override int Id => 1;
public override string Name => "Test Business";
public override Vector3 Position => throw new NotImplementedException();
public override void Load()
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,4 +1,5 @@
using reallife_gamemode.Server.Util;
using reallife_gamemode.Server.Business;
using reallife_gamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
@@ -14,8 +15,8 @@ namespace reallife_gamemode.Server.Entities
public int Id { get; set; }
public int Balance { get; set; }
[ForeignKey("Business")]
public int BusinessId { get; set; }
public Business Business { get; set; }
[NotMapped]
public BusinessBase Business { get; set; }
}
}

View File

@@ -16,7 +16,7 @@ using reallife_gamemode.Server.Util;
namespace reallife_gamemode.Server.Managers
{
public class BankManager : Script
public class BankManager
{
public static TransactionResult TransferMoney(IBankAccountOwner sender, IBankAccountOwner receiver, int amount, string origin)
{

View File

@@ -0,0 +1,41 @@
using GTANetworkAPI;
using reallife_gamemode.Server.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace reallife_gamemode.Server.Managers
{
class BusinessManager : Script
{
private static List<BusinessBase> businesses;
public static void LoadBusinesses()
{
businesses = new List<BusinessBase>();
IEnumerable<Type> allTypes = Assembly.GetExecutingAssembly().GetTypes().Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(BusinessBase)));
foreach (Type item in allTypes)
{
NAPI.Util.ConsoleOutput($"Loading Business {item.Name}");
if (Activator.CreateInstance(item) is BusinessBase o)
{
if (businesses.Find(b => b.GetType() == item) != null)
{
throw new InvalidOperationException($"Double Business found: {o.Id} | {o.Name}");
}
businesses.Add(o);
o.Load();
}
}
}
public static T GetBusiness<T>() where T : BusinessBase
{
return (T)businesses.Find(b => b.GetType() == typeof(T));
}
}
}