diff --git a/AmiyaBotPlayerRatingServer/Controllers/Game/GameHubController.cs b/AmiyaBotPlayerRatingServer/Controllers/Game/GameHubController.cs index 6ab309e..647fc61 100644 --- a/AmiyaBotPlayerRatingServer/Controllers/Game/GameHubController.cs +++ b/AmiyaBotPlayerRatingServer/Controllers/Game/GameHubController.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; namespace AmiyaBotPlayerRatingServer.Controllers.Game @@ -178,6 +179,18 @@ public async Task GetPlayerStatistics(string userId) }); } + public async Task GetServerStatistics() + { + var totalPlaying =await dbContext.GameInfos.Where(c => c.IsClosed != true).CountAsync(); + var totalGames = await dbContext.GameInfos.CountAsync(); + var validPlayers = await dbContext.ApplicationUserMinigameStatistics.CountAsync(); + return Ok(new + { + GamesPlaying = totalPlaying, + GamesTotal = totalGames, + PlayersTotal = validPlayers + }); + } } } diff --git a/AmiyaBotPlayerRatingServer/GameLogic/SchulteGrid/SchulteGridManager.cs b/AmiyaBotPlayerRatingServer/GameLogic/SchulteGrid/SchulteGridManager.cs index b41dcd3..d516dfc 100644 --- a/AmiyaBotPlayerRatingServer/GameLogic/SchulteGrid/SchulteGridManager.cs +++ b/AmiyaBotPlayerRatingServer/GameLogic/SchulteGrid/SchulteGridManager.cs @@ -235,66 +235,72 @@ public Task GetCloseGamePayload(Game rawGame) private void CreateStatistics(SchulteGridGame game) { - //统计第一名第二名和第三名 - var playerScoreList = game.PlayerScore.ToList(); - playerScoreList.Sort((a, b) => b.Value.CompareTo(a.Value)); + //题目没有回答超过一半的游戏不计入统计 + if (game.AnswerList.Count(a => a.Completed) < game.AnswerList.Count / 2) + { + return; + } - var firstPlace = playerScoreList.Count > 0 ? playerScoreList[0] : default; - var secondPlace = playerScoreList.Count > 1 ? playerScoreList[1] : default; - var thirdPlace = playerScoreList.Count > 2 ? playerScoreList[2] : default; + //统计第一名第二名和第三名 + var playerScoreList = game.PlayerScore.ToList(); + playerScoreList.Sort((a, b) => b.Value.CompareTo(a.Value)); - //统计每个玩家的正确和错误次数 - var playerAnswerList = game.PlayerMoveList.Where(x => x.IsOperator).GroupBy(p => p.PlayerId) - .Select(p => new - { - PlayerId = p.Key, - CorrectCount = p.Count(c => c.IsCorrect), - WrongCount = p.Count(c => !c.IsCorrect) - }).ToList(); + var firstPlace = playerScoreList.Count > 0 ? playerScoreList[0] : default; + var secondPlace = playerScoreList.Count > 1 ? playerScoreList[1] : default; + var thirdPlace = playerScoreList.Count > 2 ? playerScoreList[2] : default; - foreach (var pl in game.PlayerList) + //统计每个玩家的正确和错误次数 + var playerAnswerList = game.PlayerMoveList.Where(x => x.IsOperator).GroupBy(p => p.PlayerId) + .Select(p => new { - var playerId = pl.Key; + PlayerId = p.Key, + CorrectCount = p.Count(c => c.IsCorrect), + WrongCount = p.Count(c => !c.IsCorrect) + }).ToList(); - var playerSt = - dbContext.ApplicationUserMinigameStatistics.FirstOrDefault(x => x.UserId == playerId); - if (playerSt == null) - { - playerSt = new ApplicationUserMinigameStatistics() - { - Id = Guid.NewGuid().ToString(), - UserId = playerId, - TotalGamesPlayed = 0, - TotalGamesFirstPlace = 0, - TotalGamesSecondPlace = 0, - TotalGamesThirdPlace = 0, - TotalAnswersCorrect = 0, - TotalAnswersWrong = 0 - }; - dbContext.ApplicationUserMinigameStatistics.Add(playerSt); - } - - playerSt.TotalGamesPlayed++; - playerSt.TotalAnswersCorrect += - playerAnswerList.FirstOrDefault(x => x.PlayerId == playerId)?.CorrectCount ?? 0; - playerSt.TotalAnswersWrong += - playerAnswerList.FirstOrDefault(x => x.PlayerId == playerId)?.WrongCount ?? 0; - - if (firstPlace.Key == playerId) - { - playerSt.TotalGamesFirstPlace++; - } - else if (secondPlace.Key == playerId) - { - playerSt.TotalGamesSecondPlace++; - } - else if (thirdPlace.Key == playerId) + foreach (var pl in game.PlayerList) + { + var playerId = pl.Key; + + var playerSt = + dbContext.ApplicationUserMinigameStatistics.FirstOrDefault(x => x.UserId == playerId); + if (playerSt == null) + { + playerSt = new ApplicationUserMinigameStatistics() { - playerSt.TotalGamesThirdPlace++; - } + Id = Guid.NewGuid().ToString(), + UserId = playerId, + TotalGamesPlayed = 0, + TotalGamesFirstPlace = 0, + TotalGamesSecondPlace = 0, + TotalGamesThirdPlace = 0, + TotalAnswersCorrect = 0, + TotalAnswersWrong = 0 + }; + dbContext.ApplicationUserMinigameStatistics.Add(playerSt); + } + + playerSt.TotalGamesPlayed++; + playerSt.TotalAnswersCorrect += + playerAnswerList.FirstOrDefault(x => x.PlayerId == playerId)?.CorrectCount ?? 0; + playerSt.TotalAnswersWrong += + playerAnswerList.FirstOrDefault(x => x.PlayerId == playerId)?.WrongCount ?? 0; - dbContext.SaveChanges(); + if (firstPlace.Key == playerId) + { + playerSt.TotalGamesFirstPlace++; + } + else if (secondPlace.Key == playerId) + { + playerSt.TotalGamesSecondPlace++; + } + else if (thirdPlace.Key == playerId) + { + playerSt.TotalGamesThirdPlace++; } + + dbContext.SaveChanges(); + } } public async Task GetGamePayload(Game rawGame) diff --git a/AmiyaBotPlayerRatingServer/RealtimeHubs/GameHub.cs b/AmiyaBotPlayerRatingServer/RealtimeHubs/GameHub.cs index 6c1800a..1cfe2c2 100644 --- a/AmiyaBotPlayerRatingServer/RealtimeHubs/GameHub.cs +++ b/AmiyaBotPlayerRatingServer/RealtimeHubs/GameHub.cs @@ -158,6 +158,7 @@ public async Task GetGame(string id) await Groups.AddToGroupAsync(Context.ConnectionId, game.Id!); await Clients.Caller.SendAsync("GameInfo", JsonConvert.SerializeObject(new { + Message = "请注意,以Game开头的一系列字段如GameId和GameType,以及CreatorId与CreatorConnectionId均已废弃,请改为使用Game对象。", GameId = game.Id, GameType = game.GameType, GameJoinCode = game.JoinCode,