You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class EducoderText : MonoBehaviour
{
public static EducoderText Instance;
public Text text;
private int shiftValue=3;
public string decryptedText;
public string userName = "";
public string url = "https://data.educoder.net/api/users/get_user_info.json?objective_type=shixun&objective_id=xolfhc7f&school=1";
private void Start()
{
Instance = this;
//// 示例使用
//string plaintext = "zart2007";
//string encryptedText = Encrypt(plaintext, shiftValue);
////Console.WriteLine("Encrypted text: " + encryptedText);
//Debug.Log("Encrypted text: " + encryptedText);
//decryptedText = Decrypt(encryptedText, shiftValue);
//Debug.Log("Encrypted text: " + decryptedText);
////Console.WriteLine("Decrypted text: " + decryptedText);
////三个文本解密后
//if (decryptedText.Contains(userName))
//{
// Debug.Log("yes");
//}
}
public void Open() //开始判断文本解密后对比
{
string plaintext = GameManager.Instance.answers[2];
string encryptedText = Encrypt(plaintext, shiftValue);
//Console.WriteLine("Encrypted text: " + encryptedText);
Debug.Log("Encrypted text: " + encryptedText);
decryptedText = Decrypt(encryptedText, shiftValue);
Debug.Log("Encrypted text: " + decryptedText);
// StartCoroutine(SendGetRequest());
SendRequest();
}
private void SendRequest()
{
//发送请求并接收
EduCoderTool.WebConnecter.Singleton.SendMsg("type", "getTaskData");
//解析
string jsonstr = text.text;
JObject jo = (JObject)JsonConvert.DeserializeObject(jsonstr);
if (jo == null)
{
Debug.Log("获取用户信息失败");
return;
}
string userLogin = jo["user"]["login"].ToString();
Debug.Log("Login"+userLogin);
if (userLogin.Contains(decryptedText))
{
if (GameManager.Instance.succeed)
{
GameManager.Instance.OverWnd.SetActive(true);
EduCoderTool.WebConnecter.Singleton.SendResultToWeb(true);
Debug.Log("yes");
}
else
{
GameManager.Instance.FailWnd.SetActive(true);
}
}
else
{
GameManager.Instance.FailWnd.SetActive(true);
}
}
IEnumerator SendGetRequest()
{
using (UnityWebRequest request = UnityWebRequest.Get(url))
{
yield return request.SendWebRequest();
if (request.isNetworkError)
{
Debug.Log(request.error);
Debug.Log("网络错误");
}
else
{
Debug.Log("成功");
// 打印服务端返回的数据
Debug.Log(request.downloadHandler.text);
//解析获取用户名
UserData data = JsonUtility.FromJson(request.downloadHandler.text,typeof(UserData))as UserData;
Debug.Log(data.login);
Debug.Log(data.user_id);
userName = data.login;
//在这里获取了平台的用户名
//TODO用通过用解密方法去解密那三个文本的结果和这个用户名进行对比 有一个相符的,就算通关
if (userName.Contains(decryptedText))
{
if (GameManager.Instance.succeed)
{
GameManager.Instance.OverWnd.SetActive(true);
EduCoderTool.WebConnecter.Singleton.SendResultToWeb(true);
Debug.Log("yes");
}
else
{
GameManager.Instance.FailWnd.SetActive(true);
}
}
else
{
GameManager.Instance.FailWnd.SetActive(true);
}
//向平台发送通关结果指令如下
// EducoderTool.Web.SinleTon.SendResult(true);
}
}
}
public static string Encrypt(string text, int shift)
{
string encryptedText = "";
foreach (char c in text)
{
// 获取字符的 UTF-16 编码
int utf16Val = (int)c;
// 根据编码值进行移位加密
int shiftedVal = utf16Val + shift;
// 将移位后的编码值转换为字符,并添加到加密文本中
encryptedText += (char)shiftedVal;
}
return encryptedText;
}
public static string Decrypt(string encryptedText, int shift)
{
string decryptedText = "";
foreach (char c in encryptedText)
{
// 获取字符的 UTF-16 编码
int utf16Val = (int)c;
// 根据编码值进行移位解密
int shiftedVal = utf16Val - shift;
// 将移位后的编码值转换为字符,并添加到解密文本中
decryptedText += (char)shiftedVal;
}
return decryptedText;
}
//public static string Decrypt(string encrypted_text, int shift = 3)
//{
// string decrypted_text = "";
// for (int i = 0; i < encrypted_text.Length; i++)
// {
// // 获取字符的 UTF-16 编码
// int utf16_val = (int)encrypted_text[i];
// // 将 UTF-16 编码转换为 UTF-8 编码
// byte[] utf8_bytes = Encoding.UTF8.GetBytes(char.ConvertFromUtf32(utf16_val));
// int utf8_val = utf8_bytes[0];
// // 根据编码值进行移位解密
// int shifted_val = utf8_val - shift;
// // 将移位后的编码值转换为字符,并添加到解密文本中
// decrypted_text += Convert.ToChar(shifted_val);
// }
// return decrypted_text.Substring(0, decrypted_text.Length - 3);
//}
[System.Serializable]
public class UserData
{
public string login;
public string real_name;
public int user_id;
}
}