Jump to content

Finding the best result


strike667

Recommended Posts

Hello. I think I have a little unique and difficult to solve problem that I really need to post here.

 

I, as a counter-strike portal administrator, am running some CS servers. With automatical script, the server uploads demos (videos of gameplay) on FTP in this format: 101229154428.dem and so on. The number is a date in this format: year, month, day, hours, minutes, seconds => for the example it's 29th December 2010, 15h, 44m and 28s.

 

Then I have a "report" page. The important part on it is that, there is a date in unix timestamp in each row like 1336837680 (29th december 2010, 15h, 47m, 00s). And now what I need:

 

Find a demo that corresponds to the unix timestamp. If there is a timestamp 1336837680, it should find 101229154428.dem.

 

 

You know what I mean, there is not one demofile, there are plenty. Just find the demo, that contains a record of the time used in timestamp.

 

Pretty difficult, isn't it? I would love to see if anybody came up with anything.

 

Best regards!

Link to comment
Share on other sites

It is actually pretty simple to take the unix date and convert it to the format used for the demo files. But, one thing that is not clear in your post above is if there will be an exact match between the unix data and the demo file name. If you are only looking for an exact match this is very simple. If you are looking for a demo file that is closest to the report data you need to provide more information: are you wanting the one that is mathmatically closest or do you need the one that is closest but not past or before???

Link to comment
Share on other sites

By the way, I don't know where you are coming up with those timestamps but 1336837680 is not 29th december 2010, 15h, 47m, 00s it is 12th may 2012, 10h, 48m, 00s (in CST)

 

I don't know what timezone you are in, but that wouldn't acctount for a 2+ year difference! You need to figure out what values you are actually getting if you want to compare them properly.

Link to comment
Share on other sites

Dunno, put it inside a web converter and this is what came from it.

 

It is actually pretty simple to take the unix date and convert it to the format used for the demo files. But, one thing that is not clear in your post above is if there will be an exact match between the unix data and the demo file name. If you are only looking for an exact match this is very simple. If you are looking for a demo file that is closest to the report data you need to provide more information: are you wanting the one that is mathmatically closest or do you need the one that is closest but not past or before???

 

Basically yes. Let's say the demo files are created every map change (that's ~20min). Which means, if the demo started recording at 14:00 and another demo at 15:00, if we have a timestamp with 14:35, it should find the demo that started recording at 14:00. Simply the demofile in which the time should be.

Link to comment
Share on other sites

OK, so you want the first demo file that comes before the report time.

 

The following code will do what you want assuming the date/time used for the file name and for the report timestapm are actually in sync. As I stated above, the timestamp you showed is in the year 2012.

 

Anyway, the following code should work for you. Just be sure to set the correct value for $demoFldr in the getDemoFile() function. Just pass the report date to the function getDemoFile() and it will return the closest, previous demo file for that date from the same day or previous day. If there are none, it will return false.

 

<?php

function getDemoFile($rptDate)
{
    $demoFldr = 'demo_files/';
    $fileExt  = 'dem';
    
    //Convert report date to same format as demofile names for comparison
    $reportDateTime = date('ymdHis', $rptDate); //Year month day hour min sec

    //Get demofiles for SAME day of report date
    $patternDate = date('ymd', $rptDate);
    $pattern = "{$demoFldr}{$patternDate}??????.{$fileExt}";
    $matches = glob($pattern);
    $demoMatch = prevDemoFile($reportDateTime, $matches, $demoFldr);
    //Check if match was made for same day
    if($demoMatch!==false) { return $demoMatch; }

    //No match made for SAME day, get files for PREV day of report date
    $patternDate = date('ymd', strtotime(date('d F Y', $rptDate).' -1 day'));
    $pattern = "{$demoFldr}{$patternDate}??????.{$fileExt}";
    $matches = glob($pattern);
    //Return result (either file from prev day or false)
    return prevDemoFile($reportDateTime, $matches, $demoFldr);
}

function prevDemoFile($reportDateTime, $demoFilesAry, $demoFldr)
{
    //Create vars for the output
    $prevDemoFile = false;
    $smallestDiff = false;
    
    //Find the closest demo file before the report date
    foreach($demoFilesAry as $demoFile)
    {
        $fileDateTime = substr($demoFile, strlen($demoFldr), 12);
        if( $fileDateTime<$reportDateTime)
        {
            if(!$smallestDiff || ($reportDateTime-$fileDateTime)<$smallestDiff)
            {
                $smallestDiff = ($reportDateTime-$fileDateTime);
                $prevDemoFile = $demoFile;
            }
        }
    }
    return $prevDemoFile;
}

//Usage
$demofile = getDemoFile($report_date);

?>

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.