Skip to content

Instantly share code, notes, and snippets.

@swng
Last active May 14, 2024 23:56
Show Gist options
  • Save swng/5ca8eed3f4b62885c09ee487b3ef8f68 to your computer and use it in GitHub Desktop.
Save swng/5ca8eed3f4b62885c09ee487b3ef8f68 to your computer and use it in GitHub Desktop.
notation bot
client.on("messageCreate", async function (message) {
// console.log(message.content);
if (message.content.startsWith(prefix)) {
let split = message.content.split(" ");
let command = split[0].toLowerCase();
command = command.slice(1); // assuming prefix is legnth 1, remove prefix
let command_arguments = split.slice(1); // not including command
if (command == "notation") {
console.log(command);
console.log(command_arguments);
if (command_arguments.length == 0 || command_arguments[0] == "help") {
await message.channel.send(
`tetris piece placement notation. [Introduced by frey](https://twitter.com/frey_tetris/status/1790032690021847536). Precise definition subject to change.\nUsage: ${prefix}notation initial_fumen {sequence of piece placements in [notation] format, space delimited}\n.e.g.Js-2^3\nBot should output fumen and gif.
`
);
// should I learn how to do a fancy embed idk
return;
}
const notationPattern = /^([LJSZIOT])([nsew])([+\-]?\d*)?(\^\d*)?$/;
const orientationMap = {
"n": "spawn",
"e": "right",
"s": "reverse",
"w": "left"
};
let initial_fumen = command_arguments[0];
if (command_arguments.length == 1) return; // not gonna bother even outputting anything in this case
let notation_sequence = command_arguments.slice(1);
if (initial_fumen == "empty") initial_fumen = "v115@vhAAgH";
let pages = decoder.decode(initial_fumen);
let field = pages[pages.length - 1].field;
for (notation of notation_sequence) {
const matches = notation.match(notationPattern);
if (matches) {
const piece = matches[1];
const orientation = matches[2];
const column = matches[3] !== "" ? matches[3] : undefined;
const depth = matches[4] ? parseInt(matches[4].substring(1)) : 0;
// console.log("Piece:", piece);
// console.log("Orientation:", orientation);
// console.log("Column:", column);
// console.log("Depth:", depth);
let operation = {
type: piece,
rotation: orientationMap[orientation],
x: 4,
y: 0
} // welp just gonna assume x=4 is correct for spawn column, this may not be correct for all pieces in all orientations, whatever, idc enough
let empty_field = Field.create('');
while (!empty_field.canLock(operation)) {
operation.y++;
}
if (column != undefined) {
let num = parseInt(column.substring(1));
if (column[0] == "+") {
while (empty_field.canFill(operation)) {
operation.x--;
}
for(let i = 0; i < num + 1; i++) { // one extra because we need to reverse the very last operation
operation.x++;
}
}
if (column[0] == "-") {
while (empty_field.canFill(operation)) {
operation.x++;
}
for(let i = 0; i < num + 1; i++) {
operation.x--;
}
}
}
while (!field.canLock(operation)) {
operation.y++;
}
for(i = 0; i < depth; i++) {
operation.y++;
while (!field.canLock(operation)) {
operation.y++;
}
}
field.put(operation);
field.clearLine();
pages.push({
operation: operation
})
} else {
// console.log("Notation doesn't match the pattern.");
}
}
let fumen = encoder.encode(pages);
await message.channel.send(fumen);
try {
let pages = decoder.decode(fumen)
let size = 22;
let height = undefined;
let page = 0;
let start = 0;
let end = undefined;
let delay = 500;
let gif = drawFumens(pages, size, height, start, end, false, delay);
await gif.createReadStream().pipe(fs.createWriteStream("output.gif"));
await gif.finish();
await message.channel.send({ files: ["output.gif"] });
} catch {
await message.channel.send("something went wrong");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment