| 265 | } |
| 266 | |
| 267 | JSValue ScNetwork::sendMessage(JSContext* ctx, JSValue thisVal, int argc, JSValue* argv) |
| 268 | { |
| 269 | #ifndef DISABLE_NETWORK |
| 270 | JS_UNPACK_STR(message, ctx, argv[0]); |
| 271 | JSValue players = argv[1]; |
| 272 | |
| 273 | if (JS_IsArray(players)) |
| 274 | { |
| 275 | if (Network::GetMode() == Network::Mode::server) |
| 276 | { |
| 277 | std::vector<uint8_t> playerIds; |
| 278 | JSIterateArray(ctx, players, [&playerIds](JSContext* ctx2, JSValue val) { |
| 279 | playerIds.push_back(static_cast<uint8_t>(JSToInt(ctx2, val))); |
| 280 | }); |
| 281 | if (!playerIds.empty()) |
| 282 | { |
| 283 | Network::SendChat(message.c_str(), playerIds); |
| 284 | } |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | JS_ThrowPlainError(ctx, "Only servers can send private messages."); |
| 289 | return JS_EXCEPTION; |
| 290 | } |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | Network::SendChat(message.c_str()); |
| 295 | } |
| 296 | #endif |
| 297 | return JS_UNDEFINED; |
| 298 | } |
| 299 | |
| 300 | #ifndef DISABLE_NETWORK |
| 301 | JSValue ScNetwork::createListener(JSContext* ctx, JSValue thisVal, int argc, JSValue* argv) |