Files
reallife-gamemode/Server/Managers/DoorManager.cs
2018-12-26 12:19:49 +01:00

84 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
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.Util;
/**
* @overview Life of German Reallife - Managers BankManager (BankManager.cs)
* @author VegaZ
* @copyright (c) 2008 - 2018 Life of German
*/
namespace reallife_gamemode.Server.Managers
{
public class DoorManager : Script
{
private static Dictionary<int, NetHandle> _doorColShapes = new Dictionary<int, NetHandle>();
public static void LoadDoors()
{
using (var dbContext = new DatabaseContext())
{
foreach(Door door in dbContext.Doors)
{
_doorColShapes[door.Id] = NAPI.ColShape.CreateSphereColShape(door.Position, 30f);
}
}
}
public static void ReloadDoors()
{
_doorColShapes.Clear();
LoadDoors();
}
public static void ChangeDoorState(Client player)
{
var user = player.GetUser();
using (var dbContext = new DatabaseContext())
{
IQueryable<Door> NearDoors = dbContext.Doors.Where(d => d.Position.DistanceTo(player.Position) <= d.Radius);
foreach (Door d in NearDoors)
{
if(!user.IsAdmin(AdminLevel.ADMIN) && (d.FactionId != user.FactionId || d.FactionId == null))
{
string lockState = "~r~Du hast kein Recht diese T\u00fcr " + (d.Locked == true ? "auf" : "ab") + "zuschlie\u00dfen!";
player.SendNotification(lockState, true);
continue;
}
d.Locked = !d.Locked;
string notStr = d.Name + " " + (d.Locked == false ? "~g~auf" : "~r~ab") + "geschlossen";
player.SendNotification(notStr, true);
NAPI.Pools.GetAllPlayers().ForEach(p => p.TriggerEvent("changeDoorState", d.Model, d.X, d.Y, d.Z, (d.Locked ? 1 : 0), 0.0f, 0.0f, 0.0f));
}
dbContext.SaveChanges();
}
}
[ServerEvent(Event.PlayerEnterColshape)]
public void DoorManagerPlayerEnterColShapeEvent(ColShape colShape, Client player)
{
if(_doorColShapes.ContainsValue(colShape.Handle))
{
int doorId = _doorColShapes.Where(d => d.Value.Value == colShape.Handle.Value).FirstOrDefault().Key;
NAPI.Util.ConsoleOutput("entered door colshape id = " + doorId);
using(var dbContext = new DatabaseContext())
{
Door door = dbContext.Doors.Where(d => d.Id == doorId).First();
player.TriggerEvent("changeDoorState", door.Model, door.X, door.Y, door.Z, (door.Locked ? 1 : 0), 0.0f, 0.0f, 0.0f);
}
}
}
}
}