| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- using Microsoft.EntityFrameworkCore.Metadata.Internal;
- using NEducation.Code;
- using NEducation.Models;
- using NEducation.NEduService;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- namespace NEducation.Controllers
- {
- public class MiniGameController : Controller
- {
- private bool CheckAuthToken()
- {
- if (Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null)
- {
- if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- // GET: MiniGame
- //public ActionResult Index()
- //{
- // //Session["courseType"] = "10";
- // //return View("Index");
- // if (CheckAuthToken())
- // {
- // CheckMiniGameModel model = new CheckMiniGameModel();
- // CheckMiniGameRequest miniGameRequest = new CheckMiniGameRequest();
- // miniGameRequest.users = Session["msisdn"] as string;
- // miniGameRequest.serviceId = "3";
- // String rs = UtilsController.SendPost(miniGameRequest, Session.SessionID, UtilsController.WsType.GetMiniGameCondition);
- // System.Diagnostics.Debug.WriteLine("rs: " + rs);
- // UserActionResult res = new UserActionResult(rs);
- // MiniGame miniGame = new MiniGame(rs);
- // model.MiniGame = miniGame;
- // return View("Index", model);
- // }
- // else
- // {
- // Session.Clear();
- // return Redirect("/Home/Index");
- // }
- //}
- public ActionResult Index()
- {
- if (CheckAuthToken())
- {
- CheckMiniGameModel model = new CheckMiniGameModel();
- //List<SubInfo> listSub = Session["subInfo"] as List<SubInfo>;
- //if (listSub == null || listSub.Count <= 0)
- //{
- // ViewBag.checkOPP = "NO_SUB";
- // return View("Index", model);
- //}
- CheckMiniGameRequest miniGameRequest = new CheckMiniGameRequest
- {
- users = Session["msisdn"] as string,
- serviceId = "3"
- };
- string rs = UtilsController.SendPost(miniGameRequest, Session.SessionID, UtilsController.WsType.GetMiniGameCondition);
- System.Diagnostics.Debug.WriteLine("rs: " + rs);
- UserActionResult res = new UserActionResult(rs);
- MiniGame miniGame = new MiniGame(rs);
- model.MiniGame = miniGame;
- string format = "dd/MM/yyyy HH:mm:ss";
-
- DateTime sysdate = DateTime.ParseExact(model.MiniGame.Sysdate, format, null);
- DateTime startDate = DateTime.ParseExact(model.MiniGame.StartDate, format, null);
- DateTime endDate = DateTime.ParseExact(model.MiniGame.EndDate, format, null);
- bool isPlay = model.MiniGame.IsPlay == "1";
- bool status = model.MiniGame.Status == "0";
- string isSubscribed = model.MiniGame.UserIsActive;
- // Convert dates to strings in a format that JavaScript can understand
- ViewBag.Sysdate = sysdate.ToString("MM/dd/yyyy HH:mm:ss");
- ViewBag.StartDate = startDate.ToString("MM/dd/yyyy HH:mm:ss");
- ViewBag.EndDate = endDate.ToString("MM/dd/yyyy HH:mm:ss");
- ViewBag.ShowPlayButton = false;
- ViewBag.ShowClock = false;
- if (sysdate < startDate)
- {
- ViewBag.CountdownDate = startDate.ToString("MM/dd/yyyy HH:mm:ss");
- ViewBag.ShowClock = true;
- }
- else if (sysdate >= startDate && sysdate <= endDate )
- {
-
- ViewBag.CountdownDate = endDate.ToString("MM/dd/yyyy HH:mm:ss");
- if(isSubscribed == "0")
- {
- ViewBag.ShowPlayButton = true;
- ViewBag.checkOPP = "NO_SUB";
- }
- else if (isPlay && status)
- {
- ViewBag.CountdownDate = endDate.ToString("MM/dd/yyyy HH:mm:ss");
- ViewBag.ShowPlayButton = true;
- if(!status)
- {
- ViewBag.checkOPP = "DA_CHOI";
- }
- }
- else if(!status)
- {
- ViewBag.checkOPP = "DA_CHOI";
- ViewBag.ShowPlayButton = true;
- }
-
- }
- else
- {
- if (isSubscribed == "0")
- {
- ViewBag.ShowPlayButton = true;
- ViewBag.checkOPP = "NO_SUB";
- }
- else if (!status)
- {
- ViewBag.checkOPP = "DA_CHOI";
- }
- ViewBag.ShowClock = true;
- }
- return View("Index", model);
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- //Winner
- public ActionResult GetWinner()
- {
- if (CheckAuthToken())
- {
- WinnerMiniGameModel model = new WinnerMiniGameModel();
- CheckMiniGameRequest miniGameRequest = new CheckMiniGameRequest();
- miniGameRequest.users = Session["msisdn"] as string;
- miniGameRequest.serviceId = "3";
- String rs = UtilsController.SendPost(miniGameRequest, Session.SessionID, UtilsController.WsType.GetRankMiniGame);
- System.Diagnostics.Debug.WriteLine("rs: " + rs);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == UtilsController.Constant.SUCCESS)
- {
- Winner winner = new Winner(rs);
- model.Winner = winner;
- foreach (var winnerElement in winner.listWinner)
- {
- if (winnerElement.msisdn == miniGameRequest.users)
- {
- model.currentWinner = winnerElement;
- break;
- }
- }
- return PartialView("_RankingDataPartial", model);
- }
- else
- {
- return PartialView("_RankingDataPartial", model);
- }
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- public ActionResult StartPlay()
- {
- Session.Remove("model");
- GrammarModel model = new GrammarModel();
- model.answerList = new List<AnswerResult>();
- model.currentQuestionIndex = 1;
- model.remainTime = UtilsController.questionTimeout;
- model.startTime = DateTime.Now;
- UserRequest userRequest = new UserRequest();
- userRequest.users = Session["msisdn"] as string;
- userRequest.lessonId = "-1";
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.GetContentOfQuestion);
- UserActionResult res = new UserActionResult(rs);
- System.Diagnostics.Debug.WriteLine("grammar res: " + rs);
- if (res.status == "0")
- {
- ListLessonData lessons = new ListLessonData(rs);
- System.Diagnostics.Debug.WriteLine(lessons.questions);
- model.questions = lessons.questions;
- model.question = lessons.questions[model.currentQuestionIndex];
- System.Diagnostics.Debug.WriteLine(model.questions);
- Session["model"] = model;
- return Redirect("/Minigame/Play");
- }
- else
- {
- return Redirect("/Minigame/Index");
- }
- }
- public ActionResult Play()
- {
- //Session["courseType"] = "10";
- System.Diagnostics.Debug.WriteLine("Play: ");
- if (CheckAuthToken())
- {
- GrammarModel model = Session["model"] as GrammarModel;
- if (model == null)
- {
- System.Diagnostics.Debug.WriteLine(model);
- return Redirect("/Minigame/Index");
- }
- else
- {
- model.remainTime = UtilsController.questionTimeout - (int)DateTime.Now.Subtract(model.startTime).TotalSeconds;
- //int time = ((int)DateTime.Now.Subtract(model.startTime).TotalSeconds);
- return View("Play", model);
- }
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
-
- public void ConfirmPlayQuestion(List<AnswerResult> questionResult)
- {
- QuestionResult result = new QuestionResult();
- result.ANSWER_LIST = questionResult;
- result.msisdn = Session["msisdn"] as String;
- result.users = Session["msisdn"] as String;
- result.LESSON_ID = "-1";
- // Assuming you have a method to send data to an external service
- var response = UtilsController.SendPost(result,
- Session.SessionID, UtilsController.WsType.GetAnsweerOfQuestion);
- // Log response for debugging
- System.Diagnostics.Debug.WriteLine("Response: " + response);
-
- }
- [HttpPost]
- public JsonResult SendAnswer(AnswerResult data)
- {
- // check model
- GrammarModel model = Session["model"] as GrammarModel;
- if (model == null)
- {
- return Json(new { error = "1" });
- }
- else
- {
- if (model.question.id != data.QUESTION_ID)
- {
- return Json(new { error = "1" });
- }
- // check result correct or not
- if (model.question.answerTrue == data.ANSWER)
- {
- // add answer list
- AnswerResult result = new AnswerResult();
- result.QUESTION_ID = model.question.id;
- result.ANSWER = data.ANSWER;
- result.RESULT = "1";
- result.TIME = (UtilsController.questionTimeout - model.remainTime).ToString();
- model.answerList.Add(result);
- // check finish
- if (model.currentQuestionIndex == model.questions.Count)
- {
- // submit result
- ConfirmPlayQuestion(model.answerList);
- // clear cache
- Session.Remove("model");
- return Json(new
- {
- error = "0",
- finish = "1"
- });
- }
- else
- {
- // next question
- model.currentQuestionIndex++;
- model.question = model.questions[model.currentQuestionIndex-1];
- model.startTime = DateTime.Now;
- model.remainTime = UtilsController.questionTimeout;
- Session["model"] = model;
- return Json(new
- {
- error = "0",
- question = model.question
- });
- }
- }
- else
- {
- // wrong
- ConfirmPlayQuestion(model.answerList);
- return Json(new { error = "2" });
- }
- }
- }
- public ActionResult Result(int check)
- {
- ViewBag.check = check;
- return View("result");
- }
- }
- }
|