Long PHP script

Associate
Joined
24 Oct 2002
Posts
980
Location
Manchester
I am writing an online game and progressing nicely with PHP and MySQL (of course)

I need a script to run in the background that processes player positions, combat and stats etc which is getting quite long and difficult to read.

What is the best way to handle a large script? I've tried using includes but due to some looping they were getting messy and caused more errors than it was worth as I've not got an environment I can debug so I'm a notepad developer.

The script is triggered by ajax so the length of time it runs for doesn't bother me and there is no direct return to the client either.

It's just getting a bit difficult to read and manage.

Any tips?
 
OO baby.

and tbh, PHP is a requested based scripting language. It sounds like you need a complete redesign if it A) takes ages, and B) doesn't return anything to the user despite a request (via AJAX) being sent.
 
How long is long? 1000 Lines? 5000 lines?

I'm assuming it's not object oriented as you'd already have your classes in seperate files right? :)

Break the code down into functions that do a specific task each, then group group similar functions together in a file. Have your main code block just call your functions, try to keep any logic in the functions themselves, i.e.

Code:
<?php

function one () {
 // code here
}

function two () {
 // code here
}

function three () {
 // code here
}


// main block
one();
two();
three();
?>

Then there's all the standard good programming practice bits n bobs: indent properly, stick to naming conventions.

When you say "notepad developer" you don't actually mean "notpad.exe" that comes with Windows do you? If yes then change your editor immediately for something which at least has syntax highlighting. Notepad++ / SciTE / Textpad / EditPad / Crimson Editor / etc.

Lastly, you can debug on your own machine very easily, just install Xampp.
 
I'm probably missing the point but can't you offload some of the load by using a cron? I.e. have all user stats updated every hour rather than instantly.
 
Sorry about not replying sooner.

I'm only at 350 lines so it's by no means mammoth, it's just hard to follow.

The game has characters on a map that can move around independently, the script needs to run every 2-10 seconds to update player positions and calculate attacks etc so and as attacks depend on the order of movements and entering/leaving buildings etc is done, they all have to be handled in the order requested. There is a command queue table which the script works through for an amount of time before relinquishing a lock on the queues so another process can do some work.

The script is sent by ajax precisely because it has no response by design, the client will not know they are running it. CRON can only run hourly but I'm not allowed a continuous process on my hosting package so lots of discrete runs that do not affect the clients was the way to go.

I guess I'm struggling with scope putting everything in functions as there are so many variables flying around governing movements and attacks, functions were 5 lines long but needed 10 parameters or an object. I've not really got into writing classes as I haven't felt the need yet.

I can't put the game online yet to demonstrate as it's a reasonably unique idea and I don't want to be pipped to the post but really just talking about it has helped.


PS I'm still using one big script at the moment as sometimes OO is just more work than it's worth.
 
Back
Top Bottom