Make it so you don't rematch with people you just dived in with

This commit is contained in:
jvyden 2021-11-13 11:21:57 -05:00
commit 688bd9a58b
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 72 additions and 47 deletions

View file

@ -1,6 +1,5 @@
#nullable enable #nullable enable
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.Json; using System.Text.Json;
@ -66,60 +65,28 @@ namespace LBPUnion.ProjectLighthouse.Controllers
#region Process match data #region Process match data
if (matchData is UpdateMyPlayerData) if (matchData is UpdateMyPlayerData) MatchHelper.SetUserLocation(user.UserId, token.UserLocation);
{
if (MatchHelper.UserLocations.TryGetValue(user.UserId, out string? _)) MatchHelper.UserLocations.Remove(user.UserId);
MatchHelper.UserLocations.Add(user.UserId, token.UserLocation);
}
if (matchData is FindBestRoom findBestRoom) if (matchData is FindBestRoom && MatchHelper.UserLocations.Count > 1)
{
if (MatchHelper.UserLocations.Count > 1)
{ {
foreach ((int id, string? location) in MatchHelper.UserLocations) foreach ((int id, string? location) in MatchHelper.UserLocations)
{ {
if (id == user.UserId) continue; if (id == user.UserId) continue;
if (location == null) continue; if (location == null) continue;
if (MatchHelper.DidUserRecentlyDiveInWith(user.UserId, id)) continue;
User? otherUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id); User? otherUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
if (otherUser == null) continue; if (otherUser == null) continue;
FindBestRoomResponse response = new() FindBestRoomResponse response = MatchHelper.FindBestRoomResponse(user.Username, otherUser.Username, token.UserLocation, location);
{
Players = new List<Player>
{
new()
{
MatchingRes = 0,
PlayerId = otherUser.Username,
},
new()
{
MatchingRes = 1,
PlayerId = user.Username,
},
},
Locations = new List<string>
{
location,
token.UserLocation,
},
Slots = new List<List<int>>
{
new()
{
5,
0,
},
},
};
string serialized = JsonSerializer.Serialize(response, typeof(FindBestRoomResponse)); string serialized = JsonSerializer.Serialize(response, typeof(FindBestRoomResponse));
MatchHelper.AddUserRecentlyDivedIn(user.UserId, id);
return new ObjectResult($"[{{\"StatusCode\":200}},{serialized}]"); return new ObjectResult($"[{{\"StatusCode\":200}},{serialized}]");
} }
} }
}
#endregion #endregion

View file

@ -1,6 +1,7 @@
#nullable enable #nullable enable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -10,7 +11,64 @@ namespace LBPUnion.ProjectLighthouse.Helpers
{ {
public static class MatchHelper public static class MatchHelper
{ {
public static Dictionary<int, string?> UserLocations = new(); public static readonly Dictionary<int, string?> UserLocations = new();
public static readonly Dictionary<int, List<int>?> UserRecentlyDivedIn = new();
public static void SetUserLocation(int userId, string location)
{
if (UserLocations.TryGetValue(userId, out string? _)) UserLocations.Remove(userId);
UserLocations.Add(userId, location);
}
public static void AddUserRecentlyDivedIn(int userId, int otherUserId)
{
if (!UserRecentlyDivedIn.TryGetValue(userId, out List<int>? recentlyDivedIn))
{
UserRecentlyDivedIn.Add(userId, recentlyDivedIn = new List<int>());
}
Debug.Assert(recentlyDivedIn != null, nameof(recentlyDivedIn) + " is null, somehow.");
recentlyDivedIn.Add(otherUserId);
}
public static bool DidUserRecentlyDiveInWith(int userId, int otherUserId)
{
if (!UserRecentlyDivedIn.TryGetValue(userId, out List<int>? recentlyDivedIn) || recentlyDivedIn == null) return false;
return recentlyDivedIn.Contains(otherUserId);
}
public static FindBestRoomResponse FindBestRoomResponse(string username, string otherUsername, string location, string otherLocation)
=> new()
{
Players = new List<Player>
{
new()
{
MatchingRes = 0,
PlayerId = otherUsername,
},
new()
{
MatchingRes = 1,
PlayerId = username,
},
},
Locations = new List<string>
{
location,
otherLocation,
},
Slots = new List<List<int>>
{
new()
{
5,
0,
},
},
};
public static IMatchData? Deserialize(string data) public static IMatchData? Deserialize(string data)
{ {