Skip to content
Snippets Groups Projects
Commit c243957a authored by Michael Chen's avatar Michael Chen
Browse files

Console interface program

parent cc216b71
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Helpers;
namespace BreakoutHighscoreInterface
{
static class Program
{
public class LeaderRequest
{
#pragma warning disable IDE1006 // Benennungsstile
public static string URI = @"https://breakouthighscores.000webhostapp.com/getLeader.php";
public bool success { get; set; }
public string error { get; set; }
public User entry { get; set; }
}
public class HighScoreRequest
{
public static string URI = @"https://breakouthighscores.000webhostapp.com/getHighest.php";
public bool success { get; set; }
public string error { get; set; }
public int count { get; set; }
public IEnumerable<User> entries { get; set; }
}
public class UploadScoreRequest
{
public static string URI = @"https://breakouthighscores.000webhostapp.com/uploadScore.php?username={0}&score={1}";
public bool success { get; set; }
public string error { get; set; }
}
public class LoginRequest
{
public static string URI = @"https://breakouthighscores.000webhostapp.com/login.php?username={0}&password={1}";
public bool success { get; set; }
public string error { get; set; }
public User entry { get; set; }
public string query { get; set; }
}
public class RegisterRequest
{
public static string URI = @"https://breakouthighscores.000webhostapp.com/registerUser.php?username={0}&password={1}";
public bool success { get; set; }
public string error { get; set; }
public User entry { get; set; }
public string query { get; set; }
}
public class DeleteScoresRequest
{
public static string URI = @"https://breakouthighscores.000webhostapp.com/clearTable.php";
public bool success { get; set; }
public string error { get; set; }
}
public class User
{
public string username { get; set; }
public long score { get; set; }
#pragma warning restore IDE1006 // Benennungsstile
}
public enum Mode { Upload, Leader, Top, Register, Login };
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
static int Main(string[] args)
{
Mode mode;
if (args.Length < 2)
{
Console.WriteLine("Invalid Arguments!");
PrintHelp();
return 1;
}
switch (args[0].ToLower())
{
case "upload":
mode = Mode.Upload;
break;
case "leader":
mode = Mode.Leader;
break;
case "top":
mode = Mode.Top;
break;
case "register":
mode = Mode.Register;
break;
case "login":
mode = Mode.Login;
break;
//case "clear":
// mode = Mode.Clear;
// break;
case "help":
PrintHelp();
return 0;
default:
Console.WriteLine("Invalid mode: " + args[0]);
return 1;
}
int returnval = DoWork(mode, args);
if (returnval == 1)
{
//Console.WriteLine("Failed!");
return 1;
}
//Console.WriteLine("Success!");
return 0;
}
private static void PrintHelp()
{
//Console.WriteLine("Usage: <exe> [mode] [outputfile] [username] [score|password]\nMode can be:\n\t- top: gets the top 10 scores, requires outputfile\n\t- leader: gets the current leader, requires outputfile\n\t- upload: uploads a score, requires outputfile, username and score\n\t- (inactive) clear: deletes the entire data, requires outputfile\n\t- login: checks login credentials, requires outputfile, username and password\n\t- upload: registers a user, requires outputfile, username and password");
}
static int DoWork(Mode mode, string[] args)
{
string file = args[1];
string username, scoreString, password;
using (StreamWriter sw = new StreamWriter(file, false))
{
switch (mode)
{
case Mode.Upload:
if (args.Length < 4)
{
Console.WriteLine("Too few arguments!");
PrintHelp();
return 1;
}
username = args[2];
scoreString = args[3];
try
{
UploadScoreRequest usr = Json.Decode<UploadScoreRequest>(ReadJSONFromURI(UploadScoreRequest.URI, username, scoreString));
sw.WriteLine(usr.success ? "1" : "0");
sw.WriteLine(usr.error);
if (!(usr.success))
Console.WriteLine("Error: {0}", usr.error);
}
catch (Exception e)
{
sw.WriteLine("0");
sw.WriteLine("Error pushing score: " + e.Message);
return 1;
}
break;
case Mode.Leader:
try
{
LeaderRequest lr = Json.Decode<LeaderRequest>(ReadJSONFromURI(LeaderRequest.URI));
sw.WriteLine(lr.success ? "1" : "0");
if (lr.success)
sw.WriteLine($"{lr.entry.username}+{lr.entry.score}");
}
catch (Exception e)
{
sw.WriteLine("0");
sw.WriteLine("Error getting leader: " + e.Message);
return 1;
}
break;
case Mode.Top:
try
{
HighScoreRequest hsr = Json.Decode<HighScoreRequest>(ReadJSONFromURI(HighScoreRequest.URI));
sw.WriteLine(hsr.success ? "1" : "0");
if (hsr.success)
{
foreach (User user in hsr.entries)
sw.WriteLine($"{user.username}+{user.score}");
}
else
sw.WriteLine(hsr.error);
}
catch (Exception e)
{
sw.WriteLine("0");
sw.WriteLine("Error getting highscores: " + e.Message);
return 1;
}
break;
//case Mode.Clear:
// try
// {
// DeleteScoresRequest usr = Json.Decode<DeleteScoresRequest>(ReadJSONFromURI(DeleteScoresRequest.URI));
// sw.WriteLine(usr.success ? "1" : "0");
// }
// catch (Exception e)
// {
// sw.WriteLine("0");
// sw.WriteLine("Error clearing: " + e.Message);
// return 1;
// }
// break;
case Mode.Register:
if (args.Length < 4) { Console.WriteLine("Too few arguments!"); PrintHelp(); return 1; }
username = args[2];
password = args[3];
try
{
RegisterRequest usr = Json.Decode<RegisterRequest>(ReadJSONFromURI(RegisterRequest.URI, username, password));
sw.WriteLine(usr.success ? "1" : "0");
sw.WriteLine(usr.error);
if (!(usr.success))
Console.WriteLine("Error: {0}", usr.error);
}
catch (Exception e)
{
sw.WriteLine("0");
sw.WriteLine("Error: {0}", e.Message);
Console.WriteLine("Error: {0}", e.Message);
return 1;
}
break;
case Mode.Login:
if (args.Length < 4) { Console.WriteLine("Too few arguments!"); PrintHelp(); return 1; }
username = args[2];
password = args[3];
try
{
LoginRequest usr = Json.Decode<LoginRequest>(ReadJSONFromURI(LoginRequest.URI, username, password));
sw.WriteLine(usr.success ? "1" : "0");
sw.WriteLine(usr.error);
if (!(usr.success))
Console.WriteLine("Error: {0}", usr.error);
}
catch (Exception e)
{
sw.WriteLine("0");
sw.WriteLine("Error: " + e.Message);
Console.WriteLine("Error: {0}", e.Message);
return 1;
}
break;
default:
Console.WriteLine("Unknown mode: Enum did not take valid value!");
return 1;
}
}
return 0;
}
private static string ReadJSONFromURI(string URI, params string[] uriformat)
{
for (int i = 0; i < uriformat.Length; i++)
uriformat[i] = System.Web.HttpUtility.UrlEncode(uriformat[i]);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(URI, uriformat));
//Console.WriteLine("Call: {0}", string.Format(URI, uriformat));
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string rawJson = readStream.ReadToEnd();
response.Close();
readStream.Close();
return rawJson;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment