summaryrefslogtreecommitdiffstats
path: root/modules-available/roomplanner/inc/simpleroom.inc.php
blob: b4d3e74477246205d0679f0305d7cb35b586fb86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php

class SimpleRoom extends Room
{

	const CLIENT_SIZE = 4;

	private $machines = [];

	private $bb = false;

	private $tutorIp = false;

	/** @var ?string */
	private $managerIp = false;

	public function __construct($row)
	{
		parent::__construct($row);
		$locationId = (int)$row['locationid'];
		$ret = Database::simpleQuery(
			'SELECT machineuuid, clientip, position FROM machine WHERE fixedlocationid = :locationid',
			['locationid' => $locationId]);

		foreach ($ret as $clientRow) {
			$position = json_decode($clientRow['position'], true);

			if ($position === false || !isset($position['gridRow']) || !isset($position['gridCol']))
				continue; // TODO: Remove entry/set to NULL?

			$rotation = 'north';
			if (preg_match('/(north|east|south|west)/', $position['itemlook'], $out)) {
				$rotation = $out[1];
			}
			$this->machines[] = array(
				'machineuuid' => $clientRow['machineuuid'],
				'clientip' => $clientRow['clientip'],
				'gridRow' => $position['gridRow'],
				'gridCol' => $position['gridCol'],
				'rotation' => $rotation,
			);
		}
		// Runmode info overrides IP given
		if (Module::isAvailable('runmode')) {
			$pc = RunMode::getForMode('roomplanner', $locationId, true);
			if (!empty($pc)) {
				$pc = array_pop($pc);
				$row['managerip'] = $pc['clientip'];
			}
		}
		if (!empty($row['managerip'])) {
			$this->managerIp = $row['managerip'];
		}
		if (!empty($row['tutorip'])) {
			$this->tutorIp = $row['tutorip'];
		}
	}

	public function machineCount(): int
	{
		return count($this->machines);
	}

	public function getSize(?int &$width, ?int &$height)
	{
		if (empty($this->machines)) {
			$width = $height = 0;
			return;
		}
		$minX = $minY = $maxX = $maxY = 0;
		$this->boundingBox($minX, $minY, $maxX, $maxY);
		// client's size that cannot be configured as of today
		$width = max($maxX - $minX + self::CLIENT_SIZE, 1);
		$height = max($maxY - $minY + self::CLIENT_SIZE, 1);
	}

	public function getIniClientSection(int &$i, int $offX = 0, int $offY = 0): string
	{
		/* output individual client positions, shift coordinates to requested position */
		$out = '';
		$minX = $minY = $maxX = $maxY = 0;
		$this->boundingBox($minX, $minY, $maxX, $maxY);
		foreach ($this->machines as $pos) {
			$i++;
			$out .= "client\\$i\\ip={$pos['clientip']}\n"
				. "client\\$i\\pos=@Point(" . ($pos['gridCol'] + $offX -$minX) . ' ' . ($pos['gridRow'] + $offY - $minY) . ")\n";
		}

		return $out;
	}

	public function getShiftedArray(int $offX = 0, int $offY = 0): ?array
	{
		/* output individual client positions, shift coordinates to requested position */
		$ret = [];
		$minX = $minY = $maxX = $maxY = 0;
		$this->boundingBox($minX, $minY, $maxX, $maxY);
		foreach ($this->machines as $pos) {
			$pos['gridCol'] += $offX - $minX;
			$pos['gridRow'] += $offY - $minY;
			$ret[] = $pos;
		}

		return $ret;
	}

	private function boundingBox(int &$minX, int &$minY, int &$maxX, int &$maxY): void
	{
		if ($this->bb !== false) {
			$minX = $this->bb[0];
			$minY = $this->bb[1];
			$maxX = $this->bb[2];
			$maxY = $this->bb[3];
		} else {
			$minX = $minY = PHP_INT_MAX; /* PHP_INT_MIN is only available since PHP 7 */
			$maxX = $maxY = ~PHP_INT_MAX;
			foreach ($this->machines as $pos) {
				$minX = min($minX, $pos['gridCol']);
				$maxX = max($maxX, $pos['gridCol']);
				$minY = min($minY, $pos['gridRow']);
				$maxY = max($maxY, $pos['gridRow']);
			}
			$this->bb = [$minX, $minY, $maxX, $maxY];
		}
	}

	public function getManagerIp()
	{
		return $this->managerIp;
	}

	public function getTutorIp()
	{
		return $this->tutorIp;
	}

	public function isLeaf(): bool
	{
		return true;
	}

	public function shouldSkip(): bool
	{
		return empty($this->machines);
	}

	protected function sanitize()
	{
		// Nothing
	}

}