6 Replies - 2029 Views - Last Post: 10 March 2013 - 10:00 PM

#1 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Merging jQuery and OOP PHP

Posted 10 March 2013 - 02:01 PM

Hey guys,

As you know I'm working on a project that will hopefully one day because a browser based game.

I have the registration/login system more or less complete and at present it is 100% php. No Javascript in it whatsoever.

However, once I get the game portion going I want the front end of the game to be done with jQuery so that things like the players resources and such update in real time.

Now, I'm just wondering how I throw jQuery into the mix. At present I have a couple files that contain the classes and methods that relate to registering and logging in. For instance, cryptPassword, protectPage, registerUser, etc etc etc.

The logic portion is on the pages themselves and those scripts call on the class methods and so on.

Now let's say I'm setting up the base system for houses generating population and farms producing food every so many hours.

Should I put the methods for these things in their own file? And how do I use OOP PHP for this? Since Javascript can't actually specify a method or anything to call do I essentially have a bunch of logic at the top of the page sorting the information to decide what classes and methods to call?

Or do I set up files for each and every different thing? Such as farms.php, housing.php, etc etc.

Is This A Good Question/Topic? 0
  • +

Replies To: Merging jQuery and OOP PHP

#2 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Merging jQuery and OOP PHP

Posted 10 March 2013 - 03:25 PM

Its hard to tell what you already have in mind for how this will work, but at a stab i'll say it really depends on how many functions you plan to be in each class (farms, houses, etc..) You can quite viably do everything you need to for the basics in one file, and call the methods that you see fit from there, but for the sake of cleanliness, 90 functions in one file will be much more difficult to maintain than 3 files with 30 functions in each.

My vote says separate it no more than is necessary, but as much as possible in regards to naming and sanities sake.. If you narrow the question down a bit more, I'll gladly be keeping up with this thread to offer anything I can.

As for getting jQuery into the mix, it's pretty straight forward to have jQuery on a timer call a page to update all of your information in the databases and using the information returned from that page, or a second query to an outputData page, to return the information which needs to be updated in the page your user is currently viewing.

And just to clarify, the query string which javascript submits should be relevant to which method you wanted to call

query = /houses/getUpdates


would be your getUpdates method in your houses class, the real dependent factor here is how you handle what is requested in your backend
Was This Post Helpful? 0
  • +
  • -

#3 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Merging jQuery and OOP PHP

Posted 10 March 2013 - 03:38 PM

Alrighty,

I appreciate the input.

I guess the first thing I have to do is to figure out where I need to start. Being my first browser based game I have no clue where to lay my foundation.

Since this is to be a multiplayer style game that has to do with buildings generating resources, I figure perhaps I should start with the construction system where the player can go into the building page and start construction on a building.

However, I don't want this to be instant, I want buildings to take time to build.

I do admit that I don't have the game 100% down. I have the overall game planned but not all of the smaller aspects.

So maybe my next move should be to get the webpages all made up and looking more or less the way I want them to look and then that could give me a starting point.

This post has been edited by Darkranger85: 10 March 2013 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Merging jQuery and OOP PHP

Posted 10 March 2013 - 04:48 PM

If you're looking for a little direction, I appreciate that you're going at a project that's 100% your own idea and initiative rather than hacking together stuff you can google search for.

You're right to be working on your GUI, but in my opinion, I like to start with a rough outline for a gui then dial things in as I get them working, just on the thought that I would normally run into new problems along the way and end up tweaking it to something that only roughly looked like what I had put together originally.

So, a little direction that I can provide with what you've said so far as to how to accomplish this overall, check out the list of actions below and see if it gives you any ideas on how you want it to specifically function, the code to make it happen is another story, but if you needed a hand on the general idea..

1. Player clicks create house
2. add a database record for this user including the date and time he began production
3. when either your timer (mentioned in my last post) ticks, or when the user refreshes the page/visits again
you'll need to compare the date and time the record was created with the current date and time.
You can give them a % of completion here, or you can update or move that database record into a form which represents 1 created house
4. every tick(time which it takes to create a new unit(city population unit)) you will update their total population in the database
5. update the user with this information

I would suggest getting the main game interface functioning while you have to hit refresh each time to get the back end code to do all your math and display the information for you, once you get that to work, it will be pretty easy(in a sense) to get javascript/jQuery to request that the build que be processed and update the UI in realtime.

To clarify a bit more how javascript works with this type of thing:

javascript.getURLData("runMyUpdates.php") //Will run through your build queue and update database as necessary to show if units have been created

myStuf = javascript.checkForUpdates() // will return a formatted list of updates that have taken place since the last time you ran this

assume myStuf = "{textbox1:100%}:{textbox2:95%}:{textbox3:80%}"

Then evaluate the myStuff variable and update textbox1, textbox2, and textbox3 with the updated values



So in essence all javascript will do is call PHP pages without actually visiting them in your browser, in php using include("runMyUpdates.php") will accomplish the same thing as javascript would given the code in that file should simply run through each record in the database checking for updates that need to be made given the correct time/date conditions have been satisfied.

Hope this helps some, and sorry if you know all this stuff, just trying to get you nudged in the right direction without knowing exactly how much experience you have with programming in general.
Was This Post Helpful? 0
  • +
  • -

#5 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Merging jQuery and OOP PHP

Posted 10 March 2013 - 08:56 PM

Yeah, I refuse to rip off someone elses work and then call it my own.

It might take me 50 years to get it working but it's going to be mine when I'm done.

So anyway, I agree with you on the GUI aspects. I plan on just making it look more or less like I want without bending over backwards. Fine tuning it can come later.

As far as my knowledge of programming. . .

I'm alright at PHP, though I'm still what I would consider an intermediate newb. And with Javascript and jQuery I'm just learning now. So, complete and utter newb would be the term I would use.

Yeah, I refuse to rip off someone elses work and then call it my own.

It might take me 50 years to get it working but it's going to be mine when I'm done.

So anyway, I agree with you on the GUI aspects. I plan on just making it look more or less like I want without bending over backwards. Fine tuning it can come later.

As far as my knowledge of programming. . .

I'm alright at PHP, though I'm still what I would consider an intermediate newb. And with Javascript and jQuery I'm just learning now. So, complete and utter newb would be the term I would use.
Was This Post Helpful? 0
  • +
  • -

#6 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Merging jQuery and OOP PHP

Posted 10 March 2013 - 09:51 PM

Lol, just don't be discouraged, I was asking more for reference to how you structure things in your code.

The number of resources for someone who wants to learn a new language are plentiful, you can pick up a book or google searches all day long that will tell you what a command does, or how to do a specific task, but usually experience(trial/error) is your only option for learning the methodology of organizing your project or 'how stuff works' in general, just wanted to make sure I wasn't giving broad explanations and all you really wanted was a bit of syntax. :)
Was This Post Helpful? 0
  • +
  • -

#7 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: Merging jQuery and OOP PHP

Posted 10 March 2013 - 10:00 PM

Yeah I gotcha.

Unfortunately by the end of this project I'll probably end up needing many in depth explanations lol.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1