public bool FinishGame(GaiaDbContext.Models.HomeViewModels.GameInfoModel gameInfoModel, GaiaGame result) { //实际上已经结束,但是数据库没有更新的 if (result.GameStatus.stage == GaiaCore.Gaia.Stage.GAMEEND) { gameInfoModel.GameStatus = 8; //状态 gameInfoModel.round = 7; //代表结束 gameInfoModel.UserCount = result.Username.Length; //玩家数量 gameInfoModel.endtime = DateTime.Now; //结束时间 return(true); } return(false); }
/// <summary> /// 从内存更新游戏 /// </summary> /// <returns></returns> public IActionResult UpdateGame() { var list = GameMgr.GetAllGame(); foreach (KeyValuePair <string, GaiaGame> keyValuePair in list) { var result = keyValuePair.Value; GaiaDbContext.Models.HomeViewModels.GameInfoModel gameInfoModel; var listG = this.dbContext.GameInfoModel.Where(item => item.name == keyValuePair.Key).OrderByDescending(item => item.starttime).ToList(); if (listG.Count > 0) { gameInfoModel = listG[0]; } else { continue; } //如果不存在 bool isExist = true; if (gameInfoModel == null) { //测试游戏跳过 if (result.IsTestGame) { continue; } isExist = false; gameInfoModel = new GaiaDbContext.Models.HomeViewModels.GameInfoModel() { name = keyValuePair.Key, userlist = string.Join("|", result.Username), UserCount = result.Username.Length, MapSelction = keyValuePair.Value.MapSelection.ToString(), IsTestGame = keyValuePair.Value.IsTestGame ? 1 : 0, GameStatus = 0, starttime = DateTime.Now, endtime = DateTime.Now, round = 0, //username = HttpContext.User.Identity.Name, }; this.FinishGame(gameInfoModel, result); } else { //结束的游戏不需要更新 if (gameInfoModel.GameStatus == 8) { continue; } else { gameInfoModel.round = result.GameStatus.RoundCount; //如果游戏结束 if (this.FinishGame(gameInfoModel, result)) { //并且没有找到任何的种族信息 if (!this.dbContext.GameFactionModel.Any(item => item.gameinfo_id == gameInfoModel.Id)) { //保存种族信息 DbGameSave.SaveFactionToDb(this.dbContext, result, gameInfoModel); } } } } gameInfoModel.ATTList = string.Join("|", result.ATTList.Select(item => item.name)); gameInfoModel.FSTList = string.Join("|", result.FSTList.Select(item => item.GetType().Name)); gameInfoModel.RBTList = string.Join("|", result.RBTList.Select(item => item.name)); gameInfoModel.RSTList = string.Join("|", result.RSTList.Select(item => item.GetType().Name)); gameInfoModel.STT3List = string.Join("|", result.STT3List.GroupBy(item => item.name).Select(g => g.Max(item => item.name))); gameInfoModel.STT6List = string.Join("|", result.STT6List.GroupBy(item => item.name).Select(g => g.Max(item => item.name))); gameInfoModel.scoreFaction = string.Join(":", result.FactionList.OrderByDescending(item => item.Score) .Select(item => string.Format("{0}{1}({2})", item.ChineseName, item.Score, item.UserName))); //最后的得分情况 gameInfoModel.loginfo = string.Join("|", result.LogEntityList.Select(item => item.Syntax)); if (isExist) { this.dbContext.GameInfoModel.Update(gameInfoModel); } else { this.dbContext.GameInfoModel.Add(gameInfoModel); } } this.dbContext.SaveChanges(); return(Redirect("/GameInfo/Index?status=0")); }