12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using MafiaTelegramBot.Resources;
- namespace MafiaTelegramBot.Game
- {
- public static class RoomEncrypter
- {
- private static readonly Dictionary<string, string> ShortName = new();
- private static readonly Dictionary<string, string> FullName = new();
-
- public static string GetCode(string roomName)
- {
- if (ShortName.ContainsKey(roomName)) return ShortName[roomName];
- var shortName = RandomString();
- while (ShortName.ContainsKey(shortName)) shortName = RandomString();
- ShortName.Add(roomName, shortName);
- FullName.Add(shortName, roomName);
- return shortName;
- }
- public static string GetName(string roomKey)
- {
- return FullName[roomKey];
- }
-
- public static void RemoveCode(string roomName)
- {
- FullName.Remove(GetCode(roomName));
- ShortName.Remove(roomName);
- }
-
- private static readonly Random Random = new();
- private static string RandomString(int length = Constants.ROOM_CODE_LENGTH)
- {
- const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- return new string(Enumerable.Repeat(chars, length)
- .Select(s => s[Random.Next(s.Length)]).ToArray());
- }
- }
- }
|