Completed
Push — v3 ( ee04c9...7c9364 )
by Austin
04:32
created

Tibia   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 107
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

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