Tibia   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 30
c 1
b 0
f 0
dl 0
loc 104
ccs 14
cts 14
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A processResponse() 0 33 5
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\Exception\Protocol as Exception;
22
use GameQ\Protocol;
23
use GameQ\Result;
24
25
/**
26
 * Tibia Protocol Class
27
 *
28
 * Tibia server query protocol class
29
 *
30
 * Credit to Ahmad Fatoum for providing Perl based querying as a roadmap
31
 *
32
 * @author  Yive <[email protected]>
33
 * @author  Austin Bischoff <[email protected]>
34
 */
35
class Tibia extends Protocol
36
{
37
    /**
38
     * Array of packets we want to query.
39
     *
40
     * @var array
41
     */
42
    protected $packets = [
43
        self::PACKET_STATUS => "\x06\x00\xFF\xFF\x69\x6E\x66\x6F",
44
    ];
45
46
    /**
47
     * The transport mode for this protocol is TCP
48
     *
49
     * @var string
50
     */
51
    protected $transport = self::TRANSPORT_TCP;
52
53
    /**
54
     * The query protocol used to make the call
55
     *
56
     * @var string
57
     */
58
    protected $protocol = 'tibia';
59
60
    /**
61
     * String name of this protocol class
62
     *
63
     * @var string
64
     */
65
    protected $name = 'tibia';
66
67
    /**
68
     * Longer string name of this protocol class
69
     *
70
     * @var string
71
     */
72
    protected $name_long = "Tibia";
73
74
    /**
75
     * The client join link
76
     *
77
     * @var string
78
     */
79
    protected $join_link = "otserv://%s/%d/";
80
81
    /**
82
     * Normalize settings for this protocol
83
     *
84
     * @var array
85
     */
86
    protected $normalize = [
87
        // General
88
        'general' => [
89
            // target       => source
90
            'dedicated'  => 'dedicated',
91
            'gametype'   => 'server',
92
            'hostname'   => 'servername',
93
            'motd'       => 'motd',
94
            'maxplayers' => 'players_max',
95
            'numplayers' => 'players_online',
96
            'map'        => 'map_name',
97
        ],
98
    ];
99
100
    /**
101
     * Process the response for the Tibia server
102
     *
103
     * @return array
104
     * @throws \GameQ\Exception\Protocol
105
     */
106 24
    public function processResponse()
107
    {
108
        // Merge the response packets
109 24
        $xmlString = implode('', $this->packets_response);
110
111
        // Check to make sure this is will decode into a valid XML Document
112 24
        if (($xmlDoc = @simplexml_load_string($xmlString)) === false) {
113 12
            throw new Exception(__METHOD__ . " Unable to load XML string.");
114
        }
115
116
        // Set the result to a new result instance
117 12
        $result = new Result();
118
119
        // All servers are dedicated as far as I can tell
120 12
        $result->add('dedicated', 1);
121
122
        // Iterate over the info
123 12
        foreach (['serverinfo', 'owner', 'map', 'npcs', 'monsters', 'players'] as $property) {
124 12
            foreach ($xmlDoc->{$property}->attributes() as $key => $value) {
125 12
                if (!in_array($property, ['serverinfo'])) {
126 12
                    $key = $property . '_' . $key;
127
                }
128
129
                // Add the result
130 12
                $result->add($key, (string)$value);
131
            }
132
        }
133
134 12
        $result->add("motd", (string)$xmlDoc->motd);
135
136 12
        unset($xmlDoc, $xmlDoc);
137
138 12
        return $result->fetch();
139
    }
140
}
141