Skip to content

Instantly share code, notes, and snippets.

@VienosNotes
Created August 3, 2012 08:16
Show Gist options
  • Select an option

  • Save VienosNotes/3245725 to your computer and use it in GitHub Desktop.

Select an option

Save VienosNotes/3245725 to your computer and use it in GitHub Desktop.
マインクラフト廃人ランキング生成スクリプト
class User {
has Int $.count is rw;
has Int $.time is rw;
has DateTime $.lastlogin is rw;
method new () {
return self.bless(*, count => 1, time => 0);
}
}
role Minecraft::ServerLog::Actions {
method log_login ($/) {
my $name = $<username>.Str.chomp;
if self.users{$name} -> $user {
$user.count++;
} else {
self.users{$name} = User.new;
}
my $time = $<timestamp>;
my $dt = DateTime.new(year => $time<year>.Int, month => $time<month>.Int, day => $time<day>.Int,
hour => $time<hour>.Int, minute => $time<minute>.Int, second => $time<second>.Int);
self.users{$name}.lastlogin = $dt;
}
method log_logout ($/) {
my $name = $<username>.Str.chomp;
if self.users.exists($name) && self.users{$name}.lastlogin.defined {
my $time = $<timestamp>;
my $dt = DateTime.new(year => $time<year>.Int, month => $time<month>.Int, day => $time<day>.Int,
hour => $time<hour>.Int, minute => $time<minute>.Int, second => $time<second>.Int);
self.users{$name}.time += ($dt.Instant.Int - self.users{$name}.lastlogin.Instant.Int);
}
self.users{$name}.lastlogin = DateTime;
}
}
class Minecraft::ServerLog {
also does Minecraft::ServerLog::Actions;
has $.logfile;
has %.users is rw;
grammar Minecraft::ServerLog::Format {
rule timestamp {
$<year>=(\d ** 4) '-' $<month>=(\d ** 2) '-' $<day>=(\d ** 2) <.ws> $<hour>=(\d ** 2) ':' $<minute>=(\d ** 2) ':' $<second>=(\d ** 2)
}
rule ipaddress {
\d ** 1..3 \. \d ** 1..3 \. \d ** 1..3 \. \d ** 1..3 [':' \d ** 1..5]?
}
rule username {
<[0..9a..zA..Z_]>+
}
rule TOP {
<log_login> | <log_logout>
}
rule log_login {
<timestamp> <.ws> '[INFO]' <.ws> <username> <.ws> '[/' <ipaddress> ']' <.ws> 'logged in with' .*
}
rule log_logout {
<timestamp> <.ws> '[INFO]' <.ws> [<username>|'/' <ipaddress>] <.ws> ['[/' <ipaddress> ']']? <.ws> 'lost connection:' .*
}
}
method new (Str $filename where { $_.IO.f }) {
return self.bless(*, logfile => $filename);
}
method get_user_list {
my Int $now;
for $!logfile.IO.lines -> $line {
print "\rproccessing line " ~ $now++;
Minecraft::ServerLog::Format.parse($line, actions => self);
}
say "\nAnalyze completed...\n";
}
method login_count () {
for %!users.kv -> $name, $user {
say $name ~ " login " ~ $user.count ~ " times, for " ~ ($user.time / 3600) ~ " hours" ~
($user.lastlogin.defined ?? ", and playing now!" !! ".");
}
}
}
my $log = Minecraft::ServerLog.new("./server.log");
$log.get_user_list;
$log.login_count;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment