ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.172 by root, Sun Jan 14 13:32:48 2007 UTC vs.
Revision 1.178 by root, Thu Jan 18 00:06:55 2007 UTC

8use Storable; 8use Storable;
9use Opcode; 9use Opcode;
10use Safe; 10use Safe;
11use Safe::Hole; 11use Safe::Hole;
12 12
13use Coro 3.3 (); 13use Coro 3.4 ();
14use Coro::Event; 14use Coro::Event;
15use Coro::Timer; 15use Coro::Timer;
16use Coro::Signal; 16use Coro::Signal;
17use Coro::Semaphore; 17use Coro::Semaphore;
18use Coro::AIO; 18use Coro::AIO;
1113 1113
1114use Fcntl; 1114use Fcntl;
1115use Coro::AIO; 1115use Coro::AIO;
1116 1116
1117use overload 1117use overload
1118 '""' => \&as_string; 1118 '""' => \&as_string,
1119 fallback => 1;
1119 1120
1120our $MAX_RESET = 3600; 1121our $MAX_RESET = 3600;
1121our $DEFAULT_RESET = 3000; 1122our $DEFAULT_RESET = 3000;
1122 1123
1123sub generate_random_map { 1124sub generate_random_map {
1124 my ($self, $rmp) = @_; 1125 my ($self, $rmp) = @_;
1125
1126 # mit "rum" bekleckern, nicht 1126 # mit "rum" bekleckern, nicht
1127 $self->_create_random_map ( 1127 $self->_create_random_map (
1128 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, 1128 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1129 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, 1129 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
1130 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, 1130 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1534 my ($self) = @_; 1534 my ($self) = @_;
1535 1535
1536 $self->reset_at <= $cf::RUNTIME 1536 $self->reset_at <= $cf::RUNTIME
1537} 1537}
1538 1538
1539sub rename {
1540 my ($self, $new_path) = @_;
1541
1542 normalise $new_path;
1543
1544 $self->unlink_save;
1545
1546 delete $cf::MAP{$self->path};
1547 $self->{path} = $new_path; $self->path ($self->{path});
1548 $cf::MAP{$self->path} = $self;
1549
1550 $self->save;
1551}
1552
1553sub reset { 1539sub reset {
1554 my ($self) = @_; 1540 my ($self) = @_;
1555 1541
1556 my $lock = cf::lock_acquire "map_data:$self->{path}"; 1542 my $lock = cf::lock_acquire "map_data:$self->{path}";
1557 1543
1574my $nuke_counter = "aaaa"; 1560my $nuke_counter = "aaaa";
1575 1561
1576sub nuke { 1562sub nuke {
1577 my ($self) = @_; 1563 my ($self) = @_;
1578 1564
1565 delete $cf::MAP{$self->path};
1566
1567 $self->unlink_save;
1568
1569 bless $self, "cf::map";
1570 delete $self->{deny_reset};
1579 $self->{deny_save} = 1; 1571 $self->{deny_save} = 1;
1580 $self->reset_timeout (1); 1572 $self->reset_timeout (1);
1581 $self->rename ("{nuke}/" . ($nuke_counter++)); 1573 $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++));
1574
1575 $cf::MAP{$self->path} = $self;
1576
1582 $self->reset; # polite request, might not happen 1577 $self->reset; # polite request, might not happen
1583} 1578}
1584 1579
1585=item cf::map::unique_maps 1580=item cf::map::unique_maps
1586 1581
1817 1812
1818sub parse_random_map_params { 1813sub parse_random_map_params {
1819 my ($spec) = @_; 1814 my ($spec) = @_;
1820 1815
1821 my $rmp = { # defaults 1816 my $rmp = { # defaults
1822 xsize => 10, 1817 xsize => -1,
1823 ysize => 10, 1818 ysize => -1,
1824 }; 1819 };
1825 1820
1826 for (split /\n/, $spec) { 1821 for (split /\n/, $spec) {
1827 my ($k, $v) = split /\s+/, $_, 2; 1822 my ($k, $v) = split /\s+/, $_, 2;
1828 1823
1850 1845
1851 $rmp->{random_seed} ||= $exit->random_seed; 1846 $rmp->{random_seed} ||= $exit->random_seed;
1852 1847
1853 my $data = cf::to_json $rmp; 1848 my $data = cf::to_json $rmp;
1854 my $md5 = Digest::MD5::md5_hex $data; 1849 my $md5 = Digest::MD5::md5_hex $data;
1850 my $meta = "$cf::RANDOM_MAPS/$md5.meta";
1855 1851
1856 if (my $fh = aio_open "$cf::RANDOM_MAPS/$md5.meta", O_WRONLY | O_CREAT, 0666) { 1852 if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) {
1857 aio_write $fh, 0, (length $data), $data, 0; 1853 aio_write $fh, 0, (length $data), $data, 0;
1854 undef $fh;
1855 aio_rename "$meta~", $meta;
1858 1856
1859 $exit->slaying ("?random/$md5"); 1857 $exit->slaying ("?random/$md5");
1860 $exit->msg (undef); 1858 $exit->msg (undef);
1861 } 1859 }
1862} 1860}
2232 2230
2233sub main { 2231sub main {
2234 # we must not ever block the main coroutine 2232 # we must not ever block the main coroutine
2235 local $Coro::idle = sub { 2233 local $Coro::idle = sub {
2236 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 2234 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
2235 (async {
2237 async { Event::one_event }; 2236 Event::one_event;
2237 })->prio (Coro::PRIO_MAX);
2238 }; 2238 };
2239 2239
2240 cfg_load; 2240 cfg_load;
2241 db_load; 2241 db_load;
2242 load_extensions; 2242 load_extensions;
2390 } 2390 }
2391 2391
2392 warn "reloaded"; 2392 warn "reloaded";
2393}; 2393};
2394 2394
2395our $RELOAD_WATCHER; # used only during reload
2396
2395register_command "reload" => sub { 2397register_command "reload" => sub {
2396 my ($who, $arg) = @_; 2398 my ($who, $arg) = @_;
2397 2399
2398 if ($who->flag (FLAG_WIZ)) { 2400 if ($who->flag (FLAG_WIZ)) {
2399 $who->message ("start of reload."); 2401 $who->message ("reloading server.");
2402
2403 # doing reload synchronously and two reloads happen back-to-back,
2404 # coro crashes during coro_state_free->destroy here.
2405
2406 $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub {
2400 reload; 2407 reload;
2401 $who->message ("end of reload."); 2408 undef $RELOAD_WATCHER;
2409 });
2402 } 2410 }
2403}; 2411};
2404 2412
2405unshift @INC, $LIBDIR; 2413unshift @INC, $LIBDIR;
2406 2414

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines