HTML Logo by World Wide Web Consortium (www.w3.org). Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS, which is now in beta. ocPortal 9 will be superseded by Composr 10.

Head over to compo.sr for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.


Understanding do_lang_tempcode

Login / Search

 [ Join | More ]
 Add topic 
Posted
Rating:
#83781 (In Topic #17378)
Avatar

Community saint

Can someone please explain what I am doing wrong here and what is the correct way to do this:

*** EDIT Stuff removed to make thread smaller.


Last edit: by Duck
Back to the top
 
Posted
Item has a rating of 5 (Liked by Chris Graham)  
Rating:
#83784
Avatar

Community saint

That's because do_lang_tempcode returns and object not a string. I think you need to use make_string_tempcode to extract it. ie:
$message .= make_string_tempcode(do_lang_tempcode('W_BOTH_LOST_ALL_ITEMS
'));

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#83786
Avatar

Community saint

that didn't work but thanks for trying.
Back to the top
 
Posted
Rating:
#83787
Avatar

Community saint

do_lang() is what I needed then I used make_string_tempcode before output

Thanks though you pointed me in the right direction
Back to the top
 
Posted
Rating:
#83788
Avatar

Community saint

hmm almost there but my html in the lang is not rendered but instead output.
Back to the top
 
Posted
Item has a rating of 5 (Liked by Chris Graham)  
Rating:
#83789
Avatar

Community saint

Whoops I got it back-to-front, that creates a tempcode from text. This should do it:
$message .= do_lang_tempcode('W_BOTH_LOST_ALL_ITEMS
')->evaluate();

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#83790
Avatar

Community saint

That seemed to work as well as my do_lang option but my html is still not being render it is outputting the sentence showing the html tags?
Back to the top
 
Posted
Item has a rating of 5 (Liked by Chris Graham)  
Rating:
#83791
Avatar

Community saint

It could be an escaping issues. ocPortal Developer's Guide: Language: code and content - ocPortal.com states:
Language strings must not include html symbols like ' " < > and &. These should be turned into HTML entities, such as & or <

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#83792
Avatar

Community saint

I finally got it!

The final code that seems to work. Although I might make some adjustments on the damage values cause as is I get random numbers from each persons max health when deciding damage  but it hardly seems fair that a person with 10 health attacking a person with 100 health can knock 60 (or more) health off them while only losing a max of 10 themselves. Not sure this makes sense. so what I may do is calculate the difference in health and the larger it is the larger the advantage of not getting hurt. Right now i just pick who is larger and give them a a 25% chance of not getting hurt and the smaller has only a 15% chance. I shall think on this. Here is final function fixed for Chris to quickly scan for the wrong methods if he likes.

Code

function attack($member_id, $target) {
    // Make sure they are not attacking themselves
    if ($member_id==$target) ocw_refresh_with_message(do_lang_tempcode('W_SELF_MUTILATION'),'warn');
    // Make sure they are not attacking the troll if so Troll wins!
    if ($target == -1) {
        take_items($member_id);
        kill_member($member_id);
        ocw_refresh_with_message(do_lang_tempcode('W_KILLED_BY_TROLL'),'warn');
    }
    // Check members are in same room/realm
    if (!check_coexist($member_id, $target)) ocw_refresh_with_message(do_lang_tempcode('W_NOT_SAME_REALM'),'warn');
    // Check members are Not in Jail or Lobby
    list($realm,$x,$y)=get_loc_details($member_id);
    if (($x==0) && (($y==1) || ($y==2) || ($y==0))) ocw_refresh_with_message(do_lang_tempcode('W_YOU_CANT_ATTACK_HERE'),'warn');    
    $minHurt = 1;
    $attacker_safe = false;
    $defender_safe = false;
    $defender_died = false;
    $attacker_died = false;
    $method =  mt_rand(1,8);
    // get the targets name
    $target_member_name=$GLOBALS['FORUM_DRIVER']->get_username($target);
    $message = '';
    // Get each current health
    $maxTargetHurt = intval($GLOBALS['SITE_DB']->query_value('w_members','health',array('id'=>$target)));
    $maxMemberHurt = intval($GLOBALS['SITE_DB']->query_value('w_members','health',array('id'=>$member_id)));
    // Check for Deaths
    if ($maxTargetHurt < 1) ocw_refresh_with_message(do_lang_tempcode('W_MEMBER_ALREADY_DEAD'),'warn');    
    if ($maxMemberHurt < 1) ocw_refresh_with_message(do_lang_tempcode('W_YOU_ALREADY_DEAD'),'warn');
    //Decide who gets hurt
    $attacker_base_number = 4;
    $target_base_number = 17;
    // Give advantage healthier member
    if($maxTargetHurt > $maxMemberHurt){
        $target_base_number = $target_base_number -1;
        $attacker_base_number = $attacker_base_number -1;
    } elseif ($maxTargetHurt < $maxMemberHurt){
        $target_base_number = $target_base_number +1;
        $attacker_base_number = $attacker_base_number +1;        
    }
    $advantage = mt_rand(1,20);
    if ($advantage <= $attacker_base_number ){
        $attacker_safe = true;
    }
    if ($advantage >= $target_base_number ){
        $defender_safe = true;
    }
    if (!$defender_safe){
        $targethurt = mt_rand($minHurt,$maxTargetHurt);
        $defender_health = hurt($target,$targethurt);
        if ($defender_health[0] < 1){
            $defender_died = true;
            $message .= '<br />'.do_lang_tempcode('W_YOU_KILLED_DEFENDER_'.strval($method),strval($target_member_name),strval($targethurt))->evaluate();            
        } else {
            $message .= '<br />'.do_lang_tempcode('W_YOU_HURT_DEFENDER_'.strval($method),strval($target_member_name),strval($targethurt))->evaluate();
        }
    }
    if (!$attacker_safe){
        $memberhurt = mt_rand($minHurt,$maxMemberHurt);
        $attacker_health = hurt($member_id,$memberhurt);
        if ($attacker_health[0] < 1){
            $attacker_died = true;
            $message .= '<br />'.do_lang_tempcode('W_YOU_DIED_ATTACKER_'.strval($method),strval($memberhurt))->evaluate();            
        } else {
            $message .= '<br />'.do_lang_tempcode('W_YOU_HURT_ATTACKER_'.strval($method),strval($memberhurt))->evaluate();    
        }
    }
    // process the results
    if($defender_died && !$attacker_died){
        take_items($target, $member_id);
        kill_member($target);
        $message .= '<br />'.do_lang_tempcode('W_ATTACKER_LOOTED_DEFENDER')->evaluate();
    } elseif ($attacker_died && !$defender_died) {
        take_items($member_id, $target);
        kill_member($member_id);
        $message .= '<br />'.do_lang_tempcode('W_DEFENDER_LOOTED_ATTACKER')->evaluate();
    } elseif ($attacker_died && $defender_died){
        drop_all_items($member_id);
        kill_member($member_id);
        drop_all_items($target);
        kill_member($target);
        $message .= '<br />'.do_lang_tempcode('W_BOTH_LOST_ALL_ITEMS')->evaluate();
    }
    ocw_refresh_with_message(make_string_tempcode($message),'inform');
}
Back to the top
 
Posted
Rating:
#83794
Avatar

Community saint

ok so I increased the advantages by dividing the difference of who has more health. the larger the difference the larger the percentage of chance (loosely speaking) one has of not getting hurt. if a low health attacks a high health the high health can earn as much as 95% chance of not getting hurt while the attacker only has a 15% chance

If the Attacker is High and the defender low the attacker can have a maximum of  85% chance of not being hurt while the low defender only gets 15%

I think that should even things out a little consider a 600 health can loose 500 pts if he's hurt against a 10 point health that might only lose 8 pts. Of course both could die at any time hehe.
Back to the top
 
Posted
Rating:
#83798
Avatar

Going to quickly whizz through the technical points here. Great to have some code discussion.

do_lang_tempcode does indeed return a Tempcode object. The main reason for doing this is it allows ocPortal to know that this data came from a language string. Then if a template happens to escape (sanitise) a parameter that was given this data (escaped using '*'), it won't actually do the escaping. This is because the system is smart enough to know language screens are already HTML-safe and don't need to be sanitised. Meaning you can put HTML into language strings and use that even inside template parameters that are marked as escaped.

Code

$message .= do_lang_tempcode('W_BOTH_LOST_ALL_ITEMS')->evaluate();
should really be:

Code

$message .= static_evaluate_tempcode(do_lang_tempcode('W_BOTH_LOST_ALL_ITEMS'));
as I think it was only PHP 5.3 that added support for the chained method invocations. But that is functionally equivalent to:

Code

$message .= do_lang('W_BOTH_LOST_ALL_ITEMS');


Become a fan of ocPortal on Facebook or add me as a friend. Add me on on Twitter.
Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about ocPortal whenever you see the opportunity.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying ocPortal on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Back to the top
 
Posted
Rating:
#83839
Avatar

Community saint

Thanks for the help Chris. BTW does the static method evaluate html breaks at the end of a line  and/or newline characters? Cause I was trying each of those before (in the language ini file) but they were always being ignored. I had to rest on adding the breaks outside the methods. ;(
Back to the top
 
Posted
Rating:
#83840
Avatar

Well…

\n works in language strings to create a textual line break.

However textual line breaks just show as a space in HTML.

And ultimately, the language strings are just getting output as part of the HTML.

So you'd need to use <br />.


Become a fan of ocPortal on Facebook or add me as a friend. Add me on on Twitter.
Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about ocPortal whenever you see the opportunity.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying ocPortal on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Back to the top
 
1 guests and 0 members have just viewed this: None
Control functions:

Quick reply   Contract

Your name:
Your message: