-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathStatisticsEndpoints.cs
More file actions
139 lines (124 loc) · 4.67 KB
/
StatisticsEndpoints.cs
File metadata and controls
139 lines (124 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Filter;
using LBPUnion.ProjectLighthouse.Filter.Filters.Slot;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Servers.API.Responses;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Servers.API.Controllers;
/// <summary>
/// A collection of endpoints relating to statistics.
/// </summary>
public class StatisticsEndpoints : ApiEndpointController
{
private readonly DatabaseContext database;
public StatisticsEndpoints(DatabaseContext database)
{
this.database = database;
}
/// <summary>
/// Gets everything that StatisticsHelper provides.
/// </summary>
/// <returns>An instance of StatisticsResponse</returns>
[HttpGet("statistics")]
[ProducesResponseType(typeof(StatisticsResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> GetStatistics()
=> this.Ok
(
new StatisticsResponse
{
Photos = await StatisticsHelper.PhotoCount(this.database),
Slots = await StatisticsHelper.SlotCount(this.database, new SlotQueryBuilder()),
Users = await StatisticsHelper.UserCount(this.database),
RecentMatches = await StatisticsHelper.RecentMatches(this.database),
TeamPicks = await StatisticsHelper.SlotCount(this.database, new SlotQueryBuilder().AddFilter(new TeamPickFilter())),
}
);
private static readonly List<GameVersion> gameVersions = new()
{
GameVersion.LittleBigPlanet1,
GameVersion.LittleBigPlanet2,
GameVersion.LittleBigPlanet3,
GameVersion.LittleBigPlanetVita,
GameVersion.LittleBigPlanetPSP,
};
private static readonly List<Platform> platforms = new()
{
Platform.PS3,
Platform.RPCS3,
Platform.Vita,
Platform.PSP,
};
/// <summary>
/// Get player counts for each individual title
/// </summary>
/// <returns>An instance of PlayerCountByGameResponse</returns>
[HttpGet("playerCount")]
[HttpGet("playerCount/game")]
[ProducesResponseType(typeof(PlayerCountByGameResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> GetPlayerCounts()
{
List<PlayerCountObject> gameList = new();
foreach (GameVersion version in gameVersions)
{
gameList.Add(new PlayerCountByGameObject
{
Game = version.ToString(),
PlayerCount = await StatisticsHelper.RecentMatches(this.database, l => l.GameVersion == version),
});
}
PlayerCountByGameResponse response = new()
{
TotalPlayerCount = await StatisticsHelper.RecentMatches(this.database),
Games = gameList,
};
return this.Ok(response);
}
/// <summary>
/// Get player counts for each individual platform
/// </summary>
/// <returns>An instance of PlayerCountByPlatformResponse</returns>
[HttpGet("playerCount/platform")]
[ProducesResponseType(typeof(PlayerCountByPlatformResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> GetPlayerCountsByPlatform()
{
List<PlayerCountObject> platformList = new();
foreach (Platform platform in platforms)
{
platformList.Add(new PlayerCountByPlatformObject
{
Platform = platform.ToString(),
PlayerCount = await StatisticsHelper.RecentMatches(this.database, l => l.Platform == platform),
});
}
PlayerCountByPlatformResponse response = new()
{
TotalPlayerCount = await StatisticsHelper.RecentMatches(this.database),
Platforms = platformList,
};
return this.Ok(response);
}
/// <summary>
/// Gets a list of online players
/// </summary>
/// <returns>An instance of PlayerListResponse</returns>
[HttpGet("players")]
[ProducesResponseType(typeof(PlayerListResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> GetPlayerList()
{
List<PlayerListObject> players = await this.database.LastContacts.Where(l => TimeHelper.Timestamp - l.Timestamp < 300)
.Select(l => new PlayerListObject
{
Username = l.User!.Username,
Game = l.GameVersion.ToString(),
Platform = l.Platform.ToString(),
})
.ToListAsync();
PlayerListResponse response = new()
{
Players = players,
};
return this.Ok(response);
}
}