-
Notifications
You must be signed in to change notification settings - Fork 7
Added lobby code encoders #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d65b21b
6030797
606ebfd
06ea594
cbd548e
8297036
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace PurrLobby | ||
| { | ||
| public static class LobbyCode | ||
| { | ||
| private static IBaseEncoder encoder; | ||
| public static string Encode(ulong value) | ||
| { | ||
| if (encoder == null) | ||
| { | ||
| return value.ToString(); | ||
| } | ||
| return encoder.Encode(value); | ||
| } | ||
| public static ulong Decode(string value) | ||
| { | ||
| if (encoder == null) | ||
| { | ||
| return ulong.Parse(value); | ||
| } | ||
| return encoder.Decode(value); | ||
| } | ||
|
ripercheto marked this conversation as resolved.
|
||
|
|
||
| public static List<Type> GetEncoderTypes() => | ||
| AppDomain.CurrentDomain.GetAssemblies() | ||
| .SelectMany(a => a.GetTypes()) | ||
| .Where(t => typeof(IBaseEncoder).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract) | ||
| .ToList(); | ||
|
|
||
| public static void AssignEncoder(string name) | ||
| { | ||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| return; | ||
| } | ||
| var types = GetEncoderTypes(); | ||
| var type = types.FirstOrDefault(t => t.Name == name); | ||
| if (type == null) | ||
| { | ||
| return; | ||
| } | ||
| encoder = (IBaseEncoder)Activator.CreateInstance(type); | ||
| } | ||
|
ripercheto marked this conversation as resolved.
|
||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ namespace PurrLobby | |
| public class LobbyManager : MonoBehaviour | ||
| { | ||
| [SerializeField] private MonoBehaviour currentProvider; | ||
| [SerializeField] private string lobbyCodeEncoderType; | ||
|
ripercheto marked this conversation as resolved.
|
||
| private ILobbyProvider _currentProvider; | ||
|
|
||
| private readonly Queue<Action> _delayedActions = new Queue<Action>(); | ||
|
|
@@ -64,6 +65,8 @@ private void Awake() | |
| else | ||
| PurrLogger.LogWarning("No lobby provider assigned to LobbyManager."); | ||
|
|
||
| LobbyCode.AssignEncoder(lobbyCodeEncoderType); | ||
|
|
||
| SetupDataHolder(); | ||
| } | ||
|
|
||
|
|
@@ -268,6 +271,16 @@ public void LeaveLobby(string lobbyId) | |
| OnRoomLeft?.Invoke(); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Join the lobby with the given lobby code | ||
| /// </summary> | ||
| /// <param name="lobbyCode">lobby code of the lobby to join</param> | ||
| public void JoinLobbyByCode(string lobbyCode) | ||
| { | ||
| var roomId = LobbyCode.Decode(lobbyCode); | ||
| JoinLobby(roomId.ToString()); | ||
| } | ||
|
Comment on lines
+279
to
+283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🐛 Proposed fix public void JoinLobbyByCode(string lobbyCode)
{
- var roomId = LobbyCode.Decode(lobbyCode);
- JoinLobby(roomId.ToString());
+ if (string.IsNullOrEmpty(lobbyCode))
+ {
+ OnRoomJoinFailed?.Invoke("Lobby code is null or empty.");
+ return;
+ }
+ try
+ {
+ var roomId = LobbyCode.Decode(lobbyCode);
+ JoinLobby(roomId.ToString());
+ }
+ catch (Exception ex)
+ {
+ OnRoomJoinFailed?.Invoke($"Invalid lobby code: {ex.Message}");
+ }
}🤖 Prompt for AI Agents |
||
|
|
||
| /// <summary> | ||
| /// Join the lobby with the given ID | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System; | ||
|
|
||
| namespace PurrLobby | ||
| { | ||
| public class Base36Encoder : IBaseEncoder | ||
| { | ||
| private const int MaxBase36Length = 13; // ceil(log36(ulong.MaxValue)) | ||
| private const string Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
|
|
||
| public string Encode(ulong value) | ||
| { | ||
| if (value == 0) | ||
| { | ||
| return "0"; | ||
| } | ||
|
|
||
| var result = ""; | ||
| while (value > 0) | ||
| { | ||
| result = Chars[(int)(value % 36)] + result; | ||
| value /= 36; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public ulong Decode(string value) | ||
| { | ||
| if (string.IsNullOrEmpty(value)) | ||
| { | ||
| throw new ArgumentException("Value cannot be null or empty."); | ||
| } | ||
|
|
||
| value = value.ToUpper().Trim(); | ||
| if (value.Length > MaxBase36Length) | ||
| { | ||
| throw new FormatException($"Input exceeds maximum Base36 length ({MaxBase36Length})."); | ||
| } | ||
|
|
||
| ulong result = 0; | ||
| foreach (var c in value) | ||
| { | ||
| var digit = Chars.IndexOf(c); | ||
| if (digit < 0) | ||
| { | ||
| throw new FormatException($"Invalid Base36 character: {c}"); | ||
| } | ||
| result = result * 36 + (ulong)digit; | ||
| } | ||
| return result; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using System; | ||
|
|
||
| namespace PurrLobby | ||
| { | ||
| public class Base62Encoder : IBaseEncoder | ||
| { | ||
| private const int MaxBase62Length = 11; // ceil(log62(ulong.MaxValue)) | ||
| private const string Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
|
|
||
| public string Encode(ulong value) | ||
| { | ||
| if (value == 0) | ||
| { | ||
| return "0"; | ||
| } | ||
|
|
||
| var result = ""; | ||
| while (value > 0) | ||
| { | ||
| result = Chars[(int)(value % 62)] + result; | ||
| value /= 62; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public ulong Decode(string value) | ||
| { | ||
| if (string.IsNullOrEmpty(value)) | ||
| { | ||
| throw new ArgumentException("Value cannot be null or empty."); | ||
| } | ||
|
|
||
| if (value.Length > MaxBase62Length) | ||
| { | ||
| throw new FormatException($"Input exceeds maximum Base62 length ({MaxBase62Length})."); | ||
| } | ||
|
|
||
| ulong result = 0; | ||
| foreach (var c in value) | ||
| { | ||
| var digit = Chars.IndexOf(c); | ||
| if (digit < 0) | ||
| { | ||
| throw new FormatException($"Invalid Base62 character: {c}"); | ||
| } | ||
| result = result * 62 + (ulong)digit; | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| namespace PurrLobby | ||
| { | ||
| public interface IBaseEncoder | ||
| { | ||
| public string Encode(ulong value); | ||
| public ulong Decode(string value); | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.