Commit d6c25c1f authored by nanahira's avatar nanahira

test mycard

parent 2dd80e7c
using UnityEngine;
using UnityEngine.Networking;
using System;
using System.Collections.Generic;
using System.IO;
public class JSONObject {
public string Stringify()
{
return JsonUtility.ToJson(this);
}
}
public class LoginUserObject : JSONObject {
public int id;
public string username;
public string name;
public string email;
public string password_hash;
public bool active;
public bool admin;
public string avatar;
public string locale;
public string registration_ip_address;
public string ip_address;
public string created_at;
public string updated_at;
public string token;
}
public class LoginObject : JSONObject {
public LoginUserObject user;
public string token;
public string message;
}
public class LoginRequest : JSONObject {
public string account;
public string password;
public LoginRequest(string user, string pass) {
this.account = user;
this.password = pass;
}
}
public class MatchObject : JSONObject {
public string address;
public int port;
public string password;
}
public class MyCardHelper {
string username;
int userid = 0;
public bool login(string name, string password, out string fail_reason) {
try {
LoginRequest data = new LoginRequest(name, password);
string data_str = data.Stringify();
UnityWebRequest www = UnityWebRequest.Post("https://api.moecube.com/accounts/signin", data_str);
www.SetRequestHeader("Content-Type", "application/json");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
fail_reason = www.error;
return false;
}
else if (www.responseCode >= 400)
{
fail_reason = "Login failed";
return false;
}
else
{
string result = www.downloadHandler.text;
LoginObject result_object = JsonUtility.FromJson<LoginObject>(result);
username = result_object.user.username;
userid = result_object.user.id;
}
} catch (Exception e) {
fail_reason = e.Message;
return false;
}
return true;
}
public string requestMatch(string match_type, out string fail_reason) {
string ret;
if (!username || !userid) {
fail_reason = "Not logged in";
return null;
}
try {
string auth_str = Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + userid));
UnityWebRequest www = UnityWebRequest.Post("https://api.mycard.moe/ygopro/match?locale=zh-CN&arena=" + match_type, new WWWForm());
www.SetRequestHeader("Authorization", "Basic " + auth_str);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
fail_reason = www.error;
return null;
}
else if (www.responseCode >= 400)
{
fail_reason = "Match failed";
return null;
}
else
{
string result = www.downloadHandler.text;
MatchObject result_object = JsonUtility.FromJson<MatchObject>(result);
ret = result_object.password;
}
} catch (Exception e) {
fail_reason = e.Message;
return null;
}
return ret;
}
}
......@@ -55,6 +55,8 @@ public class SelectServer : WindowServantSP
serversList.items.Add("[AI]Doom Bots of Doom");
//serversList.items.Add("[OCG&TCG]한국서버");
//serversList.items.Add("[OCG&TCG]YGOhollow (JP)");
serversList.items.Add("[MyCard]Athletic");
serversList.items.Add("[MyCard]Entertain");
if (Application.systemLanguage == SystemLanguage.Chinese || Application.systemLanguage == SystemLanguage.ChineseSimplified || Application.systemLanguage == SystemLanguage.ChineseTraditional)
{
serversList.items.Add("[自定义]");
......@@ -126,6 +128,26 @@ public class SelectServer : WindowServantSP
UIHelper.getByName<UIInput>(gameObject, "port_").value = "573";
Config.Set("serversPicker", "[AI]Doom Bots of Doom");
inputIP_.enabled = false;
inputPort_.enabled = false;
break;
}
case "[MyCard]Athletic":
{
UIHelper.getByName<UIInput>(gameObject, "ip_").value = "tiramisu.mycard.moe";
UIHelper.getByName<UIInput>(gameObject, "port_").value = "8911";
Config.Set("serversPicker", "[MyCard]Athletic");
inputIP_.enabled = false;
inputPort_.enabled = false;
break;
}
case "[MyCard]Entertain":
{
UIHelper.getByName<UIInput>(gameObject, "ip_").value = "tiramisu.mycard.moe";
UIHelper.getByName<UIInput>(gameObject, "port_").value = "7911";
Config.Set("serversPicker", "[MyCard]Entertain");
inputIP_.enabled = false;
inputPort_.enabled = false;
break;
......@@ -290,6 +312,33 @@ public class SelectServer : WindowServantSP
UIHelper.getByName<UIInput>(gameObject, "version_").value = str;
}
bool isMyCard() {
string server = serversList.value;
return server == "[MyCard]Athletic" || server == "[MyCard]Entertain";
}
void startMyCard(string name, string password, string match_type = "entertain") {
mycard = new MyCardHelper();
Program.PrintToChat(InterString.Get("正在登录至MyCard。"));
string fail_reason;
bool res = mycard.login(name, password, out fail_reason);
if (!res) {
Program.PrintToChat(InterString.Get("MyCard登录失败。原因: ") + fail_reason);
return;
}
Program.PrintToChat(InterString.Get("正在请求匹配。匹配类型: ") + match_type);
string pswString = mycard.requestMatch(match_type, out fail_reason);
if (!pswString) {
Program.PrintToChat(InterString.Get("匹配请求失败。原因: ") + fail_reason);
return;
}
string ipString = UIHelper.getByName<UIInput>(gameObject, "ip_").value;
string portString = UIHelper.getByName<UIInput>(gameObject, "port_").value;
string versionString = UIHelper.getByName<UIInput>(gameObject, "version_").value;
Program.PrintToChat(InterString.Get("匹配成功。正在进入房间。"));
KF_onlineGame(name, ipString, portString, versionString, pswString);
}
void onClickJoin()
{
if (!isShowed)
......@@ -301,12 +350,16 @@ public class SelectServer : WindowServantSP
string portString = UIHelper.getByName<UIInput>(gameObject, "port_").value;
string pswString = UIHelper.getByName<UIInput>(gameObject, "psw_").value;
string versionString = UIHelper.getByName<UIInput>(gameObject, "version_").value;
KF_onlineGame(Name, ipString, portString, versionString, pswString);
if (isMyCard()) {
startMyCard(Name, pswString, portString == "8911" ? "athletic" : "entertain");
} else {
KF_onlineGame(Name, ipString, portString, versionString, pswString);
}
}
public void onClickRoomList()
{
if (!isShowed)
if (!isShowed || isMyCard())
{
return;
}
......@@ -335,22 +388,24 @@ public class SelectServer : WindowServantSP
{
if (name != "")
{
//string fantasty = "(" + versionString + ")" + ipString + ":" + portString + " " + pswString;
string fantasty = "psw: " + pswString;
list.items.Remove(fantasty);
list.items.Insert(0, fantasty);
list.value = fantasty;
if (list.items.Count > 5)
{
list.items.RemoveAt(list.items.Count - 1);
}
string all = "";
for (int i = 0; i < list.items.Count; i++)
{
all += list.items[i] + "\r\n";
if (!isMyCard()) {
//string fantasty = "(" + versionString + ")" + ipString + ":" + portString + " " + pswString;
string fantasty = "psw: " + pswString;
list.items.Remove(fantasty);
list.items.Insert(0, fantasty);
list.value = fantasty;
if (list.items.Count > 5)
{
list.items.RemoveAt(list.items.Count - 1);
}
string all = "";
for (int i = 0; i < list.items.Count; i++)
{
all += list.items[i] + "\r\n";
}
File.WriteAllText("config/passwords.conf", all);
printFile(false);
}
File.WriteAllText("config/passwords.conf", all);
printFile(false);
(new Thread(() => { TcpHelper.join(ipString, name, portString, pswString, versionString); })).Start();
}
else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment