Add Faction Ranks

This commit is contained in:
hydrant
2018-09-23 21:10:42 +02:00
parent bba0f7206c
commit bac725f2dc
6 changed files with 72 additions and 18 deletions

View File

@@ -35,6 +35,7 @@ namespace reallife_gamemode.Model
}
public DbSet<Server.Entities.Faction> Factions { get; set; }
public DbSet<Server.Entities.FactionRank> FactionRanks { get; set; }
public DbSet<Server.Entities.User> Users { get; set; }
}
}

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using GTANetworkAPI;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Entities;
using reallife_gamemode.Server.Extensions;
using reallife_gamemode.Server.Services;
using reallife_gamemode.Server.Util;
@@ -240,18 +241,34 @@ namespace reallife_gamemode.Server.Commands
using (var dbContext = new DatabaseContext())
{
Entities.Faction f = dbContext.Factions.FirstOrDefault(x => x.Id == faction);
if(f == null)
if(f == null && faction != 0)
{
player.SendChatMessage("~r~[FEHLER]~s~ Diese Fraktion existiert nicht (Liste: ~m~/factionlist).");
return;
}
target.GetUser(dbContext).FactionId = f.Id;
dbContext.SaveChanges();
User u = dbContext.Users.SingleOrDefault(x => x.Name == target.Name);
if(faction != 0)
{
u.FactionId = f.Id;
u.FactionRankId = dbContext.FactionRanks.
OrderBy(x => x.Order)
.FirstOrDefault(r => r.FactionId == f.Id)?.Id ?? null;
player.SendChatMessage("~b~[ADMIN]~s~ Du hast hast den Spieler ~y~" + target.Name + "~s~ administrativ in die Fraktion ~o~" + f.Name + "~s~ eingeladen.");
target.SendChatMessage("~b~[ADMIN]~s~ Du wurdest von ~y~" + player.Name + "~s~ administrativ in die Fraktion ~o~" + f.Name + "~s~ eingeladen.");
}
else
{
u.FactionId = null;
u.FactionRankId = null;
player.SendChatMessage("~b~[ADMIN]~s~ Du hast hast den Spieler ~y~" + target.Name + "~s~ administrativ aus seiner Fraktion geworfen.");
target.SendChatMessage("~b~[ADMIN]~s~ Du wurdest von ~y~" + player.Name + "~s~ administrativ aus deiner Fraktion geworfen.");
}
dbContext.SaveChanges();
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

View File

@@ -0,0 +1,30 @@
using reallife_gamemode.Model;
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
{
public class FactionRank
{
[Key]
public int Id { get; set; }
public string RankName { get; set; }
public int Order { get; set; }
public int FactionId { get; set; }
public Faction Faction { get; set; }
public Faction GetFaction()
{
using (var context = new DatabaseContext())
{
return context.Factions.FirstOrDefault(f => f.Id == FactionId);
}
}
}
}

View File

@@ -37,20 +37,25 @@ namespace reallife_gamemode.Server.Entities
public int? FactionId { get; set; }
public Faction Faction { get; set; }
public int? FactionRankId { get; set; }
public FactionRank FactionRank { get;set; }
public Faction GetFaction()
{
using(var context = new DatabaseContext())
{
return context.Factions.FirstOrDefault(f => f.Id == FactionId);
}
}
public FactionRank GetFactionRank()
{
using (var context = new DatabaseContext())
{
return context.FactionRanks.FirstOrDefault(fR => fR.Id == FactionRankId);
}
}
public bool IsAdmin(AdminLevel level) => AdminLevel >= level;
public Faction GetFaction(DatabaseContext context = null)
{
if(context == null)
{
using(context = new DatabaseContext())
{
return context.Factions.FirstOrDefault(f => f.Id == FactionId);
}
}
else
{
return context.Factions.FirstOrDefault(f => f.Id == FactionId);
}
}
}
}

View File

@@ -40,14 +40,14 @@ namespace reallife_gamemode.Server.Extensions
{
User u = client.GetUser();
if (u == null) return null;
return context.Factions.FirstOrDefault(f => f.Id == u.FactionId);
return u.GetFaction();
}
}
else
{
User u = client.GetUser();
if (u == null) return null;
return context.Factions.FirstOrDefault(f => f.Id == u.FactionId);
return u.GetFaction();
}
}
}