refactored the welcome plugin to use a web api instead of a hard coded file

removed deprecated file class
don't wait for response when setting dvar/sending command in T6
potential fix for duplicate kick message in JS plugin
This commit is contained in:
RaidMax
2018-10-08 21:15:59 -05:00
parent b289917319
commit 7caee55a7e
13 changed files with 57 additions and 296 deletions

View File

@ -10,6 +10,9 @@ using SharedLibraryCore.Database.Models;
using System.Linq;
using SharedLibraryCore.Database;
using Microsoft.EntityFrameworkCore;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
namespace IW4MAdmin.Plugins.Welcome
{
@ -84,9 +87,9 @@ namespace IW4MAdmin.Plugins.Welcome
{
Player newPlayer = E.Origin;
if (newPlayer.Level >= Player.Permission.Trusted && !E.Origin.Masked)
E.Owner.Broadcast(ProcessAnnouncement(Config.Configuration().PrivilegedAnnouncementMessage, newPlayer));
E.Owner.Broadcast(await ProcessAnnouncement(Config.Configuration().PrivilegedAnnouncementMessage, newPlayer));
newPlayer.Tell(ProcessAnnouncement(Config.Configuration().UserWelcomeMessage, newPlayer));
newPlayer.Tell(await ProcessAnnouncement(Config.Configuration().UserWelcomeMessage, newPlayer));
if (newPlayer.Level == Player.Permission.Flagged)
{
@ -101,30 +104,49 @@ namespace IW4MAdmin.Plugins.Welcome
.FirstOrDefaultAsync();
}
E.Owner.ToAdmins($"^1NOTICE: ^7Flagged player ^5{newPlayer.Name} ^7({penaltyReason}) has joined!");
E.Owner.ToAdmins($"^1NOTICE: ^7Flagged player ^5{newPlayer.Name} ^7({penaltyReason}) has joined!");
}
else
E.Owner.Broadcast(ProcessAnnouncement(Config.Configuration().UserAnnouncementMessage, newPlayer));
E.Owner.Broadcast(await ProcessAnnouncement(Config.Configuration().UserAnnouncementMessage, newPlayer));
}
}
private string ProcessAnnouncement(string msg, Player joining)
private async Task<string> ProcessAnnouncement(string msg, Player joining)
{
msg = msg.Replace("{{ClientName}}", joining.Name);
msg = msg.Replace("{{ClientLevel}}", Utilities.ConvertLevelToColor(joining.Level, joining.ClientPermission.Name));
try
// this prevents it from trying to evaluate it every message
if (msg.Contains("{{ClientLocation}}"))
{
CountryLookupProj.CountryLookup CLT = new CountryLookupProj.CountryLookup($"{Utilities.OperatingDirectory}Plugins{System.IO.Path.DirectorySeparatorChar}GeoIP.dat");
msg = msg.Replace("{{ClientLocation}}", CLT.LookupCountryName(joining.IPAddressString));
}
catch (Exception)
{
joining.CurrentServer.Logger.WriteError("Could not open file Plugins\\GeoIP.dat for Welcome Plugin");
msg = msg.Replace("{{ClientLocation}}", await GetCountryName(joining.IPAddressString));
}
msg = msg.Replace("{{TimesConnected}}", TimesConnected(joining));
return msg;
}
/// <summary>
/// makes a webrequest to determine IP origin
/// </summary>
/// <param name="ip">IP address to get location of</param>
/// <returns></returns>
private async Task<string> GetCountryName(string ip)
{
using (var wc = new WebClient())
{
try
{
string response = await wc.DownloadStringTaskAsync(new Uri($"http://extreme-ip-lookup.com/json/{ip}"));
var responseObj = JObject.Parse(response);
return responseObj["country"].ToString();
}
catch
{
return "a third world country";
}
}
}
}
}