Jump to content
MakeWebGames

player_info hook and DisplayAvatar


Someone

Recommended Posts

I think it would be better to have the hook player_info adding data to an array or something, than echoing raw HTML. As it is now, your profile stat display is limited to be a 2 column table.

http://www.nw-engine.com/wiki/index.php/Player_info.php

This also reminds me of DisplayAvatar();

I changed it to take a second argument, so I can have the avatar HTML loaded before displaying.

function DisplayAvatar ($user, $return = false)
....
       if(!$return){
   		echo "<img src='index.php?p=avatar&display=$uid'>";
       } else {
       	return "<img src='index.php?p=avatar&display=$uid'>";;
       }
....
Edited by Someone
Link to comment
Share on other sites

I can appreciate the simplicity, but here is a suggestion for increased usability.

modules/view_player/content.php (around line 62)

    
global $profileStats;
   $profileStats = array();
   RunHook("player_info.php", 'profileStats');

   foreach($profileStats as  $k => $v){
   	echo '<tr><td><b>'.$profileStats[$k]['statName']."</b></td><td>".$profileStats[$k]['statValue'].'</td></tr>';   
   }
   echo "</table>";

 

modules/avatar/player_info.php

Its the only module using the player_info hook, so no worries about compatibility. This requires the change to DisplayAvatar mentioned above.

 

<?php
if (file_exists("$baseDir/modules/avatar/images/" . ($_GET["id"] + 0) . ".png"))
{
$profileStats[] = array(
		'statName' => str_replace(" ", " ", Translate("Avatar")), 
		'statValue' => DisplayAvatar($_GET["id"], true)
		);
}

 

The array $profileStats will look like:

Array
(
   [0] => Array
       (
           [statName] => Avatar
           [statValue] => <img src='index.php?p=avatar&display=2&token=194bc652595a0ffc3435092589eb5d65'>
       )

   [1] => Array
       (
           [statName] => Test Stat Name
           [statValue] => Test Stat Value
       )

)
Link to comment
Share on other sites

As it is now, if you are to use the hook, you are limited to having a table with 2 columns to display the data. This way you can choose how to present the data however you like. Be it a 4 column table or no table at all. Its up to each game owner how to present the data in their player profile page.

EDIT: My point is, I dont think anyone , ever, will use the hook player_stats as it is now in a game. ANd if it is to be changed, its best to have it done before modules rely on the format of the hook.

Does this look good?

[ATTACH=CONFIG]843[/ATTACH]

playerstat.thumb.png.baa2034e358525d6e34ed3a9623d374d.png

Edited by Someone
Link to comment
Share on other sites

Here I have to agree, if you're expecting other modules to use the hook, it should return just the data.

That allows other modules to use the data, without having to live with having some table tags, that may or may not even be within a table in a different module.

(Only applies to player_info: But on another note, if I didn't want the the table markup and just an populated img tag, I'd just use the displayAvatar function and bypass the 'hook'.)

Link to comment
Share on other sites

Djkanna explained it very well.

And yes, as for just displaying the avatar I would not use the hook. Its the only module that comes with NWE that uses the hook, so changed the sample file to make it faster for Alain to look into.

When speaking of avatars, perhaps there should be away to get the avatar url only, without the image HTML tag.

Added to modules/avatar/lib.php

function GetAvatarUrl ($user)
{
global $baseDir;

$uid = FindUser($user);
if ($uid == null)
	return;
if (file_exists("$baseDir/modules/avatar/images/$user.png")){
		return "index.php?p=avatar&display=$uid";
} else {
	return null;
}
}

 

Why? What if you want a class/id added to the avatar image

Or want width/height in the attributes.

I know that I am not dependent on having this in that file, I can add it myself through another module lib or a number of other ways. But I see a potential where several module makers have their own version of that function included for that purpose.

Edited by Someone
forgot to return null if no avatar found
Link to comment
Share on other sites

Djkanna explained it very well.

And yes, as for just displaying the avatar I would not use the hook. Its the only module that comes with NWE that uses the hook, so changed the sample file to make it faster for Alain to look into.

When speaking of avatars, perhaps there should be away to get the avatar url only, without the image HTML tag.

Added to modules/avatar/lib.php

function GetAvatarUrl ($user)
{
   global $baseDir;

   $uid = FindUser($user);
   if ($uid == null)
       return;
   if (file_exists("$baseDir/modules/avatar/images/$user.png")){
           return "index.php?p=avatar&display=$uid";
   } else {
       return null;
   }
}

Why? What if you want a class/id added to the avatar image

Or want width/height in the attributes.

I know that I am not dependent on having this in that file, I can add it myself through another module lib or a number of other ways. But I see a potential where several module makers have their own version of that function included for that purpose.

I also agree with this, this is what I would call more of a hook, as a module developer, getting markup and data back isn't always what you want, so as an engine, I think it should only provide the data, and let the module using the data itself determine the markup.

-2cents.

edit: Unless of course, the data required was expected to return markup too, for example DisplayAvatarImg() one would expect to get a populated img tag in return.

Could simplify it a little.

function GetAvatarUrl ( $user ) 
{
   global $baseDir;

   $uid = FindUser ( $user );
   if ( $uid != null && file_exists ( "$baseDir/modules/avatar/images/$user.png" ) )
       return "index.php?p=avatar&display=$uid";

   return;
}
Edited by Djkanna
Link to comment
Share on other sites

function GetAvatarUrl ( $user ) 
{
   global $baseDir;

   $uid = FindUser ( $user );
   if ( $uid != null && file_exists ( "$baseDir/modules/avatar/images/$user.png" ) )
       return "index.php?p=avatar&display=$uid";

   return;
}

Yup I am happy with the changes. Only missing the function I quoted above...

Link to comment
Share on other sites

Ooooops

 

Error: Invalid argument supplied for foreach()
Error in F:/xampp/htdocs/nwe/modules/view_player/content.php
Line 69
Error in F:\xampp\htdocs\nwe\libs\common.php
Line 554
Error in F:\xampp\htdocs\nwe\index.php
Line 321

 

I think it breaks if the player profile being viewed does not have an avatar. My admin profile works, btw its showing a nerd again since I updated the module :p (big deal, people should not update directly to live games anyways)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...