14 Replies - 1346 Views - Last Post: 24 August 2011 - 05:56 PM Rate Topic: -----

#1 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Web Based Game Theory.

Posted 23 August 2011 - 03:02 AM

Hey,

Just after someone checking/editing my theory to make sure this is the best way to do it.

I'm considering starting a web based game, and I want to be able to set 'rounds' with different multipliers, things such as speed, score, land gain, defence boost etc. I'm also wanting to create a backend system to setup new games, and/or edit the multipliers to get the calculations right.

So I'm guessing I'd have a class object for game, which would be passed the multipliers each time it's called into action, (which would be pulled from a DB table(say 'rounds'), which would be stored in the 'rounds' table when they are created through the backend), and would contain every function for the round, build, combat, explore, random events etc?

Is This A Good Question/Topic? 0
  • +

Replies To: Web Based Game Theory.

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Web Based Game Theory.

Posted 23 August 2011 - 03:24 AM

That sounds fine to me from what I can understand.

However, the Game object wouldn't handle combat etc, you'd delegate this off to instances of objects that are actually fighting, for example Npc and Player. These would do the fighting and their HP would be affected accordingly.

These classes can, of course, use the multipliers that are stored centrally in the Game object.
Was This Post Helpful? 3
  • +
  • -

#3 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Web Based Game Theory.

Posted 23 August 2011 - 01:47 PM

Hey, thanks for getting back to me.
Using a player class, what would be the best way to go about handling different races? For example, race one and two have some unique buildings, or units, how would I best encompass this? Maybe call the player class with an array of unique buildings, or units, depending on the race?
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Web Based Game Theory.

Posted 24 August 2011 - 12:45 AM

Usually you'd have something like this:
abstract class Player {
    protected $damageMultiplier = 1;
    public function Fight() {
        // Use $this->damageMultiplier etc
    }
}

public class Human extends Player {
    protected $damageMuliplier = 1.4;
}

public class Dragon extends Player {
    protected $damageMultiplier = 0.6;
}


So you'd have a base that does everything, for example Player or Building and then extend that for your different races/races' buildings. You can always store in the database your different races, the buildings they have etc and what classes represent them.

Do you see what I mean? Too early for me to explain any better haha sorry!
Was This Post Helpful? 0
  • +
  • -

#5 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Web Based Game Theory.

Posted 24 August 2011 - 01:55 AM

I vaguely understand what your getting at.
Say I had an empire within the game, so this empire has a class unit, i'd use an abstract class empire, having public class trooper extends empire, and trooper would hold all of troops info, attack values, defence values ... Right?
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Web Based Game Theory.

Posted 24 August 2011 - 02:52 AM

Not really no, the way I would do it,as an example, would be this:

RaceAbstract (Abstract Base Class for a race)
BuildingAbstract (Abstract Base Class for a building)
CharacterAbstract (Abstract Base Class for a character)

The abstract classes above would define the common methods that will be called across all Races, Buildings and Characters. So buildings can, for example, TakeDamage, characters can, for example GiveDamage. TakeDamage could take an integer of the amount of damage, then multiply it via the defense multiplier, so say you passed in an integer of 10 to take off this building's HP, however it had upgraded it's shields, or something, you'd have a defense multiplier now of 0.8 (20% defense upgrade!), which means only 8HP would be taken off.

Same way for GiveDamage, apart from it will return an integer based on the attack multiplier, know what I mean?

All multipliers will default to 1, so the damage given/taken is just exactly as you put in.

Now, let's say you had a Barracks, you would do this:
class HumanBarracks extends BuildingAbstract {
   public $hp = 1000;
   public $multiplier_defense = 0.8;
}


This means that the barracks building has a 20% defense above all default buildings, and 1000HP.

In the same way, let's say that you had a trooper (normal soldier with a machine gun or something):
class HumanSoldier extends CharacterAbstract {
    public $hp = 50;
    public $base_damage = 20;
    public $multiplier_attack_building = 0.6; // He only has a machine gun so won't really do much damage to them..
    public $multiplier_attack_character = 1.1; // But machine guns rip other dudes apart, muahaha!
}


Now you'd get some damage to give to the barracks from the soldier, so this method would return 14. Then you'd pass it into the damage function of the building, and a further 20% would be taken off! So you'd get approx 11 damage taken off the building.

Now then, let's say you have a rocket soldier who kicks ass against buildings:
class HumanRocketSoldier extends CharacterAbstract {
    public $hp = 50;
    public $base_damage = 40;
    public $multiplier_attack_building = 1.8; // Rockets pwn buildings
    public $multiplier_attack_character = 0.8; // Even tho rockets are like big boom weapons, for some reason in games they arn't effective against characters so let's copy them!
}


Then you'd do the same calculations but against the rocket soldier guy over a machine gun guy.

Now I've prefixed the classes with Human, this will define the race that the players belong to, you can do this in an autoloader for example if you wanted to load an AlienRocketSoldier or a HumanRocketSoldier.

So you'd have class Human extends Race { }.

I don't really know what you'd store in a race, perhaps some global multipliers that you apply. So all human buildings are stronger or weaker by default?

Now do you see where I'm going :D:D:D
Was This Post Helpful? 2
  • +
  • -

#7 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Web Based Game Theory.

Posted 24 August 2011 - 04:49 AM

Thanks for going through it like that for me! I can see how i'm going to handle it now, thank you so very much :)
Was This Post Helpful? 0
  • +
  • -

#8 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Web Based Game Theory.

Posted 24 August 2011 - 05:09 AM

Hey no prob at all, was fun!

Makes me want to start writing my own game again haha :)

Have fun making it!
Was This Post Helpful? 1
  • +
  • -

#9 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Web Based Game Theory.

Posted 24 August 2011 - 11:34 AM

Another thing has just crossed my mind,
How would I have a sort of 'Live' ticking, so a players resources are recalculated and updates as time goes by. So for example, they gain 1,000 gold every hour, how would I go about updating their values when they aren't logged in, keeping a constant stream of updates (As Antrophia does).
Would I have something ran on each page load, such as a Tick function, which would find the time of the last update, then calculate their hourly income, calculate that into the time passed since the last update, then save the new values, including the current time. Would this be the best way of looking at it?

Also, when showing 'Time to train troops', say a 'train' would take 8 hours, would I just get the time 8 hours in the future and store it as finish time, then show time left by calculating the time left until that time?
Was This Post Helpful? 0
  • +
  • -

#10 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Web Based Game Theory.

Posted 24 August 2011 - 11:39 AM

For the training, you could have a dateReady column in your database. When they click 'Train troop!' or whatever, it will insert into dateReady = DATE_ADD(NOW(), INTERVAL 8 HOUR);. Obviously when you're selecting out 'active' or 'available' troops, you would select WHERE dateReady < NOW().

With regards to the 1000 per hour, you could have a cron script, and they'd gain 1000 coins (everyone?!) every hour, on the hour. If it varied per person, you can just store this somewhere, and and increase the coin amount when that cron runs.

Of course, the 'cron' job can just be a PHP script.
Was This Post Helpful? 0
  • +
  • -

#11 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Web Based Game Theory.

Posted 24 August 2011 - 12:21 PM

The 1,000 was just an example i was using, amounts will depend on population amount and other factors, just wasn't sure how to do it.

I mean for all resources, so I'd make calculations based on mines, farms etc, on a per-user basis, which is why I was thinking of having a Tick() function run on page load.
Was This Post Helpful? 0
  • +
  • -

#12 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Web Based Game Theory.

Posted 24 August 2011 - 12:27 PM

Doing it on page load is fine if you only need to update that current player and don't rely on them having money when they're not there (like other players viewing their profile, or something).

I mean, let's say for example you provide an API that allows people to embed an image that shows how much gold they have. Let's say that it does go up by 1000 gold every hour, and they don't login for 48 hours.

They'll have an image on their profile everywhere they go saying they have something like 3000 coins. Then they log in, and all of a sudden this jumps up to 51000 coins... Know what I mean?!

If you was to have a cron, you can update all players every single hour in one fell swoop...
UPDATE players SET gold = gold + 1000;


Or something like (not every hour on the hour, but every hour!):
UPDATE players SET gold = gold + 1000 WHERE lastUpdated < DATE_SUB(NOW(), INTERVAL 1 HOUR)


Or if it was a different amount per player:
UPDATE players SET gold = gold + timeIncreaseGold;


Know what I mean? Doing it in a cron would also offset the time it takes to another process than your webserver, meaning that pages load at a consistent time.
Was This Post Helpful? 1
  • +
  • -

#13 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Web Based Game Theory.

Posted 24 August 2011 - 01:09 PM

Thankyou yet again, and thanks so much for all the help.

This post has been edited by E_Geek: 24 August 2011 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

#14 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Web Based Game Theory.

Posted 24 August 2011 - 01:27 PM

Well at the end of the day, you could make your units fight via a central class that will handle everything... For example:
class BattleSystem
{
    public function Fight(CharacterAbstract $attacker, CharacterAbstract $defender)
    {
        $base_attack_amount = $attacker->base_damage * $attacker->multiplier_attack_character;
        $actual_attack_amount = $base_attack_amount * $defender->muliplier_defense;

        $defender->hp -= $actual_attack_amount;
        $defender->save();
    }
}


This assumes that you're using the classes as outlined above, and your CharacterAbstract has a ->save() method that will save the variables back out to the database for that character.

Even better you could make a generic fight method that accepts a character and then anything. You could check that the $defender's class extends from the base class, detect whether it's a building or character, and use the appropriate modifiers.
Was This Post Helpful? 1
  • +
  • -

#15 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: Web Based Game Theory.

Posted 24 August 2011 - 05:56 PM

This is actually a really good post for people who want to start game development in any language. I myself have been in development of a Text based browser game :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1