Created
July 28, 2019 05:54
-
-
Save seokju-na/f91eb9f9e714e64a2a8e076feea8e5e8 to your computer and use it in GitHub Desktop.
GDG Campus Korea - Try Flutter 경품 추천 코드
This file contains 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
const chalkAnimation = require('chalk-animation'); | |
const { blue, gray, cyan, white, bold } = require('chalk'); | |
const { prompt } = require('node-ask'); | |
const parse = require('csv-parse/lib/sync'); | |
const fs = require('fs'); | |
const { shuffle, sample } = require('lodash'); | |
const center = require('center-align'); | |
const fileData = fs.readFileSync('./codelab.csv', 'utf8'); | |
const candidates = parse(fileData).slice(2); | |
const MAX_LOTTERY_COUNT = 6; | |
const stocks = [ | |
{ | |
name: '라인 프렌즈 무선 마우스', | |
count: 2, | |
}, | |
{ | |
name: '오아 아이브릭 블루투스 스피커', | |
count: 2, | |
}, | |
{ | |
name: '숯먹은 잠자는 강아지 탈취제 (사모예드)', | |
count: 2, | |
}, | |
]; | |
function clearConsole() { | |
process.stdout.write('\033c'); | |
} | |
function displayOnCenter(maxWidth, text) { | |
console.log(center(text, maxWidth)); | |
} | |
function displayStocks() { | |
console.log(bold('다음 경품을 추천합니다 🎉')); | |
console.log(white('-------------------------')); | |
for (const { name, count } of stocks) { | |
if (count > 0) { | |
console.log(cyan(name) + bold(` : ${count}개`)); | |
} else { | |
console.log(gray(`[품절] ${name}`)); | |
} | |
} | |
console.log('\n'); | |
} | |
async function drawLottery() { | |
clearConsole(); | |
displayStocks(); | |
await prompt(white('경품 추천을 하시겠어요? (press ENTER) ')); | |
const availableStocks = stocks.filter(stock => stock.count > 0); | |
const chosenStock = sample(shuffle(availableStocks)); | |
clearConsole(); | |
chalkAnimation.rainbow(`${chosenStock.name}를 추첨 중 입니다 🚀`); | |
await prompt(''); | |
clearConsole(); | |
const winner = sample(shuffle(candidates)); | |
const winnerIndex = candidates.indexOf(winner); | |
const [,name] = winner; | |
const title = `🎊🎊🎊 ${chosenStock.name} 당첨 🎊🎊🎊`; | |
const titleWidth = title.length + 18; | |
console.log(bold(title)); | |
console.log(); | |
displayOnCenter(titleWidth, cyan(`${name} 님`)); | |
console.log(); | |
console.log(); | |
candidates.splice(winnerIndex, 1); | |
chosenStock.count -= 1; | |
await prompt(''); | |
} | |
async function main() { | |
let index = 0; | |
clearConsole(); | |
console.log(bold(`총 ${candidates.length}명이 코드랩을 완료하셨습니다.`)); | |
console.log(bold('모두들 수고하셨습니다.')); | |
await prompt(''); | |
while (index < MAX_LOTTERY_COUNT) { | |
await drawLottery(); | |
index++; | |
} | |
clearConsole(); | |
console.log(blue('경품 추천이 완료되었습니다 :)')); | |
console.log(); | |
console.log(); | |
await prompt(''); | |
} | |
main().catch((error) => { | |
console.error(error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment