Files
reallife-gamemode/ReallifeGamemode.Server/Entities/User.cs
Lennart Kampshoff a34c03eae9 start group system
2019-05-05 17:07:29 +02:00

161 lines
5.3 KiB
C#

using GTANetworkAPI;
using ReallifeGamemode.Server.Models;
using ReallifeGamemode.Server.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
/**
* @overview Life of German Reallife - Entities User (User.cs)
* @author VegaZ, hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
namespace ReallifeGamemode.Server.Entities
{
public class User : IBankAccountOwner
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[StringLength(32)]
public string Name { get; set; }
[StringLength(32)]
public string SocialClubName { get; set; }
[StringLength(64)]
public string Password { get; set; }
public int LogUserId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime RegistrationDate { get; set; }
[EmailAddress]
[StringLength(64)]
public string Email { get; set; }
public AdminLevel AdminLevel { get; set; }
public bool Dead { get; set; }
public int Handmoney { get; set; }
public float PositionX { get; set; }
public float PositionY { get; set; }
public float PositionZ { get; set; }
[ForeignKey("Character")]
public int? CharacterId { get; set; }
public Character Character { get; set; }
[ForeignKey("Ban")]
public int? BanId { get; set; }
public Ban Ban { get; set; }
public int? FactionId { get; set; }
public Faction Faction { get; set; }
public bool FactionLeader { get; set; }
public int? FactionRankId { get; set; }
public FactionRank FactionRank { get; set; }
public Group Group { get; set; }
public FactionRank GetFactionRank()
{
using (var dbContext = new DatabaseContext())
{
FactionRank toReturn = dbContext.FactionRanks.FirstOrDefault(fR => fR.Id == FactionRankId);
if (toReturn == null)
{
toReturn = dbContext.FactionRanks.OrderBy(f => f.Order).FirstOrDefault(f => f.FactionId == FactionId);
}
if (toReturn == null)
{
toReturn = new FactionRank
{
RankName = "Rang-Fehler"
};
}
return toReturn;
}
}
public int? BusinessId { get; set; }
public void BanPlayer(Client admin, string reason, int mins)
{
using (var banUserContext = new DatabaseContext())
{
int unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
Ban banUser;
if (mins == 0)
{
NAPI.Chat.SendChatMessageToAll("!{#FF4040}[BAN] " + this.Name + " wurde von " + admin.Name + " permanent gebannt. [" + reason + "]");
banUser = new Ban { UserId = this.Id, Reason = reason, BannedBy = admin.Name, Applied = unixTimestamp, UntilDateTime = unixTimestamp };
this.Client?.Kick();
mins--;
}
else
{
NAPI.Chat.SendChatMessageToAll("!{#FF4040}[BAN] " + this.Name + " wurde von " + admin.Name + " für " + mins + " Minuten gebannt. [" + reason + "]");
banUser = new Ban { UserId = this.Id, Reason = reason, BannedBy = admin.Name, Applied = unixTimestamp, UntilDateTime = unixTimestamp + mins * 60 };
this.Client?.Kick();
}
banUserContext.Bans.Add(banUser);
banUserContext.SaveChanges();
var targetUser = banUserContext.Users.FirstOrDefault(u => u.Name == this.Name);
targetUser.BanId = banUser.Id;
banUserContext.SaveChanges();
}
}
public void UnbanPlayer()
{
using (var unbanUser = new DatabaseContext())
{
var targetUser = unbanUser.Users.FirstOrDefault(u => u.Id == this.Id);
targetUser.BanId = null;
unbanUser.SaveChanges();
}
}
public List<UserItem> GetItems()
{
using(var dbContext = new DatabaseContext())
{
return dbContext.UserItems.ToList().FindAll(u => u.UserId == this.Id);
}
}
public bool IsAdmin(AdminLevel level) => AdminLevel >= level;
public IBankAccount GetBankAccount(DatabaseContext databaseContext = null)
{
if (databaseContext == null)
{
using (databaseContext = new DatabaseContext())
{
return databaseContext.UserBankAccounts.FirstOrDefault(u => u.UserId == this.Id);
}
}
else
{
return databaseContext.UserBankAccounts.FirstOrDefault(u => u.UserId == this.Id);
}
}
[NotMapped]
public Client Client
{
get => NAPI.Player.GetPlayerFromName(this.Name);
}
}
}