Skip to content

Instantly share code, notes, and snippets.

@evinjaff
Created December 15, 2024 16:50
Show Gist options
  • Save evinjaff/59c0005bed3620b93c053bc140330cd9 to your computer and use it in GitHub Desktop.
Save evinjaff/59c0005bed3620b93c053bc140330cd9 to your computer and use it in GitHub Desktop.
2024 Secret Santa Script
DRY_RUN = true
SEED_PHRASE = "Yeag!"
async function myFunction() {
/* Random Number Generation Setup */
// Create cyrb128 state:
var seed = cyrb128(SEED_PHRASE);
// Four 32-bit component hashes provide the seed for sfc32.
var rand = sfc32(seed[0], seed[1], seed[2], seed[3]);
// Obtain sequential random numbers like so:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach((i) => {
console.log( (rand() * 80) % 1 )
})
var sheet = SpreadsheetApp.getActiveSheet()
var data = sheet.getDataRange().getValues();
console.log(data)
//slice data to exclude descriptor
data = data.slice(1, undefined)
nrows = data.length
//Turn each row into an object
arr_of_recipients = []
data.forEach( (person) => {
console.log(person)
type_raw = person[3]
type_proc = []
console.log(typeof(type_raw))
if (type_raw.includes("Physical")) {
type_proc.push("physical")
}
if (type_raw.includes("Digital")) {
type_proc.push("digital")
}
if (type_raw.includes("Steam")) {
type_proc.push("steam")
}
arr_of_recipients.push({
"Name": person[1],
"Email": person[2],
"Types": type_proc,
"Address": person[4],
"Steam": person[5],
"Notes": person[6]
})
})
//randomly sort arr
arr_of_recipients.sort(function(a, b) {
return 0.5 - rand()});
console.log(arr_of_recipients)
arr_of_sender = JSON.parse(JSON.stringify(arr_of_recipients));
//shift sender by one
arr_of_sender = [].concat(arr_of_sender.slice(-1), arr_of_sender.slice(0, -1))
// Verify that the arrays have the same length
console.log(arr_of_sender.length, arr_of_recipients.length)
if (arr_of_sender.length != arr_of_recipients.length) {
throw Error("Length of arrays don't match")
}
for (i=0;i<arr_of_sender.length;i++){
send = arr_of_sender[i]
receive = arr_of_recipients[i]
steam_bool = "steam" in receive["Types"] ? `Steam: ${receive["Types"]}` : ``
send_email(send["Email"], send["Name"], receive["Name"], receive["Email"], receive["Types"], receive["Address"], steam_bool, receive["Notes"])
}
}
function send_email(sender_email, sender_name, recipient_name, recipient_email, types, recipient_addr, steam, notes) {
console.log(sender_email, sender_name, recipient_name, recipient_email, types, recipient_addr, steam, notes)
email_body = `Hello <strong>${sender_name}</strong>,\nYou have been assigned to <strong>${recipient_name}</strong> this year. <br> Here are some details. <br><br> Email: ${recipient_email}<br>Types of Gifts Allowed: ${types}<br>Receivable Address: ${recipient_addr}<br>${steam}\nOther Notes: ${notes}
<br><br>
By popular demand, GPT-4 is no longer being used to suggest gift ideas. Instead, enjoy this random pokemon sprite
<br>
<strong>UPDATE</strong>: Because Vincent was late to the form, I have removed Minior and Chespin from the drawing pool. <a href="">I am not kidding lol</a>
<br>
<img src='cid:snakeImage' />
<br>
\nHappy hunting!`
pokemon = Math.round( Math.random() * 1024 )
// The Vincent clause
if (pokemon == 774 || pokemon == 650) {
pokemon += Math.round(Math.random() * 10) + 1
}
const pngUrl = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon}.png`
const response = UrlFetchApp.fetch(pngUrl);
const imageBlob = response.getBlob();
const imageBase64 = Utilities.base64Encode(imageBlob.getBytes());
if (!DRY_RUN) {
// Send the email with the inline image attachment
MailApp.sendEmail({
to: sender_email,
subject: "Catboy Baseball Team White Elephant - You have been assigned someone",
htmlBody: email_body,
name: "Catboy Baseball Team White Elephant - Automated Email Script",
inlineImages: {
snakeImage: imageBlob // Attach the image with the reference ID 'snakeImage'
}
})
}
else {
console.log(email_body)
}
}
async function runGeminiTextOnly(genAI, prompt) {
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash-8b" });
const result = await model.generateContent(prompt);
const response = await result.response;
return await response.text();
}
function sfc32(a, b, c, d) {
return function() {
a >>>= 0; b >>>= 0; c >>>= 0; d >>>= 0;
var t = (a + b) | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = (c << 21 | c >>> 11);
d = d + 1 | 0;
t = t + d | 0;
c = c + t | 0;
return (t >>> 0) / 4294967296;
}
}
function cyrb128(str) {
let h1 = 1779033703, h2 = 3144134277,
h3 = 1013904242, h4 = 2773480762;
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
h1 ^= (h2 ^ h3 ^ h4), h2 ^= h1, h3 ^= h1, h4 ^= h1;
return [h1>>>0, h2>>>0, h3>>>0, h4>>>0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment