Simple Karma Challenge

phpBB3 and MOD challenges setup by the staff to test and challenge your phpBB3 coding skills.

Re: Simple Karma Challenge

Postby Highway of Life » 15 Feb 2007, 16:42

AH! okay... that explains it. Smile
I was wondering what was going on...
Again, porting 2 Mods to 3 won't work (for obvious reasons demonstrated so nicely above. Grin )
I know you can write up the code from scratch, just use theirs as a template of ideas. -- That's basically what I did with yours to achieve what I posted above in the example. -- but don't use their coding techniques... you can bet that 90% of it is going to be using incorrect methods.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10


Re: Simple Karma Challenge

Postby Pace » 17 Feb 2007, 10:21

Okay, I've finally got it working perfectly in "viewprofile". But I'm having trouble with "viewtopic".
See, I'm supposed to call this "functions_karma.php" file:
Spoiler:
Code: Select all
<?php
function grab_karma_rank($karma$user_level)
{
    global 
$user$template;
    
// when we expand this mod for ACP and database control,
    // we would probably use regular expressions to determine the values,
    // but for now, we can be somewhat static.
    
$applaud_img $applaud_alt $applaud_url $smite_img $smite_alt $smite_url '';
    
$i $karma;
    switch (
$i)
    {
        case (
$i == || $i == -0):
            
$user_karma $user->lang['karma_negative'][0];
        break;
        case (
$i == -1):
            
$user_karma $user->lang['karma_negative'][1];
        break;
        case (
$i == -2):
            
$user_karma $user->lang['karma_negative'][2];
        break;
        case (
$i == -3):
            
$user_karma $user->lang['karma_negative'][3];
        break;
        case (
$i == -4):
            
$user_karma $user->lang['karma_negative'][4];
        break;
        case (
$i == -5):
            
$user_karma $user->lang['karma_negative'][5];
        break;
        case (
$i <= -&& $i >= -7):
            
$user_karma $user->lang['karma_negative'][6];
        break;
        case (
$i <= -&& $i >= -9):
            
$user_karma $user->lang['karma_negative'][7];
        break;
        case (
$i <= -10):
            
$user_karma $user->lang['karma_negative'][8];
        break;
        
// this last one would be "banned"
        // then start on the positive case...
        
case ($i >= && $i <= 5):
            
$user_karma $user->lang['karma_positive'][0];
        break;
        case (
$i >= && $i <= 12):
            
$user_karma $user->lang['karma_positive'][1];
        break;
        case (
$i >= 13 && $i <= 21):
            
$user_karma $user->lang['karma_positive'][2];
        break;
        case (
$i >= 22 && $i <= 32):
            
$user_karma $user->lang['karma_positive'][3];
        break;
        case (
$i >= 33 && $i <= 45):
            
$user_karma $user->lang['karma_positive'][4];
        break;
        case (
$i >= 46 && $i <= 50):
            
$user_karma $user->lang['karma_positive'][5];
        break;
        case (
$i >= 51 && $i <= 67):
            
$user_karma $user->lang['karma_positive'][6];
        break;
        case (
$i >= 68 && $i <= 86):
            
$user_karma $user->lang['karma_positive'][7];
        break;
        case (
$i >= 87 && $i <= 99):
            
$user_karma $user->lang['karma_positive'][8];
        break;
        case (
$i >= 100):
            
$user_karma $user->lang['karma_positive'][9];
        break;
        
// etc... and repeat until you reach something like oh... 100 or so
        // which would be Supreme Ruler, or some such thing
    
}
    
    if (
$user_level == USER_FOUNDER)
    {
        
$user_karma $user->lang['karma_positive'][9]; // assuming 9 is the highest level
        
$karma 'n<sup>n</sup>';
    }

    
$template->assign_vars(array(
        
'KARMA_LEVEL'    => $user_karma " ($karma)",
        
'KARMA' => $user->lang['KARMA'],
    ));
    
    return;
}

?>


using
Code: Select all
        // Advanced KARMA Mod
        
include_once($phpbb_root_path 'includes/functions_karma.' $phpEx);
        
grab_karma_rank($row['karma'], $row['user_type']);

where "$row" is to be replaced by whatever I can use to make this work. Unfortunately, looking at viewtopic.php, the $row seems to be the best thing:

Spoiler:
Code: Select all
$sql $db->sql_build_query('SELECT', array(
    
'SELECT'    => 'u.*, z.friend, z.foe, p.*',

    
'FROM'        => array(
        
USERS_TABLE        => 'u',
        
POSTS_TABLE        => 'p',
    ),

    
'LEFT_JOIN'    => array(
        array(
            
'FROM'    => array(ZEBRA_TABLE => 'z'),
            
'ON'    => 'z.user_id = ' $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
        
)
    ),

    
'WHERE'        => $db->sql_in_set('p.post_id'$post_list) . '
        AND u.user_id = p.poster_id'
));

$result $db->sql_query($sql);

$now getdate(time() + $user->timezone $user->dst date('Z'));

// Posts are stored in the $rowset array while $attach_list, $user_cache
// and the global bbcode_bitfield are built
while ($row $db->sql_fetchrow($result))
{
(taken straight from viewtopic.php)

Now, unfortunately, calling the "grab_karma_rank" function anywhere after the "{" makes the karma be the karma of one of the posters (don't know which one), but it repeats that karma on the whole topic. See for yourselves.

Now, how do I fix that? What variable should I use?
Also, I've tried using the following, but it doesn't work either:
Spoiler:
Code: Select all
    $sql 'SELECT *
        FROM ' 
POSTS_TABLE '
        WHERE topic_id = ' 
$topic_data['topic_id'];
    
$result $db->sql_query($sql);
    
$poster $db->sql_fetchrow($result);
    
$db->sql_freeresult($result);
    
$sql 'SELECT *
        FROM ' 
USERS_TABLE '
        WHERE user_id = ' 
$poster['poster_id'];
    
$result $db->sql_query($sql);
    
$target $db->sql_fetchrow($result);
    
$db->sql_freeresult($result);
    include_once(
$phpbb_root_path 'includes/functions_karma.' $phpEx);
    
grab_karma_rank($target['karma'], $target['user_type']);
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Simple Karma Challenge

Postby Handyman » 17 Feb 2007, 11:24

The last code you posted is calling an array of posts, not just a single post? so you would have to do this
Code: Select all
while ($target $db->sql_fetchrow($result))
{
 include_once(
$phpbb_root_path 'includes/functions_karma.' $phpEx);
    
grab_karma_rank($target['karma'], $target['user_type']);
}
$db->sql_freeresult($result);

in place of $target = $db->sql_fetchrow($result);
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7459
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Simple Karma Challenge

Postby Pace » 17 Feb 2007, 11:55

Doesn't solve it, at least, not where I put it, but HoL has given me another suggestion:
basically, look in viewtopic at the way it's pulling and displaying the custom profile fields, you'll want to call it about the same way, it's using classes, so you would need something like:
include_once($phpbb_root_path . 'includes/functions_karma.' . $phpEx);
$karma = new karma_rank();
$karma_level_cache = $karma->grab_karma_rank($target['karma'], $target['user_type']);
Though that is a very rudimentary example, I'm hoping it will steer you in the right direction.

Remember, it's using cache for the viewtopic, so see how you can utilize that, and include a functions file that would do that it instead of modifying any of the core code.


I'll let you guys know if that works.
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Simple Karma Challenge

Postby Incognito » 22 Apr 2007, 12:30

Sorry that I asks such a stupid question, but where I should install the MOD?
Incognito
Crewman
Crewman
 
Posts: 8
Joined: 26 Mar 2007, 10:16
Gender: Male

Re: Simple Karma Challenge

Postby Handyman » 22 Apr 2007, 12:34

actually, it's not a stupid question? it's in the wrong topic.
The StarFleetAcademy is for learning? not installing mods.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7459
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Simple Karma Challenge

Postby Pace » 22 Apr 2007, 12:52

At some point, when I feel like it, I'll get back to working on this.

And once it works fully, I'll release it as a MOD.

Until then?
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Not so simple?

Postby Pace » 26 Apr 2007, 06:43

Hi guys, I've been trying to get this to work for viewtopic, but I just can't figure out how to make it work like (custom) profile fields.
Once again, the best I can get is either the last person who posted, or the topic starter.

Edit: I'm in despair:
Parse error: syntax error, unexpected '{' in /home.8/a/r/p/arpia/www/phpBB3/includes/functions_karma_p.php on line 3

And what is that file?
Spoiler:
Code: Select all
<?php
function grab_karma($target_karma$user_level)
{
    global 
$user$template;
    
// when we expand this mod for ACP and database control,
    // we would probably use regular expressions to determine the values,
    // but for now, we can be somewhat static.
    
$i $target_karma;
    switch (
$i)
    {
        case (
$i == || $i == -0):
            
$user_karma $user->lang['karma_negative'][0];
        break;
        case (
$i == -1):
            
$user_karma $user->lang['karma_negative'][1];
        break;
        case (
$i == -2):
            
$user_karma $user->lang['karma_negative'][2];
        break;
        case (
$i == -3):
            
$user_karma $user->lang['karma_negative'][3];
        break;
        case (
$i == -4):
            
$user_karma $user->lang['karma_negative'][4];
        break;
        case (
$i == -5):
            
$user_karma $user->lang['karma_negative'][5];
        break;
        case (
$i <= -&& $i >= -7):
            
$user_karma $user->lang['karma_negative'][6];
        break;
        case (
$i <= -&& $i >= -9):
            
$user_karma $user->lang['karma_negative'][7];
        break;
        case (
$i <= -10):
            
$user_karma $user->lang['karma_negative'][8];
        break;
        
// this last one would be "banned"
        // then start on the positive case...
        
case ($i >= && $i <= 5):
            
$user_karma $user->lang['karma_positive'][0];
        break;
        case (
$i >= && $i <= 12):
            
$user_karma $user->lang['karma_positive'][1];
        break;
        case (
$i >= 13 && $i <= 21):
            
$user_karma $user->lang['karma_positive'][2];
        break;
        case (
$i >= 22 && $i <= 32):
            
$user_karma $user->lang['karma_positive'][3];
        break;
        case (
$i >= 33 && $i <= 45):
            
$user_karma $user->lang['karma_positive'][4];
        break;
        case (
$i >= 46 && $i <= 50):
            
$user_karma $user->lang['karma_positive'][5];
        break;
        case (
$i >= 51 && $i <= 67):
            
$user_karma $user->lang['karma_positive'][6];
        break;
        case (
$i >= 68 && $i <= 86):
            
$user_karma $user->lang['karma_positive'][7];
        break;
        case (
$i >= 87 && $i <= 99):
            
$user_karma $user->lang['karma_positive'][8];
        break;
        case (
$i >= 100):
            
$user_karma $user->lang['karma_positive'][9];
        break;
    }
    
    
$template->assign_vars(array(
        
'KARMA_LEVEL'    => $user_karma " ($target_karma)",
        
'KARMA' => $user->lang['KARMA'],
    ));
    
    return;
}

?>


:beg: :cry:
Last edited by Highway of Life on 26 Apr 2007, 10:39, edited 1 time in total.
Reason: cleaned up
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Simple Karma Challenge

Postby Handyman » 26 Apr 2007, 10:45

that's really puzzling me because that code looks fine.

Couple things you can try, make sure there is no blank space after the ?>
make sure there is no blank space before the <?php

put a new line between the <?php the the function and see if it moves to line 4
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7459
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Simple Karma Challenge

Postby Highway of Life » 26 Apr 2007, 10:48

Hmm... I don?t see how { is a problem on line 3.
Your function looks like it should work.

I would look at the get rank function to see how that one works... that?s what you want to emulate, I believe in this situation. :scratch:
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

PreviousNext

Return to phpBB3 Challenges at phpBB Academy

Who is online

Users browsing this forum: No registered users and 2 guests

cron