Page 1 of 1

PvP Live

Posted: Tue Aug 09, 2011 11:38 pm
by Losdivinos
Hi guys!

I'm pretty long in the making of a browsergame, and i want to add a PvP system.

Could somebody explain how to do this in HTML, PHP, MySQL and with a little bit Ajax in it?

Los Divinos

Re: PvP Live

Posted: Tue Aug 09, 2011 11:51 pm
by Jackolantern
That is a pretty big question to easily explain. I will give the gist of it, but maybe in the next few days I may try to make a written tutorial that will show the basics and provide a skeleton to work off of in jQuery.

You have to institute polling, basically. While that is pretty much a bad word in most web development, at least for right now, there is no other way around it (unless you are game for making a highly-complex, custom socket or Comet server, which pretty much none of us around here want to do). What you do is create a Javascript timer interval in your page's Javascript, like this:

Code: Select all

var pvpTimer = setInterval(runPVP, 2500);
What this snippet does is run the runPVP() function every 2.5 seconds. This function will need to make an AJAX call to your server to a PHP script that checks your database's "PvP battles table" to see if the player has been added to a PvP battle. If they have been added to a PvP battle, that means another player has just made an attack on them, and you use a snippet like this to force them to a combat page:

Code: Select all

window.location = 'pvpbattle.php';
You won't be able to make the combat traditional turn-based. Instead, you will have to probably make a Javascript/AJAX front end for the combat which calls server-side PHP scripts that compare the time of the last attack to the current time you are trying to make an attack. You can sync those waits to client-side graphics in Javascipt so players have some estimate (it really can't be 100% accurate) of how long their attacks and spells will take to cooldown.

This would all need to work in concert with a "padlock" system that I think already exists in Halls' tutorial series whereby pages check the status of the player when they first start up a script to ensure a player is not trying to run out on combat. That way if a player tries to leave during PvP or even regular combat, you can basically say "Nope! Sorry, you are still in combat" and automatically route them back to the page.

A system like this, fully implemented, would allow for nearly real-time PvP, where even players away from their keyboard could be stalked, targeted, attacked and killed without any input from the victim (the kind of environment most real-time PvP games seek to create). It really can beef up the purely turn-based combat system that PHP can produce.

Re: PvP Live

Posted: Wed Aug 10, 2011 12:05 am
by Losdivinos
Thanks! :)

I understand the most of it. Sounds like a big job. But yeah! I really wanna do it, so i'm gonna give it a try! :)

Re: PvP Live

Posted: Wed Aug 10, 2011 12:25 am
by Jackolantern
It is a big job, yes, but it can pay off. Live PvP is not just a feature. It an entire core mechanic of any PBBG that uses it. It will affect the way combat likely works in your entire game, and not many PBBGs have it right now. I think it is very worthy of the work, and it can make a combat system look very interactive, responsive and exciting.

Re: PvP Live

Posted: Wed Aug 10, 2011 7:28 am
by Xaleph
I agree, it`s the core mechanics that make ( or break ) your game. FIghting and battling are one of those mechanics.

Ok first some rules:
1. the person instantiating the fight is P1.
2. The person getting hit is P2.

Do you want P2 to auto redirect to the battle page as soon as P1 instantiates the fight? Because that would be logical. The only downside is you`ll have to poll a lot. Perhaps it`s an idea to broadcast fights every minute. Poll everyone on every whole minute ( minutes:00) that way loads are reduced. P1 can still fight fast, if he`s instantiating a fight at :59. You could also poll every 30 seconds, or 15. That way, you reduce load.

Re: PvP Live

Posted: Wed Aug 10, 2011 8:00 am
by Callan S.
I'd be inclined to be really simple about it - nothing fancy, just have a javascript code that refreshes the page every ten seconds or so (and I've seen plenty of browser games that monster fighting required more refreshes than this). And have it that turns are always ten seconds long. Someone doesn't act in time, on page refresh the code detects that and it makes them miss their turn.

Personally I would code that, as it's like crawling and to me what jacks talking about is running. I haven't even done code like this, so for myself I'd try and complete the crawler code first :)

Re: PvP Live

Posted: Wed Aug 10, 2011 8:35 am
by Jackolantern
Probably a good idea. I won't lie, the way I outlined it to work is quite complex. And, as Xaleph pointed out, it would be very wasteful with bandwidth due to polling (if you want a combat system like the one I outlined, where it is nearly real-time with live skill cooldowns, you would have to have pretty short poll times or attacking players could get several attacks off before the other player knows they are even being attacked). Of course, most of us won't be dealing with having thousands or more players, and a handful of players won't break the bandwidth bank even with fairly fast polling provided you have a good, paid host plan (hopefully at least a dinky VPS plan that gives you a set amount of server resources; not shared hosting).

I will be considering making a full tutorial on live PvP, because it is something that comes up over and over on here.

Re: PvP Live

Posted: Wed Aug 10, 2011 11:48 am
by Losdivinos
I think i wanna do it like that the person being attacked has to be in the arena first. :)

Re: PvP Live

Posted: Tue Aug 16, 2011 4:25 am
by Callan S.
One idea is that one person has their turn, then it's the other persons turn.

If one person has their turn one day, then the other guy logs on the next day and has his turn, then the turns are a day long.

But if they are on at about the same time, they can keep doing one turn after the other very quickly.

This way it sort of takes your population into account.

Though if it's a day long, might want to make the rewards for winning get a bit bigger for that one, but return to normal if the turns only a few minutes long/relatively instant.

Re: PvP Live

Posted: Tue Aug 16, 2011 5:10 am
by Jackolantern
That is pretty much the de facto PHP PvP system. It works, and it can be fun, but it is still very different from near-real-time PvP.