Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 346 lines (294 sloc) 8.666 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
# Description:
# A drunkly coded, ASCII version of the famous game.
# Sort of assumes Campfire
# Game mechanics are easy: http://bruteforcex.blogspot.com/2008/03/1-4-24-dice-game.html
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# dice start - starts a game of one, four, twenty four
# dice take <dice letters> - takes dice at given levels
# dice stats - displays your statistics
# dice stats all - displays all players' statistics
#
# Authors:
# zbowling
# sukima

dieMap = ['A','B','C','D','E','F']


class BuddhaGame
  constructor: () ->
    @diceLeft = 6
    @diceTaken = []
    @lastDice = []
    @steps = 0

  toJSON: ->
    diceLeft: @diceLeft
    diceTaken: @diceTaken
    lastDice: @lastDice
    steps: @steps

  @build: (data) ->
    buddha = new BuddhaGame
    for id, value of data
      buddha[id] = value
    buddha

  rollRemainingDice: () ->
    @lastDice = []
    printlines = ['','','','','','','']
    for dieIdx in [0...@diceLeft]
      die = @randomDice()
      diestr = @diceString(die,dieMap[dieIdx],die)
      dielines = diestr.split "\n"
      for i in [0...printlines.length]
        printlines[i] += dielines[i]
      @lastDice.push die
    printlines
  
  roll: ->
    "new game!\n" +
    @rollRemainingDice().join("\n")
  
  
  take: (diceToTake) ->
    dieLetters = diceToTake.toUpperCase().replace(" ","").split("")
    lastDiceLeftCount = @diceLeft
    for dieLetter in dieLetters
      dieIndex = dieMap.indexOf dieLetter
      if dieIndex != -1 and dieIndex < @lastDice.length
        die_val = @lastDice[dieIndex]
        if die_val != -1
          @diceTaken.push die_val
          @lastDice[dieIndex] = -1 #marking it invalid so you can pick it twice
          @diceLeft -= 1

    if @diceLeft == lastDiceLeftCount
      return "(you didn't pick any dice.)"
    
    @steps += 1
    
    printlines = ['','','','','','','']
    if @diceLeft > 1
      printlines = @rollRemainingDice()
      for i in [0...printlines.length]
        printlines[i] += "\t > "
    else if @diceLeft == 1 #only one left so lets just end the suffering
      die = @randomDice()
      @diceTaken.push die
      @diceLeft = 0
      
    takenString = @diceTakenStringArray()
    for i in [0...printlines.length]
      printlines[i] += takenString[i]
      
    return_str = printlines.join("\n") + "\n" + @calculateScoreString()
    
    if @diceLeft <= 0
      return_str += "\nNice job!"
    
    return_str
    
  scoreValue: ()->
    score = 0
    has_one = no
    has_four = no
    for dieIdx in [0...@diceTaken.length]
      dice_val = @diceTaken[dieIdx]
      if dice_val == 4 and has_four == no
        has_four = yes
      else if dice_val == 1 and has_one == no
        has_one = yes
      else
        score += dice_val
    
    if @diceLeft <= 0 and (has_one == no or has_four == no)
      score = 0
      
    {score:score,taken:@diceTaken,hasOne:has_one,hasFour:has_four,steps:@steps}
  
  
  calculateScoreString: () ->
    score = 0
    has_one = no
    has_four = no
    for dieIdx in [0...@diceTaken.length]
      dice_val = @diceTaken[dieIdx]
      if dice_val == 4 and has_four == no
        has_four = yes
      else if dice_val == 1 and has_one == no
        has_one = yes
      else
        score += dice_val
    
    if @diceLeft <= 0 and (has_one == no or has_four == no)
      "score: 0 // bummer..."
    else if has_one == no and has_four == no
      "score: #{score} (you still need to take a 1 and a 4)"
    else if has_one == no and has_four == yes
      "score: #{score} (you still need to take a 1)"
    else if has_four == no and has_one == yes
      "score: #{score} (you still need to take a 4)"
    else
      "score: #{score}"
        
  diceTakenStringArray: () ->
    printlines = ['','','','','','','']
    has_one = no
    has_four = no
    for dieIdx in [0...@diceTaken.length]
      dice_val = @diceTaken[dieIdx]
      dice_score = "-"
      if dice_val == 4 and has_four == no
        has_four = yes
      else if dice_val == 1 and has_one == no
        has_one = yes
      else
        dice_score = dice_val
      diestr = @diceString(dice_val,"*",dice_score)
      dielines = diestr.split "\n"
      for i in [0...printlines.length]
        printlines[i] += dielines[i]
    printlines
    
  gameover: () ->
    if @diceLeft <= 0
      return true
    else
      return false
    
  randomDice: () ->
    1 + Math.floor(Math.random() * 6)
  
  diceString: (value,i,x) ->
    switch value
      when 1
        "
#{x} \n
--------- \n
| | \n
| o | \n
| | \n
--------- \n
#{i} \n
"
      when 2
        "
#{x} \n
--------- \n
| o | \n
| | \n
| o | \n
--------- \n
#{i} \n
"
      when 3
        "
#{x} \n
--------- \n
| o | \n
| o | \n
| o | \n
--------- \n
#{i} \n
"
      when 4
        "
#{x} \n
--------- \n
| o o | \n
| | \n
| o o | \n
--------- \n
#{i} \n
"
      when 5
        "
#{x} \n
--------- \n
| o o | \n
| o | \n
| o o | \n
--------- \n
#{i} \n
"
      when 6
        "
#{x} \n
--------- \n
| o o | \n
| o o | \n
| o o | \n
--------- \n
#{i} \n
"
  

    
    

class BuddhaLounge
  constructor: (@robot) ->
    @games = {}
    @playerdata = {}
    @players = {}

    @robot.brain.on 'loaded', =>
      if @robot.brain.data.buddhagames?
        for id, game_data of @robot.brain.data.buddhagames
          @games[id] = BuddhaGame.build game_data
      if @robot.brain.data.playerdata?
        @playerdata = @robot.brain.data.playerdata

  startGame: (msg, player) ->
    game = new BuddhaGame
    msg.reply "\n" + game.roll()
    @games[player.id] = game
    if not @players[player.id]?
      @players[player.id] = player

    if not @playerdata[player.id]?
      @playerdata[player.id] = { totalScore: 0, totalGamesStarted: 0, totalGamesFinished: 0, lastScore:0}
    @playerdata[player.id].totalGamesStarted += 1;

    @save()

  take: (msg, player, take) ->
    if @games[player.id]?
      game = @games[player.id]
      msg.reply "\n" + game.take(take)

      scoreValue = game.scoreValue()
      if scoreValue.taken.length == 4 and not (scoreValue.hasOne and scoreValue.hasFour)
        msg.play "drama"

      if game.gameover()
        if scoreValue.score == 24
          if scoreValue.taken.steps == 1
            msg.play "yeah"
          else
            msg.play "pushit"
        else if scoreValue.taken.steps == 1 and scoreValue.hasOne and scoreValue.hasFour
          msg.play "live"
        else if scoreValue.score >= 22
          msg.play "tada"
        else if scoreValue.score >= 18
          msg.play "greatjob"
        else if scoreValue.score > 0
          msg.play "crickets"
        else
          msg.play "trombone"

        @playerdata[player.id].lastScore = scoreValue.score
        @playerdata[player.id].totalScore = scoreValue.score + @playerdata[player.id].totalScore
        @playerdata[player.id].totalGamesFinished = @playerdata[player.id].totalGamesFinished + 1
        delete @games[player.id]
    else
      msg.reply "you aren't playing a game."

    @save()

  playerstats: (player) ->
    data = @playerdata[player.id]
    return "You have not played a game yet." unless data?
    average = if data.totalGamesFinished > 0
      data.totalScore / data.totalGamesFinished
    else
      0
    """
\n\tlast score: #{data.lastScore}
\taverage score: #{average}
\ttotal games finished: #{data.totalGamesFinished}
\ttotal games started: #{data.totalGamesStarted}
"""

  save: ->
    @robot.brain.data.buddhagames = @games
    @robot.brain.data.playerdata = @playerdata

  stats: (msg) ->
    return_str = ""
    for k,v of @players
      player = k
      return_str += @playerstats(v)
    msg.reply return_str

  reset: (msg, player) ->
    delete @playerdata[player.id]
    msg.reply "Your stats have been reset."
    @save()

module.exports = (robot) ->

  buddha = new BuddhaLounge robot
  
  robot.hear /buddha start|dice start|bdstart/i, (msg) ->
    buddha.startGame msg, msg.message.user

  robot.hear /buddha stats|dice stats/i, (msg) ->
    msg.reply buddha.playerstats(msg.message.user)

  robot.hear /buddha stats all|dice stats all|bdstat/i, (msg) ->
    buddha.stats msg

  robot.hear /buddha reset|dice reset/i, (msg) ->
    buddha.reset msg, msg.message.user

  robot.hear /(buddha take|dice take|bdt) ([\w .-]+)/i, (msg) ->
    buddha.take msg, msg.message.user, msg.match[2]
Something went wrong with that request. Please try again.