public static User GetCurrentUser() { User user = null; HttpContext ctx = HttpContext.Current; if (ctx != null) { string key = string.Format("{0}-SessionId:{1}", TNHelper.LoginKey, ctx.Session.SessionID); // user = ctx.Session[TNHelper.LoginKey] as User; user = CMSCache.Get(key) as User; if (user != null) { return(user); } if (ctx.User != null && !string.IsNullOrEmpty(ctx.User.Identity.Name)) { user = TNHelper.GetUserByEmail(ctx.User.Identity.Name); if (user != null) { CMSCache.Insert(key, user); } else { // ko tìm thấy thông tin user, redirect về tragn login ctx.Response.Redirect("/dang-nhap?logout=true", true); } } } return(user); }
private void SaveCurrentDataToCache() { int index = int.Parse(hfIndex.Value); int total = int.Parse(hfTotal.Value); int id = int.Parse(hfID.Value); int answer = -1; if (!string.IsNullOrEmpty(radList.SelectedValue)) { answer = int.Parse(radList.SelectedValue); } string key = string.Format("UserAnswerList-{0}", hfCache.Value); Dictionary <int, int> qa = CMSCache.Get(key) as Dictionary <int, int>; if (qa == null) { qa = new Dictionary <int, int>(); } if (!qa.Keys.Contains(id)) { qa.Add(id, answer); } if (answer >= 0) { CMSCache.Insert(key, qa); } }
protected void LoadData() { // load setting BizQuestionGameSettings settings = TNHelper.GetQuestionGameSettings(); if (settings != null && settings.Timer > 0) { hfTimer.Value = settings.Timer.ToString(); pnlQuestion.Attributes["style"] = "display: none"; } // load radom question and save to cache QuestionGame qgame = TNHelper.GetCurrentQuestionGame(); if (qgame != null && qgame.Questionses.Count > 0) { string key = string.Format("Question-{0}", Guid.NewGuid().ToString()); CMSCache.Insert(key, qgame); Question question = qgame.Questionses[0] as Question; LoadAnswerList(question); litQuestion.Text = question.QuestionName; litInfo.Text = string.Format("Bạn đang trả lời câu hỏi {0}/{1}", 1, qgame.Questionses.Count); hfIndex.Value = "0"; hfTotal.Value = qgame.Questionses.Count.ToString(); hfCache.Value = key; hfID.Value = question.Id.ToString(); } else { Utils.ShowMessage(lblMsg, "Mời bạn quay lại sau, bạn vui lòng xem thông báo ở cột bên phải để biết thêm chi tiết"); divContainer.Visible = false; } }
protected void LoadData() { string key = string.Format("Prediction-{0}", Guid.NewGuid().ToString()); hfCache.Value = key; // load setting BizPredictionGameSettings settings = TNHelper.GetPredictionGameSettings(); if (settings != null && settings.Timer > 0) { hfTimer.Value = settings.Timer.ToString(); pnlQuestion.Attributes["style"] = "display: none"; } // check user already play this game User user = Utils.GetCurrentUser(); PredictionGameUser pgu = TNHelper.GetPredictionGameUserByGameId(settings.PredictionGameID, user.Id); prePlayedInfo.Visible = false; if (pgu != null) { PredictionGameUser = pgu; prePlayedInfo.Visible = true; divContainer.Attributes["class"] = "invisible " + divContainer.Attributes["class"]; } // load radom question and save to cache PredictionGame pgame = TNHelper.GetCurrentPredictionGame(); if (pgame != null && pgame.Predictionses.Count > 0) { Prediction prediction = pgame.Predictionses[0] as Prediction; LoadAnswerList(prediction); litQuestion.Text = prediction.PredictionName; litInfo.Text = string.Format("Bạn đang trả lời câu hỏi {0}/{1}", 1, pgame.Predictionses.Count); CMSCache.Insert(key, pgame); hfIndex.Value = "0"; hfTotal.Value = pgame.Predictionses.Count.ToString(); hfID.Value = prediction.Id.ToString(); } else { Utils.ShowMessage(lblMsg, "Mời bạn quay lại sau, bạn vui lòng xem thông báo ở cột bên phải để biết thêm chi tiết."); divContainer.Visible = false; prePlayedInfo.Visible = false; } }
protected void btnNext_Click(object sender, EventArgs e) { pnlQuestion.Attributes.Remove("style"); int index = int.Parse(hfIndex.Value); int total = int.Parse(hfTotal.Value); if (index < total) { SaveCurrentDataToCache(); } index++; if (index < total) { hfIndex.Value = index.ToString(); litInfo.Text = string.Format("Bạn đang trả lời câu hỏi {0}/{1}", (index + 1), total); radList.ClearSelection(); string key = hfCache.Value; PredictionGame pgame = CMSCache.Get(key) as PredictionGame; if (pgame != null && pgame.Predictionses.Count == total && index < total) { Prediction prediction = pgame.Predictionses[index] as Prediction; LoadAnswerList(prediction); litQuestion.Text = prediction.PredictionName; hfID.Value = prediction.Id.ToString(); } } if (index + 1 == total) { btnNext.Text = "Xem kết quả"; if (PredictionGameUser is PredictionGameUser) { btnNext.Text = "Cập nhật"; } btnNext.CssClass = "buttonL last"; } if (index == total) { btnSubmit_Click(btnSubmit, e); } else { pnlQuestion.Visible = true; pnlSummary.Visible = false; } }
private int GetUserAnswer(Prediction p) { int answerValue = -1; string key = string.Format("UserAnswerList-{0}", hfCache.Value); Dictionary <int, int> qa = CMSCache.Get(key) as Dictionary <int, int>; if (qa != null && qa.Count > 0) { if (!qa.TryGetValue(p.Id, out answerValue)) { answerValue = -1; } } return(answerValue); }
private Answer GetUserAnswer(Question p) { int answerValue = -1; string key = string.Format("UserAnswerList-{0}", hfCache.Value); Dictionary <int, int> qa = CMSCache.Get(key) as Dictionary <int, int>; if (qa != null && qa.Count > 0) { if (!qa.TryGetValue(p.Id, out answerValue)) { answerValue = -1; } } return(DomainManager.GetObject <Answer>(answerValue)); }
protected void Application_Start() { Database.SetInitializer <ContentManagementDb>(null); WebSecurity.InitializeDatabaseConnection("ContentManagementSystem", "UserProfile", "UserId", "UserName", false); CMSCache.Load(); AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); RegisterLog4Net(); }
protected void btnSubmit_Click(object sender, EventArgs e) { int rightAnswer = 0; int bonusPoint = 0; int total = int.Parse(hfTotal.Value); int index = int.Parse(hfIndex.Value); int numOfAnswer = 0; string key = string.Format("UserAnswerList-{0}", hfCache.Value); Dictionary <int, int> qa = CMSCache.Get(key) as Dictionary <int, int>; if (qa != null) { numOfAnswer = qa.Count; } SavePlayGame(out rightAnswer, out bonusPoint); pnlQuestion.Visible = false; pnlSummary.Visible = true; if (bonusPoint > 0) { litBonuss.Text = string.Format("Bạn được cộng {0} điểm vào tài khoản", bonusPoint); } else { litBonuss.Text = "Bạn không được điểm thưởng trong trò chơi này"; } litSummry.Visible = false; // không hiển thị số câu trả lời đúng cho người chơi biết if (numOfAnswer == total) { litSummry.Text = string.Format("Bạn đã trả lời đúng {0}/{1} câu hỏi.", rightAnswer, total); } else { litSummry.Text = string.Format("Bạn đã trả lời {0}/{1} câu hỏi. Số câu trả lời đúng {2}", numOfAnswer, total, rightAnswer); } lblTimeout.Visible = false; pnlQuestion.Visible = false; pnlSummary.Visible = true; btnSubmit.Visible = false; }
private void SavePlayGame() { User curenttUser = Utils.GetCurrentUser(); curenttUser = DomainManager.GetObject <User>(curenttUser.Id); string cacheKey = hfCache.Value; PredictionGame pgame = CMSCache.Get(cacheKey) as PredictionGame; if (curenttUser != null && pgame != null) { PredictionGameUser pgu = new PredictionGameUser(); pgu.PlayDate = DateTime.Now; pgu.PredictionGame = pgame; pgu.User = curenttUser; TimeSpan?onsiteTime = null; if (Page.Session[TNHelper.PredictionStarTimeKey] is DateTime) { onsiteTime = DateTime.Now - (DateTime)Page.Session[TNHelper.PredictionStarTimeKey]; pgu.Time = onsiteTime.Value.Seconds; } if (pgame != null && pgame.Predictionses.Count > 0) { foreach (Prediction p in pgame.Predictionses.Cast <Prediction>().ToList()) { int answerValue = GetUserAnswer(p); Prediction prediction = DomainManager.GetObject <Prediction>(p.Id); if (answerValue >= 0) { PredictionGameUserDetail detail = new PredictionGameUserDetail(); detail.PredictionGameUser = pgu; detail.Prediction = prediction; detail.PredictionAnswer = DomainManager.GetObject <PredictionAnswer>(answerValue); pgu.PredictionGameUserDetailses.Add(detail); } } } if (PredictionGameUser is PredictionGameUser) { if (pgu.PredictionGame != null) { UpdateAnswer(pgu); TNHelper.LogAction(LogType.PredictionLog, string.Format("Cập nhận đáp án cho bộ đề <b>{0}</b>", pgu.PredictionGame.PredictionGameName)); } } else { if (pgu.PredictionGame != null) { DomainManager.Insert(pgu); TNHelper.LogAction(LogType.PredictionLog, "Chơi game thử tài dự đoán"); } } // remove all relate cache Utils.ResetCurrentUser(); CMSCache.RemoveByPattern(cacheKey); } }
private void SavePlayGame(out int rightAnswer, out int bonusPoint) { bonusPoint = 0; rightAnswer = 0; User curenttUser = Utils.GetCurrentUser(); curenttUser = DomainManager.GetObject <User>(curenttUser.Id); if (curenttUser != null) { int time = 0; QuestionUser qu = new QuestionUser(); qu.PlayDate = DateTime.Now; qu.User = curenttUser; qu.Time = time; string key2 = hfCache.Value; QuestionGame qgame = CMSCache.Get(key2) as QuestionGame; if (qgame != null) { qu.QuestionGame = qgame; foreach (Question p in qgame.Questionses.Cast <Question>()) { Answer userAnswer = GetUserAnswer(p); Question question = DomainManager.GetObject <Question>(p.Id); if (userAnswer != null) { QuestionUserDetail detail = new QuestionUserDetail(); detail.QuestionUser = qu; detail.Question = question; detail.Answer = userAnswer; qu.QuestionUserDetailses.Add(detail); } if (question != null && question.CorrectAnswer != null && userAnswer != null && question.CorrectAnswer.Id == userAnswer.Id) { bonusPoint += question.BonusPoint; rightAnswer++; } } } TimeSpan?onsiteTime = null; if (Page.Session[TNHelper.QuestionStarTimeKey] is DateTime) { onsiteTime = DateTime.Now - (DateTime)Page.Session[TNHelper.QuestionStarTimeKey]; qu.Time = onsiteTime.Value.Seconds; } qu.WinPoint = bonusPoint; if (qu.QuestionGame != null) { DomainManager.Insert(qu); curenttUser.PointQuestion += bonusPoint; // cộng vào tổng điểm của game trả lời câu hỏi curenttUser.Point += bonusPoint; // cộng điểm thưởng vào tổng điểm của game cá cược DomainManager.Insert(curenttUser); Utils.ResetCurrentUser(); if (bonusPoint == 0) { TNHelper.LogAction(LogType.QuestionLog, "Chơi game thử tài kiến thức"); } else { TNHelper.LogAction(LogType.QuestionLog, string.Format("Cộng {0} điểm thưởng vào tổng điểm game thử tài kiến thức.<br/>Cộng {0} điểm thưởng vào tổng điểm game phân tích trận đấu", bonusPoint)); } } // remove all relate cache CMSCache.RemoveByPattern(hfCache.Value); } }