├── .gitignore ├── LICENSE ├── README.md ├── package.json └── raffle.js /.gitignore: -------------------------------------------------------------------------------- 1 | raffle.json 2 | node_modules 3 | *.log 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) <2016> 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Raffle Program For JSConf China 2 | 3 | 4 | ## Algorithm 5 | 6 | Use a weight probability algorithm to calculate probability for each people. And it will follow the rule: 7 | ![image](https://cloud.githubusercontent.com/assets/914595/17977184/349b3868-6b23-11e6-9320-d2d9f906d518.png) 8 | 9 | ## UI 10 | 11 | We use [https://github.com/yaronn/blessed-contrib](https://github.com/yaronn/blessed-contrib) build a simple CLI UI application 12 | ![image](https://cloud.githubusercontent.com/assets/914595/17977176/2c1b79dc-6b23-11e6-84af-d307c79bf15e.png) 13 | 14 | ## How to use 15 | 16 | `git clone git@github.com:jsconfcn/raffle.git` 17 | 18 | `npm i ` 19 | 20 | `node raffle.js` or `node raffle.js 1000` 21 | 22 | default range from [0 ~ 800] 23 | 24 | ## Requirements 25 | 26 | * A CLI program. 27 | * Writen in one of JavaScript/TypeScript/CoffeeScript/Elm/ClojureScript. 28 | * Grow the chance for ppl who didn't win anything. 29 | 30 | ## License 31 | MIT 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raffle", 3 | "version": "0.1.0", 4 | "description": "Raffle cli program for JSConf China", 5 | "main": "raffle.js", 6 | "engines": { 7 | "node": "^4.4.7" 8 | }, 9 | "dependencies": { 10 | "blessed": "^0.1.81", 11 | "blessed-contrib": "^3.5.5" 12 | }, 13 | "devDependencies": {}, 14 | "scripts": { 15 | "test": "npm test" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/jsconfcn/raffle.git" 20 | }, 21 | "keywords": [ 22 | "Raffle", 23 | "JSConf", 24 | "China" 25 | ], 26 | "author": "xeodou@gmail.com", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/jsconfcn/raffle/issues" 30 | }, 31 | "homepage": "https://github.com/jsconfcn/raffle#readme" 32 | } 33 | -------------------------------------------------------------------------------- /raffle.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict' 4 | 5 | const fs = require('fs') 6 | const path = require('path') 7 | const blessed = require('blessed') 8 | const contrib = require('blessed-contrib') 9 | const screen = blessed.screen() 10 | const grid = new contrib.grid({rows: 2, cols: 5, screen: screen}) 11 | 12 | const argv = process.argv 13 | 14 | const name = 'raffle.json' 15 | const file = path.resolve(__dirname, name) 16 | const total = parseInt(argv[2]) || 800 17 | 18 | const store = { 19 | get luckies() { 20 | return read(file) 21 | } 22 | } 23 | 24 | const probability = (C) => { 25 | return (x) => { 26 | return parseFloat((1 / (1 + C * Math.tan(x / 5))).toFixed(4)) 27 | } 28 | } 29 | 30 | const weight = probability(total); 31 | 32 | const write = (luckies, lucky) => { 33 | luckies = luckies.concat(lucky).reduce((p, n) => { 34 | const lucky = p.filter(l => l.number === n.number)[0] 35 | if (lucky) { 36 | p = p.filter(l => l.number !== n.number) 37 | } 38 | n.times = lucky ? lucky.times ++ : (n.times || 1) 39 | delete n.weight 40 | p.push(n) 41 | return p 42 | }, []) 43 | fs.writeFileSync(file, JSON.stringify(luckies)) 44 | } 45 | 46 | const read = (f) => { 47 | if (!fs.statSync(f).isFile()) { 48 | return [] 49 | } 50 | return JSON.parse(fs.readFileSync(f).toString()) 51 | .map(l => { 52 | const wei = weight(l.times) 53 | l.weight = wei > 0 ? wei : 0 54 | return l 55 | }) 56 | } 57 | 58 | const random = (luckies, length) => { 59 | let sum = 0, 60 | res = [], 61 | wei = weight(1); 62 | 63 | for (var l in luckies) { 64 | sum += luckies[l].weight 65 | } 66 | sum += (total - luckies.length) * wei 67 | for (var i = 1; i <= total; i ++) { 68 | const lucky = luckies.filter(l => l.number === i)[0] || {} 69 | const w = lucky.weight === 0 ? 0 : ((lucky.weight || wei) + Math.random() * sum) 70 | res.push(w) 71 | } 72 | return res.map((w, i) => { 73 | return { 74 | number: i + 1, 75 | weight: w 76 | } 77 | }) 78 | .sort((a, b) => b.weight - a.weight) 79 | .map(l => { 80 | delete l.weight 81 | return l 82 | }) 83 | .slice(0, length || 1) 84 | } 85 | 86 | // Probability Graph 87 | const line = grid.set(0, 2, 1, 2, contrib.line, { 88 | style: { 89 | line: 'green', 90 | text: 'green', 91 | baseline: 'white' 92 | }, 93 | xLabelPadding: 3, 94 | xPadding: 5, 95 | label: 'Probability For Each Person' 96 | }) 97 | 98 | line.setData([{ 99 | x: ['0', '1', '2', '3', '4', '5', '6', '7', '8'], 100 | y: [0.01, 0.0061, 0.0029, 0.0018, 0.0012, 0.0008, 0.0005, 0.0002, 0] 101 | }]) 102 | 103 | 104 | // List of lucky guy 105 | var table = grid.set(0, 0, 1, 2, contrib.table, { 106 | keys: true, 107 | fg: 'white', 108 | selectedFg: 'white', 109 | selectedBg: 'blue', 110 | label: 'List Of Lucky Stars', 111 | width: '30%', 112 | height: '30%', 113 | xPadding: '42%', 114 | border: { 115 | type: 'line', 116 | fg: 'cyan' 117 | }, 118 | columnSpacing: 10, //in chars, 119 | columnWidth: [6, 10, 16] /*in chars*/ 120 | }) 121 | 122 | table.setData({ 123 | headers: ['Number', 'Lucky Times', 'Next Probability'], 124 | data: store.luckies.sort((a, b) => b.times - a.times ).map(l => [l.number, l.times, l.weight]) 125 | }) 126 | 127 | var box = grid.set(0, 4, 1, 1, blessed.BigText, { 128 | content: ' P ', 129 | fg: 'yellow' 130 | }) 131 | 132 | var bigText = grid.set(1, 0, 1, 5, blessed.BigText, { 133 | content: '', 134 | scrollable: true, 135 | fg: 'green' 136 | }) 137 | 138 | box.on('click', (mouse) => { 139 | const luckies = store.luckies 140 | const lucky = random(luckies, 1) 141 | 142 | write(luckies, lucky) 143 | 144 | bigText.setContent(`${lucky[0].number} ${bigText.getContent()}`) 145 | table.setData({ 146 | headers: ['Number', 'Lucky Times', 'Next Probability'], 147 | data: store.luckies.sort((a, b) => b.times - a.times ).map(l => [l.number, l.times, l.weight]) 148 | }) 149 | screen.render() 150 | }) 151 | 152 | screen.key(['escape', 'q', 'C-c'], (ch, key) => { 153 | return process.exit(0) 154 | }) 155 | 156 | screen.render() 157 | 158 | 159 | --------------------------------------------------------------------------------