public void Test_Session() { string testResourcesPath = UTHelper.GetTestResourceDir(Assembly.GetExecutingAssembly()); instance = this; SessionSuiteRunner = new SessionSuiteRunner(); SessionSuiteRunner.Configuration = XmlSerializerExt.Deserialize <SessionSuiteCfg>( Path.Combine(testResourcesPath, "SessionSuiteRunner_Test_Sessions.xml")); SessionSuiteRunner.Run(); Assert.AreEqual(2, _players.Count); Assert.IsNotNull(_gameRules1); Assert.AreEqual(1, _gameRules1.OnCreateCount); Assert.IsNotNull(_gameRules2); Assert.AreEqual(1, _gameRules2.OnCreateCount); for (int p = 0; p < _players.Count; ++p) { Assert.AreEqual(1, _players[p].OnServerConnectCount); Assert.AreEqual(1, _players[p].OnServerDisconnectCount); Assert.AreEqual(2, _players[p].OnSessionBeginCount); Assert.AreEqual(2, _players[p].OnSessionEndCount); Assert.AreEqual(5, _players[p].OnGameBeginCount); Assert.AreEqual(10, _players[p].OnActionRequiredCount); Assert.AreEqual(5, _players[p].OnGameEndCount); } }
/// <summary> /// Plays the following scenario: /// 1. Prepares chance abstractions. /// 2. Computes a eq-strategy by EqLp. /// 3. Computes a BR on the eq strategies. /// 4. Runs a session with 2 instances of Patience, one is playing eq, the other br. /// 5. Compares actual game result with the predicted game value. /// The test is trying to reuse chance abstractions and merge strategies for different positions /// if the abstraction is the same. /// <param name="baseDir">The function copies all config files from _testResourceDir/baseDir /// to _outDir/baseDir-eq and _outDir/baseDir-br, all intermediate files are also created here.</param> /// </summary> void PlayEqVsBr(string [] bucketizerStrings, int sessionGamesCount, int sessionRepetitionCount, double relativeTolerance) { Console.WriteLine("Run eq vs eq for chance abstractions:"); for (int p = 0; p < bucketizerStrings.Length; ++p) { Console.WriteLine("pos: {0} bucket string: {1}", p, bucketizerStrings[0]); } GameDefinition gd = XmlSerializerExt.Deserialize <GameDefinition>( Props.Global.Expand("${bds.DataDir}ai.pkr.metastrategy/${0}", "leduc-he.gamedef.xml")); string runDir = PrepareRunDir("EqVsBr_Leduc"); IChanceAbstraction [] chanceAbstractions = PrepareConfigsAndChanceAbstractions(runDir, bucketizerStrings); double [] brValues = new double[gd.MinPlayers]; SolveGame(runDir, gd, chanceAbstractions, brValues); Console.WriteLine("Values of BR strategies:"); for (int p = 0; p < brValues.Length; ++p) { Console.WriteLine("pos: {0} val: {1:0.00} mb0/g", p, brValues[p] * 1000); } // To tell the bots where the configs are: Props.Global.Set("dev.TestRunDir", runDir); runDir = CopyDirForEachBot(runDir, gd.MinPlayers); // Now runDir is eq dir. _actualResult = new TotalResult { Name = "Actual result" }; string ssConfigFile = Path.Combine(runDir, "ss.xml"); SessionSuiteRunner runner = new SessionSuiteRunner(); runner.Configuration = XmlSerializerExt.Deserialize <SessionSuiteCfg>(ssConfigFile); runner.Configuration.Sessions[0].GamesCount = sessionGamesCount; runner.Configuration.Sessions[0].RepeatCount = sessionRepetitionCount; runner.IsLoggingEnabled = false; runner.OnGameEnd += new SessionSuiteRunner.OnGameEndHandler(runner_OnGameEnd); runner.Run(); _actualResult.Print(Console.Out); for (int p = 0; p < gd.MinPlayers; ++p) { double expectedResult = brValues[p] * 1000; double actualResult = _actualResult.Players["Patience-Br"].Rate(p); double epsilon = Math.Abs(expectedResult * relativeTolerance); Assert.AreEqual(expectedResult, actualResult, epsilon); } // We can use the files created in other tests. //DeleteBotDirs(runDir); }
public void Test_Game() { string testResourcesPath = UTHelper.GetTestResourceDir(Assembly.GetExecutingAssembly()); instance = this; SessionSuiteRunner = new SessionSuiteRunner(); SessionSuiteRunner.Configuration = XmlSerializerExt.Deserialize <SessionSuiteCfg>( Path.Combine(testResourcesPath, "SessionSuiteRunner_Test_2Players.Sessions.xml")); SessionSuiteRunner.Run(); int totalCardsCount = CalculateTotalCardsCount(); for (int p = 0; p < 2; ++p) { Assert.AreEqual(1, _players[p].OnGameBeginCount); Assert.AreEqual(1, _players[p].OnGameEndCount); } // Guest player did not play. Assert.AreEqual(0, _players[2].OnGameBeginCount); Assert.AreEqual(0, _players[2].OnGameEndCount); }
// Naming convention: // Log file : baseName.log // Zipped log file: baseName.zip // Session suite : baseName-ss.xml void Replay(string baseName) { string testResourcesPath = UTHelper.GetTestResourceDir(Assembly.GetExecutingAssembly()); string tempDir = Path.GetTempPath(); string configZip = Path.Combine(testResourcesPath, baseName + ".zip"); FastZip fz = new FastZip(); fz.ExtractZip(configZip, tempDir, FastZip.Overwrite.Always, null, "", "", true); string origLogPath = Path.Combine(tempDir, baseName + ".log"); string ssConfigFile = Path.Combine(testResourcesPath, baseName + "-ss.xml"); _ssRunner = new SessionSuiteRunner(); _ssRunner.Configuration = XmlSerializerExt.Deserialize <SessionSuiteCfg>(ssConfigFile); _ssRunner.IsLoggingEnabled = true; _ssRunner.LogDir = tempDir; // Replace XML path to the log being replayed with the actual path from the temp directory. _ssRunner.Configuration.Sessions[0].ReplayFrom = origLogPath; for (int p = 0; p < _ssRunner.Configuration.LocalPlayers.Length; ++p) { _ssRunner.Configuration.LocalPlayers[p].CreationParameters.Set("ReplayFrom", origLogPath); } _ssRunner.Run(); string hint; bool comparisonResult = GameLogComparer.Compare(_ssRunner.Configuration.Sessions[0].ReplayFrom.Get(Props.Global), _ssRunner.CurrentLogFile, out hint, int.MaxValue); Console.WriteLine("Original log:" + _ssRunner.Configuration.Sessions[0].ReplayFrom); Console.WriteLine("Replayed log:" + _ssRunner.CurrentLogFile); Console.WriteLine(hint); Assert.IsTrue(comparisonResult); }
static void Run() { try { SocketServer server = new SocketServer("127.0.0.1", _cmdLine.Port); _isWaitingForRemotePlayers = true; WaitRemotePlayers(server, _suiteCfgs, _cmdLine.RemoteTimeout * 1000); _isWaitingForRemotePlayers = false; for (int ss = 0; ss < _suiteCfgs.Length && !_quit; ++ss) { _suitesCount++; _suiteName = _suiteCfgs[ss].Name; SessionSuiteRunner runner = new SessionSuiteRunner(); runner.OnGameEnd += new SessionSuiteRunner.OnGameEndHandler(runner_OnGameEnd); runner.OnSessionBegin += new SessionSuiteRunner.OnSessionBeginHandler(runner_OnSessionBegin); runner.RemotePlayers = _remotePlayers; _isWaitingForRemotePlayers = false; runner.Configuration = _suiteCfgs[ss]; runner.IsLoggingEnabled = true; if (!String.IsNullOrEmpty(_cmdLine.LogDir)) { runner.LogDir = _cmdLine.LogDir; } lock (_thisLock) { _currentSessionSuiteRunner = runner; } runner.Run(); if (!String.IsNullOrEmpty(_cmdLine.OnSessionSuiteEnd)) { try { System.Diagnostics.Process.Start(_cmdLine.OnSessionSuiteEnd, runner.CurrentLogFile); } catch (Exception e) { log.Error(e.ToString()); } } } foreach (SocketServerPlayer rp in _remotePlayers) { if (rp.IsConnected) { rp.OnServerDisconnect("Session suites finished"); rp.Disconnect(); } } } catch (Exception e) { _exitCode = -1; log.Fatal("Unhandled exception in runner thread", e); Console.Error.Write(e.ToString()); if (Debugger.IsAttached) { Debugger.Break(); } } }