示例#1
0
        private void SendDirection(Direction direction)
        {
            if (direction != lastSentDirection)
            {
                lastSentDirection = direction;

                outManager.SendCommand(new Command
                {
                    Type = Server.CommandType.Action,
                    Args = new GameMovement
                    {
                        Direction = direction
                    }
                }, OutManager.MASTER_SERVER);
            }
        }
示例#2
0
文件: Program.cs 项目: CVeniamin/DAD
        private static void ActionDispatcher(OutManager outManager, Dictionary <int, Direction> replayMoves, ArgsOptions argsOptions,
                                             GameState gameState, MainFrame mainForm)
        {
            {
                int  tickId       = 0;
                bool sentThisTick = false;

                //create new player on server
                outManager.SendCommand(new Command
                {
                    Type = CommandType.Action,
                    Args = new ClientSync
                    {
                        Pid = argsOptions.Pid
                    }
                }, OutManager.MASTER_SERVER);

                int  totalMoves     = replayMoves.Count;
                int  replayedMoves  = 0;
                int  maxRoundId     = replayMoves.Keys.ToArray().Max();
                bool replayingMoves = totalMoves > 0;

                while (true)
                {
                    sentThisTick = false;

                    if (replayingMoves && replayMoves.TryGetValue(gameState.RoundId, out Direction nextMove) && !gameState.GameOver)
                    {
                        replayedMoves++;

                        outManager.SendCommand(new Command
                        {
                            Type = CommandType.Action,
                            Args = new GameMovement
                            {
                                Direction = nextMove
                            }
                        }, OutManager.MASTER_SERVER);

                        sentThisTick = true;

                        if (replayedMoves >= totalMoves - 1 || gameState.RoundId >= maxRoundId)
                        {
                            replayingMoves = false;

                            Console.WriteLine("Trace file eneded. Keyboard events enabled.");
                            mainForm.IgnoreKeyboard = false;
                        }
                    }

                    if (!sentThisTick)
                    {
                        outManager.SendCommand(new Command
                        {
                            Type = CommandType.Action,
                            Args = new ClientSync
                            {
                                Pid = argsOptions.Pid
                            }
                        }, OutManager.MASTER_SERVER);
                    }

                    try
                    {
                        Thread.Sleep(argsOptions.TickDuration);
                    }
                    catch (ThreadInterruptedException) { }

                    tickId++;
                }
            }
        }