Exemplo n.º 1
0
 public string Exec(GameState gameState, InManager inManager, OutManager outManager)
 {
     if (gameState.PreviousGames.TryGetValue(roundId, out string localState))
     {
         return(localState);
     }
     return(String.Empty);
 }
Exemplo n.º 2
0
        public string Exec(GameState gameState, InManager inManager, OutManager outManager)
        {
            string masterServer = outManager.GetMasterServer();

            StringBuilder globalStatus = new StringBuilder();

            if (gameState.RoundId == -1)
            {
                globalStatus.AppendLine(String.Format("Game waiting for players"));
            }
            else if (gameState.GameOver)
            {
                Player winner = null;
                foreach (Player player in gameState.Players)
                {
                    if (player.Score > winner.Score)
                    {
                        winner = player;
                    }
                }

                if (winner != null)
                {
                    globalStatus.AppendLine(String.Format("Game over. {0} won after {1} rounds", winner.PlayerId, gameState.RoundId));
                }
                else
                {
                    globalStatus.AppendLine(String.Format("Game over after {0} rounds", gameState.RoundId));
                }
            }
            else
            {
                globalStatus.AppendLine(String.Format("Game live. Round {0}", gameState.RoundId));
            }

            globalStatus.AppendLine("Servers:");
            foreach (GameServer server in gameState.Servers)
            {
                globalStatus.AppendLine(String.Format("{0} [{1}]", server.Url, server.Url == masterServer ? "MASTER" : "SLAVE"));
            }

            globalStatus.AppendLine("Players:");
            globalStatus.AppendLine("PID\tScore\tAlive");
            foreach (Player player in gameState.Players)
            {
                globalStatus.AppendLine(String.Format("{0}\t{1}\t{2}", player.PlayerId, player.Score, player.Alive));
            }

            globalStatus.AppendLine(String.Format("# Players: {0}\n", gameState.Players.Count));

            return(globalStatus.ToString());
        }
Exemplo n.º 3
0
 public string Exec(GameState gameState, InManager inManager, OutManager outManager)
 {
     outManager.SetDelay(dstPid, 10000);
     return(String.Empty);
 }
Exemplo n.º 4
0
 public string Exec(GameState gameState, InManager inManager, OutManager outManager)
 {
     inManager.Freeze();
     return(String.Empty);
 }
Exemplo n.º 5
0
 public void SetOutManager(OutManager outManager)
 {
     this.outManager = outManager;
 }
Exemplo n.º 6
0
        private static void Main(string[] args)
        {
            var argsOptions = new ArgsOptions();

            if (!CommandLine.Parser.Default.ParseArguments(args, argsOptions))
            {
                Console.WriteLine("Missing required arguments");
                return;
            }

            List <string> existsingServersList = argsOptions.ServerEndpoints != null ? (List <string>)argsOptions.ServerEndpoints : new List <string>();

            existsingServersList.Add(argsOptions.ServerUrl);

            if (argsOptions.Pcs != true)
            {
                Console.WriteLine("Started Server with PID: " + argsOptions.Pid);
            }

            // Create and register a remoting channel
            try
            {
                TcpChannel channel = new TcpChannel(new Uri(argsOptions.ServerUrl).Port);
                ChannelServices.RegisterChannel(channel, true);
            }
            catch (SocketException ex)
            {
                Console.WriteLine("Could not bind to port. Either already occupied or blocked by firewall. Exiting.");
                throw new Exception("Socket not available");
            }

            // Create GameState object - used to store the current local state of the system
            GameState gameState = new GameState(existsingServersList);

            // Create OutManager - for sending messages out
            OutManager outManager = new OutManager(argsOptions.ServerUrl, existsingServersList.Count > 0 ? existsingServersList[0] : null, gameState);

            // Create action handler - for processing movements (when master)
            ActionHandler actionHandler = new ActionHandler(gameState, argsOptions.NumPlayers);

            actionHandler.SetOutManager(outManager);

            // Create state handler - for processing state updates (when slave)
            StateHandler stateHandler = new StateHandler((GameStateView gameStateView) =>
            {
                gameState.Patch(gameStateView);
            });

            stateHandler.SetOutManager(outManager);

            // Create InManager - Remoting endpoint is made available here
            InManager inManager = new InManager(argsOptions.ServerUrl, actionHandler, null, stateHandler);

            // Begin server Timer
            new Thread(() =>
            {
                long tickId = 0;

                while (true)
                {
                    if (outManager.GetMasterServer() != argsOptions.ServerUrl)
                    {
                        // This is a slave
                        outManager.SendCommand(new Command
                        {
                            Type = CommandType.Action
                        }, OutManager.MASTER_SERVER);
                    }
                    else
                    {
                        // This is a master
                        actionHandler.FinalizeTick(tickId);
                    }

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

                    tickId++;
                }
            }).Start();

            // Start listening for input
            while (true)
            {
                var input = Console.ReadLine();

                if (input == null || input.Trim() == "Quit")
                {
                    Console.WriteLine("Exit triggered by input");
                    break;
                }

                try
                {
                    ICommand cmd = CommandParser.Command.Parse(input);

                    string result = cmd.Exec(gameState, inManager, outManager);
                    if (result != String.Empty)
                    {
                        Console.Error.WriteLine(result);
                    }
                }
                catch (Exception)
                {
                }
            }
        }