CourseData.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.IO;
  6. using System.Text;
  7. using System.Linq;
  8. public class CourseData{
  9. private System.Object data;
  10. private List<int> has_Course;
  11. private List<int> has_Subject;
  12. private Dictionary<string, CourseConfigData> course_dict;
  13. private Dictionary<string, SubjectData> subject_dict;
  14. public CourseData()
  15. {
  16. subject_dict = new Dictionary<string, SubjectData>();
  17. course_dict = new Dictionary<string, CourseConfigData>();
  18. has_Course = new List<int>();
  19. has_Subject = new List<int>();
  20. }
  21. public void InitConfig(LitJson.JsonData data)
  22. {
  23. if(data.IsArray)
  24. {
  25. for(int i = 0; i< data.Count; i++)
  26. {
  27. var s_data = data[i];
  28. SubjectData cur_data = new SubjectData();
  29. cur_data.id = s_data["id"].ToString();
  30. cur_data.subject_name = s_data["subject_name"].ToString();
  31. if (!subject_dict.ContainsKey(cur_data.id))
  32. subject_dict.Add(cur_data.id, cur_data);
  33. }
  34. }
  35. else
  36. {
  37. CDebug.Log("格式错误");
  38. }
  39. }
  40. public SubjectData[] SubjectDatas{ get{return subject_dict.Values.ToArray();}}
  41. public void Init(LitJson.JsonData data)
  42. {
  43. if (data.IsArray)
  44. {
  45. for (int i = 0; i < data.Count; i++)
  46. {
  47. CourseConfigData cur_data = new CourseConfigData();
  48. LitJson.JsonData s_data = data[i];
  49. cur_data.id = s_data["id"].ToString();
  50. cur_data.course_name = s_data["course_name"].ToString();
  51. LitJson.JsonData open_audio = s_data["open_audio"];
  52. if(open_audio != null && open_audio.IsInt)
  53. {
  54. cur_data.open_audio = ((int)open_audio).ToString();
  55. }
  56. else
  57. {
  58. Debug.LogError("open_audio有错 " + cur_data.course_name);
  59. }
  60. cur_data.desc = s_data["desc"] == null? "": s_data["desc"].ToString();
  61. cur_data.subject_id = s_data["subject_id"].ToString();
  62. cur_data.sort_id = (int)s_data["sort_id"];
  63. LitJson.JsonData isFinish = s_data["isFinish"];
  64. if(isFinish != null && isFinish.IsInt)
  65. {
  66. cur_data.isFinish = ((int)isFinish) == 1;
  67. }
  68. else if(isFinish != null && isFinish.IsBoolean)
  69. {
  70. cur_data.isFinish = ((bool)isFinish);
  71. }
  72. else
  73. {
  74. Debug.LogError("isFinish有错 " + cur_data.course_name);
  75. }
  76. if (!course_dict.ContainsKey(cur_data.id))
  77. course_dict.Add(cur_data.id, cur_data);
  78. }
  79. }
  80. else
  81. {
  82. CDebug.Log("格式错误");
  83. }
  84. }
  85. public void InitHasSubJect(LitJson.JsonData data)
  86. {
  87. if (data.IsArray)
  88. {
  89. has_Subject.Clear();
  90. for (int i = 0; i < data.Count; i++)
  91. {
  92. has_Subject.Add((int)data[i]);
  93. }
  94. }
  95. }
  96. public bool SubVisiable(int id)
  97. {
  98. return has_Subject.IndexOf(id) != -1;
  99. }
  100. public void InitHasCourse(LitJson.JsonData data)
  101. {
  102. if (data.IsArray)
  103. {
  104. has_Course.Clear();
  105. for (int i = 0; i < data.Count; i++)
  106. {
  107. has_Course.Add((int)data[i]);
  108. }
  109. }
  110. }
  111. public bool CourseVisiable(int id)
  112. {
  113. return has_Course.IndexOf(id) != -1;
  114. }
  115. public List<System.Object> GetCourseBySubIndex(int index)
  116. {
  117. if(subject_dict.Keys.Count > index)
  118. {
  119. return GetListBySubjectId(subject_dict.ElementAt(index).Key);
  120. }
  121. return new List<object>();
  122. }
  123. public List<System.Object> GetCourseBySubId(string id)
  124. {
  125. if(subject_dict.ContainsKey(id))
  126. {
  127. return GetListBySubjectId(id);
  128. }
  129. return new List<object>();
  130. }
  131. private List<System.Object> GetListBySubjectId(string id_str)
  132. {
  133. List<System.Object> t_list = new List<System.Object>();
  134. foreach(CourseConfigData data in course_dict.Values)
  135. {
  136. // 5 是临时写的 随后要删除
  137. if(data.subject_id == id_str && data.isFinish)
  138. //if (data.subject_id == id_str)
  139. {
  140. object someBoxedStruct = data;
  141. if (this.CourseVisiable(int.Parse(data.id)))
  142. {
  143. t_list.Add(data);
  144. }
  145. }
  146. }
  147. t_list.Sort((xx1, xx2)=>{
  148. CourseConfigData x = (CourseConfigData)xx1;
  149. CourseConfigData y = (CourseConfigData)xx2;
  150. int x_id = int.Parse(x.id);
  151. int y_id = int.Parse(y.id);
  152. bool x_visible = this.CourseVisiable(x_id);
  153. bool y_visible = this.CourseVisiable(y_id);
  154. if(x_visible != y_visible)
  155. {
  156. return x_visible? -1:1;//不可见的往后放
  157. }
  158. //排序优先级
  159. if(x.sort_id > y.sort_id)
  160. {
  161. return 1;
  162. }
  163. else if(x.sort_id < y.sort_id)
  164. {
  165. return -1;
  166. }
  167. //id 大的往后放
  168. if(x_id > y_id)
  169. {
  170. return 1;
  171. }
  172. else if(x_id < y_id)
  173. {
  174. return -1;
  175. }
  176. else
  177. {
  178. return 0;
  179. }
  180. });
  181. /*
  182. for(int i = 0; i< t_list.Count; i++)
  183. {
  184. Debug.LogFormat("{0}, {1}", i, ((CourseConfigData)t_list[i]).id);
  185. }
  186. */
  187. return t_list;
  188. }
  189. public List<System.Object> GetList()
  190. {
  191. List<System.Object> t_list = new List<System.Object>();
  192. foreach (CourseConfigData data in course_dict.Values)
  193. {
  194. // 5 是临时写的 随后要删除
  195. if (data.isFinish)
  196. //if (data.subject_id == id_str)
  197. {
  198. object someBoxedStruct = data;
  199. t_list.Add(data);
  200. }
  201. }
  202. return t_list;
  203. }
  204. public CourseConfigData GetCourseConfigById(short id)
  205. {
  206. if(course_dict.ContainsKey(id.ToString()))
  207. {
  208. return course_dict[id.ToString()];
  209. }
  210. return new CourseConfigData();
  211. }
  212. public string GetSubjectNameById(short id)
  213. {
  214. if(subject_dict.ContainsKey(id.ToString()))
  215. {
  216. return subject_dict[id.ToString()].subject_name;
  217. }
  218. return "未知课程";
  219. }
  220. }