MiniGameController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  2. using NEducation.Code;
  3. using NEducation.Models;
  4. using NEducation.NEduService;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Web;
  11. using System.Web.Mvc;
  12. namespace NEducation.Controllers
  13. {
  14. public class MiniGameController : Controller
  15. {
  16. private bool CheckAuthToken()
  17. {
  18. if (Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null)
  19. {
  20. if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
  21. {
  22. return false;
  23. }
  24. else
  25. {
  26. return true;
  27. }
  28. }
  29. else
  30. {
  31. return false;
  32. }
  33. }
  34. // GET: MiniGame
  35. //public ActionResult Index()
  36. //{
  37. // //Session["courseType"] = "10";
  38. // //return View("Index");
  39. // if (CheckAuthToken())
  40. // {
  41. // CheckMiniGameModel model = new CheckMiniGameModel();
  42. // CheckMiniGameRequest miniGameRequest = new CheckMiniGameRequest();
  43. // miniGameRequest.users = Session["msisdn"] as string;
  44. // miniGameRequest.serviceId = "3";
  45. // String rs = UtilsController.SendPost(miniGameRequest, Session.SessionID, UtilsController.WsType.GetMiniGameCondition);
  46. // System.Diagnostics.Debug.WriteLine("rs: " + rs);
  47. // UserActionResult res = new UserActionResult(rs);
  48. // MiniGame miniGame = new MiniGame(rs);
  49. // model.MiniGame = miniGame;
  50. // return View("Index", model);
  51. // }
  52. // else
  53. // {
  54. // Session.Clear();
  55. // return Redirect("/Home/Index");
  56. // }
  57. //}
  58. public ActionResult Index()
  59. {
  60. if (CheckAuthToken())
  61. {
  62. CheckMiniGameModel model = new CheckMiniGameModel();
  63. //List<SubInfo> listSub = Session["subInfo"] as List<SubInfo>;
  64. //if (listSub == null || listSub.Count <= 0)
  65. //{
  66. // ViewBag.checkOPP = "NO_SUB";
  67. // return View("Index", model);
  68. //}
  69. CheckMiniGameRequest miniGameRequest = new CheckMiniGameRequest
  70. {
  71. users = Session["msisdn"] as string,
  72. serviceId = "3"
  73. };
  74. string rs = UtilsController.SendPost(miniGameRequest, Session.SessionID, UtilsController.WsType.GetMiniGameCondition);
  75. System.Diagnostics.Debug.WriteLine("rs: " + rs);
  76. UserActionResult res = new UserActionResult(rs);
  77. MiniGame miniGame = new MiniGame(rs);
  78. model.MiniGame = miniGame;
  79. string format = "dd/MM/yyyy HH:mm:ss";
  80. DateTime sysdate = DateTime.ParseExact(model.MiniGame.Sysdate, format, null);
  81. DateTime startDate = DateTime.ParseExact(model.MiniGame.StartDate, format, null);
  82. DateTime endDate = DateTime.ParseExact(model.MiniGame.EndDate, format, null);
  83. bool isPlay = model.MiniGame.IsPlay == "1";
  84. bool status = model.MiniGame.Status == "0";
  85. string isSubscribed = model.MiniGame.UserIsActive;
  86. // Convert dates to strings in a format that JavaScript can understand
  87. ViewBag.Sysdate = sysdate.ToString("MM/dd/yyyy HH:mm:ss");
  88. ViewBag.StartDate = startDate.ToString("MM/dd/yyyy HH:mm:ss");
  89. ViewBag.EndDate = endDate.ToString("MM/dd/yyyy HH:mm:ss");
  90. ViewBag.ShowPlayButton = false;
  91. ViewBag.ShowClock = false;
  92. if (sysdate < startDate)
  93. {
  94. ViewBag.CountdownDate = startDate.ToString("MM/dd/yyyy HH:mm:ss");
  95. ViewBag.ShowClock = true;
  96. }
  97. else if (sysdate >= startDate && sysdate <= endDate )
  98. {
  99. ViewBag.CountdownDate = endDate.ToString("MM/dd/yyyy HH:mm:ss");
  100. if(isSubscribed == "0")
  101. {
  102. ViewBag.ShowPlayButton = true;
  103. ViewBag.checkOPP = "NO_SUB";
  104. }
  105. else if (isPlay && status)
  106. {
  107. ViewBag.CountdownDate = endDate.ToString("MM/dd/yyyy HH:mm:ss");
  108. ViewBag.ShowPlayButton = true;
  109. if(!status)
  110. {
  111. ViewBag.checkOPP = "DA_CHOI";
  112. }
  113. }
  114. else if(!status)
  115. {
  116. ViewBag.checkOPP = "DA_CHOI";
  117. ViewBag.ShowPlayButton = true;
  118. }
  119. }
  120. else
  121. {
  122. if (isSubscribed == "0")
  123. {
  124. ViewBag.ShowPlayButton = true;
  125. ViewBag.checkOPP = "NO_SUB";
  126. }
  127. else if (!status)
  128. {
  129. ViewBag.checkOPP = "DA_CHOI";
  130. }
  131. ViewBag.ShowClock = true;
  132. }
  133. return View("Index", model);
  134. }
  135. else
  136. {
  137. Session.Clear();
  138. return Redirect("/Home/Index");
  139. }
  140. }
  141. //Winner
  142. public ActionResult GetWinner()
  143. {
  144. if (CheckAuthToken())
  145. {
  146. WinnerMiniGameModel model = new WinnerMiniGameModel();
  147. CheckMiniGameRequest miniGameRequest = new CheckMiniGameRequest();
  148. miniGameRequest.users = Session["msisdn"] as string;
  149. miniGameRequest.serviceId = "3";
  150. String rs = UtilsController.SendPost(miniGameRequest, Session.SessionID, UtilsController.WsType.GetRankMiniGame);
  151. System.Diagnostics.Debug.WriteLine("rs: " + rs);
  152. UserActionResult res = new UserActionResult(rs);
  153. if (res.status == UtilsController.Constant.SUCCESS)
  154. {
  155. Winner winner = new Winner(rs);
  156. model.Winner = winner;
  157. foreach (var winnerElement in winner.listWinner)
  158. {
  159. if (winnerElement.msisdn == miniGameRequest.users)
  160. {
  161. model.currentWinner = winnerElement;
  162. break;
  163. }
  164. }
  165. return PartialView("_RankingDataPartial", model);
  166. }
  167. else
  168. {
  169. return PartialView("_RankingDataPartial", model);
  170. }
  171. }
  172. else
  173. {
  174. Session.Clear();
  175. return Redirect("/Home/Index");
  176. }
  177. }
  178. public ActionResult StartPlay()
  179. {
  180. Session.Remove("model");
  181. GrammarModel model = new GrammarModel();
  182. model.answerList = new List<AnswerResult>();
  183. model.currentQuestionIndex = 1;
  184. model.remainTime = UtilsController.questionTimeout;
  185. model.startTime = DateTime.Now;
  186. UserRequest userRequest = new UserRequest();
  187. userRequest.users = Session["msisdn"] as string;
  188. userRequest.lessonId = "-1";
  189. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.GetContentOfQuestion);
  190. UserActionResult res = new UserActionResult(rs);
  191. System.Diagnostics.Debug.WriteLine("grammar res: " + rs);
  192. if (res.status == "0")
  193. {
  194. ListLessonData lessons = new ListLessonData(rs);
  195. System.Diagnostics.Debug.WriteLine(lessons.questions);
  196. model.questions = lessons.questions;
  197. model.question = lessons.questions[model.currentQuestionIndex];
  198. System.Diagnostics.Debug.WriteLine(model.questions);
  199. Session["model"] = model;
  200. return Redirect("/Minigame/Play");
  201. }
  202. else
  203. {
  204. return Redirect("/Minigame/Index");
  205. }
  206. }
  207. public ActionResult Play()
  208. {
  209. //Session["courseType"] = "10";
  210. System.Diagnostics.Debug.WriteLine("Play: ");
  211. if (CheckAuthToken())
  212. {
  213. GrammarModel model = Session["model"] as GrammarModel;
  214. if (model == null)
  215. {
  216. System.Diagnostics.Debug.WriteLine(model);
  217. return Redirect("/Minigame/Index");
  218. }
  219. else
  220. {
  221. model.remainTime = UtilsController.questionTimeout - (int)DateTime.Now.Subtract(model.startTime).TotalSeconds;
  222. //int time = ((int)DateTime.Now.Subtract(model.startTime).TotalSeconds);
  223. return View("Play", model);
  224. }
  225. }
  226. else
  227. {
  228. Session.Clear();
  229. return Redirect("/Home/Index");
  230. }
  231. }
  232. public void ConfirmPlayQuestion(List<AnswerResult> questionResult)
  233. {
  234. QuestionResult result = new QuestionResult();
  235. result.ANSWER_LIST = questionResult;
  236. result.msisdn = Session["msisdn"] as String;
  237. result.users = Session["msisdn"] as String;
  238. result.LESSON_ID = "-1";
  239. // Assuming you have a method to send data to an external service
  240. var response = UtilsController.SendPost(result,
  241. Session.SessionID, UtilsController.WsType.GetAnsweerOfQuestion);
  242. // Log response for debugging
  243. System.Diagnostics.Debug.WriteLine("Response: " + response);
  244. }
  245. [HttpPost]
  246. public JsonResult SendAnswer(AnswerResult data)
  247. {
  248. // check model
  249. GrammarModel model = Session["model"] as GrammarModel;
  250. if (model == null)
  251. {
  252. return Json(new { error = "1" });
  253. }
  254. else
  255. {
  256. if (model.question.id != data.QUESTION_ID)
  257. {
  258. return Json(new { error = "1" });
  259. }
  260. // check result correct or not
  261. if (model.question.answerTrue == data.ANSWER)
  262. {
  263. // add answer list
  264. AnswerResult result = new AnswerResult();
  265. result.QUESTION_ID = model.question.id;
  266. result.ANSWER = data.ANSWER;
  267. result.RESULT = "1";
  268. result.TIME = (UtilsController.questionTimeout - model.remainTime).ToString();
  269. model.answerList.Add(result);
  270. // check finish
  271. if (model.currentQuestionIndex == model.questions.Count)
  272. {
  273. // submit result
  274. ConfirmPlayQuestion(model.answerList);
  275. // clear cache
  276. Session.Remove("model");
  277. return Json(new
  278. {
  279. error = "0",
  280. finish = "1"
  281. });
  282. }
  283. else
  284. {
  285. // next question
  286. model.currentQuestionIndex++;
  287. model.question = model.questions[model.currentQuestionIndex-1];
  288. model.startTime = DateTime.Now;
  289. model.remainTime = UtilsController.questionTimeout;
  290. Session["model"] = model;
  291. return Json(new
  292. {
  293. error = "0",
  294. question = model.question
  295. });
  296. }
  297. }
  298. else
  299. {
  300. // wrong
  301. ConfirmPlayQuestion(model.answerList);
  302. return Json(new { error = "2" });
  303. }
  304. }
  305. }
  306. public ActionResult Result(int check)
  307. {
  308. ViewBag.check = check;
  309. return View("result");
  310. }
  311. }
  312. }