Author Topic: Complex Ajax/PHP/MySql Question  (Read 1407 times)

Offline teamcoltra

  • Level 3
  • *
  • Posts: 6
  • Reputation: +0/-0
    • View Profile
Complex Ajax/PHP/MySql Question
« on: January 28, 2008, 04:02:03 PM »
My experiance with AJAX isn't that much, I have configured a few chatboxes and such in my time, but nothing TOO extravagent.

(btw, this is my first post here, and i would like to say thank you for having me in your community)

Here a situational example of what I want to do:
Lets say I was running a website game based on running a house, I would want each user to have a live countup timer for their electricity, that would increase at a steady rate. 
Now I want that person to also buy/use items that deplete that number, and some of those items would also deplete that number at a steady rate.
For instance in this game, the electric meter would keep increasing in how much you pay a month, but if you buy a solar pannel, it would decrease the speed of that meter, you buy a bunch of them, and that meter would acutally start going backwards.

So this member would have a ticker that would be constantly going up 1..2...3..4....
then buy something and if it depletes it faster then the ticker is going up... then it would start to drop again... 4...3...2..

So think of it this way, even simpler terms hopefully, bulleted for those of you who do bullets better then storys:
  • Defined steady number that goes up
  • User can add things to his account to make that number slow down or even stop
  • Think how your electric meter works on your house... same concept

I *COULD* just have a button and the person (in this situation) would just click "Pay Electric Bill" and all the calculations would be done there and they would only get whatevers left... but thats not origional. I really want to offer my users a live ticker to make the game more fun and exciting.

(my game is not based on an electric bill, but its still in a closed disscussion, so i can't say what it IS about. The answer to this, however, will answer my question in application as well)

Offline vizion

  • Level 7
  • *
  • Posts: 32
  • Reputation: +1/-0
    • View Profile
    • Riddle Contest of Dooooom
Re: Complex Ajax/PHP/MySql Question
« Reply #1 on: January 28, 2008, 08:11:25 PM »
Seems like some math could take of this problem. Hmm..interesting problem. I'll work on it and I also look forward to answers from the others here. Also, that electric  bill  game sounds fun ;).

Offline leZourite

  • Level 12
  • *
  • Posts: 81
  • Reputation: +2/-0
    • View Profile
    • After doomsday
Re: Complex Ajax/PHP/MySql Question
« Reply #2 on: January 28, 2008, 08:42:29 PM »
To be using Ajax it all depends at what frequency the page needs a page refresh, i have longed debated (with my coding buddy) for the use of Ajax in our game, and we opted for discreet uses of Ajax (some dynamic tooltips, for forms etc).

Because if you have to refresh the pages/game a lot it will defeat completely the purpose of Ajax and you will end up using more bandwidth than saving it.

Or you could go full Ajax, but it's not too practical to use (more work with security, debugging, a nightmare in short).

Anyway, to do this (in Ajax) you need your Ajax request to add things to the user account (easy), the response from the server should indicate the way your "bill" is going (upward or downward) and at what frequency : 1, 2, -4, -10. For accuracy purpose, you should get the actually bill value too so to synchronize with the client display.

With this response  the client Javascript should reset the counter on screen and take into account the modifications to the bill (by increasing or decreasing the value mostly) this is the tricky part actually, in fact what it says is that your client Javascript auto generates its content through Ajax. the response can be XML (easy parsing) or can be plain Javascript (but your client javascript will need to eval the response because no browser will parse a response in text inside a <script></script>)

And last but not least for the sake of keeping things tidy, over time you will need a timed (say every 20 or more seconds) Ajax call, to synch data between the display and the server.


Offline teamcoltra

  • Level 3
  • *
  • Posts: 6
  • Reputation: +0/-0
    • View Profile
Re: Complex Ajax/PHP/MySql Question
« Reply #3 on: January 31, 2008, 09:11:57 PM »
You keep saying that the use of AJAX makes  it harder, but is there another way (other then flash -eww- that could display this info live)?

Offline RangerSheck

  • Level 9
  • *
  • Posts: 45
  • Reputation: +3/-0
    • View Profile
    • Aethora, the Browser-Based Tactical RPG
Re: Complex Ajax/PHP/MySql Question
« Reply #4 on: February 01, 2008, 01:07:20 AM »
Ajax doesn't have to be hard. Start with a good javascript library - there are several to choose from. I use the prototype/scriptaculous combo, but jQuery is also very popular (or Dojo Toolkit or MooTools, or some others I can't remember).

I use quite a bit of AJAX in my game, but it's still more of the standard web client-server idea; browser makes a request, server sends a response. Ajax is just saving me from refreshing a whole page (and querying more data than is necessary to draw that entire page).

I do have a chat function in my game, and that just uses polling to keep it "live". When you load the page, the id of the last message gets stored in your session. After the page is loaded, the browser sends an AJAX request every 10 seconds to ask the server, "any new messages?" The server looks does a query of the db to get the id of the last message posted and compares it to the id stored in the user's session - if they are the same, it just ignores the request. If they are different, it grabs the last 10 messages and sends them back to the browser and tells the browser to replace the chat window contents with those 10 newest messages.

If you are security conscious with non-ajax requests, it's not any harder to make ajax secure. They're all just requests to the web server :) Also, for debugging javascript and ajax, the Firebug extension for Firefox is pretty much a must have. It makes life so much easier.

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
Re: Complex Ajax/PHP/MySql Question
« Reply #5 on: February 02, 2008, 03:05:24 PM »
I dont know why you really want to use Ajax...I will write some example here

SQL: timechanged/electricity_amoun/e_addon

so when user builds something you add unix timestamp (time()) to field timechanged, then you calculate electriciy amount from last time, and you write it in table, as well as e_addon. then you use normal JS, you write such function that have some arguments (like time to change that numbers n shit)

so lets say...you have build XXX lvl 1, and you electricity is 0, addon is +10/s. so after 1minute you build lvl 2 XXX building and your electricity is (60*10=600) you write 600 into table, and addon changes to +25/s, etc etc.

if you dont know any things feel free to write me email to saljutin@gmail.com (I dont check this site much)

Offline mobeamer

  • Level 13
  • *
  • Posts: 93
  • Reputation: +0/-0
    • View Profile
    • Untouchable Games
Re: Complex Ajax/PHP/MySql Question
« Reply #6 on: February 22, 2008, 09:16:15 AM »
Not sure if this is still alive...but this is how I'd do it.

Create an image tag.
<img id="counter" src="counter.gif">

Create a php page, updateGasMeter.php,  file can take all your rules and apply them. It will return the width to display in the gasMeter as...or text to display.

I'd create an Ajax function that would call my page

function updateCounter()
{
           ...calls updateGasMeter.php
}

When the ajax completes, call it again in 1 second...or what ever your time span is

function updateCounter_Complete()
{
        //set image width
        img.style.width = resultText + "px";
         setTimeout("updateCounter()", 1000);
}

This is all Pseudo Code - don't be to critical of it


I build games
My Blog

Offline greendots

  • Level 3
  • *
  • Posts: 8
  • Reputation: +0/-0
    • View Profile
Re: Complex Ajax/PHP/MySql Question
« Reply #7 on: May 25, 2008, 01:37:35 PM »
I'd imagine you'd want to use javascript to handle the timer in all instances.

You'd only want to do an AJAX request if any of the timer details changed rather than a full page reload.

You could call a regular AJAX request perhaps to make sure the javascript timer was not getting out of sync, but when you factor in ping times to and from the server, plus the request, your timer will not be extremely accurate.  To within 1 second is perfectly reasonable, but it will not be millisecond-perfect.

If you find your timer slips out of sync often, just factor that in to the javascript, tweak it, and do your best with it.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal