Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Søren Louv-Jansen committed Apr 1, 2012
1 parent 180de53 commit 8c92e09
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
43 changes: 25 additions & 18 deletions coffee/club.coffee
@@ -1,9 +1,12 @@
class @Club
constructor: (songs, commands, cheers) ->
# options
@commandProbability = 0.3 # higher means more often commands
@clubNumber = 0 # the club to start from
@setTimerSeconds 60 # number of seconds per club
@paused = true # pause from beginning
@secondsPerClub = 60 # number of seconds per club

# initial setup
@clubNumber = 0 # the club to start from
@setTimerSeconds @secondsPerClub

# setup players
@songPlayer = new SongPlayer('songs/', songs);
Expand All @@ -18,11 +21,15 @@ class @Club
primary: "ui-icon-play"
.click => @toggleControls()


# countdown every second
setInterval (=>

return false if @paused
return false if @songPlayer.current.paused

if @clubNumber > 100
$( ".controls" ).button "disable"
@pause()
alert "Er du fuld nu?"

# decrement second by one and update UI accordingly
@decrementSecond()
Expand All @@ -33,7 +40,7 @@ class @Club
# end of song; change to next
if @timerSeconds is 0
@songPlayer.next()
@setTimerSeconds 60
@setTimerSeconds @secondsPerClub
@setCommand()
@incrementClubNumber()

Expand All @@ -44,7 +51,7 @@ class @Club

# play command
@commandPlayer.next( (duration) =>
console.log "Duration from screen: " + duration

# increase volume for song again and pause command
setTimeout ( =>
@songPlayer.setVolume 1
Expand All @@ -59,7 +66,7 @@ class @Club
decrementSecond: ->
# calculations
@timerSeconds--
progressBarWidth = (@timerSeconds / 60) * 100
progressBarWidth = (@timerSeconds / @secondsPerClub) * 100

# UI
$(".timer-seconds").html @timerSeconds
Expand All @@ -82,21 +89,21 @@ class @Club
console.log "Command will NOT be played"

resume: ->
@paused = false
$( ".controls" ).button
icons:
primary: "ui-icon-pause"

@songPlayer.start()

pause: ->
@paused = true
$( ".controls" ).button
icons:
primary: "ui-icon-play"

@songPlayer.pause()

toggleControls: ->
if @paused is true
$( ".controls" ).button
icons:
primary: "ui-icon-pause"
if @songPlayer.current.paused is true
@resume()
else
$( ".controls" ).button
icons:
primary: "ui-icon-play"
else
@pause()
9 changes: 6 additions & 3 deletions coffee/player.coffee
Expand Up @@ -3,11 +3,11 @@ class Player
@track_number = 0
@current = new Audio(@folder + @playlist[@track_number])

start: ->
start: ->
@current.play()

pause: ->
@current.pause() if @current.paused is false
@current.pause() if @current.paused is false

next: (callback) ->
# pause current track
Expand All @@ -25,13 +25,15 @@ class Player
# set track source
@current.src = @folder + filename

# start
@start()
console.log "Changed track to: " + filename

setVolume: (volume) ->
console.log "Volume change to: #{volume} "
@current.volume = volume

# songplayer with specific label
class @SongPlayer extends Player
setLabel: (filename) ->
filename_human = filename.substring(0, filename.length - 4)
Expand All @@ -40,6 +42,7 @@ class @SongPlayer extends Player
super
@setLabel @playlist[@track_number]

# commandplayer with own implementation of next for returning duration in a callback
class @CommandPlayer extends Player
next: (callback) ->
# pause current track
Expand All @@ -62,7 +65,7 @@ class @CommandPlayer extends Player
if @current.duration > 0
clearInterval(preload)

#
# return duration of command to Club()
callback(@current.duration)

# start next song
Expand Down
40 changes: 21 additions & 19 deletions js/club.js
Expand Up @@ -5,9 +5,9 @@
function Club(songs, commands, cheers) {
var _this = this;
this.commandProbability = 0.3;
this.secondsPerClub = 60;
this.clubNumber = 0;
this.setTimerSeconds(60);
this.paused = true;
this.setTimerSeconds(this.secondsPerClub);
this.songPlayer = new SongPlayer('songs/', songs);
this.commandPlayer = new CommandPlayer('commands/', commands);
this.cheersPlayer = new CheersPlayer('cheers/', cheers);
Expand All @@ -20,19 +20,23 @@
return _this.toggleControls();
});
setInterval((function() {
if (_this.paused) return false;
if (_this.songPlayer.current.paused) return false;
if (_this.clubNumber > 100) {
$(".controls").button("disable");
_this.pause();
alert("Er du fuld nu?");
}
_this.decrementSecond();
if (_this.timerSeconds === 1) _this.cheersPlayer.next();
if (_this.timerSeconds === 0) {
_this.songPlayer.next();
_this.setTimerSeconds(60);
_this.setTimerSeconds(_this.secondsPerClub);
_this.setCommand();
_this.incrementClubNumber();
}
if (_this.timerSeconds === _this.commandSecond && _this.commandSecond > 0) {
_this.songPlayer.setVolume(0.2);
return _this.commandPlayer.next(function(duration) {
console.log("Duration from screen: " + duration);
return setTimeout((function() {
_this.songPlayer.setVolume(1);
return _this.commandPlayer.pause();
Expand All @@ -49,7 +53,7 @@
Club.prototype.decrementSecond = function() {
var progressBarWidth;
this.timerSeconds--;
progressBarWidth = (this.timerSeconds / 60) * 100;
progressBarWidth = (this.timerSeconds / this.secondsPerClub) * 100;
$(".timer-seconds").html(this.timerSeconds);
return $(".progress-bar-inside").css("width", progressBarWidth + "%");
};
Expand All @@ -76,29 +80,27 @@
};

Club.prototype.resume = function() {
this.paused = false;
$(".controls").button({
icons: {
primary: "ui-icon-pause"
}
});
return this.songPlayer.start();
};

Club.prototype.pause = function() {
this.paused = true;
$(".controls").button({
icons: {
primary: "ui-icon-play"
}
});
return this.songPlayer.pause();
};

Club.prototype.toggleControls = function() {
if (this.paused === true) {
$(".controls").button({
icons: {
primary: "ui-icon-pause"
}
});
if (this.songPlayer.current.paused === true) {
return this.resume();
} else {
$(".controls").button({
icons: {
primary: "ui-icon-play"
}
});
return this.pause();
}
};
Expand Down

0 comments on commit 8c92e09

Please sign in to comment.