Skip to content

Commit

Permalink
Merge pull request #112 from mongodb-developer/main
Browse files Browse the repository at this point in the history
Merge to prod
  • Loading branch information
ctcac00 authored Mar 18, 2024
2 parents 871c0d9 + 707c3e2 commit 6cf1ae3
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 107 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ Open http://127.0.0.1:8001/ to verify the website is running.

## Contributors

- [Dominic Frei](https://linktr.ee/dominicfrei)
- [Hubert Nguyen](https://)
- [Nic Raboy](https://www.nraboy.com)
- [Sig Narváez](https://www.linkedin.com/in/signarvaez/)
- [Carlos Castro](https://www.linkedin.com/in/carloscastromdb/)
- [Ángel Martínez](https://www.linkedin.com/in/amartinezgonzalez/)
- [Hubert Nguyen](https://www.linkedin.com/in/hubertnguyen/)
- [Nic Raboy](https://www.nraboy.com)
- [Dominic Frei](https://linktr.ee/dominicfrei)
12 changes: 12 additions & 0 deletions deployment/game_database/vector_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
"path": "Player.Nickname",
"type": "filter"
},
{
"numDimensions": 1186,
"path": "similarity_vector",
"similarity": "euclidean",
"type": "vector"
},
{
"numDimensions": 9,
"path": "stats_vector",
"similarity": "euclidean",
"type": "vector"
},
{
"numDimensions": 589,
"path": "speed_vector",
Expand Down
3 changes: 0 additions & 3 deletions payload.json

This file was deleted.

3 changes: 3 additions & 0 deletions rest_service/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,6 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

*env.prod*
*env.stag*
110 changes: 107 additions & 3 deletions rest_service/Controllers/RecordingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using RestService.Entities;
using RestService.Exceptions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;

namespace RestService.Controllers;

Expand Down Expand Up @@ -60,8 +59,16 @@ public async Task<IActionResult> PostRecording([FromBody] RecordingRequest recor
// Calculate vectors
try
{
newRecording.StatsVector = CalculateStatsVector(newRecording.SessionStatisticsPlain);
newRecording.SpeedVector = CalculateSpeedVector(newRecording.Snapshots);
newRecording.AccelVector = CalculateAcceleration(newRecording.SpeedVector);

newRecording.SimilarityVector = CalculateSimilarityVector(
new List<double[]>() {
newRecording.StatsVector,
newRecording.SpeedVector,
newRecording.AccelVector });

} catch (Exception) {
// Favor persisting Recording over setting vectors
}
Expand Down Expand Up @@ -143,6 +150,23 @@ private async Task AddPlayer(Recording recording)
}
}

private static double[] CalculateStatsVector(SessionStatisticsPlain ssp)
{
double[] stats = new double[9];

stats[0] = Convert.ToDouble(ssp.Score);
stats[1] = Convert.ToDouble(ssp.DamageDone);
stats[2] = Convert.ToDouble(ssp.BulletsFired);
stats[3] = Convert.ToDouble(ssp.PelletsDestroyedLarge);
stats[4] = Convert.ToDouble(ssp.PelletsDestroyedMedium);
stats[5] = Convert.ToDouble(ssp.PelletsDestroyedSmall);
stats[6] = Convert.ToDouble(ssp.PowerUpBulletDamageCollected);
stats[7] = Convert.ToDouble(ssp.PowerUpBulletSpeedCollected);
stats[8] = Convert.ToDouble(ssp.PowerUpPlayerSpeedCollected);

return stats;
}

private static double[] CalculateSpeedVector(List<Snapshot> snapshots)
{
long vectorSize = snapshots.Count - 1;
Expand Down Expand Up @@ -176,6 +200,51 @@ private static double[] CalculateAcceleration(double[] speedVector)
return accelVector;
}

private static double[] CalculateSimilarityVector(List<double[]> vectors)
{
double[] similar = Array.Empty<double>();
foreach (double[] vector in vectors)
similar = similar.Concat(vector).ToArray();
return similar;
}

[HttpGet("similar", Name = "GetSimilar")]
public async Task<List<SimilarRecordingResponse>> Similar([FromQuery] PlayerRequest playerRequest)
{
// Get the highest scoring run for this player
Recording topRecording = _recordingsCollection
.Find(r => r.Player.Name.Equals(playerRequest.Name))
.SortByDescending(r => r.SessionStatisticsPlain.Score)
.Limit(1).ToList().First();

// Now get similar recordings
List<Recording> similarRecordings = _recordingsCollection.Aggregate()
.VectorSearch(
r => r.SimilarityVector,
topRecording.SimilarityVector,
3,
new VectorSearchOptions<Recording>()
{
IndexName = "vector_index",
NumberOfCandidates = 1000,
Filter = Builders<Recording>.Filter
.Where(r => !r.Player.Name.Equals(playerRequest.Name))
})
.ToList();

// Return this player's top recording + top similar
List<SimilarRecordingResponse> response = new()
{
new SimilarRecordingResponse(topRecording)
};
response.AddRange(
similarRecordings
.Select(r => new SimilarRecordingResponse(r))
.ToList());

return response;
}

[HttpGet("similarBySpeed", Name = "GetSimilarBySpeed")]
public async Task<List<SimilarRecordingResponse>> SimilarBySpeed([FromQuery] PlayerRequest playerRequest)
{
Expand Down Expand Up @@ -212,7 +281,6 @@ public async Task<List<SimilarRecordingResponse>> SimilarBySpeed([FromQuery] Pla

return response;
}


[HttpGet("similarByAcceleration", Name = "GetSimilarByAcceleration")]
public async Task<List<SimilarRecordingResponse>> SimilarByAcceleration([FromQuery] PlayerRequest playerRequest)
Expand Down Expand Up @@ -250,5 +318,41 @@ public async Task<List<SimilarRecordingResponse>> SimilarByAcceleration([FromQue

return response;
}


[HttpGet("similarByStats", Name = "GetSimilarByStats")]
public async Task<List<SimilarRecordingResponse>> SimilarByStats([FromQuery] PlayerRequest playerRequest)
{
// Get the highest scoring run for this player
Recording topRecording = _recordingsCollection
.Find(r => r.Player.Name.Equals(playerRequest.Name))
.SortByDescending(r => r.SessionStatisticsPlain.Score)
.Limit(1).ToList().First();

// Now get similar recordings
List<Recording> similarRecordings = _recordingsCollection.Aggregate()
.VectorSearch(
r => r.StatsVector,
topRecording.StatsVector,
3,
new VectorSearchOptions<Recording>()
{
IndexName = "vector_index",
NumberOfCandidates = 1000,
Filter = Builders<Recording>.Filter
.Where(r => !r.Player.Name.Equals(playerRequest.Name))
})
.ToList();

// Return this player's top recording + top similar
List<SimilarRecordingResponse> response = new()
{
new SimilarRecordingResponse(topRecording)
};
response.AddRange(
similarRecordings
.Select(r => new SimilarRecordingResponse(r))
.ToList());

return response;
}
}
4 changes: 4 additions & 0 deletions rest_service/Entities/Recording.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public class Recording
public double[]? SpeedVector { get; set; }
[BsonElement("accel_vector")]
public double[]? AccelVector { get; set; }
[BsonElement("stats_vector")]
public double[]? StatsVector { get; set; }
[BsonElement("similarity_vector")]
public double[]? SimilarityVector { get; set; }
}
2 changes: 1 addition & 1 deletion rest_service/RestService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="dotenv.net" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="MongoDB.Driver" Version="2.24.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />

Expand Down
35 changes: 24 additions & 11 deletions website/Pages/EventHome.razor
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@
</div>
<div class="card bg-white">
<article class="card-body mx-auto" style="max-width: 400px;">
<h2 class="text-center">Event Dashboard</h2>
<table>
<tr>
<td>
<div>
<img src="@GetQRCodeForCurrentUrl()" class="rounded mx-auto d-block" alt="logo">
</div>
</td>
<td>
@if (Event?.Name != null)
{
<h2 class="card-title mt-3 text-center">@Event.Name</h2>
}
</td>
</tr>
</table>
<h4 class="card-title mt-3 text-center">
<b>
@if (Event?.Name != null)
{
<span>@Event.Name</span>
}
else
{
<span>[Unspecified Event]</span>
}
</b>
<h2 class="text-center">Event Dashboard</h2>
</h4>
</article>
</div>
Expand Down Expand Up @@ -101,4 +106,12 @@ else
}
}

// return current url location
private string GetQRCodeForCurrentUrl()
{
string url = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" +
NavigationManager.Uri + "?EventId=" + _eventId;
Console.WriteLine(url);
return url;
}
}
Loading

0 comments on commit 6cf1ae3

Please sign in to comment.