Jump To …

slots.coffee

window.rr = 0


class ReelSpot

  constructor: (reel, num) ->


    @classes = ['cherry','apple','lemon','orange','plumb','watermellon','bar','bell','seven'
 'blank','blank','blank','blank','blank','blank','blank','blank',
 'blank','blank','blank','blank','blank','blank','blank','blank',
      'cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry',
      'cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry',
      'cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry','cherry',
      'apple','apple','apple','apple','apple','apple','apple','apple','apple','apple','apple','apple',
      'apple','apple','apple','apple','apple','apple','apple','apple','apple','apple','apple','apple',
      'lemon','lemon','lemon','lemon','lemon','lemon','lemon','lemon','lemon',
      'lemon','lemon','lemon','lemon','lemon','lemon','lemon','lemon','lemon',
      'orange','orange','orange','orange','orange','orange','orange','orange',
      'orange','orange','orange','orange','orange','orange','orange','orange',
      'plumb','plumb','plumb','plumb','plumb','plumb',
      'plumb','plumb','plumb','plumb','plumb','plumb',
      'watermellon','watermellon','watermellon','watermellon',
      'watermellon','watermellon','watermellon','watermellon',
      'bar','bar','bar',
      'bar','bar','bar',
      'bell','bell',
      'bell','bell',
      'seven'
    ]

    @r = reel
    @num = num
    @c = @classes[Math.floor(Math.random()*@classes.length)]
    @c = @classes[window.rr++]
    @el = $('<div class="reel-spot reel-'+@c+'" id="spot-' + @r.num + '-' + @num + '"></div>')
      .appendTo(@r.el)

  spin: ->
    @setC( @classes[Math.floor(Math.random()*@classes.length)] )


  setC: (c) ->
    @el.removeClass('reel-' + @c)
    @c = c
    @el.addClass('reel-' + @c)




class SlotReel

  reelSpeed: 50

  constructor: (game, num) ->
    @g = game
    @num = num
    @el = $('<div class="reel" id="reel' + @num + '"></div>').appendTo(@g.reelsView)
    @spots = [
      new ReelSpot(@, 0),
      new ReelSpot(@, 1),
      new ReelSpot(@, 2)
    ]

  advanceReel: ->
    @spots[2].setC(@spots[1].c)
    @spots[1].setC(@spots[0].c)
    @spots[0].spin();
    1

  spin: (times) ->
    @spinCount = times
    @advanceReel()
    window.setTimeout('$.g.reels['+@num+'].reSpin()', @reelSpeed)

  reSpin: =>
    @advanceReel()
    if (--@spinCount > 0)
      window.setTimeout('$.g.reels['+@num+'].reSpin()', @reelSpeed)
    else if @num == 2
        @g.spinOver()


  getSpotValue: (num) ->
    @spots[num].c






class SlotMachine extends Game

  constructor: ->
    @name = 'Slots'
    @game_key = 'slots'
    @includeHiScores = false
    @snd =
      spin: new Audio('/lib/games/snd/spinning.mp3')
      beep: new Audio('/lib/games/snd/beep.mp3')
      payoff: new Audio('/lib/games/snd/slot_payoff.mp3')
      coin: new Audio('/lib/games/snd/coin-drop.mp3')
      coin_big: new Audio('/lib/games/snd/coin-drop-big.mp3')
      coin_large: new Audio('/lib/games/snd/coin-drop-large.mp3')
    @snd.spin.loop = true
    @snd.spin.volume = 0.5
    @snd.beep.volume = 0.25

    super
    @score_label = 'Coins'
    @player = new Player(@)
    @score = @player.coins
    @linesPlayed = 1
    @coinsBet = 1
    @currentBet = 1
    @buildUI();

  newGame: ->
    super
    @score_label = 'Coins'
    @player = new Player(@)
    @score = @player.coins
    @linesPlayed = 1
    @coinsBet = 1
    @currentBet = 1
    @buildUI();

  setReelSpeed: (s) ->
    for i in [0..2] by 1
      @reels[i].reelSpeed = s

  buildUI: ->
    super

    @m = $('<div class="machine"></div>').appendTo(@el);
    @reelsView = $('<div class="reels"></div>').appendTo(@el)
    @reels = [
      new SlotReel(@, 0)
      new SlotReel(@, 1)
      new SlotReel(@, 2)
    ]

    @lines = [
      new SlotLine(@, 0, 1)
      new SlotLine(@, 1, 0)
      new SlotLine(@, 2, 2)
    ]

    @buttons = $('<div class="buttons"></div>').appendTo(@el)
    @betOneBtn = $('<a class="btn btn-success betone vert">Bet One</a>').appendTo(@buttons)
    @lineBtn = $('<a class="btn btn-warning linesbtn vert">Lines</a>').appendTo(@buttons);
    @maxBtn = $('<a class="btn btn-info maxbtn vert">Max Bet</a>').appendTo(@buttons);
    @spinBtn = $('<a class="btn btn-danger spinbtn vert">Spin</a>').appendTo(@buttons);

    @spinBtn.on('click', @spin)
    @lineBtn.on('click', @lineBtnClick)
    @betOneBtn.on('click', @betOneBtnClick)
    @maxBtn.on('click', @maxBetClick)

    h = $('<div class="slot-info"></div>').appendTo(@el)
    @coinsBetView = $('<div class="coins-played vert">1</div>').appendTo(h)
    @linesPlayedView = $('<div class="lines-played vert">1</div>').appendTo(h)
    @spinInfoPane = $('<div class="spin-info"></div>').appendTo(h)

    @currentBetView = $('<span>0</span>').appendTo(
      $('<div class="coins-bet vert">Bet: </div>').appendTo(@spinInfoPane)
    )

    @coinsWonView = $('<span>0</span>').appendTo(
      $('<div class="coins-won vert">Won: </div>').appendTo(@spinInfoPane)
    )


  spin: =>
    return if @spinning
    @spinning = true
    @snd.spin.play()

    @currentBet = @coinsBet * @linesPlayed
    @addToScore(-@currentBet)
    @updateCurrentBetView()
    @coinsWonView.text(0)
    for i in [0..2] by 1
      @lines[i].hide()
      @reels[i].spin(20+i*6)

  spinOver: ->
    spin_score = 0
    for i in [0...@linesPlayed] by 1
      if (@lines[i].classNum < @linesPlayed) && (line_score = @lines[i].checkScore())
        spin_score += line_score
        @lines[i].show()

    if (spin_score)
      won = spin_score * @linesPlayed
      @coinsWonView.text(won)
      @addToScore(won)
      if (won > 1000)
        @snd.coin_large.play()
      if (won > 100)
        @snd.coin_big.play()
      else
        @snd.coin.play()


    @snd.spin.pause()
    @spinning = false

  addToScore: (s) ->
    super
    @player.coins += s

  lineBtnClick: =>
    @snd.beep.play()
    return if @spinning
    @linesPlayed++
    if @linesPlayed > 3
      @linesPlayed = 1
    @linesPlayedView.text(@linesPlayed)
    @lines[0].hide()
    @lines[1].show()
    @lines[2].hide()
    if @linesPlayed == 3
      @lines[0].show()
      @lines[2].show()
    else if @linesPlayed == 2
      @lines[0].show()
    @updateCurrentBetView()

  betOneBtnClick: =>
    @snd.beep.play()
    return if @spinning
    @coinsBet++
    if @coinsBet > 3
      @coinsBet = 1
    @coinsBetView.text(@coinsBet)
    @updateCurrentBetView()

  maxBetClick: =>

    @linesPlayed = 3
    @coinsBet = 3
    @coinsBetView.text(@coinsBet)
    @linesPlayedView.text(@linesPlayed)
    @spin()

  updateCurrentBetView: ->
    @currentBetView.text(@coinsBet * @linesPlayed)






class SlotLine

  patterns: [
    [
      5, ['blank','blank','blank']
    ]
    [
      30, ['cherry','cherry','cherry']
    ],
    [
      20, ['cherry','cherry']
    ],
    [
      1, ['cherry']
    ],
    [
      50, ['apple','apple','apple']
    ],
    [
      25, ['apple','apple']
    ],
    [
      60, ['lemon','lemon','lemon']
    ],
    [
      30, ['lemon','lemon']
    ],
    [
      75, ['orange','orange','orange']
    ],
    [
      50, ['orange','orange']
    ],
    [
      100, ['plumb','plumb','plumb']
    ],
    [
      50, ['plumb','plumb']
    ],
    [
      200, ['watermellon','watermellon','watermellon']
    ],
    [
      100, ['watermellon','watermellon']
    ],
    [
      500, ['bar','bar','bar']
    ],
    [
      250, ['bar','bar']
    ],
    [
      50, ['bar']
    ],
    [
      1000, ['bell','bell','bell']
    ],
    [
      750, ['bell','bell']
    ],
    [
      5000, ['seven','seven','seven']
    ],

  ]

  constructor: (game, num, classNum) ->
    @g = game
    @num = num
    @classNum = classNum
    @el = $('<div class="slot-line slot-line' + classNum + '"></div>').appendTo(@g.el)
    if @classNum >= @g.linesPlayed
      @el.hide()

  show: ->
    @el.show()

  hide: ->
    @el.hide()

  checkScore: ->
    for i in [0..@patterns.length-1] by 1
      allMatch = true
      for j in [0...@patterns[i][1].length] by 1
        if @patterns[i][1][j] != @g.reels[j].getSpotValue(@num)
          allMatch = false
      return @patterns[i][0] if allMatch
    0




$ ->
  $.g = new SlotMachine();