aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterWebInterface/Handler/Json/Motd.pm
blob: c473f12657b6f66772e8f447a14fae914624c980 (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
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, 
        results  => 100,
    );
    
    my $p = 0;
    for (@{$l}) 
    {
        $p += $_->{numplayers}
    }
    
    # return json data as the response
    my $json_data = encode_json [{motd => $html}, {total => $s, players => $p}];
    print { $self->resFd() } $json_data;
    
    # set content type and allow off-domain access (for example jQuery)
    $self->resHeader("Access-Control-Allow-Origin", "*");
    $self->resHeader("Content-Type", "application/json; charset=UTF-8");
}

1;