Ejemplo n.º 1
0
        private void LoadConfig(string sConfigFile, string sOutputBase)
        {
            var doc = XDocument.Load(sConfigFile);

            // This must happen before we write the first log message
            Logger.Initialize("logs\\" + sOutputBase + "_gamelog.txt");
            TimingLogger.Initialize("logs\\" + sOutputBase + "_calllog.csv");

            Logger.Log("--- *** CONFIG *** ---");
            Logger.Log(doc.ToString());

            var holdemConfig = doc.Element("HoldemConfig");

            if (holdemConfig == null)
            {
                throw new Exception("Unable to find HoldemConfig element in HoldemConfig.xml");
            }

            var gameRules = holdemConfig.Element("GameRules");

            if (gameRules == null)
            {
                throw new Exception("Unable to find GameRules element in HoldemConfig.xml");
            }

            // Get game rules
            var gameConfigSettings = new Dictionary <string, string>
            {
                { "smallBlind", _smallBlindSize.ToString() },
                { "bigBlind", _bigBlindSize.ToString() },
                { "startingStack", _startingStack.ToString() },
                { "maxNumRaisesPerBettingRound", _maxNumRaisesPerBettingRound.ToString() },
                { "maxHands", _maxHands.ToString() },
                { "doubleBlindFrequency", _doubleBlindFrequency.ToString() },
                { "botTimeOutMilliSeconds", _botTimeOutMilliSeconds.ToString() },
                { "randomDealer", _bRandomDealer.ToString() },
                { "randomSeating", _bRandomSeating.ToString() },
                { "pauseAfterEachHand", _bPauseAfterEachHand.ToString() },
                { "sleepAfterActionMilliSeconds", _sleepAfterActionMilliSeconds.ToString() },
                { "graphicsDisplay", _bGraphicsDisplay.ToString() }
            };

            // add defaults to dictionary

            // add or update values in dictionary from values in xml
            foreach (var attr in gameRules.Attributes())
            {
                if (gameConfigSettings.ContainsKey(attr.Name.ToString()))
                {
                    gameConfigSettings[attr.Name.ToString()] = attr.Value;
                }
                else
                {
                    gameConfigSettings.Add(attr.Name.ToString(), attr.Value);
                }
            }

            // read values from dictionary
            _smallBlindSize = Convert.ToInt32(gameConfigSettings["smallBlind"]);
            _bigBlindSize   = Convert.ToInt32(gameConfigSettings["bigBlind"]);
            _startingStack  = Convert.ToInt32(gameConfigSettings["startingStack"]);
            _maxNumRaisesPerBettingRound = Convert.ToInt32(gameConfigSettings["maxNumRaisesPerBettingRound"]);
            _maxHands                     = Convert.ToInt32(gameConfigSettings["maxHands"]);
            _doubleBlindFrequency         = Convert.ToInt32(gameConfigSettings["doubleBlindFrequency"]);
            _botTimeOutMilliSeconds       = Convert.ToInt32(gameConfigSettings["botTimeOutMilliSeconds"]);
            _bRandomDealer                = Convert.ToBoolean(gameConfigSettings["randomDealer"]);
            _bRandomSeating               = Convert.ToBoolean(gameConfigSettings["randomSeating"]);
            _bPauseAfterEachHand          = Convert.ToBoolean(gameConfigSettings["pauseAfterEachHand"]);
            _sleepAfterActionMilliSeconds = Convert.ToInt32(gameConfigSettings["sleepAfterActionMilliSeconds"]);
            _bGraphicsDisplay             = Convert.ToBoolean(gameConfigSettings["graphicsDisplay"]);

            // setup displays
            if (_bGraphicsDisplay)
            {
                //_displays.Add(new GraphicsDisplay());
                _eventHandlers.Add(new ConsoleDisplayHandler());
            }
            if (_sleepAfterActionMilliSeconds > 0)
            {
                _eventHandlers.Add(new SleepHandler(_sleepAfterActionMilliSeconds));
            }

            var textDisplay = new TextDisplay();

            textDisplay.SetWriteToConsole(!_bGraphicsDisplay);

            _displays.Add(textDisplay);

            var gameConfig = new GameConfig
            {
                ConfigFileName = sConfigFile,
                OutputBase     = sOutputBase,
                SmallBlindSize = _smallBlindSize,
                BigBlindSize   = _bigBlindSize,
                StartingStack  = _startingStack,
                MaxNumRaisesPerBettingRound = _maxNumRaisesPerBettingRound,
                MaxHands               = _maxHands,
                DoubleBlindFrequency   = _doubleBlindFrequency,
                BotTimeOutMilliSeconds = _botTimeOutMilliSeconds,
                RandomDealer           = _bRandomDealer,
                RandomSeating          = _bRandomSeating
            };

            // Create players
            var xplayers = doc.Descendants("Player").ToList();
            int i;
            var numBots = xplayers.Count;

            if (numBots == 0)
            {
                throw new Exception("No Player elements found in HoldemConfig.xml");
            }

            _allBots = new List <ServerHoldemPlayer>();
            var playerConfigSettingsList = new List <Dictionary <string, string> >();

            // Create bots - work out how many player and how many observers
            var botNum = 0;

            foreach (var player in xplayers)
            {
                var playerConfigSettings = player.Attributes().ToDictionary(attr => attr.Name.ToString(), attr => attr.Value);

                // read player attributes, add to player config
                string sValue;
                bool   isTrusted = false;
                if (playerConfigSettings.TryGetValue("trusted", out sValue))
                {
                    Boolean.TryParse(sValue, out isTrusted);
                }

                playerConfigSettingsList.Add(playerConfigSettings);
                var bot = new ServerHoldemPlayer(botNum, playerConfigSettings["dll"], isTrusted);
                _allBots.Add(bot);
                botNum++;

                if (!bot.IsObserver)
                {
                    _numPlayers++;
                }
            }

            if (_numPlayers < 2 || _numPlayers > 23)
            {
                throw new Exception($"The number of live (non observer) players found is {_numPlayers}. It must be between 2 and 23");
            }

            // Create array to hold players (actual players not observers)
            _players = new ServerHoldemPlayer[_numPlayers];

            var        rnd         = new Random();
            List <int> unusedSlots = new List <int>();

            for (i = 0; i < _numPlayers; i++)
            {
                unusedSlots.Add(i);
            }

            // assign id to each bot and call InitPlayer
            int nextObserverId = _numPlayers;
            int nextPlayerId   = 0;

            botNum = 0;

            foreach (var bot in _allBots)
            {
                int botId;

                // work out player id
                if (bot.IsObserver)
                {
                    botId = nextObserverId;
                    nextObserverId++;
                }
                else
                {
                    if (_bRandomSeating)
                    {
                        int pos = rnd.Next(unusedSlots.Count);
                        botId = unusedSlots[pos];
                        unusedSlots.RemoveAt(pos);
                    }
                    else
                    {
                        botId = nextPlayerId;
                        nextPlayerId++;
                    }
                }

                // Need to ensure that playerId matches a players index in _players because some code is relying on this
                bot.InitPlayer(botId, gameConfig, playerConfigSettingsList[botNum]);

                // Just call this to preload Name and write entry to timing log
                // ReSharper disable once UnusedVariable
                var sName = bot.Name; // todo: properties shouldn't do anything, so should handle whatever this is doing differently
                botNum++;

                if (!bot.IsObserver)
                {
                    _players[botId] = bot;
                }
            }

            foreach (var display in _displays)
            {
                display.Initialise(gameConfig, _numPlayers, _sleepAfterActionMilliSeconds);
            }
        }