Completed
Push — v3 ( 9aaf4b...6c721b )
by Austin
03:53
created

Ut3::deleteResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
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
/**
22
 * Unreal Tournament 3 Protocol Class
23
 *
24
 * Note: The response from UT3 appears to not be consistent.  Many times packets are incomplete or there are extra
25
 * "echoes" in the responses.  This may cause issues like odd characters showing up in the keys for the player and team
26
 * array responses. Not sure much can be done about it.
27
 *
28
 * @author Austin Bischoff <[email protected]>
29
 */
30
class Ut3 extends Gamespy3
31
{
32
33
    /**
34
     * String name of this protocol class
35
     *
36
     * @type string
37
     */
38
    protected $name = 'ut3';
39
40
    /**
41
     * Longer string name of this protocol class
42
     *
43
     * @type string
44
     */
45
    protected $name_long = "Unreal Tournament 3";
46
47
    /**
48
     * Normalize settings for this protocol
49
     *
50
     * @type array
51
     */
52
    protected $normalize = [
53
        // General
54
        'general' => [
55
            'dedicated'  => 'bIsDedicated',
56
            'hostname'   => 'hostname',
57
            'numplayers' => 'numplayers',
58
        ],
59
    ];
60
61
    /**
62
     * Overload the response process so we can make some changes
63
     *
64
     * @return array
65
     */
66 1
    public function processResponse()
67
    {
68
69
        // Grab the result from the parent
70
        /** @type array $result */
71 1
        $result = parent::processResponse();
72
73
        // Move some stuff around
74 1
        $this->renameResult($result, 'OwningPlayerName', 'hostname');
75 1
        $this->renameResult($result, 'p1073741825', 'mapname');
76 1
        $this->renameResult($result, 'p1073741826', 'gametype');
77 1
        $this->renameResult($result, 'p1073741827', 'servername');
78 1
        $this->renameResult($result, 'p1073741828', 'custom_mutators');
79 1
        $this->renameResult($result, 'gamemode', 'open');
80 1
        $this->renameResult($result, 's32779', 'gamemode');
81 1
        $this->renameResult($result, 's0', 'bot_skill');
82 1
        $this->renameResult($result, 's6', 'pure_server');
83 1
        $this->renameResult($result, 's7', 'password');
84 1
        $this->renameResult($result, 's8', 'vs_bots');
85 1
        $this->renameResult($result, 's10', 'force_respawn');
86 1
        $this->renameResult($result, 'p268435704', 'frag_limit');
87 1
        $this->renameResult($result, 'p268435705', 'time_limit');
88 1
        $this->renameResult($result, 'p268435703', 'numbots');
89 1
        $this->renameResult($result, 'p268435717', 'stock_mutators');
90
91
        // Put custom mutators into an array
92 1
        if (isset($result['custom_mutators'])) {
93 1
            $result['custom_mutators'] = explode("\x1c", $result['custom_mutators']);
94
        }
95
96
        // Delete some unknown stuff
97 1
        $this->deleteResult($result, ['s1', 's9', 's11', 's12', 's13', 's14']);
98
99
        // Return the result
100 1
        return $result;
101
    }
102
103
    /**
104
     * Dirty hack to rename result entries into something more useful
105
     *
106
     * @param array  $result
107
     * @param string $old
108
     * @param string $new
109
     */
110 1
    protected function renameResult(array &$result, $old, $new)
111
    {
112
113
        // Check to see if the old item is there
114 1
        if (isset($result[$old])) {
115 1
            $result[$new] = $result[$old];
116 1
            unset($result[$old]);
117
        }
118 1
    }
119
120
    /**
121
     * Dirty hack to delete result items
122
     *
123
     * @param array $result
124
     * @param array $array
125
     */
126 1
    protected function deleteResult(array &$result, array $array)
127
    {
128
129 1
        foreach ($array as $key) {
130 1
            unset($result[$key]);
131
        }
132 1
    }
133
}
134