using System;
using System.Collections;
using LitJson;
using System.IO;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.XR;
using static InterfaceManager;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Newtonsoft.Json;
public class Submit : MonoBehaviour
{
///
/// 提交按钮
///
public Button submitbutton;
///
/// 交卷按钮
///
public Button onvolumebutton;
///
/// 练习的分页面
///
public GameObject practicepanl;
///
/// 查看答案按钮
///
public Button answerbutton;
///
/// 考试分数
///
public Text marktext;
///
/// 返回按钮
///
public Button backbutton;
///
/// 考核面板
///
public GameObject examinepanl;
///
/// 技能冷却时间
///
public float examinetimer = 10f;
///
/// 技能冷却时间
///
public float skilltimer = 0f;
///
/// 技能冷却图片
///
public Image coolingimage;
///
/// 技能冷却开关
///
private bool isCooldown = false;
///
/// 系统提示
///
public Text warntext;
///
/// 体统提示时间
///
public float timer = 10f;
///
/// 倒计时
///
public Text timetext;
///
/// 提示是否交卷页面
///
public GameObject submissionpromptpanl;
///
/// 确定按钮
///
public Button submitbutton2;
///
/// 取消按钮
///
public Button cancelbutton2;
///
/// 三个菜单按钮
///
public Toggle electricitychargeaccounting, powersupplycontract, contract;
///
/// 初始化用户名
///
public Text nametext;
///
/// 初始化时间
///
public Text loginname;
///
/// 覆约合同预设体
///
public GameObject retreatyobj;
///
/// 供电预设体
///
private GameObject powersupplyobj;
///
/// 电费计算预设体
///
public GameObject electricchargeobj;
///
/// 供电考试预设体
///
public GameObject Powersupplyone;
///
/// 覆约电费供电页面练习预制体生成位置
///
public RectTransform retreatypanl;
public RectTransform powersupplypanl;
public RectTransform electricchargepanl;
///
/// 接收覆约合同的预设体
///
public GameObject retreatpreform;//履约合同练习考试通用
private GameObject powersuppreform;//供电练习
public GameObject electricchargeprrform;//电费核算练习考试通用
public GameObject Powersupplys;
///
/// 初始化每个考试toggle显示按钮
///
public Vector3 pos;
///
/// 电费核算答案查询页面
///
public GameObject viewimage;
///
/// 电费核算取消按钮
///
public Button forkoffbutton;
///
/// 电费核算展示按钮
///
public Button showbutton;
///
/// 展示电费核算展示页面
///
public GameObject displaychargepanl;
///
/// 关闭展示按钮和导出
///
public Button cancelbutton;
public Button derivebutton;
public InputField pathsinput;
///
/// 查看录屏结束按钮
///
public Button StopButton;
public InputField Tipsinput;//查看路径
///
/// 考试用时文本
///
public Text testduration;
private float totalTime; // 总时间,单位为秒
private float timeRemaining; // 剩余时间
///
/// 获取当前时间
///
private string Times
{
get { return System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); }
}
private float startTime;
private float elapsedTime;
private bool isExamRunning = false;
private void Awake()
{
Initialize();//初始化
switch (ScoreManager.Instance.Subject)
{
case Subject.Performanceexercise:
Practicesubject();//履约练习
break;
case Subject.Performanceexamination:
Reviewpaper();//覆约考试
break;
case Subject.Electricchargeexercise:
Powersupplytestpaper();//电费练习
break;
case Subject.Electricityfeetest:
Electricityexaminationpaper();//电费考试
break;
case Subject.Aluminoelectricexercise:
Powersupplyexercise();//供电练习
break;
case Subject.Aluminumelectricaltest:
Powersupplytest1();//供电考试
break;
default:
break;
}
}
///
/// 初始化场景
///
private void Initialize()
{
pos = contract.transform.position;
loginname.text = PlayerPrefs.GetString("time");
nametext.text = PlayerPrefs.GetString("Name");
retreatyobj = Resources.Load("UIpanel/practice1");
powersupplyobj = Resources.Load("UIpanel/practicepanl2");
electricchargeobj = Resources.Load("UIpanel/practicepanl3");
Powersupplyone = Resources.Load("UIpanel/Powersupplytest");
if (ScoreManager.Instance.Subject == Subject.Performanceexamination || ScoreManager.Instance.Subject == Subject.Electricityfeetest || ScoreManager.Instance.Subject == Subject.Aluminumelectricaltest)
{
totalTime = (float.Parse(PlayerPrefs.GetString("Time"))*60);
timeRemaining = totalTime;
Updatetiem();
}
startTime = Time.time;
isExamRunning = true;
StartCoroutine(UpdateTimer());
}
IEnumerator UpdateTimer()
{
while (isExamRunning)
{
// 更新已经过的时间
elapsedTime = Time.time - startTime;
// 每帧更新一次
yield return null;
}
}
///
/// 90分钟倒计时
///
private void Updatetiem()
{
int minutes = Mathf.FloorToInt(timeRemaining / 60);
int hours = Mathf.FloorToInt(timeRemaining % 60);
testduration.text = String.Format("{0:00}:{1:00}", minutes, hours);
}
void Start()
{
submitbutton.onClick.AddListener(() =>
{
switch (ScoreManager.Instance.Subject)
{
case Subject.Performanceexercise:
Practicesubmission3();//履约合同提交
break;
case Subject.Performanceexamination:
break;
case Subject.Electricchargeexercise:
Practicesubmission1();//电费核算提交
break;
case Subject.Electricityfeetest:
break;
case Subject.Aluminoelectricexercise:
Practicesubmission2();//供电合同提交
break;
case Subject.Aluminumelectricaltest:
break;
default:
break;
}
});
backbutton.onClick.AddListener(() =>
{
switch (ScoreManager.Instance.Subject)
{
case Subject.Performanceexercise:
Repractice3();//履约合同返回按钮
break;
case Subject.Performanceexamination:
break;
case Subject.Electricchargeexercise:
Repractice1();//电费核算返回按钮
break;
case Subject.Electricityfeetest:
break;
case Subject.Aluminoelectricexercise:
Repractice2();//供电合同返回按钮
break;
case Subject.Aluminumelectricaltest:
break;
default:
break;
}
});
onvolumebutton.onClick.AddListener(() =>
{
submissionpromptpanl.SetActive(true);
});
cancelbutton2.onClick.AddListener(() =>
{
submissionpromptpanl.SetActive(false);
});
submitbutton2.onClick.AddListener(() =>
{
switch (ScoreManager.Instance.Subject)
{
case Subject.Performanceexercise:
break;
case Subject.Performanceexamination:
Handinanexaminationpaper1();//履约考试提交
break;
case Subject.Electricchargeexercise:
break;
case Subject.Electricityfeetest:
Handinanexaminationpaper2();//电费考试提交
break;
case Subject.Aluminoelectricexercise:
break;
case Subject.Aluminumelectricaltest:
Handinanexaminationpaper3();//供电考试提交
break;
default:
break;
}
});
Answer();//电费核算查看答案
Displaycharge();//展示电工核算功能
}
///
/// 展示电费核算功能
///
private void Displaycharge()
{
showbutton.onClick.AddListener(() =>
{
displaychargepanl.SetActive(true);
});
cancelbutton.onClick.AddListener(() =>
{
displaychargepanl.SetActive(false);
pathsinput.text = "";
});
derivebutton.onClick.AddListener(() =>
{
StartCoroutine(CaptureScreenshot());//导出发票
});
}
///
/// 电费核算查看按钮
///
private void Answer()
{
answerbutton.onClick.AddListener(() =>
{
viewimage.SetActive(true);
});
forkoffbutton.onClick.AddListener(() =>
{
viewimage.SetActive(false);
});
}
///
/// 导出发票
///
///
private IEnumerator CaptureScreenshot()
{
yield return new WaitForEndOfFrame();
// 创建一个新的Texture2D对象,尺寸为屏幕分辨率
Texture2D screenshot = new Texture2D(1700, 927, TextureFormat.RGB24, false);
// 从屏幕读取像素数据到Texture2D对象中
screenshot.ReadPixels(new Rect(112, 16, 1698, 924), 0, 0);
screenshot.Apply();
string path = Application.persistentDataPath + "/发票" + Times + ".png";
byte[] bytes = screenshot.EncodeToPNG();
File.WriteAllBytes(path, bytes);
pathsinput.text = path;
}
///
/// 电费核算交卷提交
///
public void Handinanexaminationpaper2()
{
Electricitychargeaccounting.Instance.Electricitytestscore();
electricchargeprrform = GameObject.Find("practicepanl3(Clone)");
Destroy(electricchargeprrform);
submissionpromptpanl.SetActive(false);
onvolumebutton.gameObject.SetActive(false);
testduration.gameObject.SetActive(false);
isCooldown = true;
Electricitybillaccountingsubmission();//电费核算交卷考试
examinepanl.SetActive(true);
}
private void Electricitybillaccountingsubmission()
{
EndExam();
string url = Submittheperformanceexam;
Performancetestscore performancetestscore = new Performancetestscore();
performancetestscore.userName = PlayerPrefs.GetString("Name");
performancetestscore.userId = PlayerPrefs.GetString("UserId");
performancetestscore.examId = PlayerPrefs.GetInt("Id");
performancetestscore.examClassId = PlayerPrefs.GetInt("calssID");
performancetestscore.useTime = int.Parse(elapsedTime.ToString("F0"));
for (int i = 0; i < ScoreManager.Instance.Electricitytestscores.Count; i++)
{
StepListItem stepListItem = new StepListItem();
stepListItem.stepName = ScoreManager.Instance.testcenters[i];
stepListItem.testPoint = ScoreManager.Instance.Performancetestsites[i];
stepListItem.setScore = float.Parse(ScoreManager.Instance.totalpoints[i]);
stepListItem.score = ScoreManager.Instance.Electricitytestscores[i];
Debug.Log(stepListItem.score);
performancetestscore.stepList.Add(stepListItem);
}
string dataitem = JsonConvert.SerializeObject(performancetestscore);
Debug.Log(dataitem);
StartCoroutine(PoststringByRaw(url, dataitem));
}
///
/// 供电合同考试提交
///
public void Handinanexaminationpaper3()
{
Powersupplytest.Instance.Powersupplyhandover();
Powersupplys = GameObject.Find("Powersupplytest(Clone)");
Destroy(Powersupplys);
submissionpromptpanl.SetActive(false);
onvolumebutton.gameObject.SetActive(false);
testduration.gameObject.SetActive(false);
isCooldown = true;
Powersupplyuploadinterface();//供电提交试卷
examinepanl.SetActive(true);
}
///
/// 供电考试提交试卷
///
private void Powersupplyuploadinterface()
{
EndExam();
string url = Submittheperformanceexam;
Performancetestscore performancetestscore = new Performancetestscore();
performancetestscore.userName = PlayerPrefs.GetString("Name");
performancetestscore.userId = PlayerPrefs.GetString("UserId");
performancetestscore.examId = PlayerPrefs.GetInt("Id");
performancetestscore.examClassId = PlayerPrefs.GetInt("calssID");
performancetestscore.useTime = int.Parse(elapsedTime.ToString("F0"));
for (int i = 0; i < ScoreManager.Instance.Powersupplytests.Count; i++)
{
StepListItem stepListItem = new StepListItem();
stepListItem.stepName = ScoreManager.Instance.testcenters[i];
stepListItem.testPoint = ScoreManager.Instance.Performancetestsites[i];
stepListItem.setScore = float.Parse(ScoreManager.Instance.totalpoints[i]);
stepListItem.score = ScoreManager.Instance.Powersupplytests[i];
Debug.Log(stepListItem.score);
performancetestscore.stepList.Add(stepListItem);
}
string dataitem =JsonConvert.SerializeObject(performancetestscore);
Debug.Log(dataitem);
StartCoroutine(PoststringByRaw(url, dataitem));
}
public void Handinanexaminationpaper1()
{
retreatpreform = GameObject.Find("practice1(Clone)");
Destroy(retreatpreform);
submissionpromptpanl.SetActive(false);
onvolumebutton.gameObject.SetActive(false);
testduration.gameObject.SetActive(false);
isCooldown = true;
Uploadscore();//上传履约考试试卷
examinepanl.SetActive(true);
}
private void Uploadscore()
{
EndExam();
string url = Submittheperformanceexam;
Performancetestscore performancetestscore = new Performancetestscore();
performancetestscore.userName = PlayerPrefs.GetString("Name");
performancetestscore.userId = PlayerPrefs.GetString("UserId");
performancetestscore.examId = PlayerPrefs.GetInt("Id");
performancetestscore.examClassId = PlayerPrefs.GetInt("calssID");
performancetestscore.useTime = int.Parse(elapsedTime.ToString("F0"));
for (int i = 0; i < ScoreManager.Instance.Honourscores.Count; i++)
{
StepListItem stepListItem = new StepListItem();
stepListItem.stepName = ScoreManager.Instance.testcenters[i];
stepListItem.testPoint = ScoreManager.Instance.Performancetestsites[i];
stepListItem.setScore = float.Parse(ScoreManager.Instance.totalpoints[i]);
stepListItem.score = ScoreManager.Instance.Honourscores[i];
Debug.Log(stepListItem.score);
performancetestscore.stepList.Add(stepListItem);
}
string dataitem = JsonConvert.SerializeObject(performancetestscore);
Debug.Log(dataitem);
StartCoroutine(PoststringByRaw(url, dataitem));
}
void EndExam()
{
// 停止计时
isExamRunning = false;
elapsedTime = Time.time - startTime;
Debug.Log("考试用时:" + elapsedTime.ToString("F0") + " 秒");
}
///
/// 履约合同返回初始化页面
///
private void Repractice3()
{
Instantiate(retreatyobj, retreatypanl);
practicepanl.SetActive(false);
submitbutton.gameObject.SetActive(true);
ScoreManager.Instance.SubtractScore(100);
}
///
/// 供电合同练习返回初始化页面
///
private void Repractice2()
{
practicepanl.SetActive(false);
submitbutton.gameObject.SetActive(true);
}
///
/// 电费核算练习返回初始化页面
///
private void Repractice1()
{
practicepanl.SetActive(false);
submitbutton.gameObject.SetActive(true);
answerbutton.gameObject.SetActive(false);
showbutton.gameObject.SetActive(false);
Instantiate(electricchargeobj, electricchargepanl);
ScoreManager.Instance.SubtractElectriccharge(100);
}
///
/// 履约合同提交
///
private void Practicesubmission3()
{
retreatpreform = GameObject.Find("practice1(Clone)");
Destroy(retreatpreform);
answerbutton.gameObject.SetActive(false);
submitbutton.gameObject.SetActive(false);
marktext.text = ScoreManager.Instance.Score.ToString();
StopButton.gameObject.SetActive(true);
practicepanl.SetActive(true);
}
///
/// 供电合同提交
///
private void Practicesubmission2()
{
StopButton.gameObject.SetActive(false);
Tipsinput.text = "";
submitbutton.gameObject.SetActive(false);
answerbutton.gameObject.SetActive(false);
}
///
/// 电费练习提交
///
private void Practicesubmission1()
{
submitbutton.gameObject.SetActive(false);
electricchargeprrform = GameObject.Find("practicepanl3(Clone)");
Destroy(electricchargeprrform);
Electricitychargeaccounting.Instance.Calculatescore2();
StopButton.gameObject.SetActive(false);
Tipsinput.text = "";
answerbutton.gameObject.SetActive(true);
if (ScoreManager.Instance.Electriccharge == 100)
{
showbutton.gameObject.SetActive(true);
}
marktext.text = ScoreManager.Instance.Electriccharge.ToString();
practicepanl.SetActive(true);
}
///
/// 覆约合同考试页面初始化
///
public void Reviewpaper()
{
contract.isOn = true;
contract.gameObject.SetActive(true);
electricitychargeaccounting.gameObject.SetActive(false);
powersupplycontract.gameObject.SetActive(false);
onvolumebutton.gameObject.SetActive(true);
retreatypanl.gameObject.SetActive(true);
powersupplypanl.gameObject.SetActive(false);
electricchargepanl.gameObject.SetActive(false);
Instantiate(retreatyobj, retreatypanl);
}
///
/// 履约合同练习页面
///
public void Practicesubject()
{
contract.isOn = true;
submitbutton.gameObject.SetActive(true);
retreatypanl.gameObject.SetActive(true);
powersupplypanl.gameObject.SetActive(false);
electricchargepanl.gameObject.SetActive(false);
Instantiate(retreatyobj, retreatypanl);
Instantiate(electricchargeobj, electricchargepanl);
powersuppreform = Instantiate(powersupplyobj, powersupplypanl);
}
///
/// 电费练习页面初始化
///
public void Powersupplytestpaper()
{
electricitychargeaccounting.isOn = true;
submitbutton.gameObject.SetActive(true);
onvolumebutton.gameObject.SetActive(false);
retreatypanl.gameObject.SetActive(false);
powersupplypanl.gameObject.SetActive(false);
electricchargepanl.gameObject.SetActive(true);
Instantiate(electricchargeobj, electricchargepanl);
Instantiate(retreatyobj, retreatypanl);
powersuppreform = Instantiate(powersupplyobj, powersupplypanl);
}
///
/// 电费核算考试页面初始化
///
public void Electricityexaminationpaper()
{
electricitychargeaccounting.transform.position = pos;
electricitychargeaccounting.isOn = true;
contract.gameObject.SetActive(false);
electricitychargeaccounting.gameObject.SetActive(true);
powersupplycontract.gameObject.SetActive(false);
retreatypanl.gameObject.SetActive(false);
powersupplypanl.gameObject.SetActive(false);
onvolumebutton.gameObject.SetActive(true);
electricchargepanl.gameObject.SetActive(true);
Instantiate(electricchargeobj, electricchargepanl);
}
///
/// 供电合同练习初始化页面
///
private void Powersupplyexercise()
{
powersupplycontract.isOn = true;
submitbutton.gameObject.SetActive(false);
onvolumebutton.gameObject.SetActive(false);
retreatypanl.gameObject.SetActive(false);
electricchargepanl.gameObject.SetActive(false);
powersupplypanl.gameObject.SetActive(true);
Instantiate(electricchargeobj, electricchargepanl);
Instantiate(retreatyobj, retreatypanl);
powersuppreform = Instantiate(powersupplyobj, powersupplypanl);
}
///
/// 供电合同考试初始化页面
///
private void Powersupplytest1()
{
powersupplycontract.transform.position = pos;
powersupplycontract.isOn = true;
submitbutton.gameObject.SetActive(false);
retreatypanl.gameObject.SetActive(false);
electricchargepanl.gameObject.SetActive(false);
electricitychargeaccounting.gameObject.SetActive(false);
contract.gameObject.SetActive(false);
powersupplypanl.gameObject.SetActive(true);
onvolumebutton.gameObject.SetActive(true);
Instantiate(Powersupplyone, powersupplypanl);
}
void Update()
{
//十秒后自动退出程序
if (isCooldown)
{
coolingimage.fillAmount = 1 - (skilltimer / examinetimer);
skilltimer += Time.deltaTime;
timer -= Time.deltaTime;
warntext.text = timer.ToString("0") + "秒后自动退出系统";
timetext.text = timer.ToString("0");
if (coolingimage.fillAmount == 0)
{
// 退出应用程序
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
if (ScoreManager.Instance.Subject == Subject.Performanceexamination || ScoreManager.Instance.Subject == Subject.Electricityfeetest || ScoreManager.Instance.Subject == Subject.Aluminumelectricaltest)
{
if (timeRemaining > 0)
{
timeRemaining -= Time.deltaTime;
Updatetiem();
}
else if (timeRemaining <= 0)
{
timeRemaining = 0f;
Updatetiem();
switch (ScoreManager.Instance.Subject)
{
case Subject.Performanceexamination:
Handinanexaminationpaper1();
Debug.Log("履约合同交卷");
timeRemaining = 60;
break;
case Subject.Electricityfeetest:
Handinanexaminationpaper2();
Debug.Log("电费合同考试");
timeRemaining = 60;
break;
case Subject.Aluminumelectricaltest:
Handinanexaminationpaper3();
Debug.Log("供电合同考试");
timeRemaining = 60;
break;
default:
break;
}
}
}
}
///
/// 游戏销毁执行写入
///
public void OnDestroy()
{
string path = Application.streamingAssetsPath + @"\start.ini";
if (File.Exists(path))
{
File.WriteAllText(Application.streamingAssetsPath + "/start.ini", "0");
}
}
}