Skip to content

Commit

Permalink
加入全局游戏统计
Browse files Browse the repository at this point in the history
  • Loading branch information
hsyhhssyy committed Jun 2, 2024
1 parent 67e1177 commit e24f11f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 52 deletions.
13 changes: 13 additions & 0 deletions AmiyaBotPlayerRatingServer/Controllers/Game/GameHubController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -178,6 +179,18 @@ public async Task<IActionResult> GetPlayerStatistics(string userId)
});
}

public async Task<IActionResult> 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
});
}
}
}
110 changes: 58 additions & 52 deletions AmiyaBotPlayerRatingServer/GameLogic/SchulteGrid/SchulteGridManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,66 +235,72 @@ public Task<object> 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<object> GetGamePayload(Game rawGame)
Expand Down
1 change: 1 addition & 0 deletions AmiyaBotPlayerRatingServer/RealtimeHubs/GameHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e24f11f

Please sign in to comment.