Announcement

Collapse
No announcement yet.

Counter-Strike

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Counter-Strike

    ok i was wondering if anyone can help me with creating a php script that will parse counter-strike logs and dump the data into a database for stats?!? just a thought????

  • #2
    This is _VERY_ sloppy code, but it should help you get started. you run the script:

    scriptname.php?log=l35464.log

    Like that. It parses things like the players name and things like that, kills, deaths, yadda yadda.

    I wrote this about 1 1/2 years ago so don't expect me to know much about it.


    Code:
    <?php
    if(!($logfile = file("logfiles/" . $log)))
    {
    	die("could not find the log file '$log'");
    }
    
    $mysql_link = mysql_connect("localhost", "*****", "******");
    mysql_select_db("*****", $mysql_link);
    
    $query = "SELECT * FROM csstats_logfiles WHERE filename='" . addslashes($log) . "' AND numlines='" . count($logfile) . "'";
    $results = mysql_query($query);
    $rows = mysql_num_rows($results);
    
    if($rows != 0)
    {
    	die("this log file has been processed before");
    }
    
    echo("Processing.... ");
    
    $startedlog = date("U");
    
    for($i=0; $i < count($logfile); $i++)
    {
    	$temp = explode(": ", $logfile[$i]);
    	$line = $temp[1];
    
    	$blank_line = ReplaceInsideQuotes($line);
    
    	if($blank_line == " triggered ")
    	{
    		// "[W]o[D]Illmat!c<68><70887><TERRORIST>" triggered "Spawned_With_The_Bomb"
    		// Spawned_With_The_Bomb
    		// Dropped_The_Bomb
    		// Got_The_Bomb
    		// Planted_The_Bomb
    
    		$qcnt = 0;
    		$ocnt = 0;
    		$event = "";
    		$event_wonid = "";
    
    		for($z=0; $z < strlen($line); $z++)
    		{
    			$thischar = substr($line,$z,1);
    
    			if($thischar == "\"")
    			{
    				$qcnt++;
    			}
    			else
    			{
    				if($qcnt == 3)
    				{
    					$event .= $thischar;
    				}
    				elseif($qcnt == 1)
    				{
    					if($thischar == "<")
    					{
    						$ocnt++;
    					}
    					else
    					{
    						if($ocnt == 2 && $thischar != ">")
    						{
    							$event_wonid .= $thischar;
    						}
    					}
    				}
    			}
    		}
    
    		$event_playerid = GetUserIDFromWONID($event_wonid);
    
    		if($event == "Spawned_With_The_Bomb")
    		{
    			mySQLIncreaseField("bombstarts",1,$event_playerid);
    			echo("<BR>Triggered Event: <I>$event</I> ...");
    		}
    		elseif($event == "Dropped_The_Bomb")
    		{
    			mySQLIncreaseField("bombdrops",1,$event_playerid);
    			echo("<BR>Triggered Event: <I>$event</I> ...");
    		}
    		elseif($event == "Got_The_Bomb")
    		{
    			mySQLIncreaseField("bombassists",1,$event_playerid);
    			echo("<BR>Triggered Event: <I>$event</I> ...");
    		}
    		elseif($event == "Planted_The_Bomb")
    		{
    			mySQLIncreaseField("bombplants",1,$event_playerid);
    			echo("<BR>Triggered Event: <I>$event</I> ...");
    		}
    		elseif($event == "Begin_Bomb_Defuse_Without_Kit")
    		{
    			mySQLIncreaseField("startdefusewithoutkit",1,$event_playerid);
    			echo("<BR>Triggered Event: <I>$event</I> ...");
    		}
    		elseif($event == "Begin_Bomb_Defuse_With_Kit")
    		{
    			mySQLIncreaseField("startdefusewithkit",1,$event_playerid);
    			echo("<BR>Triggered Event: <I>$event</I> ...");
    		}
    		elseif($event == "Became_VIP")
    		{
    			mySQLIncreaseField("beenvip",1,$event_playerid);
    			echo("<BR>Triggered Event: <I>$event</I> ...");
    		}
    		elseif($event == "Escaped_As_VIP")
    		{
    			mySQLIncreaseField("escapedasvip",1,$event_playerid);
    			echo("<BR>Triggered Event: <I>$event</I> ...");
    		}
    
    	}
    	elseif($blank_line == "Loading map ")
    	{
    		// Loading map "de_inferno"
    		$quotecount = 0;
    		$map = "";
    
    		for($q=0; $q < strlen($line); $q++)
    		{
    			$thischar = substr($line,$q,1);
    			if($thischar == "\"")
    			{
    				$quotecount++;
    			}
    
    			if($quotecount == 1)
    			{
    				if($thischar != "\"")
    				{
    					$map .= $thischar;
    				}
    			}
    		}
    	}
    	elseif($blank_line == " committed suicide with ")
    	{
    		// "Yoshi<88><99027><CT>" committed suicide with "world"
    		$y = strlen($line);
    		$pos = 1;
    
    		$stage1 = "";
    		$stage2 = "";
    		$stage3 = "";
    		$stage4 = "";
    
    		for($z=0; $z < $y; $z++)
    		{
    			$thischar = substr($line,$z,1);
    
    			if($pos == 1)
    			{
    				if($thischar == "\"")
    				{
    				}
    				elseif($thischar == "<")
    				{
    					$pos = 2;
    				}
    				else
    				{
    					$stage1 .= $thischar;
    				}
    			}
    			elseif($pos == 2)
    			{
    				if($thischar == ">")
    				{
    					$pos = 3;
    				}
    				else
    				{
    					$stage2 .= $thischar;
    				}
    			}
    			elseif($pos == 3)
    			{
    				if($thischar == "<")
    				{
    				}
    				elseif($thischar == ">")
    				{
    					$pos == 4;
    				}
    				else
    				{
    					$stage4 .= $thischar;
    				}
    			}
    		}
    
    		$suicideid = GetUserIDFromWONID($stage4);
    		AddSuicide($suicideid);
    		
    	}
    	elseif($blank_line == " killed  with ")
    	{
    		// "Bimboo<64><283269><CT>" killed "[W]o[D]Illmat!c<68><70887><TERRORIST>" with "m4a1"
    		$y = strlen($line);
    		$pos = 1;
    
    		$stage1 = "";
    		$stage2 = "";
    		$stage3 = "";
    		$stage4 = "";
    
    		$stage5 = "";
    		$s5qcnt = 0;
    		$stage6 = "";
    		$stage7 = "";
    		$stage8 = "";
    		$stage9 = "";
    		$s9qcnt = 0;
    
    		for($z=0; $z < $y; $z++)
    		{
    			$thischar = substr($line,$z,1);
    
    			if($pos == 1)
    			{
    				// "|Bimboo|<64><283269><CT>" killed "[W]o[D]Illmat!c<68><70887><TERRORIST>" with "m4a1"
    				if($thischar == "\"")
    				{
    				}
    				elseif($thischar == "<")
    				{
    					$pos = 2;
    				}
    				else
    				{
    					$stage1 .= $thischar;
    				}
    			}
    			elseif($pos == 2)
    			{
    				// "Bimboo<|64|><283269><CT>" killed "[W]o[D]Illmat!c<68><70887><TERRORIST>" with "m4a1"
    				if($thischar == ">")
    				{
    					$pos = 3;
    				}
    				else
    				{
    					$stage2 .= $thischar;
    				}
    			}
    			elseif($pos == 3)
    			{
    				// "Bimboo<64><|283269|><CT>" killed "[W]o[D]Illmat!c<68><70887><TERRORIST>" with "m4a1"
    				if($thischar == ">")
    				{
    					$pos = 4;
    				}
    				elseif($thischar == "<")
    				{
    				}
    				else
    				{
    					$stage3 .= $thischar;
    				}
    			}
    			elseif($pos == 4)
    			{
    				// "Bimboo<64><283269><|CT|>" killed "[W]o[D]Illmat!c<68><70887><TERRORIST>" with "m4a1"
    				if($thischar == ">")
    				{
    					$pos = 5;
    				}
    				elseif($thischar == "<")
    				{
    				}
    				else
    				{
    					$stage4 .= $thischar;
    				}
    			}
    			elseif($pos == 5)
    			{
    				// "Bimboo<64><283269><CT>" killed "|[W]o[D]Illmat!c|<68><70887><TERRORIST>" with "m4a1"
    				if($thischar == "\"")
    				{
    					$s5qcnt++;
    				}
    
    				if($s5qcnt == 3)
    				{
    					$pos = 6;
    				}
    				
    				if($thischar == "<")
    				{
    					$pos = 6;
    				}
    				else
    				{
    					if($s5qcnt == 2 && $thischar != "\"")
    					{
    						$stage5 .= $thischar;
    					}
    				}
    			}
    			elseif($pos == 6)
    			{
    				// "Bimboo<64><283269><CT|>" killed "[W]o[D]Illmat!c<|68|><70887><TERRORIST>" with "m4a1"
    				if($thischar == ">")
    				{
    					$pos = 7;
    				}
    				else
    				{
    					$stage6 .= $thischar;
    				}
    			}
    			elseif($pos == 7)
    			{
    				// "Bimboo<64><283269><CT>" killed "[W]o[D]Illmat!c<68><|70887|><TERRORIST>" with "m4a1"
    				if($thischar == "<")
    				{
    				}
    				elseif($thischar == ">")
    				{
    					$pos = 8;
    				}
    				else
    				{
    					$stage7 .= $thischar;
    				}
    			}
    			elseif($pos == 8)
    			{
    				// "Bimboo<64><283269><CT>" killed "[W]o[D]Illmat!c<68><70887><|TERRORIST|>" with "m4a1"
    				if($thischar == "<")
    				{
    				}
    				elseif($thischar == ">")
    				{
    					$pos = 9;
    				}
    				else
    				{
    					$stage8 .= $thischar;
    				}
    			}
    			elseif($pos == 9)
    			{
    				// "Bimboo<64><283269><CT>" killed "[W]o[D]Illmat!c<68><70887><TERRORIST>|" with "m4a1"|
    				if($thischar == "\"")
    				{
    					$s9qcnt++;
    				}
    
    				if($s9qcnt == 3)
    				{
    					$pos = 10;
    				}
    				
    				if($s9qcnt == 2 && $thischar != "\"")
    				{
    					$stage9 .= $thischar;
    				}
    			}
    		}
    
    		$killerid = GetUserIDFromWONID($stage3);
    		$victimid = GetUserIDFromWONID($stage7);
    
    		if($killerid == 0)
    		{
    			$query = "INSERT INTO csstats_players (nickname, wonid) VALUES('$stage1','$stage3')";
    			$results = mysql_query($query);
    			$killerid = mysql_insert_id();
    		}
    
    		if($victimid == 0)
    		{
    			$query = "INSERT INTO csstats_players (nickname, wonid) VALUES('$stage5','$stage7')";
    			$results = mysql_query($query);
    			$victimid = mysql_insert_id();
    		}
    
    		$query = "INSERT INTO csstats_kills (killer, victim, weapon, map) VALUES('$killerid','$victimid','" . addslashes($stage9) . "','" . $map . "');";
    		$results = mysql_query($query);
    
    		AddKill($killerid);
    		AddDeath($victimid);
    	}
    
    }
    
    
     $query = "INSERT INTO csstats_logfiles (filename, startedlog, finishedlog, numlines) VALUES('" . addslashes($log) . "','" . $startedlog . "','" . date("U") . "','" . count($logfile) . "')";
     $results = mysql_query($query);
    
     echo("done!");
    unlink("logfiles/" . $log);
    mysql_close($mysql_link);
    ?>
    Last edited by boneill; Sun 29 Jul '01, 1:26am.

    Comment


    • #3
      boneill-
      I don't think that your script is complete. Where did you define these functions:

      AddDeath()
      AddKill()
      AddSuicide()
      GetUserIDFromWONID()
      mySQLIncreaseField()
      ReplaceInsideQuotes()

      Comment


      • #4
        Sorry, they were in another file and I didn't notice:

        PHP Code:
        function ReplaceInsideQuotes($txt)
        {

            if (!
        is_int(strpos($txt,"\"")))
                return 
        $txt;        // No quotes.
            
        $m=strlen($txt);
            
        $inq=false;
            
        $outtxt="";
            for (
        $index=0;$index<$m;$index++)
            {
                
        $changed=false;
                
        $thischar=substr($txt,$index,1);
                if (
        strcmp($thischar,"\"") == 0)
                {
                    if (!
        $inq)
                        
        $outtxt.=$thischar;
                    
        $inq=!$inq;
                    
        $changed=true;
                }
                if (!
        $inq)
                    
        $outtxt.=$thischar;
            }

            return 
        ereg_replace("[\r\n]","",ereg_replace("\"","",$outtxt));

        }

        function 
        GetUserIDFromWONID($wonid)
        {
            global 
        $mysql_link;

            
        $query "SELECT id FROM csstats_players WHERE wonid='$wonid' LIMIT 1";
            
        $results mysql_query($query);

            
        $rows mysql_num_rows($results);

            if(
        $rows == 0)
            {
                return 
        0;
            }
            else
            {
                
        $data mysql_fetch_array($results);
                return 
        $data["id"];
            }
        }

        function 
        AddKill($playerid)
        {
            global 
        $mysql_link;

            
        $query "UPDATE csstats_players SET kills=kills+1 WHERE id='$playerid'";
            
        $results mysql_query($query);
        }

        function 
        AddDeath($playerid)
        {
            global 
        $mysql_link;

            
        $query "UPDATE csstats_players SET deaths=deaths+1 WHERE id='$playerid'";
            
        $results mysql_query($query);
        }

        function 
        AddSuicide($playerid)
        {
            global 
        $mysql_link;

            
        $query "UPDATE csstats_players SET suicides=suicides+1 WHERE id='$playerid'";
            
        $results mysql_query($query);
        }

        function 
        mySQLIncreaseField($field$number$playerid)
        {
            global 
        $mysql_link;

            
        $query "UPDATE csstats_players SET " $field "=" $field "+" $number " WHERE id='$playerid'";
            
        $results mysql_query($query);

        Comment


        • #5
          ok see im new to php and you guys are way over my head lol

          Comment


          • #6
            Parsing a Half-Life log file is quite complicated. Why not start with something a little easier?

            Comment


            • #7
              well to put it simple i would like to make a nice stats program for counter strike. maybe i should parse the file in perl and dump the data into the mySQL db and just use php templates to be the front end to the webclient wanting to view the stats. i mean i have a redhat 6.2 linux server patched out the whazzzu running mySQL and php4. i have a database created but i have created my tables yet. im still trying to figure out how im gonna parse the log files.

              Comment


              • #8
                perl would be best

                Perl would be the best language to parse any type of log. It has everything PHP has, and more, when dealing with regular expressions, plus, its a lot more fun to code.

                PHP is unimaginative, i.e., every function you will ever need is provided for you, whereas Perl has just enough functions built in to get the job done, while it promotes good programming practice at the same time.

                I would suggest using Perl over PHP any day.

                -Vic

                Comment


                • #9
                  haha you're funny if only you could see the eZboard -> vBulletin import script you wouldn't knock php and regular expressions.

                  Comment


                  • #10
                    i was not knocking regex's

                    freddie, i was not knocking regex's. they are the coolest things around since sliced bread.

                    the regex php api is what i dont like. perl is better in the sense that you don't have to use a damn function everytime you want to execute a regular expression.

                    along with that, php just took the regular expression engine from perl, wrapped it up with some special functions and shipped it right back out.

                    granted, php is faster than perl in some things, the perl regular expression engine is as rock solid as any piece of software can be.

                    finally, php regular expressions don't follow regular expression syntax, like perls.

                    regex's, spawned from the program rx, were first implemented in perl, right after version 1.0, i believe. if not, very basic ones were put in v 1.0.

                    php regex's do not follow regular regex syntax, i.e., using functions and so forth, which is why i think they are not as good as perls.

                    finally, i was not making fun of your import system, or ezboards. in fact, i think that vbulletin and ezboard are both very well made boards, both with much time spent in them.

                    but like i said, php has way too many functions built into it. over 2000 functions? if you ask me, thats kind of an insult saying that the programmer is not good enough to program, so we are gonna put the functions in there.

                    i would rather use the perl dbi over the php mysql api any day.

                    just my own $0.02.

                    -Vic

                    Comment


                    • #11
                      well if that was the case then i would write it in python and not perl. since in python i could do the same thing with 80% less code and it would still be faster than any perl script you put together to do the same thing. but what i was shooting for it to make a completely php stats program as to where it was a php script that parses the logs and php templates that pull the data out for the front end. but seeing as how i can not get any help in this then python might have to be the solution.

                      Comment

                      widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                      Working...
                      X