Completed
Pull Request — v3 (#403)
by
unknown
03:55
created

Tibia::processResponse()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 36
c 0
b 0
f 0
ccs 0
cts 30
cp 0
rs 8.8571
cc 1
eloc 28
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Protocols;
20
21
use GameQ\Protocol;
22
use GameQ\Buffer;
23
use GameQ\Result;
24
use GameQ\Exception\Protocol as Exception;
25
26
/**
27
 * Tibia Protocol Class
28
 *
29
 * Tibia server query protocol class
30
 *
31
 * Credit to Ahmad Fatoum for providing Perl based querying as a roadmap
32
 *
33
 * @author Yive <[email protected]>
34
 */
35
class Tibia extends Protocol
36
{
37
38
    /**
39
     * Array of packets we want to query.
40
     *
41
     * @type array
42
     */
43
    protected $packets = [
44
        self::PACKET_STATUS => "\x06\x00\xFF\xFF\x69\x6E\x66\x6F",
45
    ];
46
47
    /**
48
     * The transport mode for this protocol is TCP
49
     *
50
     * @type string
51
     */
52
    protected $transport = self::TRANSPORT_TCP;
53
54
    /**
55
     * The query protocol used to make the call
56
     *
57
     * @type string
58
     */
59
    protected $protocol = 'tibia';
60
61
    /**
62
     * String name of this protocol class
63
     *
64
     * @type string
65
     */
66
    protected $name = 'tibia';
67
68
    /**
69
     * Longer string name of this protocol class
70
     *
71
     * @type string
72
     */
73
    protected $name_long = "Tibia";
74
75
    /**
76
     * The client join link
77
     *
78
     * @type string
79
     */
80
    protected $join_link = "otserv://%s/%d/";
81
82
    /**
83
     * Normalize settings for this protocol
84
     *
85
     * @type array
86
     */
87
    protected $normalize = [
88
        // General
89
        'general' => [
90
            // target       => source
91
            'dedicated'   => 'dedicated',
92
            'hostname'    => 'hostname',
93
            'motd'        => 'motd',
94
            'maxplayers'  => 'max_players',
95
            'numplayers'  => 'num_players',
96
            'peakplayers' => 'peak_players',
97
            'map'         => 'map_name'
98
        ],
99
    ];
100
101
    /**
102
     * Process the response for the Tibia server
103
     *
104
     * @return array
105
     * @throws \GameQ\Exception\Protocol
106
     */
107
    public function processResponse()
108
    {
109
        $parsed = json_decode(json_encode(simplexml_load_string($this->packets_response[0])),true);
110
        // Couldn't be bothered to deal with objects so just did a hacky conversion to arrays.
111
112
        // Set the result to a new result instance
113
        $result = new Result();
114
115
        $result->add('server_version', $parsed['serverinfo']['@attributes']['version']);
116
        $result->add('client_version', $parsed['serverinfo']['@attributes']['client']);
117
        $result->add('hostname', $parsed['serverinfo']['@attributes']['servername']);
118
        $result->add('location', $parsed['serverinfo']['@attributes']['location']);
119
        $result->add('server', $parsed['serverinfo']['@attributes']['server']); // Not really sure what this is for
120
        $result->add('uptime', $parsed['serverinfo']['@attributes']['uptime']);
121
        $result->add('port', $parsed['serverinfo']['@attributes']['port']);
122
        $result->add('ip', $parsed['serverinfo']['@attributes']['ip']);
123
        $result->add('url', $parsed['serverinfo']['@attributes']['url']);
124
        $result->add('owner_name', $parsed['owner']['@attributes']['name']);
125
        $result->add('owner_email', $parsed['owner']['@attributes']['email']);
126
        $result->add('map_author', $parsed['map']['@attributes']['author']);
127
        $result->add('map_height', $parsed['map']['@attributes']['height']);
128
        $result->add('map_width', $parsed['map']['@attributes']['width']);
129
        $result->add('map_name', $parsed['map']['@attributes']['name']);
130
        $result->add('npcs', $parsed['npcs']['@attributes']['total']);
131
        $result->add('monsters', $parsed['monsters']['@attributes']['total']);
132
        $result->add('num_players', $parsed['players']['@attributes']['online']);
133
        $result->add('peak_players', $parsed['players']['@attributes']['peak']);
134
        $result->add('max_players', $parsed['players']['@attributes']['max']);
135
        $result->add('version', $parsed['@attributes']['version']);
136
        $result->add('motd', $parsed['motd']);
137
        $result->add('dedicated', 1); // All servers are dedicated as far as I can tell
138
139
        unset($parsed);
140
141
        return $result->fetch();
142
    }
143
}
144