Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Coding a Forum Level Up System

    I'm working on modifying someone else's code for allowing forum posts to register towards a pool of experience towards leveling up. This is the original section which has been edited:

    Code:
    var plevel = Math.floor(EXP/xneeded+parseInt(1));
    var xpup = (plevel*xneeded)
    var showxp= (xpup-xneeded)
    if(sPost.innerHTML.match('<!-- placeholder -->')) {
    NewCode = sPost.innerHTML.replace("<!-- placeholder -->", ""+name + ": " + sign + EXP + "<br><!-- placeholder -->")
    }
    else {
    NewCode =  topR + midR + name + ": " + sign +" "+ Math.floor(EXP/xneeded+parseInt(1)) + "<BR> XP: "+ (EXP-showxp) +"<BR> Experience needed: " + (xpup-EXP)+ "<BR>Overall Experience:" + (xneeded * (EXP/xneeded)) +"<br><!-- placeholder --><br><br>"
    }
    
    sPost.innerHTML = NewCode
    }}
    </script>
    What I've done is edited plevel to be determined by an "if," "else if," "else" string and altered the equations for xpup and showxp to work with the new system. However, whenever I try to use these sections the entire code fails to function. This is the same section, edited:

    Code:
     if (EXP<600)
      {
      plevel = 1
      }
    else if (600<EXP<700)
      {
      plevel = 2
      }
    else if (650<EXP<750)
      {
      plevel = 3
      }
    else if (800<EXP<900)
      {
      plevel = 4
      }
    else if (900<EXP<1000)
      {
      plevel = 5
      }
    else if (1050<EXP<1200)
      {
      plevel = 6
      }
    else if (1200<EXP<1350)
      {
      plevel = 7
      }
    else if (1350<EXP<1550)
      {
      plevel = 8
      }
    else if (1550<EXP<1800)
      {
      plevel = 9
      }
    else if (1800<EXP<2050)
      {
      plevel = 10
      }
    else if (2050<EXP<2350)
      {
      plevel = 11
      }
    else if (2350<EXP<2700)
      {
      plevel = 12
      }
    else if (2700<EXP<3100)
      {
      plevel = 13
      }
    else if (3100<EXP<3550)
      {
      plevel = 14
      }
    else if (3550<EXP<4100)
      {
      plevel = 15
      }
    else if (4100<EXP<4700)
      {
      plevel = 16
      }
    else if (4700<EXP<5400)
      {
      plevel = 17
      }
    else if (5400<EXP<6200)
      {
      plevel = 18
      }
    else if (6200<EXP<7150)
      {
      plevel = 19
      }
    else if (7150<EXP<8200)
      {
      plevel = 20
      }
    else if (8200<EXP<9450)
      {
      plevel = 21
      }
    else if (9450<EXP<10850)
      {
      plevel = 22
      }
    else if (10850<EXP<12450)
      {
      plevel = 23
      }
    else if (12450<EXP<14350)
      {
      plevel = 24
      }
    else if (14350<EXP<16500)
      {
      plevel = 25
      }
    else if (16500<EXP<18950)
      {
      plevel = 26
      }
    else if (18950<EXP<21800)
      {
      plevel = 27
      }
    else if (21800<EXP<25050)
      {
      plevel = 28
      }
    else if (25050<EXP<28800)
      {
      plevel = 29
      }
    else
      {
      plevel = 30
      }
    var xpup = (xneeded * Math.ceiling(Math.pow(1.15, plevel)))
    var showxp= (xpup - (xneeded * Math.ceiling(Math.pow(1.15, plevel-1)))
    if(sPost.innerHTML.match('<!-- placeholder -->')) {
    NewCode = sPost.innerHTML.replace("<!-- placeholder -->", ""+name + ": " + sign + EXP + "<br><!-- placeholder -->")
    }
    else {
    NewCode =  topR + midR + name + ": " + sign +" "+ if (EXP<600)
      {
      plevel = 1
      }
    else if (600<EXP<700)
      {
      plevel = 2
      }
    else if (650<EXP<750)
      {
      plevel = 3
      }
    else if (800<EXP<900)
      {
      plevel = 4
      }
    else if (900<EXP<1000)
      {
      plevel = 5
      }
    else if (1050<EXP<1200)
      {
      plevel = 6
      }
    else if (1200<EXP<1350)
      {
      plevel = 7
      }
    else if (1350<EXP<1550)
      {
      plevel = 8
      }
    else if (1550<EXP<1800)
      {
      plevel = 9
      }
    else if (1800<EXP<2050)
      {
      plevel = 10
      }
    else if (2050<EXP<2350)
      {
      plevel = 11
      }
    else (2350<EXP<2700)
      {
      plevel = 12
      }
    else if (2700<EXP<3100)
      {
      plevel = 13
      }
    else if (3100<EXP<3550)
      {
      plevel = 14
      }
    else if (3550<EXP<4100)
      {
      plevel = 15
      }
    else if (4100<EXP<4700)
      {
      plevel = 16
      }
    else if (4700<EXP<5400)
      {
      plevel = 17
      }
    else if (5400<EXP<6200)
      {
      plevel = 18
      }
    else if (6200<EXP<7150)
      {
      plevel = 19
      }
    else if (7150<EXP<8200)
      {
      plevel = 20
      }
    else if (8200<EXP<9450)
      {
      plevel = 21
      }
    else if (9450<EXP<10850)
      {
      plevel = 22
      }
    else if (10850<EXP<12450)
      {
      plevel = 23
      }
    else if (12450<EXP<14350)
      {
      plevel = 24
      }
    else if (14350<EXP<16500)
      {
      plevel = 25
      }
    else if (16500<EXP<18950)
      {
      plevel = 26
      }
    else if (18950<EXP<21800)
      {
      plevel = 27
      }
    else if (21800<EXP<25050)
      {
      plevel = 28
      }
    else if (25050<EXP<28800)
      {
      plevel = 29
      }
    else
      {
      plevel = 30
      } + "<BR> XP: "+ (EXP-showxp) +"<BR> Experience needed: " + (xpup-EXP)+ "<BR>Overall Experience:" + (xneeded * EXP / xneeded) +"<br><!-- placeholder --><br><br>"
    }
    
    sPost.innerHTML = NewCode
    }}
    </script>
    I am at a loss as to what I screwed up. I've tried to resolve it myself, but I haven't been able to identify exactly what the problem is other than narrowing it down to this section. Can anyone shed some light on that?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,190
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Totally bogus:
    Code:
    else if (1200<EXP<1350)
    correct but overkill:
    Code:
    else if ( 1200 < EXP && EXP < 1350 )
    On top of that, the entire code makes no sense:
    Code:
    else if (600 < EXP && EXP < 700 ) // was: (600<EXP<700)
      {
      plevel = 2
      }
    else if (650 < EXP && EXP < 750 ) // was: (650<EXP<750)
      {
      plevel = 3
      }
    else if ( 800 < EXP && EXP < 900 ) // was: (800<EXP<900)
      {
      plevel = 4
      }
    Ummm...and what if the value of EXP is, say 657?
    The 650 in the second IF will be ignored.

    OR what if the EXP is 787?
    It doesn't match *ANY* of the IF's so plevel will be just blank.

    Try it all over again:
    Code:
     if (EXP<600)  {  plevel = 1; }
    else if (EXP<700) { plevel = 2; }
    else if (EXP<750) { plevel = 3; }
    else if (EXP<900) { plevel = 4; }
    else if (EXP<1000){plevel = 5; }
    ... etc. ...
    Think about it for a while if you don't see why that works.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeaaaah those nonsense ones I totally missed. I'm not sure I understand why your version works, but I'm pretty new to Javascript.

    At any rate, I rewrote it based on your comments but something is still breaking it.

    EDIT: Okay, it's definitely these two equations:

    Code:
    var xpup = (xneeded * Math.ceiling(Math.pow(1.15, plevel)))
    var showxp= (xpup - (xneeded * Math.ceiling(Math.pow(1.15, plevel-1)))
    Last edited by FrostWyrmWraith; 01-23-2013 at 02:51 AM. Reason: Avoiding a double post

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    just run that code in any browser and have a look at the error console. Or open it in a decent code editor which show you the matching brackets - you'll see that this line:
    Code:
    var showxp= (xpup - (xneeded * Math.ceiling(Math.pow(1.15, plevel-1)))
    is missing a closing bracket

    and the error console will tell you that there is no such thing as Math.ceiling - you're probably looking for Math.ceil
    Last edited by xelawho; 01-23-2013 at 03:08 AM.

  • #5
    New Coder
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know how to run the code through a browser :/

    At any rate, it works now! Thanks for the help!

  • #6
    New Coder
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I was wrong, something is still messed up. It's accepting the codes now, but the mathematical output is coming like I didn't change the code at all. The result for:

    Code:
    (xneeded * Math.ceil(Math.pow(1.15, plevel))-EXP)
    Is coming out as the result would be for this:

    Code:
    ((plevel*xneeded)-EXP)
    I just...I don't even. It's doing the same for the other equation with this:

    Code:
    (EXP-(xpup - (xneeded * Math.ceil(Math.pow(1.15, plevel-1)))))
    Outputting like it's this:

    Code:
    (EXP-(xpup-xneeded))
    ...how does that even happen?
    Last edited by FrostWyrmWraith; 01-23-2013 at 07:48 PM.

  • #7
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    How about you give us some actual input/output values to make sure your math is right?

    Also, parseInt(1) made me laugh. Programming paranoia ... is that a thing?

  • #8
    New Coder
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...well at least the parseint thing wasn't me!

    Alright, so, Let's say the person has 50 posts. That makes EXP 50 * 50 = 2500. The xneeded value is a fixed 500.

    That makes the level 12. The first equation should be outputting 200 according to that. The second should be giving out 2150. Instead what I'm getting is 500 and 2000. It's basically ignoring everything I've changed, including the plevel values, and doing what would have made sense in the original code.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,190
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    I think it is way way past time that you show us your complete code *AS IT IS NOW*.

    And I don't understand this part, at all:
    I don't know how to run the code through a browser
    If you aren't running this code in a browser, how *ARE* you running it???

    Oh...and finally...

    This code:
    Code:
    if      (EXP<600)  {  plevel = 1; }
    else if (EXP<700) { plevel = 2; }
    else if (EXP<750) { plevel = 3; }
    else if (EXP<900) { plevel = 4; }
    else if (EXP<1000){plevel = 5; }
    ...
    happens to be written in JavaScript, but the *logic* of it is universal. It would work the same in any computer language. Or in any human who applied the logic of it without a computer involved. So don't look at it as JavaScript code. Look at it from a logical viewpoint. Pick some EXP value (just for example, say 832, or choose one of your own) and then apply the logic/formulae there to see how it works.

    You have to be able to express a problem in logical terms, in English or whatever your native language is, *FIRST*. Only then can you code it in the computer language of your choice.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New Coder
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm putting it into the forum. It's not giving me any error codes or anything like that.

    As I understood this forum's rules, I was only supposed to post the part in question. The full code:

    Code:
    <script type="text/javascript">
    //Based on the famous money code by gornakle
    //Transformed in a somewhat more automatic xp code
    //by Duveaux of http://phantom-designs.net
    //Do not remove credit note
    
    //If you are using a different var for a level, you may add a sign here
    var sign = "";
    
    //You may change the name displayed here
    var name = "Level";
    
    //How much experience does a user needs to get per post?
    var PerPost = 50;
    
    //How much experience will a member start with?
    var default_amount = 0;
    
    //how much experience is needed to level up?
    var xneeded= 500
    
    //The variable UXP[uname] allows you to alter the experience for a player
    //Make sure you use capitals and spell the name correct
    var UXP=[];
    UXP["Duveaux"]="100"
    UXP["OpleX"]='0'
    
    // No need to edit below this line!
    
    td=document.getElementsByTagName("TD")
    for(i=0;i<td.length;i++) {
    if((td[i].className=="post1"||td[i].className=="post2")&&td[i].innerHTML.match("Posts: ")&&td[i].width!="100%") {
    sPost=td[i].getElementsByTagName("SPAN")[0]
    if(sPost.innerHTML.replace(",","").replace('.','').replace(' ','').match(/Posts: (\d+)/)) Posts = parseInt(RegExp.$1)
    uname = td[i-2].innerHTML.replace(/<[^>]+>/g,"").replace(/&nbsp;/g,"")
     if(UXP[uname]){ EXP = (parseInt(UXP[uname]) + (Posts * PerPost))+parseInt(default_amount) }
    else{ EXP = (Posts * PerPost)+parseInt(default_amount) }
    var midR="Group: " + sPost.innerHTML.split('Group: ')[1]
    var topR=sPost.innerHTML.split('Group: ')[0]
     if (EXP<600) { plevel = 1; }
    else if (EXP<700) { plevel = 2; }
    else if (EXP<800) { plevel = 3; }
    else if (EXP<900) { plevel = 4; }
    else if (EXP<1050) { plevel = 5; }
    else if (EXP<1200) { plevel = 6; }
    else if (EXP<1350) { plevel = 7; }
    else if (EXP<1550) { plevel = 8; }
    else if (EXP<1800) { plevel = 9; }
    else if (EXP<2050) { plevel = 10; }
    else if (EXP<2350) { plevel = 11; }
    else if (EXP<2700) { plevel = 12; }
    else if (EXP<3100) { plevel = 13; }
    else if (EXP<3550) { plevel = 14; }
    else if (EXP<4100) { plevel = 15; }
    else if (EXP<4700) { plevel = 16; }
    else if (EXP<5400) { plevel = 17; }
    else if (EXP<6200) { plevel = 18; }
    else if (EXP<7150) { plevel = 19; }
    else if (EXP<8200) { plevel = 20; }
    else if (EXP<9450) { plevel = 21; }
    else if (EXP<10850) { plevel = 22; }
    else if (EXP<12450) { plevel = 23; }
    else if (EXP<14350) { plevel = 24; }
    else if (EXP<16500) { plevel = 25; }
    else if (EXP<18950) { plevel = 26; }
    else if (EXP<21800) { plevel = 27; }
    else if (EXP<25050) { plevel = 28; }
    else if (EXP<28800) { plevel = 29; }
    else { plevel = 30 }
    var xpup = (xneeded * Math.ceil(Math.pow(1.15, plevel)))
    var showxp= (xpup - (xneeded * Math.ceil(Math.pow(1.15, plevel-1))))
    NewCode =  topR + midR + name + ": " + sign +" "+ plevel + "<BR> XP: "+ (EXP-showxp) +"<BR> Experience needed: " +  (xpup-EXP)+ "<BR>Overall Experience:" + (xneeded * EXP / xneeded) +"<br><!-- placeholder --><br><br>"
    
    
    sPost.innerHTML = NewCode
    }}
    </script>

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,190
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    But let me make a stab at this, anyway. I'll probably mess up, but w.t.h.

    Okay, so you say that:
    50 posts means that
    EXP = 50 * 50
    EXP = 2500
    and, yes, I see from your (mildly messed up) table that
    if ( EXP == 2500 ) { plevel = 12; }

    So then let's look at your first post:
    Code:
    var xpup = (plevel*xneeded)
    var showxp= (xpup-xneeded)
    You have asserted that xneeded == 500

    SO that means:
    Code:
    var xpup = 12 * 500 ==>> xpup should be 6000.
    var showxp = 6000 - 500 ==>> showxp should be 5500.
    I *really* don't see why you are then saying
    That makes the level 12. [agreed]
    The first equation should be outputting 200 according to that. [I get 6000.]
    The second should be giving out 2150. [I get 5500]
    Care to try to explain?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    New Coder
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That probably has something to do with the fact that you plugged it into the original equation, not the new one. Which does result in 200 and 2150. I made a post following your previous one but it apparently needs to wait for mod approval? Anyway I posted the code and explained that it was acting as if it was reading off the original plevel formula as well, so the plevel input on those original equations isn't twelve but six, which gives you 500 and 2000. You've also missed the last half of the equation and only obtained the xpup and showxp values instead of plugging them then into the equations in I already provided, which give you what appears on the player's profile. Those subtract total EXP from xpup and showxp from total EXP.

  • #13
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    No, he just didn't see the post, I guess. But your math is still wrong.



    which is neither of the values you said.

  • #14
    New Coder
    Join Date
    Jan 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Because you also did it wrong.

    500*1.15^12=2700 when rounded up.

    Never did I say anything about a 1.12. And where did you even get -50?

    EDIT: For that matter, both values I gave were from the full equation and you just did the xpup equation minus a random 50.
    Last edited by FrostWyrmWraith; 01-23-2013 at 09:11 PM.

  • #15
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by FrostWyrmWraith View Post
    Code:
    (xneeded * Math.ceil(Math.pow(1.15, plevel))-EXP)
    This is the term* I am talking about. And the 1.12 is just a typo, I used 1.15 (meaning that it is in fact 2950). No random anything because your term* clearly says "-EXP".


    *) As a mathematician I have a problem with calling this an equation. It simply isn't one.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •