예제 #1
0
 /// <summary>
 /// Generates an arguably unique string of characters to be used for unique identification.
 /// The uniqueness strength boils down to the <see cref="IdKind"/> used.
 /// Some IDs may not be suitable for certain scenarios
 /// </summary>
 /// <param name="kind">The kind of ID to be generated</param>
 /// <param name="length">The length of the string to be generated, only applicable for <see cref="IdKind.Standard"/> up to 36 characters and <see cref="IdKind.Random"/> for unlimited characters</param>
 public static string GenerateId(IdKind kind = IdKind.Guid, int length = 12)
 {
     return(kind switch
     {
         IdKind.DateTime => GenerateDateTimeId(),
         IdKind.Guid => GenerateGuid(),
         IdKind.Hash => GenerateHashedId(),
         IdKind.Random => Random(length, true),
         _ => GenerateStandardId(length),
     });
예제 #2
0
        public static ulong CreateNewId(IdKind idKind)
        {
            switch (idKind)
            {
            case IdKind.User:
                _userIdCounter++;
                return(_userIdCounter);

            case IdKind.Chat:
                _chatIdCounter++;
                return(_chatIdCounter);

            case IdKind.Invate:
                _chatInvateIdCounter++;
                return(_chatInvateIdCounter);

            default:
                //TODO (Osipov);
                throw new Exception();
            }
        }
예제 #3
0
파일: BaseId.cs 프로젝트: ossup0v/SomeStaff
        public BaseId(IdKind idKind)
        {
            this.Id     = IdsManager.CreateNewId(idKind);
            this.IdKind = IdKind;
            switch (idKind)
            {
            case IdKind.User:
                Prefix = "U_";
                break;

            case IdKind.Chat:
                Prefix = "Chat_";
                break;

            case IdKind.Invate:
                Prefix = "Invate_";
                break;

            default:
                //TODO (Osipov);
                throw new Exception();
            }
        }
예제 #4
0
 public Identities(IdKind kind)
 {
     Kind = kind;
     myId = kind == IdKind.Client ? BaseClientId : BaseServerId;
 }
예제 #5
0
        public async void WaitForCommand(string[] args = null)
        {
            List <string> cmdlets;

            string cmd;

            if (args == null || args.Length == 0)
            {
                cmd = Ask();
                if (cmd == null)
                {
                    return;
                }

                cmdlets = new List <string>(cmd.Split(' '));
            }
            else
            {
                cmdlets = new List <string>(args);
                StringBuilder sb = new();

                for (int i = 0; i < cmdlets.Count; i++)
                {
                    sb.Append(cmdlets[i]);

                    if (i != cmdlets.Count - 1)
                    {
                        sb.Append(' ');
                    }
                }

                cmd = sb.ToString();
            }

            while (true)
            {
                try
                {
                    if (cmdlets[0] == "back")
                    {
                        Aretha.SoulSucceeded(Soul.Horus);
                        return;
                    }
                    else if (cmdlets[0] == "use")
                    {
                        switch (cmdlets[1])
                        {
                        case "vigenere":
                        case "caesar":
                        case "playfair":
                            await new HorusClassical().OnSession(cmdlets[1]);
                            break;

                        case "crypto-file":
                            await new HorusFile().OnSession();
                            break;

                        case "crypto-container":
                            await new HorusContainer().OnSession();
                            break;

                        case "enigma-machine":
                            await new HorusEnigma().OnSession();
                            break;

                        default:
                            throw new InvalidOperationException("Unknown operation");
                        }
                    }
                    else if (cmdlets[0] == "hash")
                    {
                        var input = Ask(isCase: true);
                        Speak(Horus.Horus.GetHash(input));
                    }
                    else if (cmdlets[0] == "id")
                    {
                        if (cmdlets.Count == 1)
                        {
                            Speak(Horus.Horus.GenerateId(IdKind.Guid));
                        }
                        else
                        {
                            int max = 12;
                            if (cmdlets.Count > 2)
                            {
                                if (!int.TryParse(cmdlets[2], out max))
                                {
                                    throw new InvalidOperationException("Value for length must be number");
                                }
                            }

                            IdKind kind = cmdlets[1] switch
                            {
                                "standard" => IdKind.Standard,
                                "date-time" => IdKind.DateTime,
                                "guid" => IdKind.Guid,
                                "random" => IdKind.Random,
                                "hash" => IdKind.Hash,
                                _ => throw new InvalidOperationException("Unkown ID Kind")
                            };

                            Speak(Horus.Horus.GenerateId(kind, max));
                        }
                    }

                    cmdlets = new List <string>(Ask().Split(' '));
                }
                catch (Exception ex)
                {
                    Aretha.SoulFailed(Soul.Horus, ex);
                    return;
                }
            }
        }
 public long GetNextId(IdKind kind)
 {
     return(_generators[kind].CreateId());
 }