=item new()
Constructor. WHen we create a new instance, we check for
a way to restore previous values, otherwise we use defaults.
The weather daemon relies on values from the centralized
time daemon. This should be passed into the constructor.
=cut
sub new {
my $class = shift;
my $time_daemon = shift;
my $self = {};
die "Cannot initialize weather system without a valid time system!" unless $time_daemon->isa('Mud::GameTime');
log_boot "- Resetting game weather";
if( -r $filename ) {
my $data = load_file $filename;
$self = decode_json $data;
$self->{_time_daemon} = $time_daemon;
} else {
$self = {
_time_daemon => $time_daemon,
_pressure => 960,
_change => 0,
_sky => 'clear',
_wind_speed => 0,
_wind_direction => 'calm',
_category_shift => 0,
};
}
bless $self, $class;
$self->update();
return $self;
}
=item TO_JSON()
This method is called by the JSON package to convert a blessed reference
into a normal hashref that can be encoded directly with JSON. We take
advantage of this to omit fields we do not want to save.
=cut
sub TO_JSON {
my $self = shift;
my $obj = {};
$obj->{$_} = $self->{$_} foreach grep { ! /^(_time_daemon)$/ } (keys %$self);
return $obj;
}
=item DESTROY()
A special function called when the weather object is destroyed.
Normally this will happen during shutdown. This code should save
the weather state back to disk.
=cut
sub DESTROY {
my $self = shift;
log_info "Saving weather data to $filename";
my $json = JSON->new->allow_blessed(1)->convert_blessed(1);
my $data = $json->encode($self) or die "Invalid JSON conversion: $!";
open FP, ">$filename" or die "Cannot open $filename: $!";
print FP "$data\n";
close FP;
log_warn "Weather daemon shut down.";
}
=item load_file()
A simple routine to read in a text file from disk and
return the result as a string. This is trivial, but it is
more convenient to use 1 line of code elsewhere than 3 or 4.
=cut
sub load_file {
my $filename = shift;
return undef if !defined $filename or !-r $filename;
my $data = '';
open FP, "<$filename" or die "Cannot open $filename: $!";
while(<FP>) {
$data .= $_;
}
close FP;
return $data;
}
{"_sky":"stormy","_change":0,"_wind_direction":"calm","_pressure":988,"_wind_speed":0,"_category_shift":0}
def name_lookup(dict, arg, key='name'):
for i, n in dict.items():
if n.__dict__[key] == arg:
return i
attack = name_lookup(attack_table, "bite")
position = name_lookup(position_table, "stand", "short_name")
def prefix_lookup(dict, arg):
if not arg:
return None
results = [v for k,v in dict.items() if k.startswith(arg)]
if results:
return results[0]
return None
skill = prefix_lookup(skill_table, argument)
def value_lookup(dict, arg):
if not arg:
return None
for k,v in dict.items():
if v == arg:
return k
return None
def mass_replace(str, dict):
for k,v in dict.items():
if v:
str = str.replace(k,v)
return str
format = "$n, $e, $m, $s"
act_trans = {}
act_trans['$n'] = "Me"
act_trans['$e'] = "He"
act_trans['$m'] = "Him"
act_trans['$s'] = "His"
format = mass_replace(format, act_trans)
def value_lookup(dict, arg):
if not arg:
return None
for k,v in dict.items():
if v == arg:
return k
return None
def value_lookup(dict, arg):
if not arg:
return None
return dict.keys()[dict.values().index(arg)]
Are you going to keep the old area formats, or do something clever like have it read/write JSON file formats natively? I've been pondering rewriting my own DikuMUD in another language (maybe perl!), and think I'd rather make the old C code emit JSON formatted files so the newer version stays lean and doesn't have to hold onto so much legacy stuff.
Once I get it translated I'm not all to sure what I'm going to do. Saving areas isn't part of stock ROM fortunately, so all I had to deal with was the loading. It's pretty slow compared to it's C counter part so a change is probably necessary. I'd like to see it tied into Django. That'd be pretty cool. We'll see. There's a lot more commands to translate haha.