Jump to content

Can you explain this code?


dodgeitorelse

Recommended Posts

I have some code that is supposed to get data from a site and then insert the gathered data into a database. The code does start adding data to database but it isn't adding data that it is getting from site that has starting data. What I mean is this. This code gets current player scores and such from gametracker and adds that to the database. then it will get player scores from my own tracker and add new scores to the ones that weere adde3d from game tracker. We did this so that our game servers continue tracking the scores for our new server trackers and not start out at zero initially. How ever when we run the code it says data was not written to database probably because server does not exists in database when in fact it really does. SO if someone could explain the code to me as to what it is doing then maybe I can figure out why it isn't working.

 

function InitializeServer( $sid, $ip, $name, &$totalplayers )
{
    !mysql_query( "DROP TABLE ".SERVER_TABLE_NAME.$sid.";" );    // we don't care if this fails if table didn't exist before
    if( !mysql_query( "CREATE TABLE ".SERVER_TABLE_NAME.$sid." ("
        ." `name` varchar(32), `score` int, `goal` int default 0, `leader` int default 0, `enemy` int default 0, `kia` int default 0, `roe` int default 0,"
        ." PRIMARY KEY(`name`) );" ) ) {
        die( "Error creating table: ".mysql_error() );
    }

    $id = 1;
    $page = 1;
    $done = FALSE;
    while( !$done ) {
        $data = http_request( "www.gametracker.com", "/server_info/".$ip."/top_players/?searchipp=50&searchpge=$page" );
        $pos = 0;
        $out = "";
        while( !$done ) {
            $num = GetStringInMiddle( $data, $pos, '<td class="c01">', '</td>' ); if( $num == NULL && $id > 1 ) break;
            if( $num != $id || $num == NULL ) { $done = TRUE; break; }
            $name = GetStringInMiddle( $data, $pos, '/">', '</a>' ); if( $name  === NULL ) break;
            $score = GetStringInMiddle( $data, $pos, '<td class="c04">', '</td>' );
            //$timePlayed  = GetStringInMiddle( $data, $pos, '<td class="c05">', '</td>' );
            //$scorePerMinute = GetStringInMiddle( $data, $pos, '<td class="c06">', '</td>' );
            
            if( $out != "" ) $out .= ",\n";
            $out .= "('$name', '$score')";
            $id ++;
            $totalplayers ++;
        }

        // Write data to database, if any                
        if( $out != "" ) 
        {
            $cmd = "INSERT INTO ".SERVER_TABLE_NAME.$sid." (`name`, `score`) VALUES ".$out.";";            
            if( !mysql_query( $cmd ) ) {
                echo "Error with insert: ".mysql_error()."\n[$cmd]";
            }
        }
        echo ($id-1)." "; flush(); ob_flush();
        $page++;
    }

    if( $id > 1 ) echo "\nInitalized server #$sid: ".$ip." - ".$name." - ".($id-1)." players added<br />";
    else echo "\nServer #$sid: ".$ip." - ".$name." - no players added (server probably not in database)<br />";

    flush(); ob_flush();
}

 

$sid, $ip and such are defined in other files that are included in the same file as this code is included in.

Link to comment
Share on other sites

and no I am not a programmer. I am currently learning to code. I have learned some basics. For example I can query databases, I can find my way through modifying a lot of code but I do not know enough yet to be able to help myself in this situation. Or perhaps I do and am just making it more complicated than it really is, I don't know.

Link to comment
Share on other sites

It attempts to drop a MySQL table.

Then it tries to create a table

Then it calls some unknown function which I can only guess loads the contents of an external page into a string, in a loop.

Then it uses a bunch more unknown functions which I guess extracts information, in a loop. This includes some logic on how to break out of the current looping structure it's in.

It then checks if it has extracted any info. If it has, dump that into the created table.

Then it echo's how many times the first loop executed, and flushes the output buffer.

It then echo's a bunch of information based on the amount of times the previous loop executed.

 

I doubt this is going to help.

 

The looping structure that function employs is pretty nasty. Also, the way it extracts data from a web page seems fragile.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.