forked from ppfun/pixelplanet
only send messages from public channels to APIWebSocket
send reply with public channels and their ids if APIWebSocket subs to chat add getflag apisocket request add yy flag for matrix
This commit is contained in:
parent
031de42878
commit
22d2ffabab
20
API.md
20
API.md
|
@ -19,9 +19,13 @@ send
|
||||||
|
|
||||||
```["sub", "chat"]```
|
```["sub", "chat"]```
|
||||||
|
|
||||||
|
you get a reply with the list of public channels you are now receiving messages of
|
||||||
|
|
||||||
|
```["chans", [id1, name1], [id2, name2], ...]```
|
||||||
|
|
||||||
All chat messages, except the once you send with `chat` or `mcchat`, will be sent to you in the form:
|
All chat messages, except the once you send with `chat` or `mcchat`, will be sent to you in the form:
|
||||||
|
|
||||||
```["msg", name, message, id, country, channelId]```
|
```["msg", name, uid, message, country, channelId]```
|
||||||
channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come.
|
channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come.
|
||||||
id is the user id
|
id is the user id
|
||||||
country is the [two-letter country code](https://www.nationsonline.org/oneworld/country_code_list.htm) in lowercase
|
country is the [two-letter country code](https://www.nationsonline.org/oneworld/country_code_list.htm) in lowercase
|
||||||
|
@ -42,6 +46,18 @@ send
|
||||||
|
|
||||||
All pixels (including your own) will be sent to you as typical binary packages
|
All pixels (including your own) will be sent to you as typical binary packages
|
||||||
|
|
||||||
|
### Get flag of User
|
||||||
|
|
||||||
|
send
|
||||||
|
|
||||||
|
```['getflag', userId]```
|
||||||
|
|
||||||
|
receive
|
||||||
|
|
||||||
|
```['flag', userId, flag]``
|
||||||
|
|
||||||
|
Flag is in lowercase two-letter country code and null if user doesn't exist or had no flag set yet
|
||||||
|
|
||||||
### Set Pixel
|
### Set Pixel
|
||||||
|
|
||||||
```[ "setpxl", minecraftid, ip, x, y, clr ]```
|
```[ "setpxl", minecraftid, ip, x, y, clr ]```
|
||||||
|
@ -64,7 +80,7 @@ coolDownSeconds is the added cooldown (negative if pixel couldn't be set because
|
||||||
|
|
||||||
### Send Chat Message
|
### Send Chat Message
|
||||||
|
|
||||||
```["chat", name, message, country, channelId]```
|
```["chat", name, userId, message, country, channelId]```
|
||||||
|
|
||||||
channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come.
|
channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come.
|
||||||
(messages with the name "info" will be displayed as red notifications in the chat window)
|
(messages with the name "info" will be displayed as red notifications in the chat window)
|
||||||
|
|
BIN
public/cf/yy.gif
Normal file
BIN
public/cf/yy.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 205 B |
|
@ -11,6 +11,8 @@
|
||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
import socketEvents from './SocketEvents';
|
import socketEvents from './SocketEvents';
|
||||||
|
import chatProvider from '../core/ChatProvider';
|
||||||
|
import RegUser from '../data/models/RegUser';
|
||||||
import { getIPFromRequest } from '../utils/ip';
|
import { getIPFromRequest } from '../utils/ip';
|
||||||
import { setPixelByCoords } from '../core/setPixel';
|
import { setPixelByCoords } from '../core/setPixel';
|
||||||
import logger from '../core/logger';
|
import logger from '../core/logger';
|
||||||
|
@ -95,9 +97,16 @@ class APISocketServer {
|
||||||
sendapi,
|
sendapi,
|
||||||
ws = null,
|
ws = null,
|
||||||
) {
|
) {
|
||||||
if (!sendapi) return;
|
if (!sendapi) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// send only messages from default and lang channels
|
||||||
|
if (!chatProvider.defaultChannels[channelId]
|
||||||
|
&& !chatProvider.langChannels[channelId]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const sendmsg = JSON.stringify(['msg', name, msg, country, channelId]);
|
const sendmsg = JSON.stringify(['msg', name, id, msg, country, channelId]);
|
||||||
this.wss.clients.forEach((client) => {
|
this.wss.clients.forEach((client) => {
|
||||||
if (client !== ws
|
if (client !== ws
|
||||||
&& client.subChat
|
&& client.subChat
|
||||||
|
@ -153,6 +162,41 @@ class APISocketServer {
|
||||||
this.broadcast(buffer, (client) => client.subPxl);
|
this.broadcast(buffer, (client) => client.subPxl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getPublicChannels() {
|
||||||
|
const chanReply = ['chans'];
|
||||||
|
const defaultChanKeys = Object.keys(chatProvider.defaultChannels);
|
||||||
|
const langChanKeys = Object.keys(chatProvider.langChannels);
|
||||||
|
for (let i = 0; i < defaultChanKeys.length; i += 1) {
|
||||||
|
const id = defaultChanKeys[i];
|
||||||
|
const [name] = chatProvider.defaultChannels[id];
|
||||||
|
chanReply.push([parseInt(id, 10), name]);
|
||||||
|
}
|
||||||
|
for (let i = 0; i < langChanKeys.length; i += 1) {
|
||||||
|
const name = langChanKeys[i];
|
||||||
|
const { id } = chatProvider.langChannels[name];
|
||||||
|
chanReply.push([id, name]);
|
||||||
|
}
|
||||||
|
return chanReply;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getFlagOfUser(userId) {
|
||||||
|
try {
|
||||||
|
const reguser = await RegUser.findOne({
|
||||||
|
attributes: ['flag'],
|
||||||
|
where: {
|
||||||
|
id: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!reguser) {
|
||||||
|
throw new Error('User not found');
|
||||||
|
}
|
||||||
|
return reguser.flag;
|
||||||
|
} catch {
|
||||||
|
logger.info(`Flag to user ${userId} could not be determined`);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
async onTextMessage(message, ws) {
|
async onTextMessage(message, ws) {
|
||||||
try {
|
try {
|
||||||
const packet = JSON.parse(message);
|
const packet = JSON.parse(message);
|
||||||
|
@ -165,14 +209,15 @@ class APISocketServer {
|
||||||
const even = packet[0];
|
const even = packet[0];
|
||||||
if (even === 'chat') {
|
if (even === 'chat') {
|
||||||
ws.subChat = true;
|
ws.subChat = true;
|
||||||
|
ws.send(JSON.stringify(APISocketServer.getPublicChannels()));
|
||||||
} else if (even === 'pxl') {
|
} else if (even === 'pxl') {
|
||||||
ws.subPxl = true;
|
ws.subPxl = true;
|
||||||
} else if (even === 'online') {
|
} else if (even === 'online') {
|
||||||
ws.subOnline = true;
|
ws.subOnline = true;
|
||||||
} else {
|
} else {
|
||||||
logger.info(`APISocket wanted to sub to unexisting ${command}`);
|
logger.info(`APISocket wanted to sub to unexisting ${even}`);
|
||||||
}
|
}
|
||||||
logger.info(`APISocket client subscribed to ${command}`);
|
logger.info(`APISocket client subscribed to ${even}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (command === 'setpxl') {
|
if (command === 'setpxl') {
|
||||||
|
@ -186,20 +231,17 @@ class APISocketServer {
|
||||||
// minecraftid support got removed
|
// minecraftid support got removed
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
logger.info(`APISocket message ${message}`);
|
|
||||||
if (command === 'chat') {
|
if (command === 'chat') {
|
||||||
const [name, msg, country, channelId] = packet;
|
const [name, id, msg, country, channelId] = packet;
|
||||||
|
const uid = id || 1;
|
||||||
/*
|
/*
|
||||||
* do not send message back up ws that sent it
|
* do not send message back up ws that sent it
|
||||||
* TODO: user id should not be hardcoded,
|
|
||||||
* consider it whenever this actually gets used and
|
|
||||||
* becomes an issue.
|
|
||||||
*/
|
*/
|
||||||
socketEvents.broadcastChatMessage(
|
socketEvents.broadcastChatMessage(
|
||||||
name,
|
name,
|
||||||
msg,
|
msg,
|
||||||
channelId,
|
channelId,
|
||||||
1,
|
uid,
|
||||||
country,
|
country,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
@ -207,15 +249,22 @@ class APISocketServer {
|
||||||
name,
|
name,
|
||||||
msg,
|
msg,
|
||||||
channelId,
|
channelId,
|
||||||
1,
|
uid,
|
||||||
country,
|
country,
|
||||||
true,
|
true,
|
||||||
ws,
|
ws,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (command === 'getflag') {
|
||||||
|
const [uid] = packet;
|
||||||
|
const flag = await APISocketServer.getFlagOfUser(uid);
|
||||||
|
ws.send(JSON.stringify(['flag', uid, flag]));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
logger.info(`Received unknown api-ws message ${message}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(`Got undecipherable api-ws message ${message}`);
|
logger.error(`Got undecipherable api-ws message ${message}, ${err}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user