From 0456aaa884f36bf3a5181f37b29a72af0db39c05 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 10 May 2020 16:06:54 +0300 Subject: Transferred game event logs to MySQL database (database Mundus, table GameEventLogs) using Entity Framework Core. --- Mundus/Data/Crafting/CraftingTableContext.cs | 3 +- Mundus/Data/DataBaseContext.cs | 15 ++++++++ Mundus/Data/GameEventLogContext.cs | 43 ++++++++++++++++++++++ Mundus/Data/Log.cs | 10 ----- Mundus/Mundus.csproj | 4 +- Mundus/Program.cs | 13 +------ Mundus/Service/GameEventLog.cs | 16 ++++++++ Mundus/Service/LogController.cs | 10 ++--- .../Service/Tiles/Crafting/CraftingController.cs | 3 +- 9 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 Mundus/Data/DataBaseContext.cs create mode 100644 Mundus/Data/GameEventLogContext.cs delete mode 100644 Mundus/Data/Log.cs create mode 100644 Mundus/Service/GameEventLog.cs diff --git a/Mundus/Data/Crafting/CraftingTableContext.cs b/Mundus/Data/Crafting/CraftingTableContext.cs index 0a8c6c1..760131a 100644 --- a/Mundus/Data/Crafting/CraftingTableContext.cs +++ b/Mundus/Data/Crafting/CraftingTableContext.cs @@ -6,11 +6,10 @@ using Mundus.Service.Tiles.Items.Presets; namespace Mundus.Data.Crafting { public class CraftingTableContext : DbContext { - public DbSet CraftingRecipes { get; set; } + public DbSet CraftingRecipes { get; private set; } public CraftingTableContext() : base() { } - public void AddRecipes() { ResetTable(); diff --git a/Mundus/Data/DataBaseContext.cs b/Mundus/Data/DataBaseContext.cs new file mode 100644 index 0000000..5e9076a --- /dev/null +++ b/Mundus/Data/DataBaseContext.cs @@ -0,0 +1,15 @@ +using System; +using Mundus.Data.Crafting; + +namespace Mundus.Data { + public static class DataBaseContext { + public static CraftingTableContext CTContext { get; private set; } + public static GameEventLogContext GELContext { get; private set; } + + public static void CreateInstances() { + CTContext = new CraftingTableContext(); + CTContext.AddRecipes(); + GELContext = new GameEventLogContext(); + } + } +} diff --git a/Mundus/Data/GameEventLogContext.cs b/Mundus/Data/GameEventLogContext.cs new file mode 100644 index 0000000..be1fe53 --- /dev/null +++ b/Mundus/Data/GameEventLogContext.cs @@ -0,0 +1,43 @@ +using System; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using Mundus.Service; + +namespace Mundus.Data { + public class GameEventLogContext : DbContext { + public DbSet GameEventLogs { get; private set; } + + public GameEventLogContext() :base() + { + ResetTable(); + } + + private void ResetTable() { + GameEventLogs.RemoveRange(GameEventLogs); + this.SaveChanges(); + } + + public void AddMessage(string message) { + GameEventLogs.Add(new GameEventLog(message)); + this.SaveChanges(); + } + + public string GetMessage(int id) { + return GameEventLogs.Single(x => x.ID == id).Message; + } + + public int GetCount() { + return GameEventLogs.Count(); + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + optionsBuilder.UseMySQL( + "server=localhost;" + + "port=3306;" + + "user id=root; " + + "password=password; " + + "database=Mundus; " + + "SslMode=none"); + } + } +} diff --git a/Mundus/Data/Log.cs b/Mundus/Data/Log.cs deleted file mode 100644 index 0da7e92..0000000 --- a/Mundus/Data/Log.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Mundus.Data { - public static class Log { - public static List LogMessages { get; set; } - - - } -} diff --git a/Mundus/Mundus.csproj b/Mundus/Mundus.csproj index 1a23716..46985db 100644 --- a/Mundus/Mundus.csproj +++ b/Mundus/Mundus.csproj @@ -267,7 +267,6 @@ - @@ -293,6 +292,9 @@ + + + diff --git a/Mundus/Program.cs b/Mundus/Program.cs index 48992b5..2750469 100644 --- a/Mundus/Program.cs +++ b/Mundus/Program.cs @@ -1,28 +1,19 @@ using Gtk; +using Mundus.Data; using Mundus.Data.Crafting; using Mundus.Data.Dialogues; using Mundus.Data.Superlayers.Mobs; using Mundus.Data.SuperLayers; using Mundus.Data.Windows; -using Mundus.Data; -using Mundus.Service; -using Mundus.Service.Tiles.Crafting; -using Mundus.Service.Tiles.Items.Presets; namespace Mundus { public static class MainClass { public static bool runGame = true; - public static CraftingTableContext CTController { get; private set; } public static void Main(string[] args) { - //DBController = new DatabaseController(); - //Data.Crafting.CraftingTableContext.CreateRecipes(); - CTController = new CraftingTableContext(); - CTController.AddRecipes(); - + DataBaseContext.CreateInstances(); Application.Init(); //All windows and dialogues that are used by user (instances) are saved and created in WindowInstances.cs - LogController.Initialize(); WI.CreateInstances(); DI.CreateInstances(); LI.CreateInstances(); diff --git a/Mundus/Service/GameEventLog.cs b/Mundus/Service/GameEventLog.cs new file mode 100644 index 0000000..f718e82 --- /dev/null +++ b/Mundus/Service/GameEventLog.cs @@ -0,0 +1,16 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Mundus.Service { + [Table("GameEventLogs", Schema = "Mundus")] + public class GameEventLog { + [Key] + public int ID { get; private set; } + public string Message { get; private set; } + + public GameEventLog(string message) { + this.Message = message; + } + } +} diff --git a/Mundus/Service/LogController.cs b/Mundus/Service/LogController.cs index ca4feb4..07a0f3a 100644 --- a/Mundus/Service/LogController.cs +++ b/Mundus/Service/LogController.cs @@ -4,20 +4,16 @@ using Mundus.Data; namespace Mundus.Service { public static class LogController { - public static void Initialize() { - Log.LogMessages = new List(); - } - public static void AddMessage(string logMessage) { - Log.LogMessages.Add(logMessage); + DataBaseContext.GELContext.AddMessage(logMessage); } public static string GetMessagage(int index) { - return (0 <= index && index < GetCount()) ? Log.LogMessages[index] : null; + return (0 <= index && index < GetCount()) ? DataBaseContext.GELContext.GetMessage(index + 1) : null; } public static int GetCount() { - return Log.LogMessages.Count; + return DataBaseContext.GELContext.GetCount(); } } } \ No newline at end of file diff --git a/Mundus/Service/Tiles/Crafting/CraftingController.cs b/Mundus/Service/Tiles/Crafting/CraftingController.cs index aedaa3a..8a61c90 100644 --- a/Mundus/Service/Tiles/Crafting/CraftingController.cs +++ b/Mundus/Service/Tiles/Crafting/CraftingController.cs @@ -5,6 +5,7 @@ using Mundus.Data.Superlayers.Mobs; using Mundus.Service.Tiles.Mobs; using Mundus.Service.Tiles.Items; using Mundus.Service.Tiles.Items.Presets; +using Mundus.Data; namespace Mundus.Service.Tiles.Crafting { public static class CraftingController { @@ -13,7 +14,7 @@ namespace Mundus.Service.Tiles.Crafting { /// /// All avalable recipies. public static CraftingRecipe[] GetAvalableRecipes() { - return MainClass.CTController.GetAvalableRecipes(); + return DataBaseContext.CTContext.GetAvalableRecipes(); } /// -- cgit v1.2.3