Skip to content

Instantly share code, notes, and snippets.

@IgorMuzyka
Last active December 29, 2015 11:49
Show Gist options
  • Save IgorMuzyka/7666246 to your computer and use it in GitHub Desktop.
Save IgorMuzyka/7666246 to your computer and use it in GitHub Desktop.
A program which lets you sleep))
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