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

@@ -34,6 +34,8 @@ namespace reallife_gamemode
TuningManager.AddTuningGarage(new Vector3(-341, -134, 38.5));
BusinessManager.LoadBusinesses();
using (var context = new DatabaseContext())
{
context.Bans.FirstOrDefault();

View File

@@ -23,6 +23,7 @@ namespace reallife_gamemode.Model
public DatabaseContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
@@ -34,6 +35,10 @@ namespace reallife_gamemode.Model
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Server.Entities.BusinessBankAccount>()
.HasIndex(b => b.BusinessId)
.IsUnique(true);
}
//User
@@ -69,7 +74,6 @@ namespace reallife_gamemode.Model
public DbSet<Server.Entities.ShopVehicle> ShopVehicles { get; set; }
// Business
public DbSet<Server.Entities.Business> Businesses { get; set; }
public DbSet<Server.Entities.BusinessBankAccount> BusinessBankAccounts { get; set; }
// Control Panel

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));
}
}
}

View File

@@ -22,6 +22,9 @@
<HintPath>..\Bootstrapper.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(ConfigurationName)' == 'Debug'">
<Exec Command="del &quot;..\..\client_packages\*&quot; /Q /S&#xD;&#xA;xcopy &quot;$(OutDir)*&quot; &quot;..\..\bridge\resources\reallife-gamemode&quot; /Y /Q&#xD;&#xA;del &quot;..\..\client_packages\*.*&quot; /Q&#xD;&#xA;xcopy &quot;..\..\reallife-gamemode\reallife-gamemode\Client\*&quot; &quot;..\..\client_packages\&quot; /S /Q /Y&#xD;&#xA;" />
</Target>