コード例 #1
0
ファイル: LoginState.cs プロジェクト: javawork/Dice
        public void RegisterMainPackageStates(iState package)
        {
            List <MainStates> states = new List <MainStates>()
            {
                MainStates.Login
            };

            StateManagerService.RegisterMainPackageStates(states, package);
        }
コード例 #2
0
ファイル: LoginState.cs プロジェクト: javawork/Dice
        public bool Loop()
        {
            Console.WriteLine("Requesting GetToken...");

            EventWaitHandle doneEvent = new ManualResetEvent(false);

            var getTokenResult = GetToken();

            if (getTokenResult)
            {
                _netService.EnterResponseState = EnterResponseState.NotYet;
                _netService.EnqueueCommand($"enter {_serverAddress} {_token} {GetDeviceId()}");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Login Failed");
                Console.ResetColor();
                StateManagerService.SetState(MainStates.Idle);
                return(false);
            }

            for (var i = 0; i < 5; ++i)
            {
                Thread.Sleep(300);
                if (_netService.EnterResponseState != EnterResponseState.NotYet)
                {
                    break;
                }
            }

            if (_netService.EnterResponseState == EnterResponseState.Succeed)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Login succeed");
                Console.ResetColor();
            }
            else if (_netService.EnterResponseState == EnterResponseState.Failed)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Login Failed");
                Console.ResetColor();
            }
            else if (_netService.EnterResponseState == EnterResponseState.NotYet)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Login Timeout");
                Console.ResetColor();
            }

            StateManagerService.SetState(MainStates.Idle);
            return(false);
        }
コード例 #3
0
        public bool Loop()
        {
            if (_state != States.Exit)
            {
                return(false);
            }
            _state = States.None;
            StateManagerService.SetState(MainStates.Idle);

            //Returning true will exit the program.
            return(true);
        }
コード例 #4
0
        public bool Loop()
        {
            Console.WriteLine("AutoMoveState");
            var rnd = new Random(Guid.NewGuid().GetHashCode());

            for (var i = 0; i < 5; ++i)
            {
                var pos = SharedUtil.GetRandomPosition(rnd);
                _netService.EnqueueCommand($"move {pos.X} {pos.Y}");
                Thread.Sleep(1000);
            }

            StateManagerService.SetState(MainStates.Idle);
            return(false);
        }
コード例 #5
0
ファイル: HelpState.cs プロジェクト: javawork/Dice
        public bool Loop()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("");
            Console.WriteLine("Commands and Usages:");
            Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("help - You are lookin at it.");
            Console.WriteLine("");
            Console.WriteLine("login - GetToken and Enter the server");
            Console.WriteLine("");
            Console.WriteLine("move - Move dice random positions");
            Console.WriteLine("");
            Console.WriteLine("exit");
            Console.ResetColor();

            StateManagerService.SetState(MainStates.Idle);
            return(false);
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: javawork/Dice
        static void Main(string[] args)
        {
            _args   = args;
            _lineSb = new StringBuilder();

            //Parse Args and determine if this is a CLI or Console mode.
            if (args.Length > 0 && !_argsRead)
            {
                _isCLI = true;
                foreach (var a in args)
                {
                    _lineSb.Append(a + " ");
                }
                _line     = _lineSb.ToString();
                _argsRead = true;
                _args     = new string[0];
            }
            else
            {
                _readLine = true;
                _argsRead = true;
            }

            Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("={ ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("Dice CLI");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(" }=");
            Console.ForegroundColor = ConsoleColor.White;

            //if this is a console app then we want to show them how to get help.
            if (!_isCLI)
            {
                Console.WriteLine("");
                Console.WriteLine("Type: 'help' for a list of commands");
                Console.WriteLine("");
                Console.Write(">");
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine("");
            }

            _networkService = new NetworkService();
            _networkService.Initialize();

            var apiUrl = System.Configuration.ConfigurationManager.AppSettings["apiUrl"];

            var consolePackage = new HelpState();

            consolePackage.RegisterMainPackageStates(consolePackage);
            var mainLoop = new MainLoopState();

            mainLoop.RegisterMainPackageStates(mainLoop);
            var login = new LoginState(_networkService, apiUrl);

            login.RegisterMainPackageStates(login);
            var autoMove = new AutoMoveState(_networkService);

            autoMove.RegisterMainPackageStates(autoMove);

            do
            {
                //if we are a console app, read the command that is entered.
                if (_args.Length == 0 && _readLine)
                {
                    if (!_isTypedTextHidden)
                    {
                        //Read the line input from the console.
                        _line = Console.ReadLine();
                    }
                    else
                    {
                        //Read the line in a different way.
                        ConsoleKeyInfo key;
                        do
                        {
                            key = Console.ReadKey(true);
                            if (key.Key != ConsoleKey.Enter)
                            {
                                var s = string.Format("{0}", key.KeyChar);
                                _lineSb.Append(s);
                            }
                        } while (key.Key != ConsoleKey.Enter);
                        _line = _lineSb.ToString();
                    }
                }

                //Set read line to true, not it will only be false if we came from a CLI.
                _readLine = true;
                var loopReturn = false;
                if (StateManagerService.IsIdle())
                {
                    //If we are idle then we want to check for commands.
                    StateManagerService.SetState(_line);
                    _currentPackage    = StateManagerService.GetPackage();
                    _isTypedTextHidden = _currentPackage.SetState(_line);
                    loopReturn         = _currentPackage.Loop();
                }
                else
                {
                    //If we are not idle, then we want to process the _line for arguments.

                    //get the correct package for the state we are in
                    _currentPackage = StateManagerService.GetPackage();

                    //process the package state
                    _isTypedTextHidden = _currentPackage.SetState(_line);

                    //do package loop, which contains logic to do stuff.
                    loopReturn = _currentPackage.Loop();
                }

                //if this is a CLI then we just want to exit.
                if (!_isCLI)
                {
                    //Prompt or exit.
                    if (!loopReturn)
                    {
                        Console.Write(">");
                    }
                    else
                    {
                        _line = null;
                    }
                }
                else
                {
                    _line = null;
                }
            } while (_line != null);

            _networkService.Deinitialize();
        }