Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7c74d3a
First pass at altering and streamline current method to send or recie…
Sep 26, 2020
c382340
Further optimization of api code
Bishbash777 Sep 27, 2020
1a416a4
remove sending of unessecary data
Bishbash777 Sep 27, 2020
1523b0b
name changes
Bishbash777 Sep 27, 2020
6c5c021
Fix various crashes and other minor improvements
Bishbash777 Sep 27, 2020
0aa1582
Move more stuff over to new API
Bishbash777 Sep 28, 2020
6f8e24d
further code optimization and move more functions to new api
Sep 28, 2020
39f8406
New SendAPIData utility and move updates to new api
Sep 28, 2020
0b83e76
Various improvements
Sep 28, 2020
9e023d1
Misc
Sep 28, 2020
909d0db
New way of getting players distance from a gate
Sep 29, 2020
42b50b7
Move player scanning into its own method and static methods into util…
Sep 29, 2020
f815e0b
Move api request methods into own class
Sep 29, 2020
63fe601
More tidy up
Sep 30, 2020
fb38526
FIXED THE GOD DAMN MESSAGE DUPILICATION
Sep 30, 2020
7e01dfb
Fix other repeating message
Sep 30, 2020
7bb4cff
Correct logging types
Oct 1, 2020
346a196
Documentation for scan method since its ugly as fuck
Oct 1, 2020
53b1f75
fix jumpgates re-opening before grid pasting
Oct 2, 2020
a517feb
Use online config branch creation
Oct 2, 2020
37f1165
Only respawn player if a character does not exist
Oct 5, 2020
fbfeb47
Merge branch 'SM-rewrite' into SM-rewrite-online-config
Oct 5, 2020
9b874f5
Tie HWID (processor) to IP
Oct 5, 2020
afa4fce
Fix key duplication
Oct 6, 2020
e43e3fa
Working online config
Oct 6, 2020
fad4332
Lock UI when online mode is enabled and add !switch password command …
Oct 8, 2020
211dce6
Final changes and rename LocalKey to BindingKey
Oct 16, 2020
a87941a
API methods push
Oct 25, 2020
aae2214
CFG rework
Bishbash777 Nov 30, 2020
ae0a6c3
remove old summary
Nov 30, 2020
9515c32
First pass at new UI
Dec 1, 2020
72eed37
UI bug fixes and misc
Dec 13, 2020
2f66849
updates
Oct 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 196 additions & 0 deletions SwitchMe/APIMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
using NLog;
using Sandbox.Game.World;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VRage.Game.ModAPI;
using Torch.API;


namespace SwitchMe {

public class APIMethods {
public static string API_URL = "http://switchplugin.net/api2/";
public static ITorchBase Torch { get; }
public static readonly Logger Log = LogManager.GetCurrentClassLogger();
private readonly SwitchMePlugin Plugin;
public APIMethods(SwitchMePlugin Plugin) {
this.Plugin = Plugin;
}


public async Task<bool> CheckExistance(string targetIP) {
utils.webdata.Add("TARGETIP", targetIP);
var api_reponse = await utils.SendAPIRequestAsync(Plugin.debug);
return api_reponse["responseCode"] == "0";
}

public async Task MarkCompleteAsync(ulong steamid) {
utils.webdata.Add("PROCESSED", steamid.ToString());
utils.webdata.Add("STEAMID", steamid.ToString());
utils.webdata.Add("CURRENTIP", Plugin.currentIP());
utils.webdata.Add("FUNCTION", "MarkCompleteAsync");
await utils.SendAPIRequestAsync(Plugin.debug);
}

public async Task<string> GetGateAsync(string steamid) {
utils.webdata.Add("STEAMID", steamid);
utils.webdata.Add("CURRENTIP", Plugin.currentIP());
utils.webdata.Add("BINDKEY", Plugin.Config.BindingKey);
utils.webdata.Add("FUNCTION", "GetGateAsync");
var api_response = await utils.SendAPIRequestAsync(Plugin.debug);
return api_response["gate"];
}

public async Task<bool> CheckConnectionAsync(IPlayer player) {
Log.Warn("Checking inbound conneciton for " + player.SteamId);
utils.webdata.Add("BINDKEY", Plugin.Config.BindingKey);
utils.webdata.Add("CURRENTIP", Plugin.currentIP());
utils.webdata.Add("STEAMID", player.SteamId.ToString());
utils.webdata.Add("FUNCTION", "CheckConnectionAsync");

var api_response = await utils.SendAPIRequestAsync(Plugin.debug);
return bool.Parse(api_response["connecting"]);
}

public async Task AddConnectionAsync(ulong SteamUserId,string ip,string targetAlias) {
utils.webdata.Add("BINDKEY", Plugin.Config.BindingKey);
utils.webdata.Add("CURRENTIP", Plugin.currentIP());
utils.webdata.Add("TARGETIP", ip);
utils.webdata.Add("TARGETALIAS", targetAlias);
utils.webdata.Add("STEAMID", SteamUserId.ToString());
utils.webdata.Add("FUNCION", "AddConnectionAsync");
await utils.SendAPIRequestAsync(Plugin.debug);
}
public async Task AddTransferAsync(string steamid, string ip, string filename, string targetpos, string grid_target) {
utils.webdata.Add("STEAMID", steamid);
utils.webdata.Add("TARGETIP", ip);
utils.webdata.Add("FILENAME", steamid + "-" + grid_target);
utils.webdata.Add("BINDKEY", Plugin.Config.BindingKey);
utils.webdata.Add("TARGETPOS", targetpos);
utils.webdata.Add("GRIDNAME", grid_target);
utils.webdata.Add("CURRENTIP", Plugin.currentIP());
utils.webdata.Add("FUNCTION", "AddTransferAsync");
await utils.SendAPIRequestAsync(Plugin.debug);
}

public async Task<bool> CheckKeyAsync(string target) {
try {

utils.webdata.Add("TARGETIP", target);
utils.webdata.Add("BINDKEY", Plugin.Config.BindingKey);
utils.webdata.Add("FUNCTION", "CheckKeyAsync");
var api_response = await utils.SendAPIRequestAsync(Plugin.debug);
return Plugin.Config.BindingKey == api_response["key"];

}
catch (Exception e) {
return false;
}
}

public async Task<string> CheckSlotsAsync(string targetIP, string NumberOfPlayers) {
utils.webdata.Add("TARGETIP", targetIP);
utils.webdata.Add("FUNCTION", "CheckSlotsAsync");
utils.webdata.Add("PLAYERCOUNT", NumberOfPlayers);

var api_response = await utils.SendAPIRequestAsync(Plugin.debug);
return api_response["available"];
}

public async Task<bool> CheckInboundAsync(string target) {
try {
utils.webdata.Add("TARGETIP", target);
utils.webdata.Add("FUNCTION", "CheckInboundAsync");
var api_response = await utils.SendAPIRequestAsync(Plugin.debug);
return api_response["allow"] == "Y";

}
catch (Exception e) {
Log.Warn("Error: " + e.ToString());
return false;
}
}

public async Task<bool> CheckStatusAsync(string target) {
try {
utils.webdata.Add("TARGETIP", target);
utils.webdata.Add("FUNCTION", "CheckStatusAsync");
var api_response = await utils.SendAPIRequestAsync(Plugin.debug);
return bool.Parse(api_response["online"]);

}
catch {
Log.Warn($"http connection error: Please check you can connect to '{API_URL}'");
}
return false;
}

public void AttemptHWIDLink() {
if (!utils.ReservedDicts.Contains("HWIDData")) {
utils.ReservedDicts.Add("HWIDData");
utils.HWIDData.Add("FUNCTION", "AttemptHWIDLink");
utils.HWIDData.Add("HWID", utils.GetMachineId());
utils.HWIDData.Add("CURRENTIP", Plugin.currentIP());
utils.SendHWIDData(false);
utils.ReservedDicts.Remove("HWIDData");
}
}

public async Task RemoveConnectionAsync(ulong player) {
Log.Warn("Removing conneciton flag for " + player);
utils.webdata.Add("BINDKEY", Plugin.Config.BindingKey);
utils.webdata.Add("CURRENTIP", Plugin.currentIP());
utils.webdata.Add("STEAMID", player.ToString());
utils.webdata.Add("FUNCTION", "RemoveConnectionAsync");
await utils.SendAPIRequestAsync(Plugin.debug);
}

public async Task<Dictionary<string, string>> LoadOnlineConfig() {
utils.webdata.Add("FUNCTION", "DownloadConfigAsync");
utils.webdata.Add("CURRENTIP", Plugin.currentIP());
utils.webdata.Add("HWID", utils.GetMachineId());
var api_reponse = await utils.SendAPIRequestAsync(Plugin.debug);
return api_reponse;
}

public async Task<Dictionary<string,string>> FindWebGridAsync(ulong steamid) {
utils.webdata.Add("STEAMID", steamid.ToString());
utils.webdata.Add("CURRENTIP", Plugin.currentIP());
utils.webdata.Add("FUNCTION", "FindWebGridAsync");
return await utils.SendAPIRequestAsync(Plugin.debug);
}

public async Task<bool> CheckServer(IMyPlayer player, string servername, string target) {
utils.ReservedDicts.Add("CheckServer");

if (target.Length < 1) {
Log.Warn("Unknown Server. Please use '!switch list' to see a list of valid servers!");
utils.NotifyMessage("Unknown Server. Please use '!switch list' to see a list of valid servers!", player.SteamUserId);
return false;
}

/*if (!await CheckExistance(target)) {
Log.Warn("Cannot communicate with target, please make sure SwitchMe is installed there!");
utils.NotifyMessage("Cannot communicate with target, please make sure SwitchMe is installed there!", player.SteamUserId);
return false;
}*/

if (!await CheckKeyAsync(target)) {
Log.Warn("Unauthorised Switch! Please make sure the servers have the same Bind Key!");
utils.NotifyMessage("Unauthorised Switch! Please make sure the servers have the same Bind Key!", player.SteamUserId, "Red");
return false;
}

/// Slot checking
bool slotsAvailable = bool.Parse(await CheckSlotsAsync(target, "1"));
if (!slotsAvailable && player.PromoteLevel != MyPromoteLevel.Admin) {
utils.NotifyMessage("Not enough slots free to use gate!", player.SteamUserId);
return false;
}
utils.ReservedDicts.Remove("CheckServer");
return true;
}

}
}
Loading