Antworten ohne Registrierung

[PHP] BF Bad Company 2 Stats-Klasse für Banner




Gast
30.03.2010, 13:21 Uhr
Wer Interesse hat, selbst sich Banner über seinen Space generieren zulassen, der kann gerne meine Klassen benutzen, die ich hier zur Verfügung stelle - ein kleiner Hinweis zum Autor wäre nett (pctflux.net).

Falls Fragen auftauchen, oder das Script nicht wie erwartet funktioniert, zögert nicht, mich zu fragen
(Am besten hier im Thread)

PHP-Code:
<?php
/******************************************************
 pctflux services BF BC 2 Stats Classes
 @Author: Hans-Heinrich Auditor // www.pctflux.net
 @Version: 0.2
 @Copyright: Hans-Heinrich Auditor
 Aenderungen sind erlaubt mit dem Hinweis auf den
 urspruenglichen Autor.
 Changes are permitted with reference to the
 original author.
 Data provided by http://bfbcs.com
 
 Requirements:
 mysql-database
 write permissions
 curl php-extension
 gd php-extension
 
 Setup: (You can jump through the #Edit marks)
 Fill in the MYSQL_Interface Data
 Set path to TrueTypeFont
 Edit the redirect-method in SignatureGenerator-Class
 Edit the Template (400x80px PNG)
 Maybe adjust the generateSignature-method
 
 Usage:
 get-bc2-stats.php?spieler=Playername
 If data exists @ bfbcs.com, data will be pulled and
 aligned. If it differs, new banner will be generated,
 else, requests gets redirected to the banner-file.
 In case there is no userdata, it will be requested at
 bfbcs.com.
 *****************************************************/

class MYSQL_Interface #Edit
{
    private 
$_mysql_user "";
    private 
$_mysql_password "";
    private 
$_mysql_server "localhost";
    private 
$_mysql_database "";
    public  
$_mysql_connection;
    public    
$_mysql_open;
    public    
$_mysql_kill_connection;
    public    
$_mysql_query_result;
    public    
$_mysql_result = array();

    public function 
connectDB()
    {
        
$this->_mysql_connection mysql_connect($this->_mysql_server$this->_mysql_user$this->_mysql_password);
        
$this->_mysql_open mysql_select_db($this->_mysql_database);
        return 
$this->_mysql_connection;
    }

    public function 
disconnectDB()
    {
        
$this->_mysql_kill_connection mysql_close($this->_mysql_connection);
    }

    public function 
getQuery($args)
    {
        
$this->connectDB();
        
$this->_query_result mysql_query($args);
        while(
$this->_mysql_query_result mysql_fetch_array($this->_query_result))
        {
            
$this->_mysql_result[] = $this->_mysql_query_result;
        }
        
$this->disconnectDB();
        return 
$this->_mysql_result;
    }

    public function 
sendQuery($args)
    {
        
$this->connectDB();
        if(
mysql_query($args))
        {    return 
true;    }
        else
        {    return 
false;    }
        
$this->disconnectDB();
    }

    public function 
countQuery($args)
    {
        
$this->connectDB();
        
$this->_query_result mysql_query($args);
        
$is_selecting preg_match_all('/SELECT/'$args$treffer);
        if(
$is_selecting == or $is_selecting == false)
        { return 
mysql_affected_rows(); }
        else
        { return 
mysql_num_rows($this->_query_result); }
        
$this->disconnectDB();
    }
}

class 
SignatureGenerator
{
    private 
$_spielername;
    private 
$_dbhandle;
    private 
$_font ""#Edit | Path to TrueTypeFont-File eg. "/var/www/yourpage/public_html/fonts/arial.ttf";
    
public     $_spielerdaten;

    function 
__construct()
    {
        
$this->_spielername $this->safeMYSQL($_REQUEST["spieler"]);
        
$this->_dbhandle = new MYSQL_Interface;
    }

    private function 
safeMYSQL($var)
    {
        return 
mysql_real_escape_string($var$this->_dbhandle->connectDB());
        
$this->_dbhandle->disconnectDB();
    }

    public function 
retrieveData()
    {
        if(empty(
$this->_spielername))
        {
            
$this->_spielerdaten["name"] = "nicht angegeben!";
            return 
false;
        }
        else
        {
            
$url 'http://api.bfbcs.com/api/pc';
            
$postdata "players=".$this->_spielername."&fields=general";
            
$ch curl_init($url);
            
curl_setopt($chCURLOPT_POSTtrue);
            
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
curl_setopt($chCURLOPT_POSTFIELDS$postdata);
            
$data curl_exec($ch);
            
curl_close($ch);
            
$data json_decode($data,true);
            
$this->_spielerdaten $data["players"][0];
            
$this->_spielerdaten["timestamp"] = strtotime($data["players"][0]["date_lastupdate"]);
            
$this->_spielerdaten["md5"] = md5($this->_spielername);
            return 
$this->_spielerdaten;
        }
    }

    public function 
necessaryAction()
    {
        
$this->retrieveData();
        if(
$this->existsData())
        {
            if(
$this->existsEntry())
            {
                if(
$this->differingDate())
                {    
$this->updateData();    }
                else
                {    
$this->incrementViews();    }
            }
            else
            {    
$this->insertData();    }
        }
        else
        {    
$this->redirect(false);    }
    }

    private function 
existsEntry()
    {
        if(
$this->_dbhandle->countQuery("SELECT * FROM bc2_stats WHERE name = '$this->_spielername'") != 0)
        {    return 
true;    }
        else
        {    return 
false;    }
    }

    private function 
existsData()
    {
        if(
$this->_spielerdaten["timestamp"] == 0)
        {    return 
false;    }
        else
        {    return 
true;    }
    }

    private function 
differingDate()
    {
        
$lup $this->_dbhandle->getQuery("SELECT last_update FROM bc2_stats WHERE name = '$this->_spielername'");
        
$aup $this->_spielerdaten["timestamp"];
        if(
$lup[0][0] == $aup)
        {    return 
false;    }
        else
        {    return 
true;    }
    }

    private function 
redirect($tf#Edit
    
{
        if(!
$tf)
        {    
header('Location: http://pctflux.net/bc2/err.png');        } #Error-image, user not existing or not recorded at bfbcs.com
        
else
        {
            
$weiterleitung "Location: http://pctflux.net/bc2/";
            
$weiterleitung .= $this->_spielerdaten["md5"];
            
$weiterleitung .= ".png";
            
header($weiterleitung);
        }
    }

    private function 
incrementViews()
    {
        
$this->_dbhandle->sendQuery("UPDATE bc2_stats SET views_slu=views_slu+1 , views=views+1 WHERE name = '$this->_spielername'");
        
$this->redirect(true);
    }

    private function 
insertData()
    {
        
$name $this->_spielerdaten["name"];
        
$name_md5 $this->_spielerdaten["md5"];
        
$timestamp $this->_spielerdaten["timestamp"];
        
$this->_dbhandle->sendQuery("INSERT INTO bc2_stats (name, name_md5, last_update) VALUES ('$name', '$name_md5', '$timestamp')");
        if(
$this->generateSignature())
        {
            
$this->_dbhandle->sendQuery("UPDATE bc2_stats SET views_slu=views_slu+1 , views=views+1 WHERE name = '$this->_spielername'");
            
$this->redirect(true);
        }
    }

    private function 
updateData()
    {
        
$aktueller_stamp $this->_spielerdaten["timestamp"];
        
$this->_dbhandle->sendQuery("UPDATE bc2_stats SET last_update = '$aktueller_stamp', views_slu = '1' WHERE name = '$this->_spielername'");
        if(
$this->generateSignature())
        {
            
$this->_dbhandle->sendQuery("UPDATE bc2_stats SET views_slu=1, views=views+1 WHERE name = '$this->_spielername'");
            
$this->redirect(true);
        }
    }

    private function 
generateSignature()
    {
        
$sname $this->_spielerdaten["name"];
        
$srank $this->_spielerdaten["rank"];
        
$srank_name $this->_spielerdaten["rank_name"];
        
$sscore $this->_spielerdaten["score"];
        
$skills $this->_spielerdaten["kills"];
        
$sdeaths $this->_spielerdaten["deaths"];
        
$skd round(($skills/$sdeaths) , 2); #K/D Ratio
        
$stime $this->_spielerdaten["time"]; #Anzahl Spielsekunden / Game seconds
        
$stime_hours intval($stime 3600); #Anzahl Spielstunden / Game hours
        
$stime_mins round(($stime 3600) / 60); #Anzahl Spielminuten minus Stunden / Game minutes minus Hours
        
$stime_mins_complete intval($stime 60); #Anzahl Spielminuten / Overall game minutes
        
$sppm round($sscore $stime_mins_complete); #Anzahl Punkte pro Minute / Points per minute
        
$supd $this->_spielerdaten["date_lastupdate"]; #Letztes Update / Last update by bfbcs.com
        
$supd_date date("d.m.Y"strtotime($supd));

        
$trc imagecreatetruecolor(40080);
        
$vorlage imagecreatefrompng("stats_vorlage.png"); #Template #Edit
        
imagecopyresampled($trc$vorlage0000400116400116);
        
$textcolor imagecolorallocate($trc000);


        
#Left Side
        
imagettftext($trc1101017$textcolor$this->_font$sname); #Playername
        
imagettftext($trc801034$textcolor$this->_font"Rang: $srank | $srank_name"); #Rank No. & Name
        
imagettftext($trc801046$textcolor$this->_font"Punkte: $sscore"); #Score
        
imagettftext($trc801058$textcolor$this->_font"Kills: $skills"); #Kills
        
imagettftext($trc801070$textcolor$this->_font"Deaths: $sdeaths"); #Deaths

        #Right Side
        
imagettftext($trc8026834$textcolor$this->_font"K/D Ratio: $skd"); #K/D Ratio
        
imagettftext($trc8026846$textcolor$this->_font"PpM: $sppm"); #Points per Minute
        
imagettftext($trc8026858$textcolor$this->_font"Zeit: $stime_hours h $stime_mins m"); #Time
        
imagettftext($trc8026870$textcolor$this->_font"Upd: $supd_date"); #last Update by bfbcs.com
        
imagepng($trc$this->_spielerdaten["md5"].".png");
        return 
true;
    }
}

$start = new SignatureGenerator;
$start->necessaryAction();
?>
mySQL Query zum Erstellen der Datenbankstruktur:
Code:
CREATE TABLE IF NOT EXISTS `bc2_stats` (
  `id` int(4) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `name_md5` varchar(255) NOT NULL,
  `last_update` int(11) NOT NULL default '0',
  `views` int(7) NOT NULL default '0',
  `views_slu` int(3) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM  AUTO_INCREMENT=0;
#1


Facebook

Jahresarchive