From 3470e2605595bf52b3ba07bf0b3886e5a61d3e06 Mon Sep 17 00:00:00 2001 From: Darkelarious Date: Sun, 5 Sep 2021 20:35:44 +0200 Subject: first version of masterinterface --- lib/MasterWebInterface/Handler/Json/Motd.pm | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 lib/MasterWebInterface/Handler/Json/Motd.pm (limited to 'lib/MasterWebInterface/Handler/Json/Motd.pm') diff --git a/lib/MasterWebInterface/Handler/Json/Motd.pm b/lib/MasterWebInterface/Handler/Json/Motd.pm new file mode 100755 index 0000000..c473f12 --- /dev/null +++ b/lib/MasterWebInterface/Handler/Json/Motd.pm @@ -0,0 +1,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 "

$gamedesc

Thank you for using the $self->{site_name} masterserver. For more information, visit {site_url}\">$self->{site_url}.

"; +} + +# 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; -- cgit v1.2.3