using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.IO; using System.Text; using System.Linq; public class CourseData{ private System.Object data; private List has_Course; private List has_Subject; private Dictionary course_dict; private Dictionary subject_dict; public CourseData() { subject_dict = new Dictionary(); course_dict = new Dictionary(); has_Course = new List(); has_Subject = new List(); } public void InitConfig(LitJson.JsonData data) { if(data.IsArray) { for(int i = 0; i< data.Count; i++) { var s_data = data[i]; SubjectData cur_data = new SubjectData(); cur_data.id = s_data["id"].ToString(); cur_data.subject_name = s_data["subject_name"].ToString(); if (!subject_dict.ContainsKey(cur_data.id)) subject_dict.Add(cur_data.id, cur_data); } } else { CDebug.Log("格式错误"); } } public SubjectData[] SubjectDatas{ get{return subject_dict.Values.ToArray();}} public void Init(LitJson.JsonData data) { if (data.IsArray) { for (int i = 0; i < data.Count; i++) { CourseConfigData cur_data = new CourseConfigData(); LitJson.JsonData s_data = data[i]; cur_data.id = s_data["id"].ToString(); cur_data.course_name = s_data["course_name"].ToString(); LitJson.JsonData open_audio = s_data["open_audio"]; if(open_audio != null && open_audio.IsInt) { cur_data.open_audio = ((int)open_audio).ToString(); } else { Debug.LogError("open_audio有错 " + cur_data.course_name); } cur_data.desc = s_data["desc"] == null? "": s_data["desc"].ToString(); cur_data.subject_id = s_data["subject_id"].ToString(); cur_data.sort_id = (int)s_data["sort_id"]; LitJson.JsonData isFinish = s_data["isFinish"]; if(isFinish != null && isFinish.IsInt) { cur_data.isFinish = ((int)isFinish) == 1; } else if(isFinish != null && isFinish.IsBoolean) { cur_data.isFinish = ((bool)isFinish); } else { Debug.LogError("isFinish有错 " + cur_data.course_name); } if (!course_dict.ContainsKey(cur_data.id)) course_dict.Add(cur_data.id, cur_data); } } else { CDebug.Log("格式错误"); } } public void InitHasSubJect(LitJson.JsonData data) { if (data.IsArray) { has_Subject.Clear(); for (int i = 0; i < data.Count; i++) { has_Subject.Add((int)data[i]); } } } public bool SubVisiable(int id) { return has_Subject.IndexOf(id) != -1; } public void InitHasCourse(LitJson.JsonData data) { if (data.IsArray) { has_Course.Clear(); for (int i = 0; i < data.Count; i++) { has_Course.Add((int)data[i]); } } } public bool CourseVisiable(int id) { return has_Course.IndexOf(id) != -1; } public List GetCourseBySubIndex(int index) { if(subject_dict.Keys.Count > index) { return GetListBySubjectId(subject_dict.ElementAt(index).Key); } return new List(); } public List GetCourseBySubId(string id) { if(subject_dict.ContainsKey(id)) { return GetListBySubjectId(id); } return new List(); } private List GetListBySubjectId(string id_str) { List t_list = new List(); foreach(CourseConfigData data in course_dict.Values) { // 5 是临时写的 随后要删除 if(data.subject_id == id_str && data.isFinish) //if (data.subject_id == id_str) { object someBoxedStruct = data; if (this.CourseVisiable(int.Parse(data.id))) { t_list.Add(data); } } } t_list.Sort((xx1, xx2)=>{ CourseConfigData x = (CourseConfigData)xx1; CourseConfigData y = (CourseConfigData)xx2; int x_id = int.Parse(x.id); int y_id = int.Parse(y.id); bool x_visible = this.CourseVisiable(x_id); bool y_visible = this.CourseVisiable(y_id); if(x_visible != y_visible) { return x_visible? -1:1;//不可见的往后放 } //排序优先级 if(x.sort_id > y.sort_id) { return 1; } else if(x.sort_id < y.sort_id) { return -1; } //id 大的往后放 if(x_id > y_id) { return 1; } else if(x_id < y_id) { return -1; } else { return 0; } }); /* for(int i = 0; i< t_list.Count; i++) { Debug.LogFormat("{0}, {1}", i, ((CourseConfigData)t_list[i]).id); } */ return t_list; } public List GetList() { List t_list = new List(); foreach (CourseConfigData data in course_dict.Values) { // 5 是临时写的 随后要删除 if (data.isFinish) //if (data.subject_id == id_str) { object someBoxedStruct = data; t_list.Add(data); } } return t_list; } public CourseConfigData GetCourseConfigById(short id) { if(course_dict.ContainsKey(id.ToString())) { return course_dict[id.ToString()]; } return new CourseConfigData(); } public string GetSubjectNameById(short id) { if(subject_dict.ContainsKey(id.ToString())) { return subject_dict[id.ToString()].subject_name; } return "未知课程"; } }