1 |
root |
1.1 |
#! perl |
2 |
|
|
|
3 |
|
|
# this extension swaps out maps and resets them, in essence managing |
4 |
|
|
# the reset/swap policy of the server. |
5 |
|
|
|
6 |
root |
1.7 |
our $DEACTIVATE_TIMEOUT = 20; # number of seconds after which maps get deactivated to save cpu |
7 |
root |
1.10 |
our $SWAP_TIMEOUT = 35; # number of seconds after which maps inactive get swapped out |
8 |
root |
1.7 |
our $SCHEDULE_INTERVAL = 2; # time the map scheduler sleeps between runs |
9 |
root |
1.10 |
our $SAVE_TIMEOUT = 20; # save maps every n seconds |
10 |
|
|
our $SAVE_INTERVAL = 0.1; # save at max. one map every $SAVE_INTERVAL |
11 |
root |
1.1 |
|
12 |
|
|
our $SCHEDULER = cf::coro { |
13 |
|
|
while () { |
14 |
|
|
Coro::Timer::sleep $SCHEDULE_INTERVAL; |
15 |
|
|
|
16 |
root |
1.9 |
# this weird form of iteration over values is used because |
17 |
|
|
# the hash changes underneath us frequently, and for |
18 |
|
|
# keeps a direct reference to the value without (in 5.8 perls) |
19 |
|
|
# keeping a reference, so this is prone to crashes or worse. |
20 |
root |
1.8 |
my @maps = keys %cf::MAP; |
21 |
|
|
for (@maps) { |
22 |
|
|
my $map = $cf::MAP{$_} |
23 |
|
|
or next; |
24 |
|
|
$map->valid or next; |
25 |
root |
1.9 |
|
26 |
root |
1.1 |
eval { |
27 |
|
|
# not yet, because maps might become visible to players nearby |
28 |
root |
1.3 |
# we need to remove the map from %cf::MAP and all tiled map links |
29 |
root |
1.1 |
# if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) { |
30 |
|
|
# $map->deactivate; |
31 |
|
|
# delete $map->{active}; |
32 |
|
|
# } |
33 |
|
|
if ($map->should_reset) { |
34 |
|
|
$map->reset; |
35 |
root |
1.3 |
} elsif ($map->in_memory == cf::MAP_IN_MEMORY) { |
36 |
root |
1.6 |
if ($map->last_access + $SWAP_TIMEOUT <= $cf::RUNTIME && !$map->players) { |
37 |
root |
1.3 |
$map->swap_out; |
38 |
|
|
Coro::Timer::sleep $SAVE_INTERVAL; |
39 |
|
|
} elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
40 |
|
|
$map->save; |
41 |
|
|
Coro::Timer::sleep $SAVE_INTERVAL; |
42 |
|
|
} |
43 |
root |
1.1 |
} |
44 |
|
|
}; |
45 |
|
|
warn $@ if $@; |
46 |
|
|
Coro::cede; |
47 |
root |
1.8 |
}; |
48 |
root |
1.1 |
} |
49 |
|
|
}; |
50 |
root |
1.5 |
|
51 |
root |
1.1 |
$SCHEDULER->prio (-2); |
52 |
|
|
|