Created
August 27, 2024 06:49
-
-
Save notarikon-nz/023f27b543acecc50a61372c9678d579 to your computer and use it in GitHub Desktop.
HackMUD aperture.anagram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function(context, args) // {get:"characters"} | |
| { | |
| var lib = #fs.aperture.lib(); | |
| const load = "".split(" ") | |
| const HEADER = `\n\`HAPERTURE SCIENCE :: MAGNARA DICTIONARY\`\n\n` | |
| function getUsage() { | |
| let totalWords = 0; | |
| let countArray = []; | |
| for (let i = 4; i <= 9; i++) { | |
| let count = #db.f({group:"magnara",value:{$regex:`^[a-zA-Z]{${i}}$`}}).count(); | |
| countArray.push({length:i,words:String(count*1).padStart(5," ")}) | |
| totalWords += count | |
| count = null; | |
| } | |
| return HEADER + `\`1Usage aperture.anagram { get: "characters" }\`\n\n` + createTable(countArray,['length','words']) + `\n\nTotal words in dictionary: ${totalWords}` | |
| } | |
| if (!args) { | |
| return getUsage() | |
| } | |
| if (args.strip) { | |
| const romanRegex = /^[IVXLCDM]+$/; | |
| let c = #db.f({group:"magnara",value:{$regex:'^[IVXLCDM]+$'}}).count(); | |
| return c; | |
| } | |
| // GET ANSWER | |
| if (args.get && typeof args.get == 'string') { | |
| return getAnswer(args.get) | |
| } | |
| if (!lib.isAuthorised(context.caller,context.calling_script)) return getUsage() | |
| // DELETE ENTRY | |
| if (args.delete && typeof args.delete == 'string') { | |
| return HEADER + deleteAnswer(args.delete) | |
| } | |
| // ADD ENTRY | |
| if (args.add) { | |
| let result = "" | |
| if (typeof args.add == 'object') { | |
| let k = Object.keys(args.add), v = Object.values(args.add) | |
| if (typeof k == 'string' && typeof v == 'string') | |
| result += addAnswer(k,v) + ", " | |
| } else if (typeof args.add == 'string') { | |
| result = addAnswer(args.add,args.add) | |
| } | |
| return "added: " + result | |
| } | |
| // FUNCTIONS | |
| function normalize(word) { | |
| return word.split('').sort().join(''); | |
| } | |
| function getAnswer(key) { | |
| const normalizedKey = normalize(key); | |
| let result = #db.f({ group: "magnara", key: normalizedKey }, { value: 1, _id: 0 }).first(); | |
| if (result == null) return null; | |
| return result.value; | |
| } | |
| function deleteAnswer(key) { | |
| let delete_output = "" | |
| const normalizedKey = normalize(key); | |
| try { | |
| let result = #db.r({ group: "magnara", key: normalizedKey }); | |
| if (result.deletedCount > 0) { | |
| delete_output += (`Successfully deleted key: ${normalizedKey}`) + "\n"; | |
| } else { | |
| delete_output += (`No entry found for key: ${normalizedKey}`) + "\n"; | |
| } | |
| } catch (e) { | |
| delete_output += (`Error deleting key: ${normalizedKey}`) + "\n"; | |
| delete_output += (e) + "\n"; | |
| } | |
| } | |
| function addAnswer(key, value) { | |
| const normalizedKey = normalize(key); | |
| const query = { group: "magnara", key: normalizedKey }; | |
| const update = { $set: { value: value } }; | |
| try { | |
| #db.us( query, update ); | |
| } catch (e) {} | |
| return value | |
| } | |
| function decolor (val) { | |
| return String(val).replaceAll(/`\w(.*?)`/g, '$1') | |
| } | |
| function createTable(data, columns) { | |
| if (!data.length) { | |
| return ''; | |
| } | |
| // Use the provided columns or get all keys from the first object | |
| columns = columns || Object.keys(data[0]); | |
| // Calculate the width for each column | |
| const colWidths = columns.map(key => Math.max(key.length, ...data.map(row => decolor(String(row[key] || '')).length))); | |
| // Padding function | |
| const pad = (str, length) => String(str).padEnd(length, ' '); | |
| // Function to create the border | |
| const border = (left, mid, right, horizontal) => | |
| left + colWidths.map(width => horizontal.repeat(width + 2)).join(mid) + right; | |
| // Create the header row | |
| const header = "┃" + columns.map((key, i) => `\`1 ${pad(key.toUpperCase(), colWidths[i])} \``).join('┃') + "┃"; | |
| // Create the data rows | |
| const rows = data.map(row => | |
| "┃" + columns.map((key, i) => ` ${pad(row[key] || '', colWidths[i])} `).join('┃') + "┃" | |
| ); | |
| // Create the borders | |
| const topBorder = border('┏', '┳', '┓', '━'); | |
| const midBorder = border('┣', '╋', '┫', '━'); | |
| const bottomBorder = border('┗', '┻', '┛', '━'); | |
| // Combine all parts to create the table | |
| return [ | |
| topBorder, | |
| header, | |
| midBorder, | |
| ...rows, | |
| bottomBorder | |
| ].join('\n'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment