Created
January 21, 2011 22:29
-
-
Save gavinblair/790555 to your computer and use it in GitHub Desktop.
script that periodically checks the minecraft server log, and tweets when players log in/out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//run via cron every 2 minutes | |
//Get the log file | |
$log = file_get_contents("/opt/minecraft/server.log"); | |
$log = explode("\n", $log); | |
$loggedinusers = array(); | |
//only look at lines that mention logging in or logging out | |
foreach($log as $line) { | |
if(strpos($line, "logged in with entity id")){ | |
//logged in | |
$parts = explode("[INFO] ", $line); | |
$parts = $parts[1]; | |
$parts = explode(" ", $parts); | |
$username = $parts[0]; | |
$loggedinusers[$username] = $username; | |
} else if (strpos($line, " lost connection: ")) { | |
//logged out | |
$parts = explode("[INFO] ", $line); | |
$parts = $parts[1]; | |
$parts = explode(" ", $parts); | |
$username = $parts[0]; | |
unset($loggedinusers[$username]); | |
} | |
} | |
//create users.txt if not exists | |
if(!file_exists("/opt/minecraft/users.txt")){ | |
$handle = fopen("/opt/minecraft/users.txt", "w"); | |
foreach($loggedinusers as $user) { | |
fwrite($handle, $user."\n"); | |
} | |
fclose($handle); | |
//don't bother tweeting, wait until next run | |
} else { | |
//or get the contents, same type of array as from server.log | |
$oldusers = file_get_contents("/opt/minecraft/users.txt"); | |
$oldusers = explode("\n", $oldusers); | |
$newusers = array(); | |
foreach($loggedinusers as $loggedinuser) { | |
//is this user in $oldusers? | |
if(!in_array($loggedinuser, $oldusers)) { | |
//new user! add to the new users array | |
$newusers[] = $loggedinuser; | |
} | |
} | |
$lostusers = array(); | |
foreach($oldusers as $olduser) { | |
//is this user in $loggedusers? | |
if(!in_array($olduser, $loggedinusers)) { | |
//lost a user. add to lost users array | |
$lostusers[] = $olduser; | |
} | |
} | |
//update users.txt if needed | |
if(count($newusers) || count($lostusers)) { | |
$handle = fopen("/opt/minecraft/users.txt", "w"); | |
foreach($loggedinusers as $user) { | |
fwrite($handle, $user."\n"); | |
} | |
fclose($handle); | |
//copy users.txt to the web accessible directory | |
copy("/opt/minecraft/users.txt", "/var/www/users.txt"); | |
} | |
//check to see if this mc username is also a twitter username | |
//if so, prepend a ".@" to the tweet | |
$headers = 'From: [email protected]' . "\r\n" . | |
'Reply-To: [email protected]' . "\r\n" . | |
'X-Mailer: PHP/' . phpversion(); | |
//tweet! | |
foreach($newusers as $user) { | |
if(strlen($user) > 0) { | |
$test = false; | |
@$test = file_get_contents("http://twitter.com/$user"); | |
if($test) { $dotat=".@"; } else { $dotat=""; } | |
$text = "$dotat$user has logged in! http://minecraft.net/skin/skin.jsp?user=$user#".time(); | |
echo $text."\n"; | |
echo mail("[email protected]", "tweet", $text, $headers); | |
} | |
} | |
foreach($lostusers as $user) { | |
if(strlen($user) > 0) { | |
@$test = file_get_contents("http://twitter.com/$user"); | |
if($test) { $dotat=".@"; } else { $dotat=""; } | |
$text = "$dotat$user has logged out. http://minecraft.net/skin/skin.jsp?user=$user#".time(); | |
echo $text."\n"; | |
echo mail("[email protected]", "tweet", $text, $headers); | |
} | |
} | |
} |
live user list: http://198.20.49.65:1337/users.txt
using hmod you can tweet when someone dies, etc. http://www.ohloh.net/p/hmod
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example log: http://198.20.49.65:1337/serverlog.txt