-
Notifications
You must be signed in to change notification settings - Fork 0
keybind: Add /bind keybinding command #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| function sendKeyData( player, key, cmd ) { | ||
| Stream.StartWrite(); | ||
| Stream.WriteInt(418); | ||
| Stream.WriteInt(key); | ||
| Stream.WriteString(cmd); | ||
| Stream.SendStream(player); | ||
| } | ||
|
|
||
| keys <- { | ||
| "backspace" : 0x08, | ||
| "return" : 0x0D, | ||
| "lshift" : 0xA0, | ||
| "rshift" : 0xA1, | ||
| "lctrl" : 0xA2, | ||
| "rctrl" : 0xA3 | ||
| }; | ||
|
|
||
| function onScriptLoad() { | ||
| db <- ConnectSQL("scripts/keybinds.db"); | ||
| if (!db) { | ||
| print("ERROR: SQLite database file could not be opened"); | ||
| } | ||
| QuerySQL(db, "CREATE TABLE IF NOT EXISTS keybinds(name TEXT, key INTEGER, " + | ||
| "cmd TEXT, PRIMARY KEY(name,key));"); | ||
| local i; | ||
| for(i = 0; i < 10; ++i) { | ||
| keys[format("%i", i)] <- 0x30 + i; | ||
| keys[format("num%i", i)] <- 0x60 + i; | ||
| } | ||
| for(i = 0; i < 26; ++i) { | ||
| keys[(97 + i).tochar()] <- 0x41 + i; | ||
| } | ||
| for(i = 1; i < 13; ++i) { | ||
| keys[format("f%i", i)] <- 0x6F + i; | ||
| } | ||
| } | ||
|
|
||
| function partition( input, separator ) { | ||
| local ret = array(2, null); | ||
| local firstSpace = input.find(separator); | ||
| if (!firstSpace) { | ||
| firstSpace = input.len(); | ||
| } else { | ||
| ret[1] = input.slice(firstSpace+1); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. My trail of thought was that the used functions return |
||
| ret[0] = input.slice(0, firstSpace); | ||
| return ret; | ||
dracc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| function onPlayerCommand( player, cmd, text ) { | ||
| cmd = cmd.tolower(); | ||
| if (cmd == "bind") { | ||
| if (!text) { | ||
| local currbinds = "Current binds: \n"; | ||
| local q = QuerySQL(db, "SELECT key, cmd FROM keybinds WHERE name = '" + | ||
| player.Name + "';"); | ||
| if (q) { | ||
| do { | ||
| currbinds += "'" + GetSQLColumnData(q, 0) + "': '" + | ||
| GetSQLColumnData(q, 1) + "'\n"; | ||
| } while (GetSQLNextRow(q)); | ||
| } | ||
| FreeSQLQuery(q); | ||
| MessagePlayer(currbinds + "Usage:\n" + | ||
| "Bind key: /" + cmd + " <key> <command>\n" + | ||
| "Unbind: /" + cmd + " <key>\n" + | ||
| "Get help: /" + cmd, player); | ||
| return; | ||
| } | ||
| text = text.tolower(); | ||
| local argArray = partition(text, " "); | ||
| local key = argArray[0]; | ||
| if (key in keys) { | ||
| key = keys[key]; | ||
| } else if (IsNum(key)) { | ||
| key = key.tointeger(); | ||
| } else { | ||
| MessagePlayer("Invalid key!", player); | ||
| return; | ||
| } | ||
| if (argArray[1] && argArray[1] != "") { | ||
| QuerySQL(db, "REPLACE INTO keybinds(name, key, cmd) VALUES ('" + | ||
| player.Name + "'," + key + ",'" + argArray[1]+ "');"); | ||
| sendKeyData(player, key, argArray[1]); | ||
| } else { | ||
| QuerySQL(db, "DELETE FROM keybinds WHERE name = '" + player.Name + | ||
| "' AND key = " + key + ";"); | ||
| sendKeyData(player, key, "null"); | ||
| } | ||
| } else if (cmd == "help") { | ||
| MessagePlayer("[#8080A0]/bind", player); | ||
| } | ||
| } | ||
|
|
||
| function onPlayerJoin( player ) { | ||
| local q = QuerySQL(db, "SELECT key, cmd FROM keybinds WHERE name = '" + | ||
| player.Name + "'"); | ||
| if (q) { | ||
| do { | ||
| sendKeyData(player, GetSQLColumnData(q, 0), GetSQLColumnData(q, 1)); | ||
| } while (GetSQLNextRow(q)); | ||
| } | ||
| FreeSQLQuery(q); | ||
| } | ||
|
|
||
| function onClientScriptData( player ) { | ||
| local type = Stream.ReadInt(); | ||
| if (type == 451) { | ||
| local cmdstring = Stream.ReadString(); | ||
| if (cmdstring && cmdstring != "") { | ||
| local cmdArray = partition(cmdstring, " "); | ||
| getroottable()["onPlayerCommand"].call(this, player, cmdArray[0], | ||
| cmdArray[1]); | ||
| } | ||
| } else { | ||
| print("Unknown scriptdata type " + type + " received."); | ||
| } | ||
| return 1; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| binds <- {}; | ||
|
|
||
| function Script::ScriptLoad() { | ||
| } | ||
|
|
||
| function Server::ServerData( stream ) { | ||
| // Identitifier token, 418 selected because of HTTP error code | ||
| // 418 - I'm a teapot | ||
| // as defined in RFC2324 | ||
| local type = stream.ReadInt(); | ||
|
|
||
| if (type == 418) { | ||
| local keyID = stream.ReadInt(); | ||
| local cmd = stream.ReadString(); | ||
| if (cmd == "null") { | ||
| binds.rawdelete(KeyBind(keyID)); | ||
| return; | ||
| } | ||
| binds[KeyBind(keyID)] <- cmd; | ||
| } else { | ||
| Console.Print("Received some unknown data stream"); | ||
| } | ||
| } | ||
|
|
||
| function KeyBind::OnDown( keyBind ) { | ||
| if (keyBind in binds) { | ||
| local message = Stream(); | ||
| // 451 - Unavailable for legal reasons | ||
| message.WriteInt(451); | ||
| message.WriteString(binds[keyBind]); | ||
| Server.SendData(message); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.