-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logfile.pm
executable file
·254 lines (215 loc) · 7.58 KB
/
Logfile.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
###############################################################################
# Copyright (c) 2006 Shawn M Moore #
# #
# Permission is hereby granted, free of charge, to any person obtaining a #
# copy of this software and associated documentation files (the "Software"), #
# to deal in the Software without restriction, including without limitation #
# the rights to use, copy, modify, merge, publish, distribute, sublicense, #
# and/or sell copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
###############################################################################
# Version: 1.2.1 [16:23 05 May 06]
# This module's purpose is to get the logfile from a file and put it into an
# array of hashes. That's all. The return value of parse_file is an array of
# hashes sorted in the order that the games appear in the logfile. To see what
# goes into the hash, check near the end of this file.
# This module will work with or without the turns-in-logfile patch. If the
# turn count is undetected, a value of zero is used. I would've preferred to
# use '?' but I figure we're going to be doing everything with turns in numeric
# context. No sense in getting hammered by warnings about the conversion.
# TODO:
# * An alternate function/module to parse AardvarkJoe's xlogfiles.
# Warning: Unless you're running a high-end computer, I wouldn't recommend
# trying to fit huge logfiles through parse_file. Use parse_line if
# possible. nethack.alt.org's multimegabyte logfile grinds my computer
# to a halt.
package Logfile;
use strict;
use warnings;
my $logline = qr/^(\d\.\d\.\d) (\d+) (\d+) (-?\d+) (-?\d+) (-?\d+) (-?\d+) (\d+) (\d{8}) (\d{8}) (\d+) ([A-Z][a-z]{2}) ([A-Z][a-z]{2}) ([A-Z][a-z]{2}) ([A-Z][a-z]{2}) ([^,]+),(.+?)(?: {(\d+)})?$/;
{
# Let's try to guess what the game ID and ascension number are.
# The caller can, of course, overwrite these values if he's doing his own thing.
my $id = 0;
my $ascension = 0;
# We specifically reset when we run parse_contents or parse_file. User can do so as well.
sub reset_numbers
{
$id = 0;
$ascension = 0;
}
sub parse_line
{
my $line = shift;
my $basic = shift || 0;
my %game = ();
chomp $line;
my ($version, $score, $dnum, $curdlvl, $maxdlvl, $curhp, $maxhp, $deaths,
$enddate, $startdate, $user, $role, $race, $gender, $align, $name, $death,
$turns)
= $line =~ $logline;
defined $death or die "Unable to parse the following logfile line: $line";
++$id;
$turns = 0 unless defined $turns;
if (!$basic)
{
my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
my ($lifesaved, $dchar, $dungeon, $bestdlvl, $ascended, $quit, $crga, $start, $end);
$ascended = ($death eq 'ascended') ? 1 : 0;
$quit = ($death eq 'quit' || $death eq 'quit (with the Amulet)' || $death eq 'escaped') ? 1 : 0;
# Assume the player died, then correct it if he didn't.
$lifesaved = $deaths - 1;
$lifesaved++ if $ascended || $quit || $death eq 'a trickery' || $death eq 'escaped (in celestial disgrace)';
++$ascension if $ascended;
# bestdlvl is the lowest level reached, or one of the Planes (but not Heaven)
$bestdlvl = $maxdlvl;
$bestdlvl = $curdlvl if $curdlvl < 0 && $curdlvl > -6;
# "20051113" becomes "13 Nov 05"
$start = substr($startdate, -2) . ' ' . $months[substr($startdate, -4, 2) - 1] . ' ' . substr($startdate, 2, 2);
$end = substr($enddate, -2) . ' ' . $months[substr($enddate, -4, 2) - 1] . ' ' . substr($enddate, 2, 2);
$crga = "$role $race $gender $align";
if ($dnum == 0)
{
$dchar = 'D';
$dungeon = 'The Dungeons of Doom';
}
elsif ($dnum == 1)
{
$dchar = 'H'; # (Hell) -- G is too easily confused with the Gnomish Mines
$dungeon = 'Gehennom';
}
elsif ($dnum == 2)
{
$dchar = 'M';
$dungeon = 'The Gnomish Mines';
}
elsif ($dnum == 3)
{
$dchar = 'Q';
$dungeon = 'The Quest';
}
elsif ($dnum == 4)
{
$dchar = 'S';
$dungeon = 'Sokoban';
}
elsif ($dnum == 5)
{
$dchar = 'L';
$dungeon = 'Fort Ludios';
}
elsif ($dnum == 6)
{
$dchar = 'V';
$dungeon = 'Vlad\'s Tower';
}
elsif ($dnum == 7)
{
$dchar = 'P';
if ($curdlvl == -1)
{
$dungeon = 'The Plane of Earth';
}
elsif ($curdlvl == -2)
{
$dungeon = 'The Plane of Air';
}
elsif ($curdlvl == -3)
{
$dungeon = 'The Plane of Fire';
}
elsif ($curdlvl == -4)
{
$dungeon = 'The Plane of Water';
}
elsif ($curdlvl == -5)
{
$dungeon = 'The Astral Plane';
}
}
%game =
(
crga => $crga,
bestdlvl => $bestdlvl,
dchar => $dchar,
dungeon => $dungeon,
lifesaved => $lifesaved,
ascended => $ascended,
ascension => $ascended ? $ascension : 0,
quit => $quit,
start => $start,
end => $end,
);
}
%game =
(
# the logfile line itself, without \n
raw => $line,
# fields grabbed directly from the logfile
version => $version,
score => $score,
dnum => $dnum,
curdlvl => $curdlvl,
maxdlvl => $maxdlvl,
curhp => $curhp,
maxhp => $maxhp,
deaths => $deaths,
enddate => $enddate,
startdate => $startdate,
user => $user,
role => $role,
race => $race,
gender => $gender,
align => $align,
name => $name,
death => $death,
turns => $turns,
# everything else
id => $id,
%game,
);
return %game;
}
}
sub parse_contents
{
my $logfile = shift;
my $basic = shift;
my @lines = split(/\n/, $logfile);
my @games = ();
reset_numbers;
foreach my $line (@lines)
{
my %game = parse_line($line, $basic);
push @games, \%game;
}
return @games;
}
sub parse_file
{
my $logfile = shift || 'logfile';
my $basic = shift;
my @games = ();
my $line;
reset_numbers;
open (LOGFILE, '<', $logfile) or die "unable to open $logfile: $!";
while ($line = <LOGFILE>)
{
my %game = parse_line($line, $basic);
push @games, \%game;
}
close LOGFILE;
return @games;
}
1;