using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ShiftOS.Objects;
namespace ShiftOS.Unite
{
public class UniteClient
{
///
/// Gets a string represents the user token for this Unite Client.
///
public string Token { get; private set; }
///
/// Gets the base URL used in all API calls. Retrieved from the user's servers.json file.
///
public string BaseURL
{
get
{
return UserConfig.Get().UniteUrl;
}
}
///
/// Get the display name of a user.
///
/// The user ID to look at.
///
public string GetDisplayNameId(string id)
{
return MakeCall("/API/GetDisplayName/" + id);
}
///
/// Get the Pong highscore stats for all users.
///
///
public PongHighscoreModel GetPongHighscores()
{
return JsonConvert.DeserializeObject(MakeCall("/API/GetPongHighscores"));
}
///
/// Create a new instance of the object.
///
/// Unused.
/// The user API token to use for this client (see http://getshiftos.ml/Manage and click "API" to see your tokens)
public UniteClient(string baseurl, string usertoken)
{
//Handled by the servers.json file
//BaseURL = baseurl;
Token = usertoken;
}
///
/// Make a call to the Unite API using the current user token and base URL.
///
/// The path, relative to the base URL, to call.
/// The server's response.
internal string MakeCall(string url)
{
var webrequest = WebRequest.Create(BaseURL + url);
webrequest.Headers.Add("Authentication: Token " + Token);
using (var response = webrequest.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new System.IO.StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}
///
/// Get the Pong codepoint highscore for the current user.
///
/// The amount of Codepoints returned by the server
public ulong GetPongCP()
{
return Convert.ToUInt64(MakeCall("/API/GetPongCP"));
}
///
/// Get the pong highest level score for this user
///
/// The highest level the user has reached.
public int GetPongLevel()
{
return Convert.ToInt32(MakeCall("/API/GetPongLevel"));
}
///
/// Set the user's highest level record for Pong.
///
/// The level to set the record to.
public void SetPongLevel(int value)
{
MakeCall("/API/SetPongLevel/" + value.ToString());
}
///
/// Set the pong Codepoints record for the user
///
/// The amount of Codepoints to set the record to
public void SetPongCP(ulong value)
{
MakeCall("/API/SetPongCP/" + value.ToString());
}
///
/// Get the user's email address.
///
/// The user's email address.
public string GetEmail()
{
return MakeCall("/API/GetEmail");
}
///
/// Get the user's system name.
///
/// The user's system name.
public string GetSysName()
{
return MakeCall("/API/GetSysName");
}
///
/// Set the user's system name.
///
/// The system name to set the record to.
public void SetSysName(string value)
{
MakeCall("/API/SetSysName/" + value);
}
///
/// Get the user's display name.
///
/// The user's display name.
public string GetDisplayName()
{
return MakeCall("/API/GetDisplayName");
}
///
/// Set the user's display name.
///
/// The display name to set the user's account to.
public void SetDisplayName(string value)
{
MakeCall("/API/SetDisplayName/" + value.ToString());
}
///
/// Get the user's full name if they have set it in their profile.
///
/// Empty string if the user hasn't set their fullname, else, a string representing their fullname.
public string GetFullName()
{
return MakeCall("/API/GetFullName");
}
///
/// Set the user's fullname.
///
/// The new fullname.
public void SetFullName(string value)
{
MakeCall("/API/SetFullName/" + value.ToString());
}
///
/// Get the user's codepoints.
///
/// The amount of codepoints stored on the server for this user.
public ulong GetCodepoints()
{
return Convert.ToUInt64(MakeCall("/API/GetCodepoints"));
}
///
/// Set the user's codepoints.
///
/// The amount of codepoints to set the user's codepoints value to.
public void SetCodepoints(ulong value)
{
MakeCall("/API/SetCodepoints/" + value.ToString());
}
}
///
/// API data model for Unite pong highscores.
///
public class PongHighscoreModel
{
///
/// Amount of pages in this list.
///
public int Pages { get; set; }
///
/// An array representing the highscores found on the server.
///
public PongHighscore[] Highscores { get; set; }
}
///
/// API data model for a single Pong highscore.
///
public class PongHighscore
{
///
/// The user ID linked to this highscore.
///
public string UserId { get; set; }
///
/// The highscore's level record.
///
public int Level { get; set; }
///
/// The highscore's codepoint cashout record.
///
public long CodepointsCashout { get; set; }
}
}