public string UpdateFromDb(Func <GameInfoModel, bool> func) { List <GameInfoModel> list = this.dbContext.GameInfoModel.Where(func).ToList();//item => item.GameStatus != 8 foreach (GameInfoModel gameInfoModel in list) { //如果日志不是空的 if (!string.IsNullOrEmpty(gameInfoModel.loginfo)) { NewGameViewModel newGameViewModel = new NewGameViewModel() { dropHour = 72, IsAllowLook = gameInfoModel.IsAllowLook, isHall = gameInfoModel.isHall, IsRandomOrder = gameInfoModel.IsRandomOrder, IsRotatoMap = gameInfoModel.IsRotatoMap, IsSocket = false, IsTestGame = gameInfoModel.IsTestGame == 1, jinzhiFaction = gameInfoModel.jinzhiFaction, MapSelction = gameInfoModel.MapSelction, Name = gameInfoModel.name }; GameMgr.CreateNewGame(gameInfoModel.userlist.Split('|'), newGameViewModel, out GaiaGame result); // GameMgr.CreateNewGame(gameInfoModel.name, gameInfoModel.userlist.Split('|'), out GaiaGame result, gameInfoModel.MapSelction, isTestGame: gameInfoModel.IsTestGame == 1 ? true : false,version:gameInfoModel.version); GaiaGame gg = GameMgr.GetGameByName(gameInfoModel.name); //gg.dbContext = this.dbContext; gg.GameName = gameInfoModel.name; if (gameInfoModel.saveState == 0) { gg.dbContext = this.dbContext; gg.IsSaveToDb = true; gameInfoModel.saveState = 1; } gg.UserActionLog = gameInfoModel.loginfo.Replace("|", "\r\n"); gg = GameMgr.RestoreGame(gameInfoModel.name, gg); //是否应该结束 if (this.FinishGame(gameInfoModel, gg)) { this.dbContext.GameInfoModel.Update(gameInfoModel); //没有找到任何的种族信息 if (!this.dbContext.GameFactionModel.Any(item => item.gameinfo_id == gameInfoModel.Id)) { //保存种族信息 DbGameSave.SaveFactionToDb(this.dbContext, gg, gameInfoModel); } else { //总局计分问题,需要重新计算 #if DEBUG DbGameSave.SaveFactionToDb(this.dbContext, gg, gameInfoModel); #endif } } //GameMgr.DeleteOneGame(gameInfoModel.name); } } this.dbContext.SaveChanges(); return("success"); }
/// <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")); }