2022-03-02 02:52:20 +01:00
function createConsole ( root , game _name , text _colour _function , initial _command , help _url , tooltip ) {
2022-02-27 01:48:21 +01:00
const div _card = document . createElement ( 'div' ) ;
const div _card _header = document . createElement ( 'div' ) ;
const h5 = document . createElement ( 'h5' ) ;
const card _button = document . createElement ( 'button' ) ;
const div _collapse = document . createElement ( 'div' ) ;
const div _card _body = document . createElement ( 'div' ) ;
2022-02-27 00:07:54 +01:00
const header = document . createElement ( 'h4' ) ;
const output = document . createElement ( 'div' ) ;
const output _text = document . createElement ( 'p' ) ;
const input = document . createElement ( 'form' ) ;
const input _text = document . createElement ( 'input' ) ;
const input _submit = document . createElement ( 'button' ) ;
2022-03-02 02:52:20 +01:00
const input _help = document . createElement ( 'button' ) ;
2022-02-27 00:07:54 +01:00
2022-02-27 01:48:21 +01:00
div _card . className = "card" ;
div _card _header . className = "card-header" ;
h5 . className = "mb-0" ;
card _button . className = "btn btn-link" ;
card _button . innerText = game _name [ 0 ] . toUpperCase ( ) + game _name . substr ( 1 ) ;
div _collapse . className = "collapse" ;
div _card _body . className = "card-body" ;
2022-02-27 00:07:54 +01:00
output . id = game _name + "_output" ;
output . className = "console_output" ;
output _text . innerText = game _name + " console" ;
input . id = game _name + "_form" ;
input . className = "console_form" ;
input _text . id = game _name + "_command" ;
input _text . className = "console_command" ;
input _text . size = 80 ;
input _text . autocomplete = "off" ;
2022-03-02 02:52:20 +01:00
if ( tooltip ) {
input _text . title = tooltip ;
}
2022-02-27 00:07:54 +01:00
input _submit . id = game _name + "_submit" ;
input _submit . className = "console_submit" ;
input _submit . innerText = "Enter" ;
2022-03-02 02:52:20 +01:00
input _help . id = game _name + "_submit" ;
input _help . className = "console_help" ;
input _help . innerText = "?" ;
2022-02-27 00:07:54 +01:00
2022-02-27 01:48:21 +01:00
root . appendChild ( div _card ) ;
div _card . appendChild ( div _card _header ) ;
div _card _header . appendChild ( h5 ) ;
h5 . appendChild ( card _button ) ;
div _card . appendChild ( div _collapse ) ;
div _collapse . appendChild ( div _card _body ) ;
2022-02-27 00:07:54 +01:00
output . appendChild ( output _text ) ;
2022-02-27 01:48:21 +01:00
div _card _body . appendChild ( output ) ;
2022-02-27 00:07:54 +01:00
input . appendChild ( input _text ) ;
input . appendChild ( input _submit ) ;
2022-03-02 02:52:20 +01:00
input . appendChild ( input _help ) ;
2022-02-27 01:48:21 +01:00
div _card _body . appendChild ( input ) ;
2022-02-27 00:07:54 +01:00
2022-03-02 02:52:20 +01:00
input _help . addEventListener ( 'click' , function ( e ) {
e . preventDefault ( ) ;
window . open ( help _url , '_blank' ) ;
} ) ;
2022-02-27 00:07:54 +01:00
input . addEventListener ( 'submit' , function ( e ) {
e . preventDefault ( ) ;
let line = document . createElement ( 'p' )
line . innerText = input _text . value ;
line . className = "user_input" ;
output . prepend ( line ) ;
socket . send ( input _text . value ) ;
input _text . value = "" ;
} ) ;
// Create WebSocket connection.
const socket = new WebSocket ( "wss://DOMAINNAME/" + game _name )
// Connection opened
socket . addEventListener ( 'open' , function ( event ) {
socket . send ( initial _command ) ;
} ) ;
socket . addEventListener ( 'error' , function ( event ) {
console . error ( event ) ;
} ) ;
// Listen for messages
socket . addEventListener ( 'message' , function ( event ) {
const output = document . getElementById ( game _name + '_output' ) ;
let line = document . createElement ( 'p' )
line . innerHTML = text _colour _function ( event . data ) ;
output . prepend ( line ) ;
} ) ;
}
function consoles _init ( ) {
const root = document . getElementById ( 'console-div' ) ;
2022-03-02 02:52:20 +01:00
createConsole (
root ,
'armagetronad' ,
convertTerminalCodeToHtml ,
'players' ,
'http://wiki.armagetronad.org/index.php?title=Console_Commands' ,
"Helpful commands:\n• players\tShow current players\n• kick <name>\tKick the player\n• num_ais <num>\tSet the number of AIs for games with more than one player.\n• limit_rounds\tEnd the match after this number of rounds"
) ;
createConsole (
root ,
'mindustry' ,
convertTerminalCodeToHtml ,
'status' ,
'https://mindustrygame.github.io/wiki/servers/#dedicated-server-commands' ,
2022-08-05 01:07:08 +02:00
"Helpful commands:\n• status\tDisplay server status\n• runwave\tTrigger the next wave\n• kick id\tKick a person with id"
2022-03-02 02:52:20 +01:00
) ;
2022-08-05 01:00:34 +02:00
createConsole (
root ,
'teeworlds' ,
convertTerminalCodeToHtml ,
'status' ,
2022-08-05 01:07:08 +02:00
'https://www.teeworlds.com/?page=docs&wiki=server_commands' ,
"Helpful commands:\n• status\tDisplay server status\n• change_map mapname\tSwitch to mapname\n• kick <name>\tKick a person by name"
2022-08-05 01:00:34 +02:00
) ;
2022-03-02 02:52:20 +01:00
createConsole (
root ,
'unvanquished' ,
convertDaemonedCodeToHtml ,
'/status' ,
'https://wiki.unvanquished.net/wiki/Server/Running#Commands' ,
"Helpful commands:\n• /listplayers\tList current players in-game\n• /kick <name>\tKick the player\n• /nextmap\tGo to the next map in the rotation"
) ;
createConsole (
root ,
'xonotic' ,
convertTerminalCodeToHtml ,
'who' ,
'https://gitlab.com/xonotic/xonotic/-/wikis/Basic-server-configuration' ,
"Helpful commands:\n• who\tList current players in-game\n• kick <name>\tKick the player\n• gotomap <name>\tGo to the specified map"
) ;
2022-02-27 00:07:54 +01:00
2022-02-27 01:48:21 +01:00
collapse _init ( ) ;
}
function collapse _init ( ) {
const bars = document . getElementsByClassName ( 'card-header' ) ;
for ( let bar of bars ) {
bar . addEventListener ( 'click' , function ( e ) {
const bartexts = document . getElementsByClassName ( 'collapse' ) ;
for ( let bartext of bartexts ) {
bartext . classList . remove ( "show" ) ;
}
this . parentElement . children [ 1 ] . classList . add ( "show" ) ;
} )
}
document . getElementsByClassName ( 'collapse' ) [ 0 ] . classList . add ( "show" ) ;
2022-02-27 00:07:54 +01:00
}
if ( document . readyState === 'loading' ) {
document . addEventListener ( 'DOMContentLoaded' , consoles _init ) ;
} else {
consoles _init ( ) ;
}