Differential D2412 Diff 10323 binaries/data/mods/public/gui/lobby/LobbyPage/Chat/ChatCommandHandler.js
Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/lobby/LobbyPage/Chat/ChatCommandHandler.js
- This file was added.
Property | Old Value | New Value |
---|---|---|
svn:eol-style | null | native \ No newline at end of property |
/** | |||||
* The purpose of this class is to test if a given textual input of the current player | |||||
* is not a chat message to be sent but a command to be performed locally or on the | |||||
* server, and if so perform it. | |||||
*/ | |||||
class ChatCommandHandler | |||||
{ | |||||
constructor(chatMessagesPanel) | |||||
{ | |||||
this.chatMessagesPanel = chatMessagesPanel; | |||||
} | |||||
/** | |||||
* @returns {boolean} true if the input was successfully parsed as a chat command. | |||||
*/ | |||||
handleChatCommand(text) | |||||
{ | |||||
if (!text.startsWith('/')) | |||||
return true; | |||||
let [command, args] = text.substr(1).split(" ", 1); | |||||
let commandObj = this.ChatCommands[command] || undefined; | |||||
let handler; | |||||
if (!commandObj) | |||||
{ | |||||
this.chatMessagesPanel.addChatMessage({ | |||||
"from": "system", | |||||
"text": sprintf( | |||||
translate("The command '%(cmd)s' is not supported."), { | |||||
"cmd": setStringTags(command, this.ChatCommandTags) | |||||
}) | |||||
}); | |||||
this.chatMessagesPanel.flushMessages(); | |||||
} | |||||
else if (commandObj.moderatorOnly && Engine.LobbyGetPlayerRole(g_Username) != "moderator") | |||||
{ | |||||
this.chatMessagesPanel.addChatMessage({ | |||||
"from": "system", | |||||
Freagarach: Leaving us in tension ;) | |||||
"text": sprintf( | |||||
translate("The command '%(cmd)s' is restricted to moderators."), { | |||||
"cmd": setStringTags(command, this.ChatCommandTags) | |||||
}) | |||||
}); | |||||
this.chatMessagesPanel.flushMessages(); | |||||
} | |||||
else | |||||
{ | |||||
handler = commandObj && commandObj.handler || undefined; | |||||
if (handler) | |||||
handler.call(this, args); | |||||
} | |||||
return !!handler; | |||||
} | |||||
} | |||||
/** | |||||
* Color to highlight chat commands in the explanation. | |||||
*/ | |||||
ChatCommandHandler.prototype.ChatCommandTags = { "color": "200 200 255" }; | |||||
/** | |||||
* Commands that can be entered by clients via chat input. | |||||
* A handler returns true if the user input should be sent as a chat message. | |||||
*/ | |||||
ChatCommandHandler.prototype.ChatCommands = { | |||||
"away": { | |||||
"description": translate("Set your state to 'Away'."), | |||||
"handler": function (args) { | |||||
Engine.LobbySetPlayerPresence("away"); | |||||
} | |||||
}, | |||||
"back": { | |||||
"description": translate("Set your state to 'Online'."), | |||||
"handler": function (args) { | |||||
Engine.LobbySetPlayerPresence("available"); | |||||
} | |||||
}, | |||||
"kick": { | |||||
"description": translate("Kick a specified user from the lobby. Usage: /kick nick reason"), | |||||
"handler": function (args) { | |||||
Engine.LobbyKick(args[0] || "", args[1] || ""); | |||||
}, | |||||
"moderatorOnly": true | |||||
}, | |||||
"ban": { | |||||
"description": translate("Ban a specified user from the lobby. Usage: /ban nick reason"), | |||||
"handler": function (args) { | |||||
Engine.LobbyBan(args[0] || "", args[1] || ""); | |||||
}, | |||||
"moderatorOnly": true | |||||
}, | |||||
"help": { | |||||
"description": translate("Show this help."), | |||||
"handler": function (args) { | |||||
let isModerator = Engine.LobbyGetPlayerRole(g_Username) == "moderator"; | |||||
let text = translate("Chat commands:"); | |||||
for (let command in this.ChatCommands) | |||||
if (!this.ChatCommands[command].moderatorOnly || isModerator) | |||||
// Translation: Chat command help format | |||||
Not Done Inline ActionsDo these need a . as well? Freagarach: Do these need a `.` as well? | |||||
Done Inline ActionsIf they do, I won't add them in this commit, so as to minimize string changes. Also when I look at the "description" fields just below, no, theres a period in every description where applicable. elexis: If they do, I won't add them in this commit, so as to minimize string changes.
There already… | |||||
text += "\n" + sprintf(translate("%(command)s - %(description)s"), { | |||||
"command": setStringTags(command, this.ChatCommandTags), | |||||
"description": this.ChatCommands[command].description | |||||
}); | |||||
this.chatMessagesPanel.addChatMessage({ | |||||
"from": "system", | |||||
"text": text | |||||
}); | |||||
this.chatMessagesPanel.flushMessages(); | |||||
} | |||||
}, | |||||
"me": { | |||||
"description": translate("Send a chat message about yourself. Example: /me goes swimming.") | |||||
}, | |||||
"say": { | |||||
"description": translate("Send text as a chat message (even if it starts with slash). Example: /say /help is a great command.") | |||||
}, | |||||
"clear": { | |||||
"description": translate("Clear all chat scrollback."), | |||||
"handler": function (args) { | |||||
this.chatMessagesPanel.clearChatMessages(); | |||||
} | |||||
} | |||||
}; |
Wildfire Games · Phabricator
Leaving us in tension ;)