精華區beta GameDesign 關於我們 聯絡資訊
瀏覽"How i built a working online poker bot?"心得(下) 作者:NDark 時間:20100113 參考網站: http://www.codingthewheel.com/archives/how-i-built-a-working-poker-bot (續前文) 重點內容 前面提到的input-processing-output 這三個系統的元件應該互相保持最低的了解。以下分述如下。 input是指擷取資訊的方式,可以使用的方式是 1.自螢幕擷取:screenshot analysis,從圖片去分析。 2.自windows底層攔截:作者使用的方式。 3.自網路封包攔截:製作一個假的撲克軟體與server溝通:intercept network packedge 要收集的資訊有(document) a.目前桌上牌的資訊狀態-table information b.目前下牌的狀態文字-summary text , c.聊天資訊-chat data d.玩家的歷史-player history e.整個系統的歷史-system history 這個部分Devlin說明的重點是怎麼利用視窗(就是windows)底層的機制 -"DLL injection"來擷取視窗軟體的內部運作。 這邊需要相當多的視窗程式設計的背景知識, 不過Devlin先強調一點 "DLL injection isn't a hack" DLL injection是一個普遍觀察視窗軟體的方式(並不是駭客), 因為這是微軟提供來處理視窗程式的公開方法。 至少有六種DLL injection的方法 1) via the Registry 2) using Windows hooks ,作者使用的方法 3) as a Debugger 4) with Remote Threads 5) by creating a Trojan DLL 6) CreateProcess/WriteProcessMemory Devlin認為這方法並不難 "DLL injection isn't particularly difficult to implement" 最重要的是撲克客戶端軟體無法宣告所有的DLL都是非法的 (只能用一些關鍵字盯上一些已知的DLL) 有關擷取的方法簡單來講是先透過一個hook DLL 把自己掛到電腦上所有正在執行的程式中, 判斷該程式是不是我們想要找的撲克客戶端程式, 如果是,則把擷取資訊的DLL動態掛進來。 這邊會需要去了解視窗程式設計的 "Windwos Hook","CBT Hook","SetWindowsHookEx" http://msdn.microsoft.com/en-us/library/ms997537.aspx http://msdn.microsoft.com/en-us/library/ms644977(VS.85).aspx http://msdn.microsoft.com/en-us/library/ms644990.aspx 以及如何把DLL動態讀入的方法 (Load another DLL when Injector load by Poker Program) "LoadLibrary" http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx 然後抓取到該視窗的handle之後就是分析你要抓的資訊在哪裡? 這裡又分成假如對方的程式是利用基本的視窗元件作的,那麼工作會簡單得多。 如果對方的程式是自訂的元件,那麼你就需要比較多的"觀察"來找到目標的位置。 這邊提到一個已經現成的分析軟體Detours http://research.microsoft.com/en-us/projects/detours/ http://research.microsoft.com/apps/pubs/default.aspx?id=68568 以及Spy++ Using Spy++ or Winspector to get basic information from the poker client Spy++ and Winspector are useful window analysis and debugging tools which allow you to view window properties and messages for any window in the system http://www.windows-spy.com/ 要挖的部分大概是"文字內容"的部分,也就是線上撲克程式給玩家的資訊欄位。 當然,你不一定要即時取得視窗資訊, 有時候撲克程式會把資料存起來,到那些檔案挖也是可以的。 Devlin在這部分著墨很多, 主要是監控windows的底層函式庫(API hooking)擷取原本呼叫的資料後 再把修改後的結果傳給正確的函式。 "hook the core windows api DrawText(), replace the previous one" API hooking : When your "preprocessing" is done, you'll pass control back   to the original API the poker application thought it was calling in the   first place so that everything works transparently. 因此"我要透過api hooking取得哪些控制項?", 答案是以下:桌的狀態,文字欄位,按鈕,紀錄檔,下/出牌的紀錄,       程式呼叫的一些底層函式庫等。 這邊Devlin釋出了兩個範例 XMonitorBot 取得資訊的bot專案。 XFileMonitor 取得程式存取檔案的監控程式。 有關第二部分 processing是處理資料決定判斷的分歧有兩個, 決定是否要蓋牌認輸,決定要怎麼出牌獲勝。 也就是"偵測牌桌狀態,偵測目前的策略,決定是否要蓋牌,機率統計"流程的三四步驟 這個部分具體要完成的工作是 1.需要一個UI程式用來顯示目前所有bot的資訊。 2.需要一個dll用來處理擷取資訊與處理資料。 有關如何出牌是一們大學問,網路上有很多牌局理論的社群。 Devlin自己撰寫了一個網路Open Source算牌程式的收集程式來當作此部分的基底。 這個部分會形成你的算牌計算群組。會有以下的元件 Simulators , Hand Evaluators , History Analyzers , Poker Calculators 但是這部分的程式設計就他所說應該是撰寫一個 rules-based systems / poker bot frameworks 或說是可抽換的AI設計(Pluggable Intelligence)或多性格的設計(multi-agent system) 譬如說可以決定這個bot是 積極與不積極的AI,高選擇性與低選擇性的AI,唬人的與誠實的AI。 這邊提到一個網路上的工具Poker Tracker 可以用來來建置你的data base,分析你的對手或是本身的策略及漏洞. 所以你的各種AI策略就會包成一些DLL,由廣泛至限縮 1.對於各種撲克的DLL 2.對於線上撲克的DLL 3.對於特種線上軟體的撲克DLL 這邊就有提到幾個簡單的bot範例 譬如說FoldBots(含程式)就是只會蓋牌的程式:不積極,低選擇性,唬人的。 TrashBots只要遇到垃圾牌就蓋牌:不積極,低選擇性,誠實的。 FoldBots工作流程是 : . Inject the botting DLL into the poker client's address space. Step 1: Inject the DLL : inject a DLL (XPokerBot.Hook.dll) into the poker client process . Respond to (poker table) window create notifications via the CBT hook. Step 2: Detect the opening and closing of poker windows . Subclass the poker table's (RichEdit control) chat window. Step 3: Subclass the Chat Window . Intercept chat window text by listening for and parsing EM_STREAMIN messages. Step 4: Intercept chat window text . Parse the text using simple regular expressions. Step 5: Parse the Chat Text . Simulate input (clicking the Fold button) at the right time, based on those messages. Step 6: Simulate User Input . Transmit hole card and table information to the poker bot GUI via simple IPC. Step 7: Update the GUI 其中step 6是最後一部分-模擬輸出的部分。這部分提的比較少。 分為幾種方法 逆向工程破解網路封包(假的客戶端) Reverse-engineer the network protocol and communicate directly with the poker server. 透過視窗的底層 Hook into the poker client just beneath the user-interface. In this scenario, you don't simulate mouse clicks; you figure out what internal function(s) handle those mouse clicks, and call those functions directly. 用鍵盤精靈 Simulate user input - mouse clicks, keyboard input, whatever - at the operating system or even the driver level. 不只是蓋牌或是下牌, 甚至你必須把預設的字串打出去讓你的bot可以與其他玩家作簡單的對談。 所以我們不只要取得文字的資訊,你還要能夠把文字加入視窗程式的元件內容中。 這些細節在原作者的文章中都有提到。這邊就不多作解釋。 結論 經過這些分析,我們就比較清楚要完成這個專案,需要的是不同領域的知識結合, 大致上包含 視窗程式底層的工程師,人工智能的軟體工程師,以及算牌的數學工程師。 如果你只有其中之一,或是都沒有這樣的人才能輔佐你, 要完成這專案就必須付出相當多的金錢,甚至付出之後成果也有可能不是自己的。 就算完成了專案,也都還要繼續與那些線上撲克網站繼續周旋-bot的反制與與反反制。 如果與人合夥,那也要特別注意合夥的法律問題與財務問題,這就不在本篇討論的內容。 -- "May the Balance be with U"(願平衡與你同在) 視窗介面遊戲設計教學( http://0rz.tw/V28It ),討論,分享。歡迎來信。 視窗程式設計(Windows CLR Form)遊戲架構設計(Game Application Framework) 遊戲工具設計(Game App. Tool Design ) 電腦圖學架構及研究(Computer Graphics)論文代讀(含投影片製作) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.96.77.176
asoedarren:現在線上博奕似乎較少用client app 大多轉用web flash 01/14 20:41
NDark:這可能也是反制與反反制的一部分 01/14 20:49
ddavid:然而最大宗之一的PokerStars依然是使用Client App 01/14 21:58
etrexetrex:好棒 01/14 22:23
yoco315:我猜版上不少人都跟我一樣有能力獨立作一個 O_O 01/15 00:50
yoco315:只是後續需要不斷的周旋才是真正吃時間的地方.. 01/15 00:51
yoco315:另外前面說的是對的,好的 AI 玩起來超強的..真的會賺錢.. 01/15 00:51
wangm4a1:推 01/15 08:02
DreamYeh:推 01/15 22:52