コード例 #1
0
ファイル: Program.cs プロジェクト: nathantran-zl/chessdb
        public void ProcessQueue(object ThreadId)
        {
            string strThreadId = string.Format("{0:D2}", int.Parse(ThreadId.ToString()));
            int    MinDepth;

            if (!int.TryParse(ConfigurationManager.AppSettings["MinDepth"], out MinDepth))
            {
                MinDepth = 5;
            }
            int MaxDepth;

            if (!int.TryParse(ConfigurationManager.AppSettings["MaxDepth"], out MaxDepth))
            {
                MaxDepth = 5;
            }
            Console.WriteLine("[" + strThreadId + "] 正在初始化引擎...");
            CheckProtocol();
            WaitForReady();
            while (!Program.bClosing)
            {
                ChessDotNet.ChessGame board = new ChessDotNet.ChessGame(ConfigurationManager.AppSettings["StartFEN"]);
                Console.WriteLine("[" + strThreadId + "] 正在获取初始局面...");
                int CurrentDepth = 0;
                while (CurrentDepth < MinDepth)
                {
                    try
                    {
                        HttpWebRequest  req      = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["CloudBookURL"] + "?action=querylearn&board=" + board.GetFen());
                        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new Exception("获取局面失败。");
                        }
                        StreamReader myStreamReader = new StreamReader(response.GetResponseStream());
                        String       result         = TrimFromZero(myStreamReader.ReadToEnd());
                        myStreamReader.Close();
                        response.Close();
                        if (result.Length > 0)
                        {
                            if (result.Contains("move:"))
                            {
                                board.MakeMove(result.Substring(5));
                                CurrentDepth++;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                        Thread.Sleep(1000);
                    }
                }
                CurrentDepth = 0;
                try
                {
                    while (CurrentDepth < MaxDepth)
                    {
                        Console.WriteLine("[" + strThreadId + "] 正在计算...");
                        EngineStreamWriter.WriteLine(ConfigurationManager.AppSettings["FenCommand"] + " " + board.GetFen());
                        EngineStreamWriter.WriteLine(ConfigurationManager.AppSettings["GoCommand"]);
                        String outstr      = EngineStreamReader.ReadLine();
                        bool   hasBestMove = false;
                        int    score       = int.MinValue;
                        int    nps         = 0;
                        String bestmove    = null;
                        while (outstr != null)
                        {
                            var tmp = outstr.Split(' ');
                            for (int i = 0; i < tmp.Length; i++)
                            {
                                if (tmp[i] == "bestmove")
                                {
                                    hasBestMove = true;
                                    bestmove    = tmp[i + 1];
                                    break;
                                }
                                else if (tmp[i] == "score")
                                {
                                    score = int.Parse(tmp[i + 2]);
                                }
                                else if (tmp[i] == "nps")
                                {
                                    nps = int.Parse(tmp[i + 1]) / 1000;
                                }
                            }
                            if (hasBestMove)
                            {
                                break;
                            }
                            outstr = EngineStreamReader.ReadLine();
                        }
                        WaitForReady();
                        if (hasBestMove)
                        {
                            Console.WriteLine("[" + strThreadId + "] 正在提交结果...(NPS = " + nps.ToString() + "K)");
                            bool succeess = false;
                            while (!succeess)
                            {
                                try
                                {
                                    HttpWebRequest  req      = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["CloudBookURL"] + "?action=store&board=" + board.GetFen() + "&move=move:" + bestmove);
                                    HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                                    if (response.StatusCode != HttpStatusCode.OK)
                                    {
                                        throw new Exception("提交结果失败。");
                                    }
                                    response.Close();
                                    board.MakeMove(bestmove);
                                    CurrentDepth++;
                                    succeess = true;
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.ToString());
                                    Thread.Sleep(1000);
                                }
                            }
                            try
                            {
                                Console.WriteLine("[" + strThreadId + "] 正在获取局面...");
                                HttpWebRequest  req      = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["CloudBookURL"] + "?action=querylearn&board=" + board.GetFen());
                                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                                if (response.StatusCode != HttpStatusCode.OK)
                                {
                                    throw new Exception("获取局面失败。");
                                }
                                StreamReader myStreamReader = new StreamReader(response.GetResponseStream());
                                String       result         = TrimFromZero(myStreamReader.ReadToEnd());
                                myStreamReader.Close();
                                response.Close();
                                if (result.Length > 0)
                                {
                                    if (result.Contains("move:"))
                                    {
                                        board.MakeMove(result.Substring(5));
                                        CurrentDepth++;
                                    }
                                    else
                                    {
                                        Console.WriteLine("[" + strThreadId + "] 正在学习...");
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.ToString());
                                Thread.Sleep(1000);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    Thread.Sleep(1000);
                }
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: nathantran-zl/chessdb
        public void ProcessQueue(object ThreadId)
        {
            string strThreadId = string.Format("{0:D2}", int.Parse(ThreadId.ToString()));

            WaitForReady();
            bool bExitAfterEmptyQueue;

            if (!Boolean.TryParse(ConfigurationManager.AppSettings["ExitAfterEmptyQueue"], out bExitAfterEmptyQueue))
            {
                bExitAfterEmptyQueue = false;
            }
            bool bExitAfterResume;

            if (!Boolean.TryParse(ConfigurationManager.AppSettings["ExitAfterResume"], out bExitAfterResume))
            {
                bExitAfterResume = true;
            }
            while (!Program.bClosing)
            {
                try
                {
                    bool bResuming = false;
                    if (!File.Exists("last" + strThreadId + ".txt"))
                    {
                        Console.WriteLine("[" + strThreadId + "] 正在获取新队列...");
                        HttpWebRequest  req      = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["CloudBookURL"] + "?action=getqueue&token=" + ConfigurationManager.AppSettings["AccessToken"]);
                        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new Exception("获取队列失败。");
                        }
                        StreamReader myStreamReader = new StreamReader(response.GetResponseStream());
                        String       result         = TrimFromZero(myStreamReader.ReadToEnd());
                        myStreamReader.Close();
                        response.Close();
                        if (result.Length > 0)
                        {
                            if (result == "tokenerror")
                            {
                                throw new Exception("AccessToken错误。");
                            }
                            File.WriteAllText("last" + strThreadId + ".txt", result);
                        }
                        else if (bExitAfterEmptyQueue)
                        {
                            Program.bClosing = true;
                            return;
                        }
                    }
                    else
                    {
                        Console.WriteLine("[" + strThreadId + "] 正在恢复队列...");
                        if (bExitAfterResume)
                        {
                            bResuming = true;
                        }
                    }
                    if (File.Exists("last" + strThreadId + ".txt"))
                    {
                        String       fenkey = File.ReadAllText("last" + strThreadId + ".txt");
                        StringReader sr     = new StringReader(fenkey);
                        String       fen    = TrimFromZero(sr.ReadLine());
                        while (fen != null && fen.Length > 0)
                        {
                            Console.WriteLine("[" + strThreadId + "] 正在计算...");
                            String[] outdata            = fen.Split(' ');
                            ChessDotNet.ChessGame board = new ChessDotNet.ChessGame(outdata[0] + ' ' + outdata[1] + ' ' + outdata[2] + ' ' + outdata[3]);
                            if (outdata.Length > 4)
                            {
                                board.MakeMove(outdata[5]);
                            }
                            EngineStreamWriter.WriteLine("position fen " + board.GetFen());
                            EngineStreamWriter.WriteLine(ConfigurationManager.AppSettings["GoCommand"]);
                            String outstr      = EngineStreamReader.ReadLine();
                            bool   hasBestMove = false;
                            int    score       = int.MinValue;
                            int    nps         = 0;
                            long   nodes       = 0;
                            String bestmove    = null;
                            while (outstr != null)
                            {
                                var tmp = outstr.Split(' ');
                                for (int i = 0; i < tmp.Length; i++)
                                {
                                    if (tmp[i] == "bestmove")
                                    {
                                        hasBestMove = true;
                                        bestmove    = tmp[i + 1];
                                        break;
                                    }
                                    else if (tmp[i] == "score")
                                    {
                                        try
                                        {
                                            score = int.Parse(tmp[i + 2]);
                                            if (tmp[i + 1] != "cp")
                                            {
                                                if (score > 0)
                                                {
                                                    score = 30000 - score;
                                                }
                                                else
                                                {
                                                    score = -30000 - score;
                                                }
                                            }
                                        }
                                        catch { }
                                    }
                                    else if (tmp[i] == "nps")
                                    {
                                        try
                                        {
                                            nps = int.Parse(tmp[i + 1]) / 1000;
                                        }
                                        catch { }
                                    }
                                    else if (tmp[i] == "nodes")
                                    {
                                        try
                                        {
                                            nodes = int.Parse(tmp[i + 1]) / 1000;
                                        }
                                        catch { }
                                    }
                                }
                                if (hasBestMove)
                                {
                                    break;
                                }
                                ReadLineDelegate d      = EngineStreamReader.ReadLine;
                                IAsyncResult     result = d.BeginInvoke(null, null);
                                while (true)
                                {
                                    result.AsyncWaitHandle.WaitOne(300000);
                                    if (result.IsCompleted)
                                    {
                                        outstr = d.EndInvoke(result);
                                        break;
                                    }
                                    else
                                    {
                                        EngineStreamWriter.WriteLine("stop");
                                    }
                                }
                            }
                            WaitForReady();
                            if (hasBestMove && score != int.MinValue)
                            {
                                if (Math.Abs(score) > 30000)
                                {
                                    Console.WriteLine("[" + strThreadId + "] 清除Hash重新计算...");
                                    EngineStreamWriter.WriteLine("ucinewgame");
                                    WaitForReady();
                                    continue;
                                }
                                Console.WriteLine("[" + strThreadId + "] 正在提交结果...(NPS = " + nps.ToString() + "K)");
                                bool succeess = false;
                                while (!succeess)
                                {
                                    if (outdata.Length > 4)
                                    {
                                        try
                                        {
                                            HttpWebRequest  req      = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["CloudBookURL"] + "?action=store&board=" + outdata[0] + ' ' + outdata[1] + ' ' + outdata[2] + ' ' + outdata[3] + "&move=" + outdata[5] + "&score=" + score.ToString() + "&nodes=" + nodes.ToString() + "&token=" + StringToMD5Hash(ConfigurationManager.AppSettings["AccessToken"] + outdata[0] + ' ' + outdata[1] + ' ' + outdata[2] + ' ' + outdata[3] + outdata[5] + score.ToString()));
                                            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                                            if (response.StatusCode != HttpStatusCode.OK)
                                            {
                                                throw new Exception("提交结果失败。");
                                            }
                                            StreamReader myStreamReader = new StreamReader(response.GetResponseStream());
                                            String       result         = TrimFromZero(myStreamReader.ReadToEnd());
                                            myStreamReader.Close();
                                            response.Close();
                                            if (result == "tokenerror")
                                            {
                                                throw new Exception("AccessToken错误。");
                                            }

                                            board = new ChessDotNet.ChessGame(outdata[0] + ' ' + outdata[1] + ' ' + outdata[2] + ' ' + outdata[3]);
                                            board.MakeMove(outdata[5]);
                                            int tmpscore = -score;
                                            if (tmpscore < -10000)
                                            {
                                                tmpscore--;
                                            }
                                            else if (tmpscore > 10000)
                                            {
                                                tmpscore++;
                                            }
                                            req      = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["CloudBookURL"] + "?action=store&board=" + board.GetFen() + "&move=" + bestmove + "&score=" + tmpscore.ToString() + "&token=" + StringToMD5Hash(ConfigurationManager.AppSettings["AccessToken"] + board.GetFen() + bestmove + tmpscore.ToString()));
                                            response = (HttpWebResponse)req.GetResponse();
                                            if (response.StatusCode != HttpStatusCode.OK)
                                            {
                                                throw new Exception("提交结果失败。");
                                            }
                                            myStreamReader = new StreamReader(response.GetResponseStream());
                                            result         = TrimFromZero(myStreamReader.ReadToEnd());
                                            myStreamReader.Close();
                                            response.Close();
                                            if (result == "tokenerror")
                                            {
                                                throw new Exception("AccessToken错误。");
                                            }
                                            succeess = true;
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.ToString());
                                            Thread.Sleep(1000);
                                        }
                                    }
                                    else
                                    {
                                        try
                                        {
                                            int tmpscore = -score;
                                            if (tmpscore < -10000)
                                            {
                                                tmpscore--;
                                            }
                                            else if (tmpscore > 10000)
                                            {
                                                tmpscore++;
                                            }
                                            HttpWebRequest  req      = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["CloudBookURL"] + "?action=store&board=" + outdata[0] + ' ' + outdata[1] + ' ' + outdata[2] + ' ' + outdata[3] + "&move=" + bestmove + "&score=" + tmpscore.ToString() + "&nodes=" + nodes.ToString() + "&token=" + StringToMD5Hash(ConfigurationManager.AppSettings["AccessToken"] + outdata[0] + ' ' + outdata[1] + ' ' + outdata[2] + ' ' + outdata[3] + bestmove + tmpscore.ToString()));
                                            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                                            if (response.StatusCode != HttpStatusCode.OK)
                                            {
                                                throw new Exception("提交结果失败。");
                                            }
                                            StreamReader myStreamReader = new StreamReader(response.GetResponseStream());
                                            String       result         = TrimFromZero(myStreamReader.ReadToEnd());
                                            myStreamReader.Close();
                                            response.Close();
                                            if (result == "tokenerror")
                                            {
                                                throw new Exception("AccessToken错误。");
                                            }
                                            succeess = true;
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.ToString());
                                            Thread.Sleep(1000);
                                        }
                                    }
                                }
                            }
                            fen = TrimFromZero(sr.ReadLine());
                        }
                        fenkey = StringToMD5Hash(fenkey);
                        try
                        {
                            HttpWebRequest  req      = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["CloudBookURL"] + "?action=ackqueue&key=" + fenkey + "&token=" + StringToMD5Hash(ConfigurationManager.AppSettings["AccessToken"] + fenkey));
                            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                            if (response.StatusCode != HttpStatusCode.OK)
                            {
                                throw new Exception("提交结果失败。");
                            }
                            StreamReader myStreamReader = new StreamReader(response.GetResponseStream());
                            String       result         = TrimFromZero(myStreamReader.ReadToEnd());
                            myStreamReader.Close();
                            response.Close();
                            if (result == "tokenerror")
                            {
                                throw new Exception("AccessToken错误。");
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                            Thread.Sleep(1000);
                        }
                        File.Delete("last" + strThreadId + ".txt");
                        if (bResuming)
                        {
                            Program.bClosing = true;
                            return;
                        }
                    }
                    else
                    {
                        Thread.Sleep(3000);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    Thread.Sleep(1000);
                }
            }
        }