using GTANetworkAPI;
using Microsoft.EntityFrameworkCore;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Models;
using System;
using System.Linq;
/**
* @overview Life of German Reallife - Client Extension (ClientExtension.cs)
* @author hydrant
* @copyright (c) 2008 - 2018 Life of German
*/
namespace ReallifeGamemode.Server.Extensions
{
public static class ClientExtension
{
///
/// Gibt das User-Objekt eines Client's zurück.
/// Gibt nichts zurück, wenn der Client nicht eingeloggt ist
///
/// Der Client, dessen User man bekommen möchte
/// Ein eventuell vorhandener Datenbank-Context, falls man Änderungen in der Datenbank vornehmen will
///
public static User GetUser(this Client client, DatabaseContext context = null)
{
if (!client.IsLoggedIn()) return null;
if(context == null)
{
using (context = new DatabaseContext())
{
return context
.Users
.Include(u => u.Faction)
.Include(u => u.FactionRank)
.FirstOrDefault(u => u.Name == client.Name);
}
}
else
{
return context
.Users
.Include(u => u.Faction)
.Include(u => u.FactionRank)
.FirstOrDefault(u => u.Name == client.Name);
}
}
public static Character GetCharacter(this User user, DatabaseContext context = null)
{
if (context == null)
{
using (context = new DatabaseContext())
{
return context.Characters.FirstOrDefault(u => u.UserId == user.Id);
}
}
else
{
return context.Characters.FirstOrDefault(u => u.UserId == user.Id);
}
}
///
/// Gibt zurück, ob ein Client eingeloggt ist
///
/// Der Client, dessen Login-Status man bekommen möchte
///
public static bool IsLoggedIn(this Client player)
{
return player.HasData("isLoggedIn") ? player.GetData("isLoggedIn") : false;
}
public static Vector3 GetPositionFromPlayer(Client player, float distance, int offset = 0)
{
var pos = player.Position;
var a = player.Heading + offset;
var rad = a * Math.PI / 180;
var newpos = new Vector3(pos.X + (distance * Math.Sin(-rad)),
pos.Y + (distance * Math.Cos(-rad)),
pos.Z);
return newpos;
}
}
}