173 lines
5.2 KiB
C#
173 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using GTANetworkAPI;
|
|
using ReallifeGamemode.Database.Entities;
|
|
using ReallifeGamemode.Database.Models;
|
|
using ReallifeGamemode.Server.Extensions;
|
|
using ReallifeGamemode.Server.Job;
|
|
using ReallifeGamemode.Server.Types;
|
|
using ReallifeGamemode.Server.Util;
|
|
|
|
/**
|
|
* @overview Life of German Reallife - Chat Service (ChatService.cs)
|
|
* @author hydrant, xSprite
|
|
* @copyright (c) 2008 - 2018 Life of German
|
|
*/
|
|
|
|
namespace ReallifeGamemode.Server.Services
|
|
{
|
|
internal class ChatService
|
|
{
|
|
public static void NotAuthorized(Player player)
|
|
{
|
|
ChatService.ErrorMessage(player, "Du kannst diesen Befehl nicht ausführen");
|
|
}
|
|
|
|
public static void PlayerNotFound(Player player)
|
|
{
|
|
ChatService.ErrorMessage(player, "Der Spieler wurde nicht gefunden");
|
|
}
|
|
|
|
public static void ErrorMessage(Player player, string message)
|
|
{
|
|
if (message.EndsWith("."))
|
|
{
|
|
message.Substring(0, message.Length - 1);
|
|
}
|
|
|
|
ChatService.SendMessage(player, $"~r~[FEHLER]~s~ {message}~s~.");
|
|
}
|
|
|
|
public static void SendMessage(Player player, string message)
|
|
{
|
|
if (player == null) return;
|
|
if (player.GetData<bool>("isDead") == true && (!player.GetUser()?.IsAdmin(AdminLevel.SUPPORTER) ?? true)) return;
|
|
player.SendChatMessage(message);
|
|
}
|
|
|
|
public static void Broadcast(string message) => NAPI.Pools.GetAllPlayers().Where(c => c.IsLoggedIn()).ToList().ForEach(c => SendMessage(c, message));
|
|
|
|
/// <summary>
|
|
/// Sendet eine Nachricht an eine Liste von Fraktionen
|
|
/// </summary>
|
|
/// <param name="message">Die Nachricht, die gesendet werden soll</param>
|
|
/// <param name="factions">Die Liste an Fraktionen, die diese Nachricht bekommen sollen</param>
|
|
public static void BroadcastFaction(string message, List<Faction> factions, bool toAdmins = false)
|
|
{
|
|
foreach (Player c in NAPI.Pools.GetAllPlayers())
|
|
{
|
|
User user = c.GetUser();
|
|
Faction f = user?.Faction;
|
|
if (f != null && factions.Find(fT => fT.Id == f.Id) != null)
|
|
{
|
|
ChatService.SendMessage(c, message);
|
|
}
|
|
else if (user.IsAdmin(AdminLevel.ADMIN) && toAdmins)
|
|
{
|
|
ChatService.SendMessage(c, message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sendet eine Nachricht an eine Fraktion
|
|
/// </summary>
|
|
/// <param name="message">Die Nachricht, die gesendet werden soll</param>
|
|
/// <param name="f">Die Fraktion, die diese Nachricht bekommen soll</param>
|
|
public static void BroadcastFaction(string message, Faction f)
|
|
{
|
|
BroadcastFaction(message, new List<Faction>() { f });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sendet eine Nachricht an die Mitglieder der Strafverfolgungsbehörden
|
|
/// </summary>
|
|
/// <param name="message">Die Nachricht, die gesendet werden soll</param>
|
|
public static void HQMessage(string message)
|
|
{
|
|
BroadcastFaction("!{#8181E9}HQ: " + message, new List<int>() { 1, 3 });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sendet eine Nachricht an alle Spieler mit einem bestimmten Admin Level
|
|
/// </summary>
|
|
/// <param name="message">Die Nachricht, die gesendet werden soll</param>
|
|
/// <param name="minLevel">Das mindest Admin Level, das für das Erhalten dieser Nachricht benötigt wird</param>
|
|
public static void BroadcastAdmin(string message, AdminLevel minLevel, Func<Player, string> getAddInfoMessage = null, Predicate<Player> shouldSendMessage = null)
|
|
{
|
|
NAPI.Pools.GetAllPlayers().ForEach(p =>
|
|
{
|
|
if (p.GetUser()?.IsAdmin(minLevel) ?? false)
|
|
{
|
|
if(shouldSendMessage != null)
|
|
{
|
|
if(!shouldSendMessage(p))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
string toSend = message;
|
|
|
|
if(getAddInfoMessage != null)
|
|
{
|
|
toSend += getAddInfoMessage(p);
|
|
}
|
|
|
|
ChatService.SendMessage(p, toSend);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static void BroadcastDutyAdmin(string message)
|
|
{
|
|
NAPI.Pools.GetAllPlayers().ForEach(p =>
|
|
{
|
|
if (p.IsAdminDuty())
|
|
{
|
|
ChatService.SendMessage(p, message);
|
|
}
|
|
});
|
|
}
|
|
|
|
internal static void SendInRange(Vector3 position, int range, string message)
|
|
{
|
|
foreach(Player player in NAPI.Pools.GetAllPlayers())
|
|
{
|
|
if(player.Position.DistanceTo(position) <= range)
|
|
{
|
|
SendMessage(player, message);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void BroadcastGroup(string message, Group group)
|
|
{
|
|
message = $"!{{FF8080}}** Gruppe: {message}";
|
|
|
|
NAPI.Pools.GetAllPlayers().ForEach(p =>
|
|
{
|
|
Group pGroup = p.GetUser()?.Group;
|
|
if (pGroup?.Id == group.Id)
|
|
{
|
|
ChatService.SendMessage(p, message);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static void BroadcastJob(string message, JobBase job)
|
|
{
|
|
job.GetUsersInJob().ForEach(c => ChatService.SendMessage(c, message));
|
|
}
|
|
|
|
internal static void BroadcastFaction(string message, List<int> factionIds)
|
|
{
|
|
using (var dbContext = new DatabaseContext())
|
|
{
|
|
BroadcastFaction(message, dbContext.Factions.Where(f => factionIds.Contains(f.Id)).ToList());
|
|
}
|
|
}
|
|
}
|
|
}
|