From ad6e5a13f57fe6ff2b0ca50fa44815c2e6510baa Mon Sep 17 00:00:00 2001 From: hydrant Date: Thu, 22 Nov 2018 18:41:40 +0100 Subject: [PATCH] outsourced database initialization, faction bank accounts get added if none exist --- Main.cs | 10 +++------- Server/Util/DatabaseHelper.cs | 22 +++++++++++++++++++++ Server/Util/FactionHelper.cs | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 Server/Util/DatabaseHelper.cs create mode 100644 Server/Util/FactionHelper.cs diff --git a/Main.cs b/Main.cs index 0aae6bbc..875f1932 100644 --- a/Main.cs +++ b/Main.cs @@ -6,6 +6,7 @@ using reallife_gamemode.Model; using reallife_gamemode.Server.Entities; using reallife_gamemode.Server.Managers; using reallife_gamemode.Server.Saves; +using reallife_gamemode.Server.Util; /** * @overview Life of German Reallife - Main Class (Main.cs) @@ -33,13 +34,8 @@ namespace reallife_gamemode TuningManager.AddTuningGarage(new Vector3(-341, -134, 38.5)); - using (var context = new DatabaseContext()) - { - context.Bans.FirstOrDefault(); - context.Factions.FirstOrDefault(); - context.Users.FirstOrDefault(); - context.SaveChanges(); - } + DatabaseHelper.InitDatabaseFirstTime(); + FactionHelper.CheckFactionBankAccounts(); TempBlip tempBlip = new TempBlip() { diff --git a/Server/Util/DatabaseHelper.cs b/Server/Util/DatabaseHelper.cs new file mode 100644 index 00000000..e049fcee --- /dev/null +++ b/Server/Util/DatabaseHelper.cs @@ -0,0 +1,22 @@ +using GTANetworkAPI; +using reallife_gamemode.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace reallife_gamemode.Server.Util +{ + class DatabaseHelper + { + public static void InitDatabaseFirstTime() + { + NAPI.Util.ConsoleOutput("Initializing database..."); + using(var dbContext = new DatabaseContext()) + { + dbContext.Users.First(); + dbContext.SaveChanges(); + } + } + } +} diff --git a/Server/Util/FactionHelper.cs b/Server/Util/FactionHelper.cs new file mode 100644 index 00000000..daf93237 --- /dev/null +++ b/Server/Util/FactionHelper.cs @@ -0,0 +1,37 @@ +using GTANetworkAPI; +using reallife_gamemode.Model; +using reallife_gamemode.Server.Entities; +using System; +using System.Collections.Generic; +using System.Text; + +namespace reallife_gamemode.Server.Util +{ + class FactionHelper + { + public static void CheckFactionBankAccounts() + { + NAPI.Util.ConsoleOutput("Checking faction bank accounts..."); + using(var dbContext = new DatabaseContext()) + { + foreach(Faction faction in dbContext.Factions) + { + if(faction.GetBankAccount(dbContext) == null) + { + NAPI.Util.ConsoleOutput("Adding bank account for faction: " + faction.Name); + FactionBankAccount factionBankAccount = new FactionBankAccount() + { + Balance = 0, + Bic = "", + Iban = "", + FactionId = faction.Id, + Active = true + }; + dbContext.FactionBankAccounts.Add(factionBankAccount); + } + } + dbContext.SaveChanges(); + } + } + } +}