Exemple #1
0
        public GameSimulation(string configFilePath, Dictionary <TeamColor, StrategyGroup> strategyGroups)
        {
            var port       = Constants.DefaultPortNumber;
            var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            var ipAddress  = ipHostInfo.AddressList[0];

            var configLoader        = new XmlLoader <GameConfiguration>();
            var config              = configLoader.LoadConfigurationFromFile(configFilePath);
            var keepAliveInterval   = TimeSpan.FromMilliseconds((int)config.KeepAliveInterval);
            var communicationClient = new AsynchronousCommunicationClient(new IPEndPoint(ipAddress, port), keepAliveInterval,
                                                                          MessageSerializer.Instance);


            CommunicationServer =
                new CommunicationServer.CommunicationServer(MessageSerializer.Instance, keepAliveInterval, port, new ErrorsMessagesFactory(), LoggingMode.NonVerbose, ipAddress);
            GameMaster = new GameMaster.GameMaster(config, communicationClient, "game", new ErrorsMessagesFactory(), LoggingMode.NonVerbose, new GameMasterMessageFactory());
            Players    = new List <Player.Player>();
            for (var i = 0; i < 2 * config.GameDefinition.NumberOfPlayersPerTeam; i++)
            {
                communicationClient = new AsynchronousCommunicationClient(new IPEndPoint(ipAddress, port), keepAliveInterval,
                                                                          MessageSerializer.Instance);
                var player = new Player.Player(communicationClient, "game", TeamColor.Blue, PlayerType.Leader, new ErrorsMessagesFactory(), LoggingMode.NonVerbose, strategyGroups);
                Players.Add(player);
            }

            GameMaster.GameFinished += GameMaster_GameFinished;
        }
Exemple #2
0
        private static GameMaster CreateGameMasterFrom(IEnumerable <string> parameters)
        {
            var addressFlag    = false;
            var port           = default(int);
            var gameConfigPath = default(string);
            var ipAddress      = default(IPAddress);
            var gameName       = default(string);
            var loggingMode    = LoggingMode.NonVerbose;

            _runtimeMode = RuntimeMode.Console;

            var options = new OptionSet
            {
                { "port=", "port number", (int p) => port = p },
                { "conf=", "configuration filename", c => gameConfigPath = c },
                { "address=", "server adress or hostname", a => addressFlag = IPAddress.TryParse(a, out ipAddress) },
                { "game=", "name of the game", g => gameName = g },
                { "verbose:", "logging mode", v => loggingMode = LoggingMode.Verbose },
                { "visualize:", "runtime mode", r => _runtimeMode = RuntimeMode.Visualization }
            };

            options.Parse(parameters);

            if (loggingMode == LoggingMode.Verbose && _runtimeMode == RuntimeMode.Visualization)
            {
                _runtimeMode = RuntimeMode.Console;
            }


            if (!addressFlag)
            {
                addressFlag = true;
                var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                ipAddress = ipHostInfo.AddressList[0];
            }

            if (port == default(int) || gameConfigPath == default(string) || gameName == default(string) ||
                !addressFlag)
            {
                Usage(options);
            }

            var configLoader = new XmlLoader <GameConfiguration>();
            var config       = configLoader.LoadConfigurationFromFile(gameConfigPath);

            var communicationClient = new AsynchronousCommunicationClient(new IPEndPoint(ipAddress, port), TimeSpan.FromMilliseconds((int)config.KeepAliveInterval), MessageSerializer.Instance);

            return(new GameMaster(config, communicationClient, gameName, new ErrorsMessagesFactory(), loggingMode, new GameMasterMessageFactory()));
        }
Exemple #3
0
        private static Player CreatePlayerFrom(IEnumerable <string> parameters)
        {
            bool teamFlag = false, roleFlag = false, addressFlag = false;
            var  ipAddress                 = default(IPAddress);
            var  port                      = default(int);
            var  gameConfigPath            = default(string);
            var  gameName                  = default(string);
            var  team                      = default(TeamColor);
            var  role                      = default(PlayerType);
            var  loggingMode               = LoggingMode.NonVerbose;
            var  blueStrategyGroupTypeFlag = true;
            var  redStrategyGroupTypeFlag  = true;
            var  blueStrategyGroupType     = StrategyGroupType.Basic;
            var  redStrategyGroupType      = StrategyGroupType.Basic;

            _runtimeMode = RuntimeMode.Console;

            var options = new OptionSet
            {
                { "port=", "port number", (int p) => port = p },
                { "conf=", "configuration filename", c => gameConfigPath = c },
                { "address=", "server adress or hostname", a => addressFlag = IPAddress.TryParse(a, out ipAddress) },
                { "game=", "name of the game", g => gameName = g },
                { "team=", "red|blue", t => teamFlag = Enum.TryParse(t, true, out team) },
                { "role=", "leader|player", r => roleFlag = Enum.TryParse(r, true, out role) },
                {
                    "blueStrategy=", "strategy options",
                    s => blueStrategyGroupTypeFlag = Enum.TryParse(s, true, out blueStrategyGroupType)
                },
                {
                    "redStrategy=", "strategy options",
                    s => redStrategyGroupTypeFlag = Enum.TryParse(s, true, out redStrategyGroupType)
                },
                { "verbose:", "logging mode", v => loggingMode = LoggingMode.Verbose },
                { "visualize:", "runtime mode", r => _runtimeMode = RuntimeMode.Visualization }
            };

            options.Parse(parameters);

            if (loggingMode == LoggingMode.Verbose && _runtimeMode == RuntimeMode.Visualization)
            {
                _runtimeMode = RuntimeMode.Console;
            }

            if (!addressFlag)
            {
                addressFlag = true;
                var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                ipAddress = ipHostInfo.AddressList[0];
            }

            if (port == default(int) || gameConfigPath == default(string) || gameName == default(string) ||
                !addressFlag || !teamFlag || !roleFlag || !blueStrategyGroupTypeFlag || !redStrategyGroupTypeFlag)
            {
                Usage(options);
            }


            var configLoader = new XmlLoader <GameConfiguration>();
            var config       = configLoader.LoadConfigurationFromFile(gameConfigPath);

            var keepAliveInterval   = TimeSpan.FromMilliseconds((int)config.KeepAliveInterval);
            var communicationClient = new AsynchronousCommunicationClient(new IPEndPoint(ipAddress, port),
                                                                          keepAliveInterval,
                                                                          MessageSerializer.Instance);
            var strategyGroups = new Dictionary <TeamColor, StrategyGroup>
            {
                {
                    TeamColor.Blue, StrategyGroupFactory.Create(blueStrategyGroupType)
                },
                {
                    TeamColor.Red, StrategyGroupFactory.Create(redStrategyGroupType)
                }
            };
            var player = new Player(communicationClient, gameName, team, role, new ErrorsMessagesFactory(), loggingMode,
                                    strategyGroups);

            return(player);
        }