123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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<int> has_Course;
- private List<int> has_Subject;
- private Dictionary<string, CourseConfigData> course_dict;
- private Dictionary<string, SubjectData> subject_dict;
- public CourseData()
- {
- subject_dict = new Dictionary<string, SubjectData>();
- course_dict = new Dictionary<string, CourseConfigData>();
- has_Course = new List<int>();
- has_Subject = new List<int>();
- }
- 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<System.Object> GetCourseBySubIndex(int index)
- {
- if(subject_dict.Keys.Count > index)
- {
- return GetListBySubjectId(subject_dict.ElementAt(index).Key);
- }
- return new List<object>();
- }
- public List<System.Object> GetCourseBySubId(string id)
- {
- if(subject_dict.ContainsKey(id))
- {
- return GetListBySubjectId(id);
- }
- return new List<object>();
- }
- private List<System.Object> GetListBySubjectId(string id_str)
- {
- List<System.Object> t_list = new List<System.Object>();
- 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<System.Object> GetList()
- {
- List<System.Object> t_list = new List<System.Object>();
- 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 "未知课程";
- }
- }
|