public void TestGomokuMatchModeParse2()
        {
            GomokuMatchModel match = new GomokuMatchModel(@"0x1-19(1).psq");

            Assert.AreEqual("sWINe13", match.Player1);
            Assert.AreEqual("pela", match.Player2);
        }
        public void TestGomokuMatchModeParse()
        {
            GomokuMatchModel match = new GomokuMatchModel(@"1x0-16(2).psq");

            Assert.AreEqual("1x0-16(2).psq", match.FileName);
            Assert.AreEqual(20, match.Width);
            Assert.AreEqual(20, match.Height);
            Assert.AreEqual(0, match.Result);
            Assert.AreEqual("pisq7", match.Player1);
            Assert.AreEqual("renjusolver", match.Player2);

            Assert.AreEqual(16, match.Moves.Length);

            Assert.AreEqual(10, match.Moves[0].X);
            Assert.AreEqual(10, match.Moves[0].Y);
            Assert.AreEqual(456, match.Moves[0].DurationMS);

            Assert.AreEqual(11, match.Moves[1].X);
            Assert.AreEqual(12, match.Moves[1].Y);
            Assert.AreEqual(4500, match.Moves[1].DurationMS);

            Assert.AreEqual(15, match.Moves[15].X);
            Assert.AreEqual(12, match.Moves[15].Y);
            Assert.AreEqual(32, match.Moves[15].DurationMS);
        }
        private static GomokuMatchModel[] GetMatchesByTournament(string tournament)
        {
            SecurityCheckPath(tournament);

            string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
            path = Path.Combine(path, "Tournaments");
            path = Path.Combine(path, tournament);

            string[] files = Directory.GetFiles(path, "*.psq");

            GomokuMatchModel[] model = new GomokuMatchModel[files.Length];

            for (int i = 0; i < files.Length; i++)
            {
                model[i] = new GomokuMatchModel(files[i]);
            }
            return model;
        }
        public ActionResult Match(string tournamentMatch)
        {
            SecurityCheckPath(tournamentMatch);

            string path = GetMatchPath(tournamentMatch);
            GomokuMatchModel model = new GomokuMatchModel(path);
            model.FileName = tournamentMatch.Replace("\\", "\\\\");
            return View(model);
        }
        public JsonResult MatchJSON(string tournamentMatch)
        {
            SecurityCheckPath(tournamentMatch);

            string path = GetMatchPath(tournamentMatch);
            GomokuMatchModel model = new GomokuMatchModel(path);
            model.FileName = tournamentMatch.Replace("\\", "\\\"");
            return Json(model, JsonRequestBehavior.AllowGet);
        }
        private GomokuMatchModel[] GetMatchesWithSameOpening(GomokuMatchModel reference, string tournament, string engine)
        {
            int minMoves = 3;

            if (reference.Moves.Length < minMoves)
                return new GomokuMatchModel[0];


            GomokuMatchModel[] all = GetMatchesByTournament(tournament);

            List<GomokuMatchModel> selection = new List<GomokuMatchModel>();

            foreach (GomokuMatchModel item in all)
            {
                if (item.Moves.Length < minMoves)
                    continue;

                if (reference.FileName.EndsWith(item.FileName))
                    continue;//self

                int maxMoves = Math.Min(item.Moves.Length, reference.Moves.Length);

                //if( reference.Player1 == player)
                //{
                //    if (item.Player1 == reference.Player1)
                //        continue;//self
                //}
                //else if (reference.Player2 == player)
                //{
                //    if (item.Player2 == reference.Player1)
                //        continue;//self
                //}

                bool founded = true;

                for (int i = 0; i < maxMoves; i++)
                {
                    if ((item.Moves[i].X != reference.Moves[i].X) || (item.Moves[i].Y != reference.Moves[i].Y))
                    {
                        if (i % 2 == 1 && engine == reference.Player2 && i > minMoves)
                        {
                            founded = true;
                            break;
                        }

                        if (i % 2 == 0 && engine == reference.Player1 && i > minMoves)
                        {
                            founded = true;
                            break;
                        }
                        founded = false;
                        break;
                    }
                }
                if (founded)
                {
                    selection.Add(item);
                }
            }

            return selection.ToArray();
        }
        public ActionResult Compare(string engine, string tournamentMatch)
        {
            SecurityCheckPath(tournamentMatch);

            string tournament = Path.GetDirectoryName(tournamentMatch);

            string path = GetMatchPath(tournamentMatch);
            GomokuMatchModel reference = new GomokuMatchModel(path);
            reference.FileName = tournamentMatch.Replace("\\", "\\\\");


            GomokuMatchModel[] sameOpening = GetMatchesWithSameOpening(reference, tournament, engine);

            GomokuCompareModel model = new GomokuCompareModel()
            {
                Matches = sameOpening,
                Reference = reference,
                Player = engine,
            };

            return View(model);
        }
        private static GomokuMatchInfoModel[] GetMatchesModelByTournament(string tournament)
        {
            string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
            path = Path.Combine(path, "Tournaments");
            path = Path.Combine(path, tournament);

            string[] files = Directory.GetFiles(path, "*.psq");

            GomokuMatchInfoModel[] model = new GomokuMatchInfoModel[files.Length];

            for (int i = 0; i < files.Length; i++)
            {
                string file = files[i];
                GomokuMatchModel matchModel = new GomokuMatchModel(file);


                model[i] = new GomokuMatchInfoModel()
                {
                    FileName = tournament + "\\" + Path.GetFileName(file),
                    LastChange = new FileInfo(file).LastWriteTime,
                    Player1 = matchModel.Player1,
                    Player2 = matchModel.Player2,
                    Moves = matchModel.Moves.Length,
                    Result = matchModel.GetMatchResult(),
                };
            }
            return model;
        }
        private static ArraySegment<byte>? CreateObjectBuffer(string path)
        {
            object jsonModel = null;
            if (path.ToLower().EndsWith(".psq"))
            {

                GomokuMatchModel model = new GomokuMatchModel(path);
                string tournamentMatch = path.Substring(_tournamentPath.Length);
                model.FileName = tournamentMatch.Replace("\\", "_");

                jsonModel = model;
            }
            else if (path.ToLower().EndsWith(".html"))
            {
                jsonModel = JsonResultTableModel(path);
            }
            else
            {
                return null;
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(jsonModel);

            return new ArraySegment<byte>(Encoding.UTF8.GetBytes(json));
        }
        string JsonGetMatch(string tournamentMatch)
        {
            if (tournamentMatch == null || tournamentMatch.Contains(".."))
                throw new ArgumentException();

            string path = TournamentController.GetMatchPath(tournamentMatch);
            GomokuMatchModel model = new GomokuMatchModel(path);
            model.FileName = tournamentMatch.Replace("\\", "\\\"");

            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(model);
        }