CTZLauncher/CTZLauncher/Authentication/CTZAuthenticator.cs

86 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using KMCCC.Authentication;
using CityCraft;
using System.Threading.Tasks;
using System.Threading;
namespace KMCCC.Authentication
{
class CTZAuthenticator
{
/// <summary>
/// 玩家的名字
/// </summary>
public readonly string Username;
public readonly string Password;
HttpHelper http = new HttpHelper();
/// <summary>
/// 验证服务器地址
/// </summary>
public readonly string Address;
/// <summary>
/// 验证服务器端口
/// </summary>
public readonly int Port;
/// <summary>
/// 构造离线验证器
/// </summary>
/// <param name="username">玩家的名字</param>
public CTZAuthenticator(string username, string password, string address, int port = 25565)
{
Username = username;
Password = password;
Address = address.IndexOf("http") > 0 ? address : "http://" + address;
Port = port;
}
/// <summary>
/// 标注外部登陆验证器
/// </summary>
public string Type
{
get { return "KMCCC.CTZ"; }
}
public bool isRegistered()
{
return getResult(Address + ":" + Port + "/isregistered?username=" + Username);
}
public bool Register()
{
return getResult(Address + ":" + Port + "/register?username=" + Username + "&password=" + Password);
}
public bool isLogin()
{
return getResult(Address + ":" + Port + "/islogin?username=" + Username);
}
public bool Login()
{
return getResult(Address + ":" + Port + "/login?username=" + Username + "&password=" + Password);
}
public string getServerList()
{
string sl = http.Send(HttpMethod.GET, Address + ":" + Port + "/serverlist");
if (string.IsNullOrEmpty(sl))
throw new ArgumentNullException("服务器返回了空的字符串或网络连接异常!");
return sl;
}
public bool getResult(string url)
{
string result = http.Send(HttpMethod.GET, url);
if (string.IsNullOrEmpty(result))
throw new ArgumentNullException("服务器返回了空的字符串或网络连接异常!");
return result.Contains("true") ? true : false;
}
}
}