feat: db models

This commit is contained in:
2025-09-25 22:29:37 +03:00
parent ee94ffa373
commit 8bc18c25b5
14 changed files with 271 additions and 3 deletions

View File

@@ -1,16 +1,133 @@
using GamificationService.Models.Database;
using Microsoft.EntityFrameworkCore;
namespace GamificationService.Database;
public class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
{
}
{}
public required DbSet<Rank> Ranks { get; set; }
public required DbSet<Skill> Skills { get; set; }
public required DbSet<Player> Players { get; set; }
public required DbSet<MissionCategory> MissionCategories { get; set; }
public required DbSet<Mission> Missions { get; set; }
public required DbSet<MissionRankRule> MissionRankRules { get; set; }
public required DbSet<MissionSkillReward> MissionSkillRewards { get; set; }
public required DbSet<MissionItemReward> MissionItemRewards { get; set; }
public required DbSet<RankMissionRule> RankMissionRules { get; set; }
public required DbSet<RankSkillRule> RankSkillRules { get; set; }
public required DbSet<PlayerSkill> PlayerSkills { get; set; }
public required DbSet<PlayerMission> PlayerMissions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Player configurations
modelBuilder.Entity<Player>()
.HasIndex(p => p.UserId)
.IsUnique();
modelBuilder.Entity<Player>()
.HasOne(p => p.Rank)
.WithMany(r => r.Players)
.HasForeignKey(p => p.RankId);
// Rank configurations
modelBuilder.Entity<Rank>()
.HasIndex(r => r.ExpNeeded)
.IsUnique();
modelBuilder.Entity<Rank>()
.HasIndex(r => r.Title)
.IsUnique();
// Skill configurations
modelBuilder.Entity<Skill>()
.HasIndex(s => s.Title)
.IsUnique();
// MissionCategory configurations
modelBuilder.Entity<MissionCategory>()
.HasIndex(mc => mc.Title)
.IsUnique();
// Mission configurations
modelBuilder.Entity<Mission>()
.HasOne(m => m.MissionCategory)
.WithMany(mc => mc.Missions)
.HasForeignKey(m => m.MissionCategoryId)
.IsRequired();
modelBuilder.Entity<Mission>()
.HasOne(m => m.ParentMission)
.WithMany(m => m.ChildMissions)
.HasForeignKey(m => m.ParentMissionId)
.IsRequired(false);
// MissionRankRule configurations
modelBuilder.Entity<MissionRankRule>()
.HasOne(mrr => mrr.Mission)
.WithMany(m => m.MissionRankRules)
.HasForeignKey(mrr => mrr.MissionId);
modelBuilder.Entity<MissionRankRule>()
.HasOne(mrr => mrr.Rank)
.WithMany(r => r.MissionRankRules)
.HasForeignKey(mrr => mrr.RankId);
// MissionSkillReward configurations
modelBuilder.Entity<MissionSkillReward>()
.HasOne(msr => msr.Mission)
.WithMany(m => m.MissionSkillRewards)
.HasForeignKey(msr => msr.MissionId);
modelBuilder.Entity<MissionSkillReward>()
.HasOne(msr => msr.Skill)
.WithMany(s => s.MissionSkillRewards)
.HasForeignKey(msr => msr.SkillId);
// MissionItemReward configurations
modelBuilder.Entity<MissionItemReward>()
.HasOne(mir => mir.Mission)
.WithMany(m => m.MissionItemRewards)
.HasForeignKey(mir => mir.MissionId);
// RankMissionRule configurations
modelBuilder.Entity<RankMissionRule>()
.HasOne(rmr => rmr.Rank)
.WithMany(r => r.RankMissionRules)
.HasForeignKey(rmr => rmr.RankId);
modelBuilder.Entity<RankMissionRule>()
.HasOne(rmr => rmr.Mission)
.WithMany(m => m.RankMissionRules)
.HasForeignKey(rmr => rmr.MissionId);
// RankSkillRule configurations
modelBuilder.Entity<RankSkillRule>()
.HasOne(rsr => rsr.Rank)
.WithMany(r => r.RankSkillRules)
.HasForeignKey(rsr => rsr.RankId);
modelBuilder.Entity<RankSkillRule>()
.HasOne(rsr => rsr.Skill)
.WithMany(s => s.RankSkillRules)
.HasForeignKey(rsr => rsr.SkillId);
// PlayerSkill configurations
modelBuilder.Entity<PlayerSkill>()
.HasOne(ps => ps.Player)
.WithMany(p => p.PlayerSkills)
.HasForeignKey(ps => ps.PlayerId);
modelBuilder.Entity<PlayerSkill>()
.HasOne(ps => ps.Skill)
.WithMany(s => s.PlayerSkills)
.HasForeignKey(ps => ps.SkillId);
// PlayerMission configurations
modelBuilder.Entity<PlayerMission>()
.HasOne(pm => pm.Player)
.WithMany(p => p.PlayerMissions)
.HasForeignKey(pm => pm.PlayerId);
modelBuilder.Entity<PlayerMission>()
.HasOne(pm => pm.Mission)
.WithMany(m => m.PlayerMissions)
.HasForeignKey(pm => pm.MissionId);
}
}

View File

@@ -0,0 +1,8 @@
namespace GamificationService.Enums.Database;
public enum MissionRankRuleType
{
EqualOrLower = 0,
Equal = 1,
EqualOrHigher = 3
}

View File

@@ -0,0 +1,21 @@
namespace GamificationService.Models.Database;
public class Mission
{
public long Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public MissionCategory? MissionCategory { get; set; }
public long MissionCategoryId { get; set; }
public Mission? ParentMission { get; set; }
public long ParentMissionId { get; set; }
public int ExpReward { get; set; }
public int ManaReward { get; set; }
public ICollection<Mission> ChildMissions { get; set; } = new List<Mission>();
public ICollection<PlayerMission> PlayerMissions { get; set; } = new List<PlayerMission>();
public ICollection<MissionItemReward> MissionItemRewards { get; set; } = new List<MissionItemReward>();
public ICollection<MissionSkillReward> MissionSkillRewards { get; set; } = new List<MissionSkillReward>();
public ICollection<MissionRankRule> MissionRankRules { get; set; } = new List<MissionRankRule>();
public ICollection<RankMissionRule> RankMissionRules { get; set; } = new List<RankMissionRule>();
}

View File

@@ -0,0 +1,9 @@
namespace GamificationService.Models.Database;
public class MissionCategory
{
public long Id { get; set; }
public string Title { get; set; } = string.Empty;
public ICollection<Mission> Missions { get; set; } = new List<Mission>();
}

View File

@@ -0,0 +1,9 @@
namespace GamificationService.Models.Database;
public class MissionItemReward
{
public long Id { get; set; }
public long ItemId { get; set; }
public long MissionId { get; set; }
public required Mission Mission { get; set; }
}

View File

@@ -0,0 +1,13 @@
using GamificationService.Enums.Database;
namespace GamificationService.Models.Database;
public class MissionRankRule
{
public long Id { get; set; }
public long MissionId { get; set; }
public Mission Mission { get; set; } = null!;
public long RankId { get; set; }
public Rank Rank { get; set; } = null!;
public MissionRankRuleType Type { get; set; }
}

View File

@@ -0,0 +1,11 @@
namespace GamificationService.Models.Database;
public class MissionSkillReward
{
public long Id { get; set; }
public long MissionId { get; set; }
public Mission Mission { get; set; } = null!;
public long SkillId { get; set; }
public Skill Skill { get; set; } = null!;
public int Value { get; set; }
}

13
Models/Database/Player.cs Normal file
View File

@@ -0,0 +1,13 @@
namespace GamificationService.Models.Database;
public class Player
{
public long Id { get; set; }
public long UserId { get; set; }
public Rank? Rank { get; set; }
public long RankId {get; set; }
public int Experience { get; set; }
public ICollection<PlayerMission> PlayerMissions { get; set; } = new List<PlayerMission>();
public ICollection<PlayerSkill> PlayerSkills { get; set; } = new List<PlayerSkill>();
}

View File

@@ -0,0 +1,12 @@
namespace GamificationService.Models.Database;
public class PlayerMission
{
public long Id { get; set; }
public long PlayerId { get; set; }
public required Player Player { get; set; }
public long MissionId { get; set; }
public required Mission Mission { get; set; }
public DateTime? Completed { get; set; }
public DateTime? RewardsRedeemed { get; set; }
}

View File

@@ -0,0 +1,11 @@
namespace GamificationService.Models.Database;
public class PlayerSkill
{
public long Id { get; set; }
public long PlayerId { get; set; }
public Player Player { get; set; } = null!;
public long SkillId { get; set; }
public Skill Skill { get; set; } = null!;
public int Score { get; set; }
}

13
Models/Database/Rank.cs Normal file
View File

@@ -0,0 +1,13 @@
namespace GamificationService.Models.Database;
public class Rank
{
public long Id { get; set; }
public string Title { get; set; } = string.Empty;
public int ExpNeeded { get; set; }
public ICollection<Player> Players { get; set; } = new List<Player>();
public ICollection<MissionRankRule> MissionRankRules { get; set; } = new List<MissionRankRule>();
public ICollection<RankMissionRule> RankMissionRules { get; set; } = new List<RankMissionRule>();
public ICollection<RankSkillRule> RankSkillRules { get; set; } = new List<RankSkillRule>();
}

View File

@@ -0,0 +1,10 @@
namespace GamificationService.Models.Database;
public class RankMissionRule
{
public long Id { get; set; }
public long RankId { get; set; }
public Rank Rank { get; set; } = null!;
public long MissionId { get; set; }
public Mission Mission { get; set; } = null!;
}

View File

@@ -0,0 +1,11 @@
namespace GamificationService.Models.Database;
public class RankSkillRule
{
public long Id { get; set; }
public long RankId { get; set; }
public Rank Rank { get; set; } = null!;
public long SkillId { get; set; }
public Skill Skill { get; set; } = null!;
public int Min { get; set; }
}

10
Models/Database/Skill.cs Normal file
View File

@@ -0,0 +1,10 @@
namespace GamificationService.Models.Database;
public class Skill
{
public long Id { get; set; }
public string Title { get; set; } = string.Empty;
public ICollection<MissionSkillReward> MissionSkillRewards { get; set; } = new List<MissionSkillReward>();
public ICollection<RankSkillRule> RankSkillRules { get; set; } = new List<RankSkillRule>();
public ICollection<PlayerSkill> PlayerSkills { get; set; } = new List<PlayerSkill>();
}