Author Topic: Help With Achievement  (Read 532 times)

Offline capturts

  • Level 8
  • *
  • Posts: 43
  • Reputation: +1/-0
    • View Profile
    • Space Freight
Help With Achievement
« on: October 24, 2010, 04:41:56 PM »
I currently have this:

Code: [Select]
function isAchievment($number){

while (strlen($number)>1){
$number=$number/10;
}
if ($number!=round($number)){
return(false);
}

$ok=array(1,2,5);

if (in_array($number, $ok)){
return (true);
}
return (false);
}

A player has their overall score (maybe 22000 points). They go to their achievements page and see what they have managed. What it should say is something like:



* You got 1 points!
* You got 2 points!
* You got 5 points!
* You got 10 points!
* You got 20 points!
* You got 50 points!
* You got 100 points!
* You got 200 points!
* You got 500 points!

All the way up to the achievement they should currently get. So in the case of a score of 22000, they should see it up to 20000. The achievements are at 1,2 and 5, them 10,20 and 50 and so on.

At the moment I use a for-loop, from 1 to their score and then if isAchievement($i) i print out "you got $i points!"

It is very slow after about 500...

Has anyone got any idea how to do this a bit faster?

Obviously I could 'hard-code' the function with the actual ok values,  and return true if $score = 1 or if score = 500 and so on but what if they have the next multiple of the 1,2,5 sequence say 500,000,000 when I've only coded up to 50,000,000? This seems a bad way to do it...

Offline capturts

  • Level 8
  • *
  • Posts: 43
  • Reputation: +1/-0
    • View Profile
    • Space Freight
Re: Help With Achievement
« Reply #1 on: October 24, 2010, 05:05:50 PM »
OK, I thought about it differently and came up with this:
Code: [Select]
function achievements($number){
$ok=array();

$multi=1;

do {
$ok[]=1*$multi;
$ok[]=2*$multi;
$ok[]=5*$multi;
$multi=$multi*10;
} while ($ok[count($ok)-1]<$number);

return ($ok);

}

Which (I hope) returns an array with all the achievements lower than or equal to the score.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Help With Achievement
« Reply #2 on: October 24, 2010, 05:07:45 PM »
You could try some coding things (like adding zeros) or some other technique.
Code: [Select]
$zero=0;

loop(...)
{
$z=''; for($n=0;$n<=$zero;$n++) $z.='0';
$v=1; if($score>=$v*($zero*10)) echo"You got $v{$z} points!";
$v=2; if($score>=$v*($zero*10)) echo"You got $v{$z} points!";
$v=5; if($score>=$v*($zero*10)) echo"You got $v{$z} points!";
$zero++;
}

But there are 2 concerns.

1) You still have to display it somewhere, and the page space will be a limiter, therefore speed of the code is not the most important/limiting thing. Hardcoding seems a better/sufficient solution to me.
2) Gameplay wise such achievements (plain "you reached X score") are boring. If you hardcode "You reached 100/1000/10000/100000/milion points" it would be enough. Or even better, scrap the score achievement and make these like in other games "rescued princess", "killed 100 red dragons", "found a holy grail", "restored fountain of youth", "found ancient treasure", "repaired sceptre of power", "entered underworld", "was eaten by a big ooze and survived", etc.

Offline capturts

  • Level 8
  • *
  • Posts: 43
  • Reputation: +1/-0
    • View Profile
    • Space Freight
Re: Help With Achievement
« Reply #3 on: October 25, 2010, 07:47:24 PM »
Thank-you for the input. I've gone with my second post code. It didn't quite work but wasn't hard to fix 'til it did.

I see what you mean about the list being somewhat dull, but I'm going with it anyway.

Thanks.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal