Spectre Posted November 10, 2008 Share Posted November 10, 2008 Ok so I work on an online game as a coder, but for some reason my mind is seriously not being good to me today. Here's the problem. In this game we have clans, and we ahve a merger script for them, which adds together a few things, one thing I can't work out to put in there is how to merge the clan level and experience need recalced after that. So here we are, now the exptolvl field = clanlevel*clanlevel*1000 And basically what I need to do is, find how much experience in total it took from level 1 to Clan A's current level, do the same for clan B, then combine the totals and find the nearest whole level they make and the corresponding exptolvl and update Clan A's level to refelect that Can someone help, my math head is so not on today and i just keep staring at the page blankly with no lights switching on upstairs lol. Quote Link to comment https://forums.phpfreaks.com/topic/132176-solved-need-a-hand/ Share on other sites More sharing options...
.josh Posted November 10, 2008 Share Posted November 10, 2008 Need more details about your xp structure. For instance, do you have some sort of table of xp per level somewhere? If so, select sum(xp) from xpcharttable where level <= clanAcurrentlvl, do the same for clanB, then $total = $resultA + $resultb; then select level from xpcharttable where xp < $total desc limit 1 (if you want the nearest floored value, swap out < for > for nearest ceil value). Quote Link to comment https://forums.phpfreaks.com/topic/132176-solved-need-a-hand/#findComment-686960 Share on other sites More sharing options...
Spectre Posted November 10, 2008 Author Share Posted November 10, 2008 Oh right (sorry as i said brain is SO not playing ball today) There is no chart table anywhere, when battling your experience is added to your clans totals, he's the clan level excerpt from battling Ok so first we have a call to see if a person is in a clan, and updated the clan exp by the amount you got in that battle/set of battles then... oh and $totals['clevel'] = 0; to start if ($clanss) { while($clanss[exp] >= $clanss[exp_lvl]) { $totals['clevel']++; $newlvl2 = $clanss[level] + 1; $newexp2 = $newlvl2 * $newlvl2 * 1000; $updateclannews = mysql_query($db,"insert into usernews (id,user,text,time) values ('','C$playerinfo[clan]','<b>$time</b> Clan leveled up! Now level $newlvl2. (<a href=view.php?id=$playerinfo[id]>$playerinfo[username]</a>)','$realtime')"); $cexpleft = $clanss[exp] - $clanss[exp_lvl]; if($clanss[levelreward] <= $clanss[gold]) { $updateclan55 = mysql_query($db,"update clans set level='$newlvl2', exp='$cexpleft', exp_lvl='$newexp2', gold=gold-$clanss[levelreward] where id=$playerinfo[clan]"); $clanchanger1 = mysql_query($db,"update clans set power=power+1000 where id='$playerinfo[clan]'"); $guychanger1 = mysql_query($db,"update userdb set clan_infl=clan_infl+1000 where id='$playerinfo[id]'"); $goldadder7 = mysql_query($db,"update userdb set gold=gold+$clanss[levelreward] where id='$playerinfo[id]'"); $totals['gold'] += $clanss[levelreward]; $special .= "<br><font color=orange>You gained your clan a level! (+1000 influence) You have been rewarded $clanss[levelreward] gold!</font>"; }else{ $clanchanger1 = mysql_query($db,"update clans set power=power+1000 where id='$playerinfo[clan]'"); $guychanger1 = mysql_query($db,"update userdb set clan_infl=clan_infl+1000 where id='$playerinfo[id]'"); $updateclan55 = mysql_query($db,"update clans set level='$newlvl2', exp='$cexpleft', exp_lvl='$newexp2' where id=$playerinfo[clan]"); $special .= "<br><font color=orange>You gained your clan a level! (+1000 influence) You should have been rewarded $clanss[levelreward] gold but they dont have enough clan funds!</font>"; } $clanres3 = mysql_query($db,"select * FROM clans WHERE id='$playerinfo[clan]'"); $clanss = mysql_fetch_assoc($clanres3); } } Quote Link to comment https://forums.phpfreaks.com/topic/132176-solved-need-a-hand/#findComment-686972 Share on other sites More sharing options...
monkeytooth Posted November 10, 2008 Share Posted November 10, 2008 You want to total all clan A and then total all clan B? Then you want to find out some difference between the 2? is that what I'm gathering correctly from this post thus far? If so my assumption would be in a crude sense as I'm not working off of much other then your description.. Is define 2 variables, 1 being the collective total of clan A, 2 being the collective total of clan B.. then make a 3rd that's a subtraction from a and b to get the difference from a to b.. but again this might not be the concept your looking for as i don't know exactly what it is your looking for Quote Link to comment https://forums.phpfreaks.com/topic/132176-solved-need-a-hand/#findComment-686979 Share on other sites More sharing options...
Spectre Posted November 10, 2008 Author Share Posted November 10, 2008 I want to find how much experience it's taken Clan A to get to the level it is by calling $clan['level'] and finding out how much it takes from level 1 to that level, then do the same for clan B. Then add the two totals of experience together, and using the previous method to find out the experience they've used to get tot hat level, find out what level the combined experience values can make. Like say Clan A is lvl 200, and clan B 100, I want to find how much experience it's taken to get to those levels, add the two experience values. And find out what level that gets to fully. Quote Link to comment https://forums.phpfreaks.com/topic/132176-solved-need-a-hand/#findComment-686986 Share on other sites More sharing options...
.josh Posted November 10, 2008 Share Posted November 10, 2008 Okay so it looks like from your code that this is your formula per level: $newexp2 = $newlvl2 * $newlvl2 * 1000; So if I were to manually make a chart, xp per level would look like this: level : xp : running total 1 : 1000 : 1000 2 : 4000 : 5000 3 : 9000 : 14000 4 : 16000 : 30000 5 : 25000 : 55000 6 : 36000 : 91000 7 : 49000 : 140000 etc... If that is correct, then as far as merging clans: You say you want to determine the new level by adding up accumulated xp and figuring out what level they should be, based on that, right? Probably a better way but, here's my take: <?php // example clan levels $clanAlevel = 15; $clanBlevel = 10; // get first clan's total xp for ($x = 1; $x <= $clanAlevel; $x++) { $totalA += $x * $x * 1000; } // get second clan's total xp for ($x = 1; $x <= $clanBlevel; $x++) { $totalB += $x * $x * 1000; } // overall total $totalxp = $totalA + $totalB; // init some vars, use temp total var to preserve totalxp $level = 0; $xpleft = $totalxp; // loop until you have no more xp to burn while(true) { // update vars only if there's enough xp to burn if (($xpleft - $xp) > 0) { $level++; // inc to next level $xp = $level * $level * 1000; // find out how much xp it takes to get to next level $xpleft -= $xp; // subtract needed xp from total xp } else { // break out of loop if we run out of xp to burn break; } // end if xp to burn } // end loop echo "new level: $level <br/>"; echo "total xp: $totalxp<br/>"; echo "amount till next level: $xpleft / $xp<br/>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/132176-solved-need-a-hand/#findComment-687083 Share on other sites More sharing options...
Spectre Posted November 10, 2008 Author Share Posted November 10, 2008 Thank you!!! That is exactly what I needed, works a charm Thanks for that, my head just couldn't get that sorted after a few attempts and I was close to pulling my hair out xD You're a life saver, thanks mate Quote Link to comment https://forums.phpfreaks.com/topic/132176-solved-need-a-hand/#findComment-687155 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.