Hello,

I was going to do something similar with some 2 players games (which I
have yet to implement), but was planning on doing the polling on the
client-side with Javascript and AJAX.

There is a function in Javascript:
   window.setTimeout(jsFunctionToRun, timeToRunInMilliseconds);

That can be called to force the page to call the passed in JS function
after timeToRunInMilliseconds.  What I am planning on doing is having
my JSFunctionToRun be a function that makes an AJAX call (which will
call something to check the database) and gets the state of the game.
Then based on the state I get back, I'll do what I need on the
callback.  The framework would look something like:

setTimeout("CheckState()", 5000)  //Call JS Function CheckState() @ 5
seconds

function CheckState()
{
  //Make AJAX call to function that will check and get the state value
from the DB
  //Callback function:  ProcessState()
}

function ProcessState()
{
  //IF the db state says it's my turn, do code to make it my turn,
call setTimeoutAgain after I take my turn to get back in the polling
status
  //ELSE  call setTimeout("CheckState", 5000) to check if it's my turn
again in 5 seconds
}


I hope something like this helps :)

On Oct 15, 12:01 pm, meland <eland.m...@gmail.com> wrote:
> Hello,
>
> First, I would appreciate if anyone can help with this issue I'm
> facing.  Second, I am new to web2py, and not a programmer by
> profession.  I have written an application for conducting tournaments
> with the ultimatum game - a simple bargaining game.  The problem: it
> seems to work fine running on my personal machine via localhost, but
> when hosted (WebFaction) the application locks up if a particular link
> is clicked more than once.
>
> The details... one player must wait for information from another
> player.  I'm using a database poll as a means for the waiting player
> to know when information is available.  While the polling takes place,
> the player's view is still presenting an active link.  Clicking this
> link more than once causes a problem.  As I mentioned above, the
> server-side pattern I've implemented works when running locally, but
> seems to fail in a hosted situation.  Oh, since I'm less familiar with
> javascript than Python I was trying for a server-side solution.  I
> have started reading about jQuery but have not yet been able to
> get .one() working to prevent extra clicks.  Thank you in advance!
>
> The relevant code...
>
> The view and controller function from which the player clicks to begin
> the database poll...
>
> def responder_enter_game():
>     """
>     The point at which the responder enters the game.
>     The view will hang on this page as the database polling is
> processed in
>     the next page called from the view.
>     Prior to calling this function, session.first_time must be set to
> True!
>     """
>     return dict()
>
> Note: I using the default layout.html
>
> {{extend 'layout.html'}}
> <h3>You are playing as a responder in this game...</h3>
> <ul>
>   <li>{{=A(B(T("Click to enter game")), _href=URL('responder_waits'))}}
> </li>
>   <li>{{=T('Please be patient...')}}</li>
>   <li>{{=T('You will leave this page after a proposal has been
> offered.')}}</li>
> </ul>
>
> {{extend 'layout.html'}}
> <h3>Waiting for a response... please be patient</h3>
> {{redirect(URL('game_results_proposer'))}}
>
> Here is the controller code for responder_waits...
>
> def check_proposal_confirmed():
>     """
>     The resopnder's view is hanging on respnder_enters_game() until
> proposal_confirmed is True.
>     The function time.sleep(0.5) causes a 0.5 second delay between
>     polls on the database.
>     """
>     proposal_confirmed = False
>
>     while not proposal_confirmed:
>         proposal_confirmed =
> db.games[session.game_id].proposal_confirmed
>         time.sleep(0.5)
>
> def responder_waits():
>     """
>     The responder is waiting for a proposal confirmation to be found
> in the database
>     """
>     if session.first_time:                  ## The first time the
> player clicks the link processing continues
>         session.first_time = False      ## The first time has passed
>         check_proposal_confirmed()   ## This function holds until the
> proposer confirms a proposal
>     else:                                         ## Any additional
> time the player clicks the link no action is taken
>         pass
>
>     return dict()

Reply via email to