Search This Blog

Wednesday, June 8, 2011

Game Bot for javascript powered webgame

I made a few game bots for a web game powered by javascript. First ting to clarify is that I made it in my free time and played it in my free time.:) I started with a VB.Net version and later on changed to C#.
So basically, I want to show you how to make such a gamebot in this post.
First drag a System.Windows.Forms.WebBrowser control to your form, let’s say the control variable is webBrowser1. To call a javascript function, use code similar to following sample,

Me.WebBrowser1.Document.Window.Frames(0).Document.Window.Frames(0).Document.InvokeScript("cmd", _
                                         New Object() {fightcmd})

This line of code first gets to the frame where javascript function is defined, then call the function ’cmd’ with one parameter ’fightcmd’.
That’s simple isn’t it? We are half way there, there is a thread problem we need to handle.
Aside from the main thread – a form containing the webBrowser control, we need to define a background thread calling all the movements, fighting, talking functions. If we use the code above directly inside the background thread, there will be a cross thread violation (cross thread violation, control was accessed from one thread other that it was created). To solve the problem, we need to define delegate functions.

Private Function GetHP() As Integer
        Dim hp As Integer = 0
        Dim HpSpan As HtmlElement = Me.WebBrowser1.Document.Window.Frames(0).Document.Window.Frames(0).Document.GetElementById("hpLine_left_no")
        hp = Val(HpSpan.InnerText)
        Return hp
    End Function
                                     Above code is defined in main thread

Public Delegate Function GetHPDelegate() As Integer
Public Class Controller
Private m_gethp As GetHPDelegate
Public Sub New(ByRef caller As Form1, _
               ByRef gethp As GetHPDelegate, _
               …)
Private Sub Fight()
hp = m_BaseControl.Invoke(m_gethp)
End Sub
End Class
                                               Above code is defined in background thread

So problem 80% solved.
Next issue is a bit tricky, what if the javascript function is called only by object. I didn’t find a direct way to call such a function. However, gamebot usually only simulates human actions. And human actions in a webgame is clicking links. So if we can find a way to call those link clicking actions, we find a way to call those hidden javascript functions indirectly. Let’s see an example below,
Let’s say on the webpage, there is a tag element img by clicking which will trigger a javascrpt function p.closeTask().
<div id=”renPicX”><img onclick=”p.closeTask()”/></div>
public void closeTakeTaskWin()
        {
            HtmlElement renPicX = getHtmlElementById("renPicX");
            HtmlElement imgEl = renPicX.GetElementsByTagName("img")[0];
            object obj = imgEl.DomElement;
            System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
            mi.Invoke(obj, new object[0]);
        }
By using above code we can call the onclick method in img element and trigger the p.closeTask() javascript function.
With above 3 basic techniques, webbot’s functionalities should suffice.
Since it is a chinese webgame called (猫游记), I don’t think too many of you will have interest in source code. However if you happen to play the game and have interest in some code, leave your email address in the comment and I will send to you.

1 comment:

  1. Hello, I am having difficulty in creating a bot for the website "Quizlet.com" The game I am creating a bot for is "Space Race." Can you walk me through and help me please?

    ReplyDelete