-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelloPlugin.php
More file actions
183 lines (153 loc) · 5.65 KB
/
HelloPlugin.php
File metadata and controls
183 lines (153 loc) · 5.65 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
// Example Dragonfly plugin implemented in PHP (client mode).
// Requires: pecl install grpc protobuf
//
// ✅ This works with the standard PECL gRPC extension.
// The plugin connects to the Dragonfly server as a gRPC client and exchanges
// bidirectional messages over the EventStream RPC.
require_once __DIR__ . '/../vendor/autoload.php';
use Df\Plugin\Action;
use Df\Plugin\ActionBatch;
use Df\Plugin\ChatMutation;
use Df\Plugin\CommandSpec;
use Df\Plugin\EventResult;
use Df\Plugin\EventSubscribe;
use Df\Plugin\EventType;
use Df\Plugin\PluginClient;
use Df\Plugin\PluginHello;
use Df\Plugin\PluginToHost;
use Df\Plugin\SendChatAction;
use Grpc\ChannelCredentials;
define('DF_PLUGIN_API_VERSION', 'v1');
$pluginId = getenv('DF_PLUGIN_ID') ?: 'php-plugin';
$serverAddress = getenv('DF_PLUGIN_SERVER_ADDRESS') ?: '127.0.0.1:50050';
fwrite(STDOUT, "[php] connecting to {$serverAddress}...\n");
$client = new PluginClient($serverAddress, [
'credentials' => ChannelCredentials::createInsecure(),
]);
$call = $client->EventStream();
fwrite(STDOUT, "[php] connected, sending handshake\n");
$hello = new PluginToHost();
$hello->setPluginId($pluginId);
$pluginHello = new PluginHello();
$pluginHello->setName('example-php');
$pluginHello->setVersion('0.1.0');
$pluginHello->setApiVersion(DF_PLUGIN_API_VERSION);
$command = new CommandSpec();
$command->setName('/cheers');
$command->setDescription('Send a toast from PHP');
$pluginHello->setCommands([$command]);
$hello->setHello($pluginHello);
$call->write($hello);
$subscribeMsg = new PluginToHost();
$subscribeMsg->setPluginId($pluginId);
$subscribe = new EventSubscribe();
$subscribe->setEvents([
EventType::PLAYER_JOIN,
EventType::COMMAND,
EventType::CHAT,
]);
$subscribeMsg->setSubscribe($subscribe);
$call->write($subscribeMsg);
try {
while (true) {
$message = $call->read();
if ($message === null) {
fwrite(STDOUT, "[php] stream ended by host\n");
break;
}
if ($message->hasHello()) {
$hostHello = $message->getHello();
fwrite(STDOUT, "[php] host hello api=" . $hostHello->getApiVersion() . "\n");
if ($hostHello->getApiVersion() !== DF_PLUGIN_API_VERSION) {
fwrite(STDOUT, "[php] WARNING: API version mismatch (host={$hostHello->getApiVersion()}, plugin=" . DF_PLUGIN_API_VERSION . ")\n");
}
continue;
}
if ($message->hasEvent()) {
$event = $message->getEvent();
$eventId = $event->getEventId();
if ($event->getType() === EventType::PLAYER_JOIN && $event->hasPlayerJoin()) {
acknowledgeEvent($call, $pluginId, $eventId);
continue;
}
if ($event->getType() === EventType::CHAT && $event->hasChat()) {
$chat = $event->getChat();
$text = $chat->getMessage();
if (stripos($text, 'spoiler') !== false) {
cancelEvent($call, $pluginId, $eventId);
continue;
}
if (str_starts_with($text, '!cheer ')) {
$mutation = new ChatMutation();
$mutation->setMessage('🥂 ' . substr($text, 7));
mutateChat($call, $pluginId, $eventId, $mutation);
continue;
}
acknowledgeEvent($call, $pluginId, $eventId);
continue;
}
if ($event->getType() === EventType::COMMAND && $event->hasCommand()) {
$commandEvent = $event->getCommand();
if ($commandEvent->getRaw() === '/cheers') {
$action = new Action();
$send = new SendChatAction();
$send->setTargetUuid($commandEvent->getPlayerUuid());
$send->setMessage('🍻 Cheers from the PHP plugin!');
$action->setSendChat($send);
$batch = new ActionBatch();
$batch->setActions([$action]);
$resp = new PluginToHost();
$resp->setPluginId($pluginId);
$resp->setActions($batch);
$call->write($resp);
}
acknowledgeEvent($call, $pluginId, $eventId);
continue;
}
acknowledgeEvent($call, $pluginId, $eventId);
continue;
}
if ($message->hasShutdown()) {
fwrite(STDOUT, "[php] shutdown received\n");
break;
}
}
} catch (\Throwable $e) {
fwrite(STDERR, "[php] error: {$e->getMessage()}\n");
fwrite(STDERR, $e->getTraceAsString() . "\n");
exit(1);
} finally {
$call->writesDone();
fwrite(STDOUT, "[php] connection closed\n");
}
function acknowledgeEvent($call, string $pluginId, string $eventId): void
{
$result = new EventResult();
$result->setEventId($eventId);
$result->setCancel(false);
$resp = new PluginToHost();
$resp->setPluginId($pluginId);
$resp->setEventResult($result);
$call->write($resp);
}
function cancelEvent($call, string $pluginId, string $eventId): void
{
$result = new EventResult();
$result->setEventId($eventId);
$result->setCancel(true);
$resp = new PluginToHost();
$resp->setPluginId($pluginId);
$resp->setEventResult($result);
$call->write($resp);
}
function mutateChat($call, string $pluginId, string $eventId, ChatMutation $mutation): void
{
$result = new EventResult();
$result->setEventId($eventId);
$result->setChat($mutation);
$resp = new PluginToHost();
$resp->setPluginId($pluginId);
$resp->setEventResult($result);
$call->write($resp);
}