-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from rufo123/code
Code Merge
- Loading branch information
Showing
51 changed files
with
17,142 additions
and
11,463 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
TeamspeakStatsNew/TeamspeakStatsNew/.config/dotnet-tools.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": 1, | ||
"isRoot": true, | ||
"tools": { | ||
"dotnet-ef": { | ||
"version": "7.0.9", | ||
"commands": [ | ||
"dotnet-ef" | ||
] | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
TeamspeakStatsNew/TeamspeakStatsNew/Backend/AverageHoursManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace TeamspeakStatsNew.Backend | ||
{ | ||
public enum IntervalTypes | ||
{ | ||
Yearly = 0, | ||
Monthly = 1, | ||
Daily = 2, | ||
} | ||
|
||
public class AverageHoursManager | ||
{ | ||
public AverageHoursManager() | ||
{ | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
TeamspeakStatsNew/TeamspeakStatsNew/Backend/CountOfClientsRelativeToPeriod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
using System.Diagnostics; | ||
|
||
namespace TeamspeakStatsNew.Backend | ||
{ | ||
public class CountOfClientsRelativeToPeriod | ||
{ | ||
private SortedDictionary<DateTime, int>? aDictionaryCountHours; | ||
private SortedDictionary<DateTime, int>? aDictionaryCountDays; | ||
private SortedDictionary<DateTime, int>? aDictionaryCountMonths; | ||
private SortedDictionary<DateTime, int>? aDictionaryCountYears; | ||
|
||
public SortedDictionary<DateTime, int>? DictionaryCountHours => aDictionaryCountHours; | ||
|
||
public SortedDictionary<DateTime, int>? DictionaryCountDays => aDictionaryCountDays; | ||
|
||
public SortedDictionary<DateTime, int>? DictionaryCountMonths => aDictionaryCountMonths; | ||
|
||
public SortedDictionary<DateTime, int>? DictionaryCountYears => aDictionaryCountYears; | ||
|
||
private SortedDictionary<DateTime, List<int>> aDictionaryOfAlreadyCountedClientIds; | ||
|
||
|
||
public CountOfClientsRelativeToPeriod() | ||
{ | ||
aDictionaryOfAlreadyCountedClientIds = new SortedDictionary<DateTime, List<int>>(); | ||
} | ||
|
||
public void Initialise( | ||
Dictionary<int, ClientConnectedData> parDictionaryClientConnectedData, | ||
Dictionary<int, Client> parDictionaryOfClients | ||
) | ||
{ | ||
aDictionaryOfAlreadyCountedClientIds = new SortedDictionary<DateTime, List<int>>(); | ||
aDictionaryCountHours = CalculateCountOfPlayerHoursRelativeToPeriod(Periods.Hour, parDictionaryClientConnectedData, parDictionaryOfClients); | ||
aDictionaryCountDays = CalculateCountOfPlayerHoursRelativeToPeriod(Periods.Day, parDictionaryClientConnectedData, parDictionaryOfClients); | ||
aDictionaryCountMonths = CalculateCountOfPlayerHoursRelativeToPeriod(Periods.Month, parDictionaryClientConnectedData, parDictionaryOfClients); | ||
aDictionaryCountYears = CalculateCountOfPlayerHoursRelativeToPeriod(Periods.Year, parDictionaryClientConnectedData, parDictionaryOfClients); | ||
} | ||
|
||
private DateTime AddDateTimeHelper(Periods parPeriod, DateTime parDateTime, bool skipAddition) | ||
{ | ||
if (skipAddition) | ||
{ | ||
return parDateTime; | ||
} | ||
|
||
switch (parPeriod) | ||
{ | ||
case Periods.Hour: | ||
return parDateTime.AddHours(1); | ||
case Periods.Day: | ||
return parDateTime.AddDays(1); | ||
case Periods.Month: | ||
return parDateTime.AddMonths(1); | ||
case Periods.Year: | ||
return parDateTime.AddYears(1); | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(parPeriod), parPeriod, null); | ||
} | ||
} | ||
|
||
public SortedDictionary<DateTime, int>? CalculateCountOfPlayerHoursRelativeToPeriod( | ||
Periods parPeriod, | ||
Dictionary<int, ClientConnectedData> parDictionaryClientConnectedData, | ||
Dictionary<int, Client> parDictionaryOfClients) | ||
{ | ||
|
||
SortedDictionary<DateTime, int>? countOfPlayersRelativeToPeriod = new SortedDictionary<DateTime, int>(); | ||
aDictionaryOfAlreadyCountedClientIds = new SortedDictionary<DateTime, List<int>>(); | ||
|
||
|
||
foreach (var values in parDictionaryClientConnectedData.Values) | ||
{ | ||
LoopConnectedTimesAndAssignConnectedBetweenPeriod(values, parDictionaryOfClients, parPeriod, ref countOfPlayersRelativeToPeriod); | ||
|
||
} | ||
|
||
return countOfPlayersRelativeToPeriod; | ||
} | ||
|
||
private void LoopConnectedTimesAndAssignConnectedBetweenPeriod( | ||
ClientConnectedData parClientConnectedData, | ||
Dictionary<int, Client> parClientDataDictionary, | ||
Periods parPeriod, | ||
ref SortedDictionary<DateTime, int>? parRefSortedDictToSaveTo) | ||
{ | ||
|
||
DateTime lastDisconnectedTime = DateTime.UnixEpoch; | ||
|
||
foreach (var connectedTimes in parClientConnectedData.ConnectionsDataListOfArrays) | ||
{ | ||
|
||
DateTime connected = connectedTimes[1]; | ||
DateTime disconnected = connectedTimes[0]; | ||
|
||
IterateEachHourAndAssignConnectedTimes(connected, disconnected, parPeriod, parClientConnectedData.Id, ref parRefSortedDictToSaveTo); | ||
|
||
lastDisconnectedTime = disconnected; | ||
|
||
} | ||
|
||
if (lastDisconnectedTime != DateTime.UnixEpoch && parClientDataDictionary[parClientConnectedData.Id].Online) | ||
{ | ||
IterateEachHourAndAssignConnectedTimes(lastDisconnectedTime, DateTime.Now, parPeriod, parClientConnectedData.Id, ref parRefSortedDictToSaveTo); | ||
} | ||
|
||
} | ||
|
||
private void IterateEachHourAndAssignConnectedTimes(DateTime parConnected, DateTime parDisconnected, Periods parPeriod, int parClientId, ref SortedDictionary<DateTime, int>? parRefSortedDictToSaveTo) | ||
{ | ||
bool isFirstIteration = true; | ||
|
||
// Iterate through each hour between connected and disconnected times | ||
for (DateTime time = parConnected; time <= parDisconnected; time = AddDateTimeHelper(parPeriod, time, isFirstIteration)) | ||
{ | ||
|
||
isFirstIteration = false; | ||
DateTime roundedTime; | ||
|
||
switch (parPeriod) | ||
{ | ||
case Periods.Hour: | ||
roundedTime = new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0); | ||
break; | ||
case Periods.Day: | ||
roundedTime = new DateTime(time.Year, time.Month, time.Day, 0, 0, 0); | ||
break; | ||
case Periods.Month: | ||
roundedTime = new DateTime(time.Year, time.Month, 1, 0, 0, 0); | ||
break; | ||
case Periods.Year: | ||
roundedTime = new DateTime(time.Year, 1, 1, 0, 0, 0); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(parPeriod), parPeriod, null); | ||
} | ||
|
||
if (parRefSortedDictToSaveTo is null) | ||
{ | ||
throw new ArgumentNullException(nameof(parRefSortedDictToSaveTo), "parRefSortedDictToSaveTo cannot be null."); | ||
} | ||
|
||
// Update the count of connected players for the rounded hour | ||
|
||
if (aDictionaryOfAlreadyCountedClientIds.ContainsKey(roundedTime)) | ||
{ | ||
foreach (var clientId in aDictionaryOfAlreadyCountedClientIds[roundedTime]) | ||
{ | ||
if (clientId == parClientId) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
aDictionaryOfAlreadyCountedClientIds[roundedTime].Add(parClientId); | ||
} | ||
else | ||
{ | ||
aDictionaryOfAlreadyCountedClientIds.Add(roundedTime, new List<int>()); | ||
} | ||
|
||
if (parRefSortedDictToSaveTo.ContainsKey(roundedTime)) | ||
parRefSortedDictToSaveTo[roundedTime]++; | ||
else | ||
parRefSortedDictToSaveTo[roundedTime] = 1; | ||
} | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Security.Cryptography; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace TeamspeakStatsNew.Backend | ||
{ | ||
public static class Helpers | ||
{ | ||
|
||
public static bool IsETagValid(Client[] parClientArray, HttpRequest parRequest, HttpResponse parResponse) | ||
{ | ||
var hash = ComputeHash(parClientArray); // Compute a hash of the clients array | ||
var etag = Convert.ToBase64String(Encoding.UTF8.GetBytes(hash)); // Convert the hash to a base64 string | ||
|
||
// Set the ETag header in the response | ||
parResponse.Headers.Add("ETag", etag); | ||
|
||
// Check if the If-None-Match header matches the current ETag | ||
var ifNoneMatch = parRequest.Headers["If-None-Match"].FirstOrDefault(); | ||
if (ifNoneMatch != null && ifNoneMatch.Equals(etag)) | ||
{ | ||
return true; // ETag is valid, return true | ||
} | ||
|
||
return false; // ETag is not valid, return false | ||
} | ||
|
||
public static bool IsETagValid(SortedDictionary<DateTime, int>? sortedDictionary, HttpRequest parRequest, HttpResponse parResponse) | ||
{ | ||
|
||
var hash = ComputeHash(sortedDictionary); // Compute a hash of the clients array | ||
var etag = Convert.ToBase64String(Encoding.UTF8.GetBytes(hash)); // Convert the hash to a base64 string | ||
|
||
// Set the ETag header in the response | ||
parResponse.Headers.Add("ETag", etag); | ||
|
||
// Check if the If-None-Match header matches the current ETag | ||
var ifNoneMatch = parRequest.Headers["If-None-Match"].FirstOrDefault(); | ||
|
||
if (ifNoneMatch != null && ifNoneMatch.Equals(etag)) | ||
{ | ||
return true; // ETag is valid, return true | ||
} | ||
|
||
return false; // ETag is not valid, return false | ||
} | ||
|
||
|
||
private static string ComputeHash(IEnumerable<Client> parClientsArray) | ||
{ | ||
using SHA256? sha256 = SHA256.Create(); | ||
string json = JsonSerializer.Serialize(parClientsArray); | ||
byte[] bytes = Encoding.UTF8.GetBytes(json); | ||
byte[] hashBytes = sha256.ComputeHash(bytes); | ||
string hash = Convert.ToBase64String(hashBytes); | ||
return hash; | ||
} | ||
|
||
private static string ComputeHash(SortedDictionary<DateTime, int>? parSortedDictionary) | ||
{ | ||
using SHA256 sha256 = SHA256.Create(); | ||
string json = JsonSerializer.Serialize(parSortedDictionary); | ||
byte[] bytes = Encoding.UTF8.GetBytes(json); | ||
byte[] hashBytes = sha256.ComputeHash(bytes); | ||
string hash = Convert.ToBase64String(hashBytes); | ||
return hash; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace TeamspeakStatsNew.Backend | ||
{ | ||
public enum Periods | ||
{ | ||
Hour = 0, | ||
Day = 1, | ||
Month = 2, | ||
Year = 3 | ||
} | ||
|
||
} |
17 changes: 0 additions & 17 deletions
17
TeamspeakStatsNew/TeamspeakStatsNew/ClientApp/.browserslistrc
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.