Page 1 of 2 12 LastLast
Results 1 to 25 of 31

Thread: [PHP] SRL Stats Signatures

  1. #1
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default [PHP] SRL Stats Signatures

    So, the new SRL Stats system is out, and there is a nice API, why not make stats signatures?

    Things you need:
    • A web-space with PHP & GD
    • RS Font (Any font will do, but rs.ttf is what I use). Download from HERE!
    • Some knownledge of PHP, alot is just given to you and explained, but basic understanding is good.


    Lets start by having a look at the API at http://stats.villavu.com/api/script/2.

    Code:
    {
        "last_commit_by:": "whiled", 
        "vars": [
            [
                12221, 
                "Iron"
            ]
        ], 
        "script": "Iron Miner", 
        "commits": 860, 
        "time": 14490, 
        "owner": "whisker", 
        "last_commit_on:": "Sun Dec 26 23:25:21 2010"
    }
    The syntax is JSON, and PHP has built-in parser for it.

    Parse & Clean

    Parsing the data from stats API into array:
    PHP Code:
    <?php

    $stats 
    file_get_contents("http://stats.villavu.com/api/script/2");
    $stats_array json_decode($statstrue);

    ?>
    To see the structure of your $stats_array you can use function like print_r($stats_array); to see it.

    Here is what print_r outputs:
    Code:
    Array
    (
        [last_commit_by:] => whiled
        [vars] => Array
            (
                [0] => Array
                    (
                        [0] => 12221
                        [1] => Iron
                    )
    
            )
    
        [script] => Iron Miner
        [commits] => 860
        [time] => 14490
        [owner] => whisker
        [last_commit_on:] => Sun Dec 26 23:25:21 2010
    )
    Now we might want to put "vars" array into more easily accessible form. I made a simplify_vars() function for it.
    PHP Code:
    <?php

    $stats 
    file_get_contents("http://old.villavu.com/stats/api/script/2");
    $stats_array json_decode($statstrue);

    function 
    simplify_vars(&$stats_array) {
        foreach (
    $stats_array["vars"] as $array) {
            
    $stats_array[$array[1]] = $array[0];
        }
        unset(
    $stats_array["vars"]);
    }

    simplify_vars($stats_array);

    print_r($stats_array);

    ?>
    Output:
    Code:
    Array
    (
        [last_commit_by:] => whiled
        [script] => Iron Miner
        [commits] => 860
        [time] => 14490
        [owner] => whisker
        [last_commit_on:] => Sun Dec 26 23:25:21 2010
        [Iron] => 12221
    )
    And as you can see, we can now access the Iron variable with $stats_array["Iron"].

    Now that we have our array cleaned up, we can move to the fun part.

    Display Stats

    Setting font path:
    PHP Code:
    putenv("GDFONTPATH=".realpath(".")); 
    Creating image:
    PHP Code:
    $img imagecreatefrompng("sig_base_example.png"); 
    I already have some colors ready that are used in RS, but you can allocate your own colors easily with:
    PHP Code:
    imagecolorallocate($imgRedGreenBlue); 
    Now that you have allocated your colors, you should have something like this:
    PHP Code:
    <?php

    $stats 
    file_get_contents("http://old.villavu.com/stats/api/script/2");
    $stats_array json_decode($statstrue);

    function 
    simplify_vars(&$stats_array) {
        foreach (
    $stats_array["vars"] as $array) {
            
    $stats_array[$array[1]] = $array[0];
        }
        unset(
    $stats_array["vars"]);
    }

    simplify_vars($stats_array);

    putenv("GDFONTPATH=".realpath("."));

    $img imagecreatefrompng("sig_base_example.png");

    $color["yellow"] = imagecolorallocate$img255255);
    $color["white"] = imagecolorallocate$img255255255 );
    $color["green"] = imagecolorallocate$img0255128 );
    $color["orange"] = imagecolorallocate$img254179);

    ?>
    You might want to color your values differently, like RS does for item quantity, so here is two functions that you can use:
    PHP Code:
    function rs_color($value) {
      if (
    $value 100000) {
        return 
    "yellow";
      } elseif (
    $value 100000 && $value 10000000) {
        return 
    "white";
      } else {
        return 
    "green";
      }
    }

    function 
    rs_value($value) {
      if (
    $value 100000) {
        return 
    $value;
      } elseif (
    $value 100000 && $value 10000000) {
        return 
    substr($value0strlen($value) - 3)."K";
      } else {
        return 
    substr($value0strlen($value) - 6)."M";
      }

    Drawing text on your image is easy, one function does it all:
    PHP Code:
    imagettftext($img12010080$color["orange"], "rs.ttf""IRON MINED: ");
    imagettftext($img12016280$color[rs_color($stats_array["Iron"])],  "rs.ttf"rs_value($stats_array["Iron"])); 
    Also you might want to have shadow on your text, so here is a function for that, it takes the same parameters as normal imagettftext():
    PHP Code:
    function imagettftextshadow($image$size$angle$x$y$color$fontfile$text) {
      
    imagettftext($image$size$angle$x 1$y 1imagecolorallocate($image000), $fontfile$text);
      
    imagettftext($image$size$angle$x$y$color$fontfile$text);

    When you have drawn all your texts, you are ready to show the image.
    PHP Code:
    imagepng($img);
    imagedestroy($img); 
    Now some final things, and you are ready. You need to specify headers for your content, which is image/png, so:
    PHP Code:
    header("Content-type: image/png"); 
    Now your script should look like this:
    PHP Code:
    <?php

    putenv
    ("GDFONTPATH=".realpath("."));

    header("Content-type: image/png");

    function 
    simplify_vars(&$stats_array) {
        foreach (
    $stats_array["vars"] as $array) {
            
    $stats_array[$array[1]] = $array[0];
        }
        unset(
    $stats_array["vars"]);
    }

    function 
    rs_color($value) {
      if (
    $value 100000) {
        return 
    "yellow";
      } elseif (
    $value 100000 && $value 10000000) {
        return 
    "white";
      } else {
        return 
    "green";
      }
    }

    function 
    rs_value($value) {
      if (
    $value 100000) {
        return 
    $value;
      } elseif (
    $value 100000 && $value 10000000) {
        return 
    substr($value0strlen($value) - 3)."K";
      } else {
        return 
    substr($value0strlen($value) - 6)."M";
      }
    }

    function 
    imagettftextshadow$image$size$angle$x$y$color$fontfile$text ) { 
      
    imagettftext$image$size$angle$x 1$y 1imagecolorallocate$image00), $fontfile$text ); 
      
    imagettftext$image$size$angle$x$y$color$fontfile$text ); 
    }

    $stats file_get_contents("http://old.villavu.com/stats/api/script/2");
    $stats_array json_decode($statstrue);

    simplify_vars($stats_array);

    $img imagecreatefrompng("sig_base_example.png");

    $color["yellow"] = imagecolorallocate$img255255);
    $color["white"] = imagecolorallocate$img255255255 );
    $color["green"] = imagecolorallocate$img0255128 );
    $color["orange"] = imagecolorallocate$img254179);

    imagettftextshadow($img12010080$color["orange"], "rs.ttf""IRON MINED: ");
    imagettftextshadow($img12016280$color[rs_color($stats_array["Iron"])],  "rs.ttf"rs_value($stats_array["Iron"]));

    imagepng($img);
    imagedestroy($img);

    ?>
    And it should output this:


    Helpful Resources

    Last edited by Frement; 01-02-2011 at 02:29 AM.
    There used to be something meaningful here.

  2. #2
    Join Date
    Sep 2006
    Location
    Canada
    Posts
    1,124
    Mentioned
    0 Post(s)
    Quoted
    5 Post(s)

    Default

    Possible with any free hosts?

  3. #3
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by Capricorn View Post
    Possible with any free hosts?
    Almost every hosting company (even the ones that provide for free only) has PHP and GD installed.

    EDIT: And I can host the files on my site also, if someone doesn't find any suitable free host.
    There used to be something meaningful here.

  4. #4
    Join Date
    Mar 2006
    Location
    Behind you
    Posts
    3,193
    Mentioned
    61 Post(s)
    Quoted
    63 Post(s)

    Default

    Very nice Frement it's a good addition to the tut section.

    ~BraK

    "Sometimes User's don't need the Answer spelled out with Code. Sometimes all they need is guidance and explanation of the logic to get where they are going."

  5. #5
    Join Date
    Dec 2006
    Location
    Sweden
    Posts
    10,812
    Mentioned
    3 Post(s)
    Quoted
    16 Post(s)

    Default

    You should make a default one, so a user can go /stats.php?scriptid=122 - and it will print the stats of that script in a default template.

    Thanks for the info.


    Send SMS messages using Simba
    Please do not send me a PM asking for help; I will not be able to help you! Post in a relevant thread or make your own! And always remember to search first!

  6. #6
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by Harry View Post
    You should make a default one, so a user can go /stats.php?scriptid=122 - and it will print the stats of that script in a default template.

    Thanks for the info.
    The problem with this is, that the script might have different names of variables, and different amount of variables, but that doesn't mean it isn't possible, just would require some work.

    Quote Originally Posted by BraK View Post
    Very nice Frement it's a good addition to the tut section.
    Thanks
    There used to be something meaningful here.

  7. #7
    Join Date
    Dec 2010
    Location
    New York
    Posts
    124
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    ha, very cool. I guess I should start using SRL stats ._.;

  8. #8
    Join Date
    Jul 2007
    Location
    Norway.
    Posts
    1,938
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

  9. #9
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by EvilChicken! View Post
    Read my rep+ comment.
    Thats why I added the "Helpful Resources" No need for me to explain the functions again, I could have copied them from the site, but I didn't think there was any need.
    There used to be something meaningful here.

  10. #10
    Join Date
    Apr 2007
    Location
    Australia
    Posts
    4,163
    Mentioned
    9 Post(s)
    Quoted
    19 Post(s)

    Default

    Can you do it in Python?

  11. #11
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    Quote Originally Posted by The Claw View Post
    Can you do it in Python?
    Of course. I might post something about it, but I really need to do my homework. Maybe tonight.

  12. #12
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by The Claw View Post
    Can you do it in Python?
    Well, I know the syntax, but I have never worked with image manipulation in Python, I could give it a try if I find a suitable place for it, I have so much other stuff to do also.
    There used to be something meaningful here.

  13. #13
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    I'll work on it after school today or tomorrow, Frement. Don't worry about it. Stick to what you've got on your plate for now.

  14. #14
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by i luffs yeww View Post
    I'll work on it after school today or tomorrow, Frement. Don't worry about it. Stick to what you've got on your plate for now.
    Use the GD module

    http://newcenturycomputers.net/projects/gdmodule.html
    There used to be something meaningful here.

  15. #15
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    Oohh.. Thanks, Frement. I'll see what I can do with it.

    Probably. :3

  16. #16
    Join Date
    Apr 2007
    Location
    Colchester, UK
    Posts
    1,220
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks for this thread, i finally got it to work for me.

    it had been quite a while since i used PHP and once i stared remembering it was easy.

    here is what i came up with (its not very "good looking") (if it doesnt show it means there is a error and a refresh should solve it)



    image should be above (direct link direct link)

    Edit: Error message when it doesnt show
    Code:
    The image “http://www.bobzilla69.ratinator.x10.mx/RatinatorScript.php” cannot be displayed, because it contains errors.
    in error log it has
    Code:
    [25-Jan-2011 08:44:11] PHP Warning:  Invalid argument supplied for foreach() in /home/ratinato/public_html/RatinatorScript.php on line 10
    Last edited by Bobzilla69; 01-25-2011 at 01:44 PM.

  17. #17
    Join Date
    May 2007
    Location
    Tasmania, Aus
    Posts
    898
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    is there anyway you can have your personal stats for this sig?

  18. #18
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by Sgt Soul View Post
    is there anyway you can have your personal stats for this sig?
    If there is a page for that in the api.
    There used to be something meaningful here.

  19. #19
    Join Date
    Oct 2009
    Location
    Stockton, CA
    Posts
    2,040
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    http://stats.villavu.com/api/user/$USERID
    Join the IRC! irc.rizon.net:6667/srl | SQLite (0.99rc3+) | SRL Doc | Simba Doc | Extra Simba Libraries (openSSL & sqlite3)
    Quote Originally Posted by #srl
    10:45 < Toter> daphil when can get sex anyday I want
    10:45 < Toter> he is always on #SRL
    "A programmer is just a tool which converts caffeine into code"

  20. #20
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    Thanks for the tut

    It works nice when veiwing it in a separate tab, but the forums doesn't recognise the page as an image.

    Code:
    <?php
    
    header("Content-type: image/png");
    
    $stats = file_get_contents("http://stats.villavu.com/api/script/145"); 
    $stats_array = json_decode($stats, true); 
    
    function simplify_vars(&$stats_array) { 
        foreach ($stats_array["vars"] as $array) { 
            $stats_array[$array[1]] = $array[0]; 
        } 
        unset($stats_array["vars"]); 
    } 
    
    simplify_vars($stats_array);
    
    //putenv("GDFONTPATH=".realpath(".")); 
     
    $img = imagecreatefrompng("mta_sig_1.png"); 
     
    
    
    $color["yellow"] = imagecolorallocate( $img, 255, 255, 0 ); 
    $color["white"] = imagecolorallocate( $img, 255, 255, 255 ); 
    $color["green"] = imagecolorallocate( $img, 0, 255, 128 ); 
    $color["orange"] = imagecolorallocate( $img, 254, 179, 0 );
    
    function rs_color($value) { 
      if ($value < 100000) { 
        return "yellow"; 
      } elseif ($value > 100000 && $value < 10000000) { 
        return "white"; 
      } else { 
        return "green"; 
      } 
    } 
    
    function rs_value($value) { 
      if ($value < 100000) { 
        return $value; 
      } elseif ($value > 100000 && $value < 10000000) { 
        return substr($value, 0, strlen($value) - 3)."K"; 
      } else { 
        return substr($value, 0, strlen($value) - 6)."M"; 
      } 
    }  
    
    imageString($img, 10, 130, 15,  $stats_array["time"],    $color[rs_color($stats_array["Time"])]);
    imageString($img, 10, 130, 28,  $stats_array["Enchantment Points (Gained)"],  $color[rs_color($stats_array["Enchantment Points (Gained)"])]);
    imageString($img, 10, 130, 41,  $stats_array["Telekinesis Points (Gained)"],  $color[rs_color($stats_array["Telekinesis Points (Gained)"])]);
    imageString($img, 10, 360, 15,  $stats_array["Magic EXP (Gained)"], $color[rs_color($stats_array["Magic EXP (Gained)"])]);
    imageString($img, 10, 360, 28,  $stats_array["Graveyard Points (Gained)"],  $color[rs_color($stats_array["Graveyard Points (Gained)"])]);
    imageString($img, 10, 360, 41,  $stats_array["Alchemy Points (Gained)"],  $color[rs_color($stats_array["Alchemy Points (Gained)"])]);
    imageString($img, 10, 130, 53,  $stats_array["last_commit_by:"],  $color[rs_color(100000)]);
    imageString($img, 10, 335, 55,  $stats_array["last_commit_on:"],  $color[rs_color(100000)]);
    
    
     
    imagepng($img); 
    imagedestroy($img);
    
    
    
    
    ?>
    http://awkwardscripts.host56.com/MTA/sig.php

    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  21. #21
    Join Date
    Mar 2007
    Posts
    3,116
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    Quote Originally Posted by Awkwardsaw View Post
    Thanks for the tut

    It works nice when veiwing it in a separate tab, but the forums doesn't recognise the page as an image.

    Code:
    <?php
    
    header("Content-type: image/png");
    
    $stats = file_get_contents("http://stats.villavu.com/api/script/145"); 
    $stats_array = json_decode($stats, true); 
    
    function simplify_vars(&$stats_array) { 
        foreach ($stats_array["vars"] as $array) { 
            $stats_array[$array[1]] = $array[0]; 
        } 
        unset($stats_array["vars"]); 
    } 
    
    simplify_vars($stats_array);
    
    //putenv("GDFONTPATH=".realpath(".")); 
     
    $img = imagecreatefrompng("mta_sig_1.png"); 
     
    
    
    $color["yellow"] = imagecolorallocate( $img, 255, 255, 0 ); 
    $color["white"] = imagecolorallocate( $img, 255, 255, 255 ); 
    $color["green"] = imagecolorallocate( $img, 0, 255, 128 ); 
    $color["orange"] = imagecolorallocate( $img, 254, 179, 0 );
    
    function rs_color($value) { 
      if ($value < 100000) { 
        return "yellow"; 
      } elseif ($value > 100000 && $value < 10000000) { 
        return "white"; 
      } else { 
        return "green"; 
      } 
    } 
    
    function rs_value($value) { 
      if ($value < 100000) { 
        return $value; 
      } elseif ($value > 100000 && $value < 10000000) { 
        return substr($value, 0, strlen($value) - 3)."K"; 
      } else { 
        return substr($value, 0, strlen($value) - 6)."M"; 
      } 
    }  
    
    imageString($img, 10, 130, 15,  $stats_array["time"],    $color[rs_color($stats_array["Time"])]);
    imageString($img, 10, 130, 28,  $stats_array["Enchantment Points (Gained)"],  $color[rs_color($stats_array["Enchantment Points (Gained)"])]);
    imageString($img, 10, 130, 41,  $stats_array["Telekinesis Points (Gained)"],  $color[rs_color($stats_array["Telekinesis Points (Gained)"])]);
    imageString($img, 10, 360, 15,  $stats_array["Magic EXP (Gained)"], $color[rs_color($stats_array["Magic EXP (Gained)"])]);
    imageString($img, 10, 360, 28,  $stats_array["Graveyard Points (Gained)"],  $color[rs_color($stats_array["Graveyard Points (Gained)"])]);
    imageString($img, 10, 360, 41,  $stats_array["Alchemy Points (Gained)"],  $color[rs_color($stats_array["Alchemy Points (Gained)"])]);
    imageString($img, 10, 130, 53,  $stats_array["last_commit_by:"],  $color[rs_color(100000)]);
    imageString($img, 10, 335, 55,  $stats_array["last_commit_on:"],  $color[rs_color(100000)]);
    
    
     
    imagepng($img); 
    imagedestroy($img);
    
    
    
    
    ?>
    http://awkwardscripts.host56.com/MTA/sig.php

    I see the image in your sig and the post just fine?

  22. #22
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    Quote Originally Posted by MylesMadness View Post
    I see the image in your sig and the post just fine?


    it just changed then, it was red x when/ after i posted
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  23. #23
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Nice job! Glad to see this is of some use.
    There used to be something meaningful here.

  24. #24
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Quote Originally Posted by Awkwardsaw View Post


    it just changed then, it was red x when/ after i posted
    That was because 000webhost was checking his account for malicious code

  25. #25
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    Quote Originally Posted by Zyt3x View Post
    That was because 000webhost was checking his account for malicious code
    but why would it work when veiwing it in a separate window?

    anyways, atleast i know it works
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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