-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilverlightWeeklyRaffle.js
86 lines (65 loc) · 2.67 KB
/
silverlightWeeklyRaffle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Adapted from the original script by Sinjhin, https://github.com/Sinjhin/sinjhin.github.io
const fs = require('fs');
const Papa = require('papaparse');
const inputFile = 'raffle_input.csv'; // Your input CSV file
const outputFile = 'raffle_output.csv'; // File to save the filtered data
// Read the CSV file
const csvData = fs.readFileSync(inputFile, 'utf8');
// Parse the CSV data
const results = Papa.parse(csvData, {
header: false,
skipEmptyLines: true,
});
// Process the data row by row
let ticketNumber = 1;
const assignedTickets = []; // Use an array to maintain order and duplicates
results.data.forEach((row) => {
const name = row[1];
const tickets = parseInt(row[3]);
if (name && tickets) {
for (let ticket = ticketNumber; ticket < ticketNumber + tickets; ticket++) {
assignedTickets.push({ name: name, ticket: ticket });
}
ticketNumber += tickets;
}
});
// Convert the assigned tickets data to an array of strings for output
const outputTickets = assignedTickets.map(item => `${item.name}: ${item.ticket}`);
// Define an array of names to skip
const skippedNames = ['simon_pinelock', 'Brangwynn', 'Sinjhin']; // Replace with your array of names to skip
// Function to get a random winner, excluding skipped names and previous winners
const getRandomWinner = (previousWinners) => {
let winner;
do {
const randomIndex = Math.floor(Math.random() * assignedTickets.length);
const candidate = assignedTickets[randomIndex];
if (!skippedNames.includes(candidate.name) && !previousWinners.includes(candidate.name)) {
winner = candidate;
}
} while (!winner);
return winner;
};
// Array to store previous winners
const previousWinners = [];
// Get three random ticket numbers for first, second, and third prizes
const firstPrize = getRandomWinner(previousWinners);
previousWinners.push(firstPrize.name);
const secondPrize = getRandomWinner(previousWinners);
previousWinners.push(secondPrize.name);
const thirdPrize = getRandomWinner(previousWinners);
// Create a set for unique users
const uniqueUsers = new Set(assignedTickets.map(item => item.name));
// Prepare the output string
const output = `Prize Winners:
First Prize (Ticket #${firstPrize.ticket}): ${firstPrize.name}
Second Prize (Ticket #${secondPrize.ticket}): ${secondPrize.name}
Third Prize (Ticket #${thirdPrize.ticket}): ${thirdPrize.name}
Unique Users:
${Array.from(uniqueUsers).join('\n')}
Assigned Tickets:
${outputTickets.join('\n')}`;
// Output the results to the console
console.log(output);
// Save the filtered data and prize information to a new CSV file
fs.writeFileSync(outputFile, output, 'utf8');
console.log(`Assigned tickets and prize information saved to ${outputFile}`);