Skip to content

Commit

Permalink
Version 2.--Changes by XGhozt
Browse files Browse the repository at this point in the history
  • Loading branch information
loganizer405 committed Apr 29, 2014
1 parent 19ec2ab commit 97ee448
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 75 deletions.
264 changes: 190 additions & 74 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Timers;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -18,6 +18,8 @@ namespace TSWVote
[ApiVersion(1, 15)]
public class TSWVote : TerrariaPlugin
{
private WebClient webClient;

public string path = Path.Combine(TShock.SavePath, "TSWVote.txt");
public override string Name
{
Expand All @@ -26,48 +28,98 @@ public override string Name
return "TServerWebVote";
}
}

public override string Author
{
get
{
return "Loganizer + XGhozt";
}
}

public override string Description
{
get
{
return "A plugin to vote to TServerWeb in-game.";
}
}

public override Version Version
{
get
{
return new Version("1.0");
return new Version("2.0");
}
}

public TSWVote(Main game)
: base(game)
{
Order = 1000;

this.webClient = new WebClient();
this.webClient.Proxy = null;
this.webClient.Headers.Add("user-agent", "TServerWeb Vote Plugin");
this.webClient.DownloadStringCompleted += WebClient_DownloadStringCompleted;
}

public override void Initialize()
{
ServerApi.Hooks.GameInitialize.Register(this, OnInitialize);
ServerApi.Hooks.GameInitialize.Register(this, OnInitialize);
ServerApi.Hooks.ServerChat.Register(this, OnChat);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize);
ServerApi.Hooks.ServerChat.Deregister(this, OnChat);

if (this.webClient != null)
{
this.webClient.DownloadStringCompleted -= WebClient_DownloadStringCompleted;
this.webClient.Dispose();
}
}
base.Dispose(disposing);
}
}

private void OnChat(ServerChatEventArgs args)
{
//check if its chat packet otherwise ignore
if (string.IsNullOrWhiteSpace(args.Text) || !args.Text.StartsWith("/"))
{
return;
}
//make sure player exists and isn't null
var player = TShock.Players[args.Who];
//make sure message passed isn't null
if (player == null)
{
return;
}
//remove '/'
string cmdText = args.Text.Remove(0, 1);
//change message into list of params
var cmdArgs = cmdText.Split(' ').ToList();
//get command name
string cmdName = cmdArgs[0].ToLower();
//remove the command name
cmdArgs.RemoveAt(0);
//check if its vote
if (cmdName != "vote")
{
return;
}
Vote(new CommandArgs(args.Text, player, new List<string> { string.Join(" ", cmdArgs) }));
//stops return of command
args.Handled = true;
}

public void OnInitialize(EventArgs args)
{
int ID;
string message;
if (!File.Exists(Path.Combine(TShock.SavePath, "TSWVote.txt")))
{
string[] text = {"**This is the configuration file, please do not edit.**", "Help page: http://www.tserverweb.com/help/",
Expand All @@ -77,7 +129,9 @@ public void OnInitialize(EventArgs args)
}
else
{
if (!GetServerID(out ID, out message))
int id;
string message;
if (!GetServerID(out id, out message))
SendError("Configuration", message);
}
if (TShock.Config.RestApiEnabled == false)
Expand All @@ -86,130 +140,192 @@ public void OnInitialize(EventArgs args)
}
else
{
Commands.ChatCommands.Add(new Command("", Vote, "vote"));
// Commands.ChatCommands.Add(new Command("", Vote, "vote"));
Commands.ChatCommands.Add(new Command("vote.changeid", ChangeID, "tserverweb"));
//This is so it will tell you if a new version is availiable.
// This is so it will tell you if a new version is availiable.
Commands.ChatCommands.Add(new Command("vote.checkversion", CheckVersion, "tswversioncheck"));
}
}

public void CheckVersion(CommandArgs e)
{
e.Player.SendInfoMessage(Version.ToString());
}
public void Vote(CommandArgs e)

public void tswQuery(string url, object userToken = null)
{
Uri uri = new Uri("http://www.tserverweb.com/vote.php?" + url);
this.webClient.DownloadStringAsync(uri, userToken);
}

public void validateCAPTCHA(CommandArgs e)
{
int id;
string message;
if (!GetServerID(out id, out message))
{
e.Player.SendErrorMessage("[TServerWeb] Vote failed, please contact an admin.");
SendError("Configuration", message);
return;
}

string answer = HttpUtility.UrlPathEncode(e.Parameters[0].ToString());
string playerName = HttpUtility.UrlPathEncode(e.Player.Name);

string url = "answer=" + answer + "&user=" + playerName + "&sid=" + id;
tswQuery(url, e);
}

public void doVote(CommandArgs e)
{
int ID;
int id;
string message;
if (!GetServerID(out id, out message))
{
e.Player.SendErrorMessage("[TServerWeb] Vote failed, please contact an admin.");
SendError("Configuration", message);
return;
}

string url = "user=" + HttpUtility.UrlPathEncode(e.Player.Name) + "&sid=" + id;
tswQuery(url, e);
}

public void Vote(CommandArgs e)
{
try
{
if (!GetServerID(out ID, out message))
if (e.Parameters.Count == 0)
{
e.Player.SendErrorMessage("[TServerWeb] Vote failed, please contact an admin.");
SendError("Configuration", message);
return;
// Send the vote
doVote(e);
}
WebClient wc = new WebClient();
wc.Headers.Add("user-agent", "TServerWeb Vote Plugin");
Uri uri = new Uri("http://www.tserverweb.com/vote.php?user=" + HttpUtility.UrlPathEncode(e.Player.Name) + "&sid=" + ID);


Response response = Response.Read(wc.DownloadString(uri));

switch (response.response)
else
{
case "success":
e.Player.SendSuccessMessage("[TServerWeb] " + response.message);
break;
case "failure":
e.Player.SendErrorMessage("[TServerWeb] Vote failed! Please contact an administrator.");
SendError("Vote", response.message);
break;
case "":
case null:
e.Player.SendErrorMessage("[TServerWeb] Vote failed! Please contact an administrator.");
SendError("Connection", "Response is blank, something is wrong with connection. Please email [email protected] about this issue.");
break;
// Answer was provided
validateCAPTCHA(e);
}
}
catch (Exception ex)
{
e.Player.SendErrorMessage("[TServerWeb] Vote failed! Please contact an administrator.");
SendError("Vote", "Connection failure: " + ex.ToString());
return;
}
SendError("Vote", "Connection failure: " + ex);
}
}

public void ChangeID(CommandArgs e)
{
int ID;
string message;
if (e.Parameters.Count == 0)
{
if (!GetServerID(out ID, out message))
{
int id;
string message;
if (!GetServerID(out id, out message))
{
e.Player.SendErrorMessage("[TServerWeb] Server ID is currently not specified! Please type /tserverweb [number] to set it. Reason:");
e.Player.SendErrorMessage(message);
return;
}
else
{
e.Player.SendInfoMessage("[TServerWeb] Server ID is currently set to " + ID +
". Type /tserverweb [number] to change it.");
return;
}

e.Player.SendInfoMessage("[TServerWeb] Server ID is currently set to " + id + ". Type /tserverweb [number] to change it.");
return;
}

if (e.Parameters.Count >= 2)
{
e.Player.SendErrorMessage("[TServerWeb] Incorrect syntax! Correct syntax: /tserverweb [number]");
return;
}
else

int newId;
if (int.TryParse(e.Parameters[0], out newId))
{
int newID;
if (int.TryParse(e.Parameters[0].ToString(), out newID))
{
string[] text = {"**This is the configuration file, please do not edit.**", "Help page: http://www.tserverweb.com/help/",
"Server ID is on next line. Please DO NOT edit the following line, change it using \"/tserverweb [ID] in-game\"",
newID.ToString()};
File.WriteAllLines(Path.Combine(TShock.SavePath, "TSWVote.txt"), text);
e.Player.SendInfoMessage("[TServerWeb] Server ID successfully changed to " + newID + "!");
return;
}
else
string[] text =
{
e.Player.SendErrorMessage("[TServerWeb] Number not specified! Please type /tserverweb [number]");
return;
}
"**This is the configuration file, please do not edit.**", "Help page: http://www.tserverweb.com/help/",
"Server ID is on next line. Please DO NOT edit the following line, change it using \"/tserverweb [ID] in-game\"",
newId.ToString()
};

File.WriteAllLines(Path.Combine(TShock.SavePath, "TSWVote.txt"), text);
e.Player.SendInfoMessage("[TServerWeb] Server ID successfully changed to " + newId + "!");
return;
}

e.Player.SendErrorMessage("[TServerWeb] Number not specified! Please type /tserverweb [number]");
}

public void SendError(string typeoffailure, string message)
{
Log.Error("[TServerWeb] TSWVote Error: " + typeoffailure + "failure. Reason: " + message);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[TServerWeb] TSWVote Error: " + typeoffailure + " failure. Reason: " + message);
Console.ResetColor();
}
public bool GetServerID(out int ID, out string message)

public bool GetServerID(out int id, out string message)
{
string[] stringid = File.ReadAllLines(Path.Combine(TShock.SavePath, "TSWVote.txt"));
foreach (string str in stringid)
{
if(int.TryParse(str, out ID))
if (int.TryParse(str, out id))
{
if (ID == 0)
if (id == 0)
{
message = "Server ID not specified. Type /tserverweb [ID] to specify it.";
return false;
}
else
{
message = "";
return true;
}
}

message = string.Empty;
return true;
}
}
ID = 0;

id = 0;
message = "Server ID is not a number. Please type /tserverweb [ID] to set it.";
return false;
}

private void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
CommandArgs args = e.UserState as CommandArgs;
if (args == null)
return;

if (e.Error != null)
{
args.Player.SendErrorMessage("[TServerWeb] Vote failed! Please contact an administrator.");
SendError("Exception", e.Error.Message);
return;
}

Response response = Response.Read(e.Result);
switch (response.response)
{
case "success":
// Correct answer was provided
// This means a vote is placed
args.Player.SendSuccessMessage("[TServerWeb] " + response.message);
break;
case "failure":
args.Player.SendErrorMessage("[TServerWeb] Vote failed! Your answer was incorrect, please try again.");
SendError("Vote", response.message);
break;
case "captcha":
args.Player.SendSuccessMessage("[TServerWeb] Please answer the question to make sure you are human.");
args.Player.SendSuccessMessage("[TServerWeb] You can type /vote <answer>");
args.Player.SendSuccessMessage("[TServerWeb] (CAPTCHA) " + response.message);
break;
case "nocaptcha":
// Answer was provided, but there was no pending captcha
doVote(args);
SendError("Vote", response.message);
break;
case "":
case null:
args.Player.SendErrorMessage("[TServerWeb] Vote failed! Please contact an administrator.");
SendError("Connection", "Response is blank, something is wrong with connection. Please email [email protected] about this issue.");
break;
}
}
}
}
}
Loading

0 comments on commit 97ee448

Please sign in to comment.