public void BotProtectRisk(int gameRoomId, int turn, int jobType) { var userGameRiskInThisTurn = _service.Game().GetUserGameRisk(gameRoomId, turn).ToList(); var gameBattleInThisTurn = _service.Game().GetGameBattleOpenRisk(gameRoomId, turn).ToList(); var userGameRoom = _service.GameRoom().GetUserGameRoom(gameRoomId, true, jobType); var userId = userGameRoom.UserId; // var protectBotRate = jobType == (int)JobType.ExpertSpecialist ? 10 : 5; foreach (var item in userGameRiskInThisTurn) { //var riskOptionId = 0; var checkRiskInGameBattle = gameBattleInThisTurn.FirstOrDefault(x => x.RiskId == item.RiskId); var riskOptionId = AnalystRiskOption(jobType, checkRiskInGameBattle, item.RiskId); if (riskOptionId > 0) { var botExpertUserGameRisk = new UserGameRisk { RiskId = item.RiskId, Turn = turn, GameRoomId = gameRoomId, UserId = userId, CreateDate = DateTime.UtcNow, CreateBy = userId, RiskOptionId = riskOptionId }; // save bot expert user game Risk _service.Game().AddUserGameRisk(botExpertUserGameRisk); } } //Calculate Protect Risk var currentMoney = userGameRoom.MoneyValue; var botExpertUserGameRiskList = _service.Game().GetUserGameRisk(gameRoomId, turn, userId).ToList(); var moneyForProtectRisk = botExpertUserGameRiskList.Sum(x => x.RiskOption.ActionEffectValue); currentMoney = currentMoney - moneyForProtectRisk.GetValueOrDefault(); _service.GameRoom().UpdateUserGameRoom(userId, gameRoomId, currentMoney, turn); var moneyTotal = CalculateRiskCostToBot(userId, gameRoomId, currentMoney, gameBattleInThisTurn, botExpertUserGameRiskList); // update game room _service.GameRoom().UpdateUserGameRoom(userId, gameRoomId, moneyTotal, turn); var gameDone = _service.Game().CheckMaxTurn(gameRoomId, turn); if (gameDone) { _service.GameRoom().UpdateGameRoomDone(userId, gameRoomId); } }
public JsonResult HireExpertSuggestion(int expertLevel) { var moneyTotal = Singleton.Game().Money; var turn = Singleton.Game().Turn; var gameBattle = _service.Game().GetGameBattleOpenRisk(Singleton.Game().GameRoomId, turn); var expertSuggestionModel = new ExpertSuggestionModel { SuggestLevel = expertLevel, }; var listData = new List <ExpertSuggestion>(); if (expertLevel == 1) { listData.Add(new ExpertSuggestion { Level = gameBattle.FirstOrDefault().RiskOption.RiskLevel.GetValueOrDefault(), Ratio = gameBattle.FirstOrDefault().Ratio.GetValueOrDefault(), RiskType = string.Empty, TotalActualPayValue = string.Empty }); expertSuggestionModel.SuggestCost = 5000.ToString("n0"); moneyTotal -= 5000; } else if (expertLevel == 2) { listData.Add(new ExpertSuggestion { Level = gameBattle.FirstOrDefault().RiskOption.RiskLevel.GetValueOrDefault(), Ratio = gameBattle.FirstOrDefault().Ratio.GetValueOrDefault(), RiskType = Enum.GetName(typeof(Const.RiskType), (int)gameBattle.FirstOrDefault().Risk.RiskType), TotalActualPayValue = gameBattle.Sum(x => x.Ratio * x.ActionEffectValue).GetValueOrDefault().ToString("n0") }); expertSuggestionModel.SuggestCost = 10000.ToString("n0"); moneyTotal -= 10000; } else if (expertLevel == 3) { foreach (var item in gameBattle) { listData.Add(new ExpertSuggestion { Level = item.RiskOption.RiskLevel.GetValueOrDefault(), Ratio = item.Ratio.GetValueOrDefault(), RiskType = Enum.GetName(typeof(Const.RiskType), (int)item.Risk.RiskType), TotalActualPayValue = gameBattle.Sum(x => x.Ratio * x.ActionEffectValue).GetValueOrDefault().ToString("n0") }); } expertSuggestionModel.SuggestCost = 30000.ToString("n0"); moneyTotal -= 30000; } _service.GameRoom().UpdateUserGameRoom(Singleton.Game().UserId, Singleton.Game().GameRoomId, moneyTotal, turn); Singleton.UpdateGameSession(Singleton.Game().Team, Singleton.Game().Project, moneyTotal, turn, Singleton.Game().SoftwareType, Singleton.Game().PlayerImageUrl); expertSuggestionModel.MoneyTotalFormat = moneyTotal.ToString("n0"); expertSuggestionModel.ExpertSuggestion = listData; var response = CommonFunction.GetResponse(expertSuggestionModel.ExpertSuggestion.Count > 0, null, expertSuggestionModel); return(Json(response, JsonRequestBehavior.AllowGet)); }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (Singleton.Game().GameRoomId > 0 && Singleton.Game().Turn > 0) { var checkMaxTurn = _service.Game().CheckMaxTurn(Singleton.Game().GameRoomId, Singleton.Game().Turn); if (checkMaxTurn) { filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "action", "Index" }, { "controller", "GameResult" } }); } } }
// GET: Story public ActionResult Index(int id) { System.Threading.Thread.Sleep(2000); // ReCallUserGameRoom(id); var gameRoom = _service.GameRoom().GetRoomById(id); var gameBattleRisk = _service.Game().GetGameBattleByGameRoomId(id); var risk = _service.Risk().GetAll().OrderBy(x => Guid.NewGuid()).Take(10); var riskList = new List <Risk>(); riskList.AddRange(risk); riskList.AddRange(gameBattleRisk.GroupBy(x => x.RiskId).Select(x => x.FirstOrDefault().Risk)); var model = new GameRoomModel { GameRoomId = id, SoftwareType = gameRoom.SoftwareType, MoneyInGame = gameRoom.MoneyValue, Risks = riskList.GroupBy(x => x.RiskId).Select(x => x.FirstOrDefault()).ToList() }; return(View("Index", model)); }
public JsonResult GetRiskNews(int gameRoomId, int turn) { var gameInTurn = _service.Game().GetGameBattleInTurn(gameRoomId, turn); var riskNewsFormat = string.Empty; if (gameInTurn.Any()) { foreach (var item in gameInTurn) { if (item.RiskNews != null) { riskNewsFormat += item.RiskNews.RiskNewsDetail; if (item != gameInTurn.LastOrDefault()) { riskNewsFormat += ", "; } } } } var response = CommonFunction.GetResponse(riskNewsFormat != null, string.Empty, riskNewsFormat); return(Json(response, JsonRequestBehavior.AllowGet)); }
//[HttpPost] //public ActionResult OpenRisk(FormCollection form) //{ // var selectedRisk = form.AllKeys.Where(x => x.Contains("riskoption")).ToList(); // var moneySummary = 0; // if (selectedRisk.Any()) // { // foreach (var item in selectedRisk) // { // var moneyValue = form[item]; // if (moneyValue != null) // { // moneySummary += int.Parse(moneyValue); // } // //save database // } // } // var total = Singleton.User().Money - moneySummary; // UpdateGameUser(total); // ViewBag.Money = total; // return View(); //} public ActionResult OpenRisk() { // check risk, and reduce money var moneyTotal = Singleton.Game().Money; var model = new GameBattleViewModel(); var openRisk = _service.Game().GetGameBattleOpenRisk(Singleton.Game().GameRoomId, Singleton.Game().Turn); if (openRisk.Any()) { model.GameBattles = openRisk.ToList(); var gameBattleData = _service.Game().GetGameBattleData(openRisk.ToList()); var javaScriptSearilizer = new JavaScriptSerializer(); var searializedObject = javaScriptSearilizer.Serialize(gameBattleData); model.OpenRiskGameBattleModelArray = searializedObject; var userGameRisk = _service.Game().GetUserGameRisk(Singleton.Game().GameRoomId, Singleton.Game().Turn, Singleton.Game().UserId); model.UserGameRisk = userGameRisk.ToList(); foreach (var item in model.GameBattles) { var userGameBattleData = new UserGameBattleData { GameBattle = item, ProtectStatus = ProtecStatus.Lose.ToString() }; var effectItemMoney = item.Ratio.GetValueOrDefault() * item.ActionEffectValue.GetValueOrDefault(); var riskProtect = userGameRisk.FirstOrDefault(x => x.RiskId == item.RiskId); var effectMoney = 0; if (riskProtect != null && riskProtect.RiskOption.RiskLevel != (int)RiskGameLevel.ZeroLevel) { if (riskProtect.RiskOption.RiskLevel != item.RiskOption.RiskLevel) { if (riskProtect.RiskOption.RiskLevel > item.RiskOption.RiskLevel) { // ไม่ต้องจ่าย ป้องกันได้ 100% //moneyTotal = Singleton.Game().Money; userGameBattleData.ProtectStatus = ProtecStatus.Win.ToString(); } else { if (riskProtect.RiskOption.RiskLevel == (int)RiskGameLevel.ThirdLevel) { //ป้องกัน 100% // moneyTotal = Singleton.Game().Money; userGameBattleData.ProtectStatus = ProtecStatus.Win.ToString(); } else if (riskProtect.RiskOption.RiskLevel == (int)RiskGameLevel.SecondLevel) { //ป้องกัน 50% จ่าย 50% effectMoney = (int)(effectItemMoney * 0.5); moneyTotal = moneyTotal - (int)(effectItemMoney * 0.5); } else if (riskProtect.RiskOption.RiskLevel == (int)RiskGameLevel.FirstLevel) { //ป้องกัน 25% จ่าย 75% effectMoney = (int)(effectItemMoney * 0.75); moneyTotal = moneyTotal - (int)(effectItemMoney * 0.75); } else if (riskProtect.RiskOption.RiskLevel == (int)RiskGameLevel.ZeroLevel) { effectMoney = (int)(effectItemMoney * 1); moneyTotal = moneyTotal - (int)(effectItemMoney * 1); } } } else { // ถ้าเลือกแล้ว Level เท่ากัน ป้องกันได้ 100% // moneyTotal = Singleton.Game().Money; userGameBattleData.ProtectStatus = ProtecStatus.Draw.ToString(); } } else { // ถ้าไม่ได้เลือก หรือ ไม่ได้ป้องกัน จ่าย 100% effectMoney = effectItemMoney; moneyTotal = moneyTotal - effectItemMoney; } // ถ้าแพ้ และ มีข่าว จะโดนผลกระทบเพิ่ม if (item.RiskNewsId != null && userGameBattleData.ProtectStatus == ProtecStatus.Lose.ToString()) { // fact impact var riskNews = _service.Risk().GetRiskNewsById(item.RiskNewsId.GetValueOrDefault()); if (riskNews != null) { var riskNewsImpactPercent = CommonFunction.RiskImpactFormat(riskNews.RiskNewsImpact.GetValueOrDefault()); var riskNewsImpact = (int)(effectItemMoney * riskNewsImpactPercent); moneyTotal = moneyTotal - riskNewsImpact; effectMoney = effectMoney + riskNewsImpact; userGameBattleData.RiskNewsImpactPercent = riskNewsImpactPercent; userGameBattleData.RiskNewsImpact = riskNewsImpact; // ค่าเงิน } } userGameBattleData.EffectMoney = effectMoney; model.UserGameBattleData.Add(userGameBattleData); } } // get risk selected from db model.GameDone = _service.Game().CheckMaxTurn(Singleton.Game().GameRoomId, Singleton.Game().Turn); var nextTurn = Singleton.Game().Turn; if (!model.GameDone) { nextTurn += 1; } _service.GameRoom().UpdateUserGameRoom(Singleton.Game().UserId, Singleton.Game().GameRoomId, moneyTotal, nextTurn); Singleton.UpdateGameSession(Singleton.Game().Team, Singleton.Game().Project, moneyTotal, nextTurn, Singleton.Game().SoftwareType, Singleton.Game().PlayerImageUrl); return(View(model)); }
public ActionResult AddRoom(FormCollection form) { var roomName = form["Add.RoomName"]; var startMoney = int.Parse(form["Add.StartMoney"]); var goal = int.Parse(form["Add.Goal"]); var softwareProcessType = int.Parse(form["Add.SoftwareProcessType"]); var playerName = form["Add.PlayerName"]; var jobType = form["Add.JobType"]; var multiPlayer = int.Parse(form["Add.MultiPlayer"]); var round = int.Parse(form["Add.Round"]); var imageUrl = form["Add.ImageUrl"]; var expertPlayer = form["Add.ExpertPlayer"].ToLower() == "false" ? false : true; //Initial Game to Game Battle //Set game started var gameRoom = _service.GameRoom().AddRoom(new Entity.GameRoom { Active = true, CreateDate = DateTime.UtcNow, GameRoomName = roomName, Goal = goal, MoneyValue = startMoney, Multiplayer = multiPlayer, StartDate = multiPlayer > 1 ? (DateTime?)null : DateTime.UtcNow, EndDate = multiPlayer > 1 ? (DateTime?)null : DateTime.UtcNow.AddMinutes(15), TeamValue = 2, //if startup SoftwareType = softwareProcessType, ProjectValue = 0, GameRound = round, IncludeBot = expertPlayer, UserId = Singleton.User().UserId // get from session }); if (gameRoom != null) { _service.Game().CreateGameAsync(gameRoom.GameRoomId, gameRoom.SoftwareType, gameRoom.GameRound); if (multiPlayer > 1) { //multiplayer > wait room _service.GameRoom().AddUserGameRoom(new Entity.UserGameRoom { GameRoomId = gameRoom.GameRoomId, PlayerName = playerName, JobType = int.Parse(jobType), MoneyValue = gameRoom.MoneyValue, ProjectValue = gameRoom.ProjectValue, TeamValue = gameRoom.TeamValue, TurnValue = 1, GameFinished = null, JoinDate = DateTime.UtcNow, UserId = Singleton.User().UserId, Active = true, ImageUrl = imageUrl, IsBot = false, }); Singleton.CreateGameSession(gameRoom.TeamValue, gameRoom.ProjectValue, gameRoom.MoneyValue, gameRoom.GameRoomId, playerName, gameRoom.SoftwareType, imageUrl); return(RedirectToAction("WaitRoom", "Room", new { id = gameRoom.GameRoomId })); } else { //Single > start game //Todo Create UserGameRoom and log _service.GameRoom().AddUserGameRoom(new Entity.UserGameRoom { GameRoomId = gameRoom.GameRoomId, PlayerName = playerName, JobType = int.Parse(jobType), MoneyValue = gameRoom.MoneyValue, ProjectValue = gameRoom.ProjectValue, TeamValue = gameRoom.TeamValue, GameFinished = null, JoinDate = DateTime.UtcNow, UserId = Singleton.User().UserId, Active = true, TurnValue = 1, ImageUrl = imageUrl, IsBot = false, }); if (expertPlayer == true) { //create 2 bot Async _service.BotExpert().CreateBotExpertAsync(gameRoom); } Singleton.CreateGameSession(gameRoom.TeamValue, gameRoom.ProjectValue, gameRoom.MoneyValue, gameRoom.GameRoomId, playerName, gameRoom.SoftwareType, imageUrl); return(RedirectToAction("Index", "Story", new { id = gameRoom.GameRoomId })); } } return(RedirectToAction("Index", "Home")); }
public void CreateWaterFallModel(int gameRoomId, int round) { try { var listReq = new List <Risk>(); var listDesign = new List <Risk>(); var listDev = new List <Risk>(); var listQA = new List <Risk>(); var listSupport = new List <Risk>(); var listAll = new List <Risk>(); // getAll Risk with Risk option var risks = _service.Risk().GetAllRiskWithOutZeroLevel().ToList(); listAll.AddRange(risks.Where(x => x.RiskType == (int)RiskType.General)); var generalReq = listAll.OrderBy(x => Guid.NewGuid()).Take(5).ToList(); // separate type listReq.AddRange(risks.Where(x => x.RiskType == (int)RiskType.Requirement)); listReq.AddRange(generalReq); var generalDesign = listAll.OrderBy(x => Guid.NewGuid()).Take(5).ToList(); listDesign.AddRange(risks.Where(x => x.RiskType == (int)RiskType.Design)); listDesign.AddRange(generalDesign); var generalDev = listAll.OrderBy(x => Guid.NewGuid()).Take(5).ToList(); listDev.AddRange(risks.Where(x => x.RiskType == (int)RiskType.Implement)); listDev.AddRange(generalDev); var generalQA = listAll.OrderBy(x => Guid.NewGuid()).Take(5).ToList(); listQA.AddRange(risks.Where(x => x.RiskType == (int)RiskType.Testing)); listQA.AddRange(generalQA); var generalSupport = listAll.OrderBy(x => Guid.NewGuid()).Take(5).ToList(); listSupport.AddRange(risks.Where(x => x.RiskType == (int)RiskType.Support)); listSupport.AddRange(generalSupport); var gameBattleList = new List <GameBattle>(); //take Random var turn = 0; for (int i = 0; i < round; i++) { if (i > 0) { turn += 5; } gameBattleList.AddRange(GenerateWorkProcess(gameRoomId, listReq, ((int)RiskType.Requirement + turn))); gameBattleList.AddRange(GenerateWorkProcess(gameRoomId, listDesign, ((int)RiskType.Design + turn))); gameBattleList.AddRange(GenerateWorkProcess(gameRoomId, listDev, ((int)RiskType.Implement + turn))); gameBattleList.AddRange(GenerateWorkProcess(gameRoomId, listQA, ((int)RiskType.Testing + turn))); gameBattleList.AddRange(GenerateWorkProcess(gameRoomId, listSupport, ((int)RiskType.Support + turn))); } //save DB foreach (var gameBattle in gameBattleList) { _service.Game().SaveGameBattleAsync(gameBattle); } } catch (Exception ex) { } }