[refactory] use map refactory command run

This commit is contained in:
方而静 2021-03-26 14:04:53 +08:00
parent ffa722a312
commit 334bc0c89f
No known key found for this signature in database
GPG Key ID: C4F2853BC3103681
3 changed files with 65 additions and 45 deletions

View File

@ -1,6 +1,6 @@
{
"name": "wschat-ng",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "index.js",
"scripts": {

View File

@ -62,28 +62,29 @@ export function run_command(cmd_raw: string, uid: string, users: Map<string, Use
const cmd_set = cmd.split(/\s+/);
const msg = cmd.replace(/^[^\n]+\n/m, '');
if (cmd.startsWith('/disconnect')) {
const command_map: Map<string, (prefix: string) => void> = new Map();
const admin_command_map: Map<string, (prefix: string) => void> = new Map();
command_map.set('disconnect', () => {
command_reply('Diconnecting. Bye!',);
socket.disconnect();
return;
}
});
if (cmd.startsWith('/join')) {
command_map.set('join', () => {
const room_name = cmd_set[1];
set_room(room_name);
const masked_name = mask_room_name(room_name);
socket.join(masked_name);
command_reply('OK.');
return;
}
});
if (cmd.startsWith('/ls')) {
if (cmd === '/ls') {
command_map.set('ls', (prefix: string) => {
if (cmd === prefix) {
command_reply(JSON.stringify(all_rooms()));
return;
}
const filter_string = cmd.substring('/ls'.length);
const filter_string = cmd.substring(prefix.length);
const target = safe_find_user(filter_string)[0];
if (target === undefined) {
@ -97,10 +98,9 @@ export function run_command(cmd_raw: string, uid: string, users: Map<string, Use
.map(x => unmask_room_name(x))
.filter(x => x !== null)
));
return;
}
});
if (cmd.startsWith('/rename')) {
command_map.set('rename', () => {
const new_name = cmd_set[1].toString();
if (validate_username(new_name)) {
kill_username(user.name);
@ -110,20 +110,18 @@ export function run_command(cmd_raw: string, uid: string, users: Map<string, Use
} else {
command_reply('Failed to rename.');
}
return;
}
});
if (cmd.startsWith('/whoami')) {
command_map.set('whoami', () => {
command_reply(JSON.stringify({
name: user.name,
id: user.id,
is_administrator: user.is_administrator,
}));
return;
}
});
if (cmd.startsWith('/ps')) {
if (cmd === '/ps') {
command_map.set('ps', (prefix: string) => {
if (cmd === prefix) {
command_reply(JSON.stringify(Array
.from(users)
.map(x => x[1].name)
@ -131,45 +129,38 @@ export function run_command(cmd_raw: string, uid: string, users: Map<string, Use
return;
}
const filter_string = cmd.substring('/ps'.length);
const filter_string = cmd.substring(prefix.length);
const checked_user = safe_find_user(filter_string);
command_reply(JSON.stringify(Array
.from(users)
.filter(([id,]) => checked_user.includes(id))
.map(x => x[1].name)
));
return;
}
});
if (cmd.startsWith('/su')) {
command_map.set('su', () => {
const code = cmd_set[1];
if (grant_access(user, code)) { command_reply('You are administartor now.'); }
else { command_reply('Code incorrect.'); }
return;
}
});
if (cmd.startsWith('/resign')) {
command_map.set('resign', () => {
user.is_administrator = false;
command_reply('OK.');
return;
}
});
if (cmd.startsWith('/filter')) {
const filter_string = cmd.substring('/filter'.length);
command_map.set('filter', (prefix: string) => {
const filter_string = cmd.substring(prefix.length);
command_reply(JSON.stringify(safe_find_user(filter_string)));
return;
}
});
if (cmd.startsWith('/whois')) {
command_map.set('whois', () => {
const id = cmd_set[1];
command_reply(users.get(id).name);
return;
}
});
if (cmd.startsWith('/kill')) {
if (!request_administrator_access()) { return; }
const filter_string = cmd.substring('/kill'.length);
admin_command_map.set('kill', (prefix: string) => {
const filter_string = cmd.substring(prefix.length);
const checked_user = safe_find_user(filter_string);
for (let id of checked_user) {
const target = users.get(id).socket;
@ -178,13 +169,35 @@ export function run_command(cmd_raw: string, uid: string, users: Map<string, Use
}
command_reply(JSON.stringify(checked_user));
}
});
if (cmd.startsWith('/anon')) {
command_map.set('anon', () => {
io.in(Array.from(socket.rooms)).emit('new message', {
type: 'text-message',
data: msg,
sender: 'Anonymous User',
});
});
if (user.is_administrator) {
for (let val of admin_command_map) {
const [prefix, executor] = val;
const command_prefix = '/' + prefix;
if (cmd.startsWith(command_prefix)) {
executor(command_prefix);
return;
}
};
}
for (let val of command_map) {
const [prefix, executor] = val;
const command_prefix = '/' + prefix;
if (cmd.startsWith(command_prefix)) {
executor(command_prefix);
return;
}
}
command_reply(`Bad command: "${cmd.match(/^\/[^\s]+/)[0]}".`);
};

7
tsconfig.json Normal file
View File

@ -0,0 +1,7 @@
{
"compilerOptions": {
"downlevelIteration": true,
"removeComments": true,
"target": "ES5"
},
}