Last active
December 29, 2015 11:49
-
-
Save IgorMuzyka/7666246 to your computer and use it in GitHub Desktop.
A program which lets you sleep))
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 minimumSleepTime = 4 | |
maximumSleepHoursByWeekdays = [8, 8, 8, 8, 12, 12, 8] | |
zIterator = 0 | |
zSymbols = ["Z", "Z", "Z", "z", "z", "z", "z", "z", ".", ".", ".", "\n"] | |
makeZzzzz = -> | |
if zIterator == zSymbols.length | |
zIterator := 0 | |
process.stdout.write zSymbols[zIterator] | |
++zIterator | |
convertHoursToMilliseconds = (hours) -> | |
const amountOfMillisecondsInOneHour = 3600000 | |
hours * amountOfMillisecondsInOneHour | |
sleep = (timeToSleep, completion) -> | |
startTime = new Date | |
currentTime = void | |
do | |
currentTime = new Date | |
makeZzzzz! | |
while ((currentTime - startTime) < timeToSleep) | |
completion! | |
howMuchShouldISleepToday = -> | |
day = (new Date) .getDay! | |
maximumSleepTime = maximumSleepHoursByWeekdays[day] | |
Math.floor (Math.random! * maximumSleepTime + minimumSleepTime) | |
applyBadSleepCoefficient = (sleepTime) -> | |
sleepTime * Math.random! | |
class Human | |
(name) -> | |
@name? = name | |
const timeNeededToSleepDaily = convertHoursToMilliseconds 8 | |
sleep: (timeToSleep) -> | |
self = @ | |
console.log "Falling asleep..." | |
sleep timeToSleep, ( -> | |
self.awake! | |
) | |
goToSleep: -> | |
console.log "Lets go sleeping, i created" | |
awake: -> | |
console.log "#{@name} is awake and want to eat!" | |
haveAnAverageSleep: -> | |
timeToSleep = convertHoursToMilliseconds applyBadSleepCoefficient howMuchShouldISleepToday! | |
@goToSleep! | |
@sleep timeToSleep | |
haveAGoodSleep: -> | |
@goToSleep! | |
@sleep timeNeededToSleepDaily | |
you = new Human "Charlie" | |
itWillBeAGoodSleep = Math.floor (Math.random! * 2) | |
if itWillBeAGoodSleep | |
you.haveAGoodSleep! | |
else | |
you.haveAnAverageSleep! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment