Skip to content

Commit

Permalink
some fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Pirogov committed Sep 21, 2012
1 parent 276cb04 commit 634b864
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README
Expand Up @@ -6,18 +6,18 @@ Copyright (C) 2012 Anton Pirogov
Licensed under the GPLv3

requirements: ruby 1.9.3, sinatra gem
run with: ruby nodegame.rb &
run with: ruby ffnodegame.rb &

look over settings.rb and change the password before launching!

default settings:

The Server loads the nodes.json data every 5 minutes and gives points to Freifunk nodes
The server loads the nodes.json data every hour and gives points to Freifunk nodes

gateway status: +100
each currently connected client: +25
each vpn link: +10 / quality
each meshing link: +50 / quality
each vpn link: +(10/quality)
each meshing link: +(50/quality)
node is offline: -100

reset scores with: http://localhost:1337/reset?pw=<password>
Expand Down
2 changes: 1 addition & 1 deletion nodegame.rb → ffnodegame.rb
Expand Up @@ -4,7 +4,7 @@
#Copyright (C) 2012 Anton Pirogov
#Licensed under The GPLv3

#TODO: eval bonus/malus points added by hand in bonus.json
#TODO: eval bonus/penalty points added by hand in bonus.json
# add other automatic bonus points - eval some infos from mac address?

require 'net/http'
Expand Down
22 changes: 17 additions & 5 deletions generator.rb
Expand Up @@ -32,7 +32,18 @@ def self.execute
update scores, snapshot

scores.sort_by! {|s| s['points']}.reverse!
File.write "public/scores.json", JSON.generate(scores)

scorejson = JSON.generate scores
File.write "public/scores.json", scorejson
return scorejson
end

def self.calc_vpn_points(node)
node['vpns'].map{|e| SC_PERVPN / e}.inject(&:+).to_i
end

def self.calc_mesh_points(node)
node['meshs'].map{|e| SC_PERMESH / e}.inject(&:+).to_i
end

private
Expand Down Expand Up @@ -80,14 +91,15 @@ def self.transform(nodejson)
return routers
end

#calculate points for current round
#calculate sum of points for node in current round
#NOTE: on calc changes, don't forget to check and update erb file
def self.calc_points(node)
points = 0
points += SC_OFFLINE if !node['flags']['online'] #offline penalty
points += SC_GATEWAY if node['flags']['gateway']
points += SC_PERCLIENT * node['clients']
points += node['vpns'].map{|e| SC_PERVPN / e}.inject(&:+).to_i
points += node['meshs'].map{|e| SC_PERMESH / e}.inject(&:+).to_i
points += calc_vpn_points node
points += calc_mesh_points node
return points
end

Expand All @@ -98,7 +110,7 @@ def self.update(scores, data)
if i.nil? #new entry
scores.push n
scores[-1]['points'] = calc_points n
elsif #update preserving points and bonus info
elsif #update preserving points
p = scores[i]['points']
scores[i] = n
scores[i]['points'] = p + calc_points(n)
Expand Down
14 changes: 14 additions & 0 deletions public/style.css
Expand Up @@ -11,6 +11,20 @@ body {
font-size: 14px;
}

a:link {color:black; text-decoration:none}
a:visited {color:black; text-decoration:none}
a:active {color:black; text-decoration:underline}
a:hover {color:blue; text-decoration:none}

.footerlink {
border-style: dashed;
border-width: 1px;
border-color: gray;
border-radius: 5px;
margin-top: 10px;
font-size: 12px;
}

table {
margin-left: auto;
margin-right: auto;
Expand Down
9 changes: 6 additions & 3 deletions settings.rb
Expand Up @@ -10,14 +10,17 @@

#port for server
PORT=1337
#source of node data by ffmap-d3
JSONSRC='http://burgtor.ffhl/mesh/nodes.json'
#Title shown for page - change for other communities
TITLE='Freifunk Lübeck Node Highscores'
#source of node data by ffmap-d3
JSONSRC='http://burgtor.ffhl/mesh/nodes.json'
#link to ffmap-d3 map
GRAPHLINK='http://burgtor.ffhl/mesh/nodes.html'

#password to start/stop updater thread over GET requests
PWD='hackme'
#update interval in minutes
INTERVAL=5
INTERVAL=60

#score values
SC_OFFLINE=-100
Expand Down
18 changes: 7 additions & 11 deletions updater.rb
Expand Up @@ -21,19 +21,15 @@ def self.start
Generator.execute
puts 'Scores updated!' if DEBUG

INTERVAL.times do
20.times do
sleep 5
print '.' if DEBUG
if Thread.current[:stop]
Thread.current[:running]=false
puts 'Stopped updater thread!'
Thread.exit
end
lasttime = Time.now
while (Time.now-lasttime) < INTERVAL*60
sleep 1
if Thread.current[:stop]
Thread.current[:running]=false
puts 'Stopped updater thread!'
Thread.exit
end
puts if DEBUG
end
puts if DEBUG
end
end
return true #started
Expand Down
9 changes: 6 additions & 3 deletions views/index.erb
Expand Up @@ -9,7 +9,7 @@
<body>
<h1 id="title"><%= TITLE %></h1>
<h2 id="lastupd">Letztes Update <%= @lastupdate.strftime('am %d.%m.%Y um %H:%M') %></h2>
<div class="centered">
<div>
<table>
<tr><th>#</th><th>Name</th><th>Letzter Status</th><th>Punkte</th></tr>
<% @scores.each_with_index do |n,i| %>
Expand All @@ -18,12 +18,15 @@
<% if !n['flags']['online'] %>Off: <span class="neg"><%= SC_OFFLINE %></span><% end %>
<% if n['flags']['gateway'] %>Gw: <span class="pos">+<%= SC_GATEWAY %></span><% end %>
<% if n['clients']>0 %>Client: <span class="pos">+<%= SC_PERCLIENT*n['clients'] %></span><% end %>
<% unless n['meshs'].empty? %>Mesh: <span class="pos">+<%=n['meshs'].map{|e| SC_PERMESH / e}.inject(&:+).to_i %></span><% end %>
<% unless n['vpns'].empty? %>Vpn: <span class="pos">+<%= n['vpns'].map{|e| SC_PERVPN / e}.inject(&:+).to_i %></span><% end %>
<% unless n['meshs'].empty? %>Mesh: <span class="pos">+<%= Generator.calc_mesh_points n %></span><% end %>
<% unless n['vpns'].empty? %>Vpn: <span class="pos">+<%= Generator.calc_vpn_points n %></span><% end %>
</td>
<td><%= n['points'] %></td></tr>
<% end %>
</table>
</div>
<a class="footerlink" href="<%= GRAPHLINK %>">Knotengraph</a>
<span class="footerlink">© 2012 Anton Pirogov</span>
<a class="footerlink" href="http://github.com/apirogov/ffnodegame">ffnodegame source</a>
</body>
</html>

0 comments on commit 634b864

Please sign in to comment.