Files
  • main.rb
  • candidates-wg-voting-booth
  • data.csv
  • data_id.csv
  • data_id.json
main.rb
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
require 'json'

$candidates = %w(twinner	eaglespirit	ehiboss	eonwarped	inertia	jedigeiss	koh	lemouth	llfarms	pgarcgo	redpalestino	reggaemuffin	shadowspub	sorin.cristescu	steemitqa	travisung	nnnarvaez	transisto	aidarojaswriter	ura-soul	enforcer48	bobinson	thecryptodrive	neoxian)


$voters = {}

def record_vote(from, id, to)
  unless $voters.has_key?(from)
    $voters[from] = Voter.new(from, id)
  end
  voter = $voters[from]
  voter.add_vote(to)
end

def record_votes(from, data)
  data.each do | voter |
    id = "#{voter['id']}"
    tag = "#{voter['username']}##{voter['discriminator']}"
    record_vote(tag, id, from)
  end
end

class Voter
  def initialize(tag, id)
    @tag = tag
    @id = id
    @voted = []
  end

  def voted?(name)
    @voted.include?(name)
  end

  def add_vote(name)
    @voted.push(name)
    @voted.uniq!
  end

  def as_csv
    line = "#{@tag},#{@id},"
    line += $candidates.map do |candidate|
      self.voted?(candidate) ? "1" : "0"
    end.join(',')
  end

  def to_json(stuff)
    {
      tag: @tag,
      id: @id,
      voted: @voted
    }.to_json
  end
end

$candidates.each do |candidate|
  voting_data = JSON.parse(File.read("candidates-wg-voting-booth/#{candidate}.json"))
  record_votes(candidate, voting_data)
end

header = "name,id," + $candidates.join(',')

data = $voters.values.map do |voter|
  voter.as_csv
end.join("\n")

File.open('data_id.csv', 'w') { |file| file.write(header + "\n" + data) }
File.open('data_id.json', 'w') { |file| file.write($voters.values.to_json) }
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]