using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using DataModel.Model;
using LitJson;
public class GameManage : MonoBehaviour
{
public static GameManage Instance;
///
/// 初始位置
///
public Transform initPos;
public float OnceTime = 0.06f;
public int lerpTime = 2;
public bool is单机模式;
public Canvas canvas;
public ScoreManage scoreManage;
#region 回调
///
/// 科目启停回调
///
[HideInInspector]
public Action action_subject;
///
/// 步骤启停回调
///
[HideInInspector]
public Action action_step;
///
/// 开始暂停回调
///
[HideInInspector]
public Action action_Pause;
///
/// 关闭房间回调
///
[HideInInspector]
public Action action_close;
#endregion
private void Awake()
{
Instance = this;
//房间域
LoadManage.programState = ProgramState.进行中;
//初始化注册
UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects().ToList().ForEach(a =>
{
InitAllSyncInRoot(a.transform, false);
});
if (!is单机模式)
{
//获取步骤信息
StartCoroutine(MyNetMQClient.CallGet("http://" + MyNetMQClient.CallIP + "/Handler/Practice.ashx?action=queryPracticesubjectStep&PracticeId=" + LoadManage.Instance.currentPractice.Id, str =>
{
var json = JsonMapper.ToObject>(str);
if (json.state)
{
LoadManage.Instance.psteps = json.data;
//获取所有的practiceseat
StartCoroutine(MyNetMQClient.CallGet("http://" + MyNetMQClient.CallIP + "/Handler/Practice.ashx?action=querypracticeseat&PracticeId=" + LoadManage.Instance.currentPractice.Id, str2 =>
{
var json2 = JsonMapper.ToObject>(str2);
if (json2.state)
{
LoadManage.Instance.pseats = json2.data;
LoadManage.Instance.myPracticeSeat = LoadManage.Instance.pseats.FindAll(a => a.UserAccount == LoadManage.Instance.me.user.user_name);
//初始化打分系统
scoreManage.Init();
//恢复当前训练信息
var tmpps = LoadManage.Instance.psubjects.FindAll(a => a.State == 0);
if (tmpps != null && tmpps.Count > 0)
{
foreach (var item in tmpps)
{
practiceseat tmppseat = LoadManage.Instance.myPracticeSeat.Find(a => a.Field_Char1 == item.Id);
if (tmppseat != null)
{
//本人当前科目
LoadManage.Instance.currentPracticeSubejct = item;
LoadManage.Instance.currentPracticeSeat = tmppseat;
//当前正在进行中步骤
LoadManage.Instance.currentPracticeSubjectStep = LoadManage.Instance.psteps.Find(a => a.PracticeSubjectId == item.Id && a.SeatId == tmppseat.SeatId && a.StepState=="0");
Debug.Log("当前科目:" + item.Name);
Debug.Log("当前岗位:" + tmppseat.SeatName);
if (LoadManage.Instance.currentPracticeSubjectStep != null)
{
Debug.Log("当前步骤正在进行中:" + LoadManage.Instance.currentPracticeSubjectStep.Name);
}
//激活当前步骤相应打分点(科目进行中且步骤未完成)
if (LoadManage.Instance.currentPractice.MissionModel == "考核")
{
//未完成的步骤
var noOverSteps = LoadManage.Instance.psteps.FindAll(a => a.PracticeSubjectId == item.Id && a.SeatId == tmppseat.SeatId && a.StepState != "1");
var allSB=scoreManage.transform.Find(item.Name).GetComponentsInChildren(true).ToList();
if (noOverSteps!=null && noOverSteps.Count>0)
{
noOverSteps.ForEach(a =>
{
allSB.FindAll(b => b.stepOrder == a.OrderIndex).ForEach(b =>
{
b.SetActive(true);
});
});
}
}
//开二维软件
SoftManage.Instance.StartSoft(LoadManage.Instance.currentPractice.Id, LoadManage.Instance.currentPracticeSubejct.Name, LoadManage.Instance.currentPracticeSeat.SeatName, LoadManage.Instance.currentPracticeSeat.UserAccount, 0);
break;
}
}
}
//当前切换当前科目
GetMaoDian();
}
else
{
string msg = json2.message;
Debug.LogError(msg);
}
}));
;
}
else
{
string msg = json.message;
Debug.LogError(msg);
}
}));
}
}
///
/// 获取锚点
///
private void GetMaoDian()
{
//获取房间数据
UpLoadMe();
}
private void OnDestroy()
{
action_subject = null;
action_step = null;
}
///
/// 注册科目启停回调
///
/// practiceid,praticeSubjectId,科目名,是否开启
public void SetSubjectAction(Action action_subject)
{
this.action_subject += action_subject;
}
///
/// 注册步骤启停回调
///
/// practiceid,praticeSubjectId,科目名,pratciesubjectstepId,是否开启
public void SetStepAction(Action action_step)
{
this.action_step += action_step;
}
///
/// 注册开始暂停回调 (true:开始 ,false:暂停)
///
///
public void SetPauseAction(Action action_pause)
{
this.action_Pause = action_pause;
}
///
/// 注册关闭回调
///
///
public void SetCloseAction(Action action_close)
{
this.action_close = action_close;
}
///
/// 切换科目状态
///
///
public void SubjectChange(string msg)
{
// practiceId + "," + praticeSubjectId + "," + subjectName + "," + (OpenOrClose ? "开" : "关")
string[] tmps = msg.Split(',');
string practiceId = tmps[0];
string praticeSubjectId = tmps[1];
string subjectName = tmps[2];
string OpenOrClose = tmps[3];
bool oc = true;
if (OpenOrClose == "开")
{
oc = true;
}
else if (OpenOrClose == "关")
{
oc = false;
}
else
{
Debug.LogError("错误:" + OpenOrClose);
return;
}
if (practiceId == LoadManage.Instance.currentPractice.Id)
{
practicesubject ps = LoadManage.Instance.psubjects.Find(a => a.Id == praticeSubjectId);
if (ps != null)
{
if (oc)
{
//开
if (LoadManage.Instance.currentPracticeSubejct != null && LoadManage.Instance.currentPracticeSubejct.OrderIndex != ps.OrderIndex)
{
Debug.LogError("此科目不能并行");
return;
}
ps.State = 0;
LoadManage.Instance.currentPracticeSubejct = ps;
practiceseat pseat = LoadManage.Instance.myPracticeSeat.Find(a => a.Field_Char1 == praticeSubjectId);
if (pseat != null)
{
//自己参与当前科目
LoadManage.Instance.currentPracticeSeat = pseat;
Debug.Log("科目开启成功:" + ps.Name);
if (action_subject != null)
{
action_subject.Invoke(practiceId, praticeSubjectId, subjectName, oc);
}
//开二维软件
SoftManage.Instance.StartSoft(LoadManage.Instance.currentPractice.Id, LoadManage.Instance.currentPracticeSubejct.Name, LoadManage.Instance.currentPracticeSeat.SeatName, LoadManage.Instance.currentPracticeSeat.UserAccount, 0);
//激活自己相应打分
if(LoadManage.Instance.currentPractice.MissionModel=="考核")
{
scoreManage.transform.Find(ps.Name).GetComponentsInChildren(true).ToList().ForEach(c =>
{
if (c.seatName == pseat.SeatName)
{
c.SetActive(true);
}
});
}
}
else
{
Debug.Log("未参与此科目:" + ps.Name);
}
}
else
{
//关
if (LoadManage.Instance.currentPracticeSubejct != null && LoadManage.Instance.currentPracticeSubejct.Id == ps.Id)
{
//自己参与当前科目
ps.State = 1;
LoadManage.Instance.currentPracticeSubejct = null;
practiceseat pseat = LoadManage.Instance.myPracticeSeat.Find(a => a.Field_Char1 == praticeSubjectId);
if (pseat != null)
{
LoadManage.Instance.currentPracticeSeat = null;
Debug.Log("科目关闭成功:" + ps.Name);
if (action_subject != null)
{
action_subject.Invoke(practiceId, praticeSubjectId, subjectName, oc);
}
//关闭所有相应打分
if (LoadManage.Instance.currentPractice.MissionModel == "考核")
{
scoreManage.transform.Find(ps.Name).GetComponentsInChildren(true).ToList().ForEach(c =>
{
c.SetActive(false);
});
//指挥提交总表
if (pseat.SeatNo == "0")
{
scoreManage.SubmitApprise(subjectName);
}
}
}
else
{
Debug.Log("未参与此科目:" + ps.Name);
}
}
}
}
else
{
Debug.LogError("错误:未找到科目");
}
}
}
///
/// 切换步骤状态
///
///
public void StepChange(string msg)
{
// practiceId + "," + praticeSubjectId + "," + subjectName + "," + pratciesubjectstepId + "," + (OpenOrClose ? "开" : "关")
string[] tmps = msg.Split(',');
string practiceId = tmps[0];
string praticeSubjectId = tmps[1];
string subjectName = tmps[2];
string pratciesubjectstepId = tmps[3];
string OpenOrClose = tmps[4];
bool oc = true;
if (OpenOrClose == "开")
{
oc = true;
}
else if (OpenOrClose == "关")
{
oc = false;
}
else
{
Debug.LogError("错误:" + OpenOrClose);
return;
}
if (practiceId == LoadManage.Instance.currentPractice.Id)
{
practicesubjectstep step = LoadManage.Instance.psteps.Find(a => a.ID == pratciesubjectstepId);
if (step != null)
{
step.StepState = (oc ? 0 : 1).ToString();
if (action_step != null)
{
action_step.Invoke(practiceId, praticeSubjectId, subjectName, pratciesubjectstepId, oc);
}
if (oc)
{
//开启步骤
LoadManage.Instance.currentPracticeSubjectStep = step;
}
else
{
//结束步骤
LoadManage.Instance.currentPracticeSubjectStep = null;
//提交此步骤子表分数
scoreManage.transform.Find(subjectName).GetComponentsInChildren(true).ToList().ForEach(c =>
{
if (c.stepOrder == step.OrderIndex)
{
c.Submit();
}
});
}
}
else
{
Debug.LogError("错误:未找到步骤");
}
}
}
///
/// 注册一个根物体下所有同步组件
///
///
public void InitAllSyncInRoot(Transform a, bool isPlayer, string id = "")
{
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
if (isPlayer)
{
b.gameObject.name = id + b.gameObject.name;
}
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
if (isPlayer)
{
b.gameObject.name = id + b.gameObject.name;
}
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
if (isPlayer)
{
b.gameObject.name = id + b.gameObject.name;
}
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
if (isPlayer)
{
b.gameObject.name = id + b.gameObject.name;
}
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
if (isPlayer)
{
b.gameObject.name = id + b.gameObject.name;
}
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
if (isPlayer)
{
b.gameObject.name = id + b.gameObject.name;
}
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
if (b.isAutoInit)
{
b.Init();
}
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
b.Init();
});
a.GetComponentsInChildren(true).ToList().ForEach(b =>
{
if (!b.自定义回调)
{
b.InitDynamic("sync_" + b.name, null, b.valueType);
}
});
}
///
/// 初始化科目
///
public void InitSubject()
{
//初始化科目数据
//设备位置状态
//初始化评估
}
[HideInInspector]
public float checkTime = 6;
private void Update()
{
#if !UNITY_EDITOR
// //检查是否去服务器连接
// checkTime -= Time.deltaTime;
// if (checkTime < 0)
// {
// Debug.Log("时间超过6秒");
// Quit();
// }
#endif
}
///
/// 上传自己的信息(加入房间,并获取房间信息)
///
public void UpLoadMe()
{
if (!is单机模式)
{
LoadManage.Instance.RSclient.Send(LoadManage.Instance.currentRoomArea,32,new byte[1]);
Debug.Log("发送加入房间");
}
//关闭界面
}
///
/// 退出房间
///
public void Quit()
{
Debug.Log("退出房间");
//初始化
LoadManage.programState = ProgramState.结束;
OneValueSyncObject.OneAxisSyncObjectList.Clear();
FunctionSync_PositionRoate.positionRoateSyncObejctList.Clear();
FunctionSync_Video.dicVideo.Clear();
FunctionSync_Media.functionSync_MediaDic.Clear();
FunctionSync_CreateObejct.createDic.Clear();
FunctionSync_CreateObejct.Instance = null;
action_subject = null;
action_step = null;
//发送退出房间指令
if (!is单机模式 && LoadManage.Instance.me != null)
{
//byte[] bytes = Encoding.UTF8.GetBytes(LoadManage.Instance.MyId);
//byte[] names = Encoding.UTF8.GetBytes(LoadManage.Instance.MyName);
//byte[] tmps = new byte[8 + bytes.Length+ names.Length];
//Array.Copy(BitConverter.GetBytes(bytes.Length), 0, tmps, 0, 4);
////id
//Array.Copy(bytes, 0, tmps, 4, bytes.Length);
////name
//Array.Copy(BitConverter.GetBytes(names.Length), 0, tmps, 4 + bytes.Length, 4);
//Array.Copy(names, 0, tmps, 8 + bytes.Length, names.Length);
//LoadManage.Instance.RSclient.Send(LoadManage.Instance.currentRoomArea, 10, tmps);
//Debug.Log("发送退出房间");
}
//初始化LoadManage脚本
LoadManage.Instance.currentRoomArea = "";
//LoadManage.Instance.MyId = "";
LoadManage.Instance.SyncId = 0;
checkTime = 10;
//断开通信
LoadManage.Instance.RemoveRoomServerClient();
if (LoadManage.Instance.systemMode == SystemMode.PC)
{
UnityEngine.SceneManagement.SceneManager.LoadScene("MainSencePC");
}
else if (LoadManage.Instance.systemMode == SystemMode.MR)
{
UnityEngine.SceneManagement.SceneManager.LoadScene("MainSenceMR");
}
}
}