-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassign-tasks.js
109 lines (86 loc) · 2.46 KB
/
assign-tasks.js
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
var template = require('./template')
var opts = require('minimist')(process.argv.slice(2))
var cont = require('cont')
var Mentions = require('ssb-mentions')
var fs = require('fs')
var path = require('path')
if(!Number.isInteger(opts.week)) throw new Error('--week should be a number')
var MT = new (require('rng').MT)(opts.week)
var random = MT.random.bind(MT)
function fromCSV (data) {
if(!data) return undefined
var obj = {}
data.split('\n').filter(Boolean).forEach(function (line) {
var parts = line.split(/(?:,|\s)+/)
console.error(parts)
obj[parts[0]] = parts[1]
})
return obj
}
function shuffel (ary) {
if(!ary) return ary
return ary.sort(function () {
return random() - 0.5
})
}
function fromList (s) {
if(!s) return undefined
return s.split(/\s+/)
}
function read (name) {
return fs.readFileSync(opts[name] || path.join(__dirname, name+'.csv'), 'utf8')
}
var names_ordered = fromCSV(read('names'))
var names = {}
shuffel(Object.keys(names_ordered)).forEach(function (name) {
names[name] = names_ordered[name]
})
var num = Object.keys(names).length
var issues = shuffel(fromList(read('issues')))
var modules = shuffel(fromList(read('modules')))
function getPer (list, num) {
return Math.floor((list && list.length || 0) / num) || 1
}
function getNext(list, per) {
if(!list) return []
return list.splice(0, per)
}
function getRest (list, per) {
if(!list) return []
return list.splice(per)
}
var issuesPer = getPer(issues, num)
var modulesPer = getPer(modules, num)
console.log('issues per:', issuesPer, 'modules pes:', modulesPer)
var VillageTrackerBot = '@KYq92uiEKPcKZIBJ/ZCGYErdcy8n0RYD/nDUtKmuyZU=.ed25519'
require('ssb-client')(function (err, sbot) {
var msgs = []
for(var name in names) {
var id = names[name]
var _issues
var _issues = getNext(issues, issuesPer)
var _modules = getNext(modules, modulesPer)
var data = {
name, id, issues: _issues, modules: _modules, week: opts.week, checklist: opts.checklist
}
var text = template(data)
var content = {
type: 'post',
text: text,
mentions: Mentions(text),
channel: 'village-tracker'
}
msgs.push(content)
}
if(opts.dry) return console.log(JSON.stringify(msgs, null, 2))
cont.series(msgs.map(function (content) {
return function (cb) {
sbot.identities.publishAs({
id: VillageTrackerBot,
content: content
}, cb)
}
}))(function (err) {
if(err) throw err
})
})