Page 1 of 1
return last accurance
Posted: Tue Nov 16, 2004 1:51 am
by krumble
Hi all,
I am trying to make a script that will pull the last instance of a preg match off a game.log
withen a game i have created a game-script that will dump a string on the game.log.. i can find it with the php below.. but of corse it returns the first to last match.. what i need is for it to print on a log only the last time the line was found- plus the word end below the line.
<?php
$stdin = fopen("qconsole.log", "r");
while($line = fgets($stdin)) {
if (preg_match("/level.clientss\D.\D.\D/", $line))
echo $line;
}
?>
RESULTS
level.clientss = 8
level.clientss = 5
level.clientss = 3
level.clientss = 1
this is the code witch works.. and creates a .scr file.. i then exec the script in game.. (hence the need for an end) and it will print the last result on the game server but this scr will get to large after a day.
any help witch would teach me would be great .
best regards.
krumble
Posted: Tue Nov 16, 2004 3:11 am
by rehfeld
reads the file into an array by lines, then starts checking them starting at the end of the file, working towards beggingin. first match it finds, it stops(which should be the most recent)
Code: Select all
<?php
$lines = file('qconsole.log');
for ($i = count($lines); $i >= 0; $i--) {
if (false !== strpos($lines[$i], 'level.clientss')) {
$match = $lines[$i];
break;
}
}
?>
but if you dont need this file, and just need the most recent, you could keep a seperate file just for that. just keep using fwrite in 'w' mode each time a more recent entry is aquired. that way the file will only have the most recent and nothiing else
Posted: Tue Nov 16, 2004 4:12 am
by timvw
there is a program, tac (the opposite of cat) that returns the file contents from end to beginning.
Posted: Tue Nov 16, 2004 5:37 am
by krumble
tried the above php and recieved the following error
<br />
<b>Parse error</b>: parse error in <b>C:\==GAMES==\EA_GAMES\MOHAA\main\player.php</b> on line <b>6</b><br />
here is a snippet of the log and the line i need to capture.
RE_RegisterModel: Registration failed for 'models/player/american_army_n_fps.tik'
LOCALIZATION ERROR: 'BAR' does not have a localization entry
level.clientss = 1
RE_RegisterModel: Registration failed for 'models/player/american_army_n_fps.tik'
cache models/fx/fs_puddle.tik
^~^~^ Add the following line to the *_precache.scr map script:
cache models/fx/fs_sand.tik
^~^~^ Add the following line to the *_precache.scr map script:
cache models/fx/fs_snow.tik
level.clientss = 4
------- Sound End Registration -------
------- Sound End Registration Complete -------
----- Server Shutdown -----
level.clientss = 4 is what im trying to capture, with an end under it.
level.clientss = 4
end
i have set a .bat file to exec every 45 secounds so after a day the new .scr file created from my first script would be to long for the game to procces..
i thank you for your help on this.. i should spend the time to learn myself but after two days of trying i have come to the conclusion my brain is fried from game scripting.
Posted: Tue Nov 16, 2004 5:39 am
by josh
Instead of fopen you could use
file_get_contents() .... makes your code cleaner, then check out erag() or erag_replace() or something i think it loads all the matches into an array then from there you can go through the array backwards and take the first match
example or erag's array function stuff:
Code: Select all
<?php
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo "$regs[3].$regs[2].$regs[1]";
} else {
echo "Invalid date format: $date";
}
?>
It stores the matches in array $regs so the function can return the data in the right format
Posted: Tue Nov 16, 2004 8:43 am
by timvw
What exactly is cleaner? If you have logfiles of 100mb file_get_contents becomes a major pita
Posted: Tue Nov 16, 2004 9:38 am
by krumble
I'm sorry to be so stupid, but i have never written php before, basicly the first string i posted above took me two days to figure out. i got it to do almost what i wanted but it can get too big.
the secound post in this string seems to be what i need but wont work, it looked like it wants to work ... but is sick.
The examples below the first, also look promising... but i would not know how to edit them to fit my needs.
I hate asking others to finish my puzzle but i just cant.
oh wise ones please help humble me.
Posted: Tue Nov 16, 2004 11:38 am
by rehfeld
try mine again, i made a typo
Posted: Tue Nov 16, 2004 3:55 pm
by krumble
still getting the error.
<br />
<b>Notice</b>: Undefined offset: 460 in <b>C:\==GAMES==\EA_GAMES\MOHAA\main\player.php</b> on line <b>5</b><br />
thanks,, im still trying..
Posted: Tue Nov 16, 2004 6:27 pm
by rehfeld
krumble wrote:still getting the error.
<br />
<b>Notice</b>: Undefined offset: 460 in <b>C:\==GAMES==\EA_GAMES\MOHAA\main\player.php</b> on line <b>5</b><br />
thanks,, im still trying..
it works fine on my computer, u sure its not because you have modified it incorrectly or something like that?
Posted: Wed Nov 17, 2004 2:19 am
by krumble
here is a copy of what and how i'm doing.
.bat file contains.
C:/php/php.exe -q C:/==GAMES==/EA_GAMES/MOHAA/main/player.php <qconsole.log> clientss.log
player.php
<?php
$lines = file('qconsole.log');
for ($i = count($lines); $i >= 0; $i--) {
if (false !== strpos($lines[$i], 'level.clients')) {
$match = $lines[$i];
break;
}
}
?>
clientss.log (results)
<br />
<b>Notice</b>: Undefined offset: 161 in <b>C:\==GAMES==\EA_GAMES\MOHAA\main\player.php</b> on line <b>4</b><br />
the 161 is the line count of the test log...
the test log had 2 level.clients
LOCALIZATION ERROR: 'graffics..' does not have a localization entry
level.clients = 2
clientCommand: =lmao=[rct]{SB}Beerha21 : 19 : dmmessage 0 lol
^~^~^ Script Error: binary '+' applied to incompatible types 'NIL' and 'NIL'
level.clients = 0
^~^~^ Add the following line to the *_precache.scr map script:
cache models/static/cratelid2.tik
could there be a symbol within the log that errors your script??
thanks for your help.
would it help if i gave you a complete log sample??
Posted: Wed Nov 17, 2004 2:40 pm
by krumble
could it be my version of php .. think i'm running 4.4
krumble
Posted: Wed Nov 17, 2004 3:53 pm
by rehfeld
my mistake.
i didnt test it w/ error reporting E_ALL
Code: Select all
<?php
$lines = file('qconsole.log');
for ($i = count($lines) - 1; $i >= 0; $i--) {
if (false !== strpos($lines[$i], 'level.clientss')) {
$match = $lines[$i];
break;
}
}
?>
if lets say our log file had 4 lines
the issue was count would return 4, but the loopi was
checking for $lines[4], which didnt exist because the $lines array is 0 indexed, so we would only have $lines[3], 2 1, 0
i fixed it by making $i = count($lines) -1;
Posted: Wed Nov 17, 2004 4:33 pm
by josh
timvw wrote:
What exactly is cleaner? If you have logfiles of 100mb file_get_contents becomes a major pita
true.
I was just trying to help some one new to php by showing him a function that relates to what he is asking about
in a way
Posted: Sat Nov 20, 2004 1:08 am
by krumble
want to thank you for the help, got it working with very good results.
now on server 1 i can inform players how many are playing on server 3 and vs ... by exec the new client log in the game script,
only bug i have is that i can only exec once per map,,, once i exec or thread the file the game will not update the information again even if the numbers are changed.
i could make 4 new files and have it find a new one every 5 minutes... but either way the info could be 20 minutes old if it started at file one.... but file 4 was the one with the most recent info.
anyway. it works..
thanks so much.
Krumble
ps i tried the file_get_contents but got strange results.. i will play with it some more and get it working.