-
Notifications
You must be signed in to change notification settings - Fork 0
/
char.pl
69 lines (55 loc) · 1.63 KB
/
char.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use Config::INI::Reader;
use DBI;
use DBD::mysql;
use lib './lib';
use DataUnits;
use Vaffel;
our ($listener,$select,$last_tick,%clients);
our $config = Config::INI::Reader->read_file('config.ini');
require './subs/common.pl';
console_prefix('CHAR ') if $ENV{'vaffel_threads'};
console('Connecting to database' . ($config->{'MainDB'}->{'channel'} ? '' : 's') . '...');
our ($mdb,$cdb) = db_connect(1);
if (!$mdb || !$cdb) {
console('Could not connect to database!');
exit;
}
our $server = Vaffel->new(
'bind_address' => $config->{'Char'}->{'address'},
'bind_port' => $config->{'Char'}->{'port'},
'first_command' => 0x070B
);
$server->register_handler('start',\&event_start);
$server->register_handler('connect',\&event_connect);
$server->register_handler('disconnect',\&event_disconnect);
$server->register_handler('unknown',\&event_unknown);
console('Loading packet handlers...');
for (<./packets/char/*.pl>) {
my $filename = lc($_);
$filename =~ s/.*\/(.+)\..+/$1/i;
console(' ' . $filename);
do $_;
warn $@ if $@;
warn $! if $!;
}
console('Starting server...');
$server->run();
console('Server stopped!');
exit;
sub event_disconnect {
my ($handle) = @_;
console(who($handle) . ' disconnected. Time online: ' . (int(time()) - $clients{$handle}{'connected'}) . ' seconds.');
delete $clients{$handle};
}
sub event_tick {
return if $last_tick || $last_tick == time(); # Only tick once a second.
$last_tick = time();
if (!$mdb || !$cdb) {
console('Lost database connection!');
console('Reconnecting...');
db_connect();
}
}