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