Skip to content

Instantly share code, notes, and snippets.

@tphummel
Last active April 16, 2016 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tphummel/f757412870387c9897b9 to your computer and use it in GitHub Desktop.
Save tphummel/f757412870387c9897b9 to your computer and use it in GitHub Desktop.
live-updating bowling scoreboard
node_modules
playerGames: [
name: "Colin"
frames: ["2-", "-2","-1","x","x","x","17","5-","5/","17"]
,
name: "Jon"
frames: ["X", "8-","31","32","5-","3-","1/","81","8-","51"]
,
name: "Jesse"
frames: ["-4", "8-","41","x","5-","x","-2","-9","17","-5"]
,
name: "Kyle"
frames: ["--", "7-","4-","14","23","31","-1","15","x","4-"]
,
name: "Jordan"
frames: ["x", "-3","8-","-/","-5","51","x","1/","17","5/5"]
,
name: "Tom"
frames: ["--","5/","24","17","x","x","1/","13","x","-/1"]
,
name: "Mike"
frames: ["61","--","--","--","--","-2","-3","22","-5","-8"]
,
]
playerGames: [
name: "Fast Eddie"
frames: ["-/","1-","54","3-","1/","1/","1/","-1","12","24"]
,
name: "Spare Me"
frames: ["6/", "6/","12","33","9-","9-","15","x","9-","xx-"]
,
name: "Jordan"
frames: ["x","8-","1-","23","1/","5/","8-","52","x","-1"]
,
name: "Saba"
frames: ["6-","4/","7-","9-","4-","5-","x","1-","51","x17"]
,
name: "Strike Lane"
frames: ["2-","5-","-7","9-","45","17","--","-7","x","x-2"]
,
name: "Senor Bolos"
frames: ["6-","3-","7-","-3","23","3-","23","9-","4-","51"]
,
name: "$cheddar$$$$"
frames: ["13","5-","21","5-","5-","x","2-","x","x","5-"]
,
name: "Ian"
frames: ["7-","26","14","5-","1/","24","8-","x","52","x8-"]
,
name: "Triple North"
frames: ["8-","45","8-","5/","7-","8-","17","-8","3-","8/x"]
,
name: "Kyle"
frames: ["x","-6","x","25","-2","x","x","53","4-","5-"]
,
name: "Tom"
frames: ["7-","72","43","1/","32","X","8-","4-","32","16"]
,
name: "Dart Vader"
frames: ["x","5-","3-","x","53","-8","9-","13","-7","2/x"]
]
playerGames: [
name: "Tim"
frames: ['x','x','x','-2','2/','1-','x','-2','1/','1-']
,
name: "Allen"
frames: ['-2','-7','5-','2/','5-','8-','x','2/','51','x24']
,
name: "Colin"
frames: ['17','16','x','17','x','13','x', '8/','8-','5/x']
,
name: "Simon"
frames: ['x','7-','5-','4-','-/', '4-', '-5','8-','9-','62']
,
name: "Tom"
frames: ['24','6-','8-', '2/', '17', '8-', '8-', '1/', '2/','xxx']
,
name: "Brittany"
frames: ['-8','6-','5-', 'x', '5-','-5','7-', '5-', '7-','6-']
,
name: "Games"
frames: ['x','4-','1/','x','x','1/','23','-5','1/','1/7']
]
playerGames: [
name: "Tim"
frames: ["-/", "4/","13","X","5-","2-","2-","-1","52","4-"]
,
name: "Colin"
frames: ["3/","2/","X","X","8-","x","x","5/","1/","x5/"]
,
name: "Games"
frames: ["52", "X","X","x","13","x","x","x","x","5/8"]
,
name: "Allen"
frames: ["81","-2","4-","5/","X","8-","9-","4-","x","21"]
,
name: "Monica"
frames: ["7-","3-","9-","-3","3-","4/","x","--","3-","41"]
,
name: "Tom"
frames: ["21","51","5-","x","-5","12","5-","12","1-","5/5"]
]
playerGames: [
name: "Tom"
frames: ["-/", "5/", "1/", "5-", "x", "1/", "51","x","9-","-/x"]
,
name: "Colin"
frames: ["4-", "33", "13", "1/", "x", "x", "x", "1/", "x", "x17"]
,
name: "Games"
frames: ["1-", "13", "x", "x", "5/", "5-", "5-", "13", "8-","x13"]
,
name: "Andrea"
frames: ["4-", "x", "-6", "5-", "8-", "5-", "9-", "43", "4-", "32"]
,
name: "Simon"
frames: ["2-", "5-", "x", "25", "x", "6-", "3-", "4-", "11", "6-"]
]

live-updating bowling scoreboard

bowling-scoreboard

install

git clone https://gist.github.com/f757412870387c9897b9.git bowling-scoreboard
cd bowling-scoreboard
npm install

start a new game

npm run new
npm run start

add results

update the new <date>-id.cson file in an editor. results will be automatically updated in your terminal window on file save

errors

if you enter invalid cson or invalid bowling scores the file watcher will exit. fix the file and then run npm start again.

path = require 'path'
fs = require 'fs'
cs = require 'coffee-script'
watch = require 'watch'
bowling = require 'bowling'
moment = require 'moment'
cliTable = require 'cli-table'
gameToTable = (gameData) ->
table = new cliTable
head: ['','1','2','3','4','5','6','7','8','9','10']
style:
'padding-left': 3
gameData.playerGames.forEach (pg) ->
row = {}
scoredFrames = bowling pg.frames
formattedFrames = scoredFrames.map (frame) ->
"#{frame.outcome ? ''}\n#{frame.cumulative ? ''}"
row["#{pg.name}\n"] = formattedFrames
table.push row
return table
clearTerminal = -> process.stdout.write '\u001B[2J\u001B[0;0f'
printHeader = (file, stat) ->
console.log ' game:', file
created = moment stat.ctime
updated = moment stat.mtime
[created,updated].forEach (ts, i) ->
console.log "#{if i is 0 then 'created' else 'updated'}:"
, (ts.format 'HH:mm:ss')
, "(#{ts.fromNow()})"
padIncompleteFrames = (table) ->
table.forEach (row) ->
(Object.keys row).forEach (key) ->
while row[key].length < 10
row[key].push ""
isCson = (file) ->
isCson = /\.cson$/.test file
return isCson
opts =
ignoreDirectoryPattern: /node_modules|\.git/
filter: isCson
watch.createMonitor '.', opts, (monitor) ->
monitor.on 'changed', (filename, currStat, prevStat) ->
clearTerminal()
gameDataStr = (fs.readFileSync (path.join '.', filename)).toString()
gameData = cs.eval gameDataStr
printHeader filename, currStat
table = gameToTable gameData
padIncompleteFrames table
console.log table.toString()
playerGames: [
name: "Tom"
frames: []
,
name: "Colin"
frames: []
,
name: "Badar"
frames: []
,
name: "Jordan"
frames: []
]
{
"name": "node-bowling-scoreboard",
"version": "1.0.0",
"description": "",
"main": "index.coffee",
"dependencies": {
"bowling": "^1.2.2",
"watch": "^0.13.0",
"cli-table": "^0.3.1",
"moment": "^2.9.0",
"coffee-script": "^1.9.0"
},
"devDependencies": {},
"scripts": {
"start": "coffee index.coffee",
"new": "cp new-game.cson `date +'%Y-%m-%d-%H%M'`.cson",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@gist.github.com:/f757412870387c9897b9.git"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment