aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterWebInterface/Handler/Json/Motd.pm
blob: 15c57a6e5562703401101f2b0d5a644e3053da67 (plain)
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
package MasterWebInterface::Handler::Json::Motd;

use strict;
use utf8;
use JSON;
use TUWF ':html';
use Exporter 'import';
our @EXPORT = qw| motd_static |;

TUWF::register(
    qr{json/([\w]{1,20})/motd} => \&json_motd,
);

# Message of the Day for things like the JSON API or updateserver page
sub motd_static 
{
    my ($self, $gamedesc) = @_;
    return "<h1>$gamedesc</h1><p>Thank you for using the $self->{site_name} masterserver. For more information, visit <a href=\"$self->{site_url}\">$self->{site_url}</a>.</p>";
}

# MOTD for json api
sub json_motd
{
    my ($self, $gamename) = @_;
    
    # gamename defined
    my $gn_desc = $self->dbGetGameDesc($gamename) || $gamename;
    my $html = $self->motd_static($gn_desc);
    
    # get numServers
    my ($l,$x,$s) = $self->dbServerListGet(
        gamename => $gamename, 
        updated  => $self->{window_time},
        limit => 9999,
        sort     => "numplayers", 
        reverse  => 1,
    );
    
    my $p = 0;
    for (@{$l}) 
    {
        $p += $_->{numplayers};
        last unless $_->{numplayers}
    }
    
    # response as json data
    $self->resHeader("Access-Control-Allow-Origin", "*");
    $self->resJSON([
        {motd => $html}, 
        {total => $s, players => $p}
    ]);

}

1;