Skip to content
Newer
Older
100644 174 lines (149 sloc) 3.9 KB
ec58c49 @schacon test sinatra app
authored Jun 29, 2009
1 require 'rubygems'
2 require 'sinatra'
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
3 require 'dm-core'
4 require 'open-uri'
5 require 'json'
ec58c49 @schacon test sinatra app
authored Jun 29, 2009
6
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
7 ## -- DATABASE STUFF --
add9961 @schacon remove testing
authored Jun 29, 2009
8
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
9 DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/local.db")
10
11 class ContestEntry
12 include DataMapper::Resource
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
13 has n, :scores
14 has n, :pushes
add9961 @schacon remove testing
authored Jun 29, 2009
15 property :id, Serial
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
16 property :name, String, :key => true
17 property :owner, String
18 property :email, String
19 property :description, String
20 property :homepage, String
21 property :entered, DateTime
22 property :highscore, Integer
f75ccb0 @schacon leaderboard improvements
authored Jun 29, 2009
23
24 def nwo
25 self.owner + '/' + self.name
26 end
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
27 end
28
29 class Score
30 include DataMapper::Resource
31 belongs_to :contest_entry
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
32 belongs_to :push
33 property :id, Serial
34 property :score, Integer
35 end
36
37 class Push
38 include DataMapper::Resource
683c0c2 @schacon action page tweaks
authored Jul 29, 2009
39 has 1, :score
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
40 belongs_to :contest_entry
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
41 property :id, Serial
42 property :ref, String
43 property :sha, String
59ec1cd @schacon fix debug page, dont process unless its new
authored Jul 28, 2009
44 property :results_sha, String, :index => true
f606da7 @schacon more debug information
authored Jul 28, 2009
45 property :message, String
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
46 property :entered, DateTime
47 end
48
49 DataMapper.auto_upgrade!
50
51 ## -- WEBSITE STUFF --
52
1bd87ee @schacon index?
authored Jun 29, 2009
53 get '/' do
54 File.read('public/index.html')
55 end
56
866785c @schacon register page
authored Jun 29, 2009
57 get '/register' do
58 erb :register
59 end
60
7a1645e @schacon added leaderboard
authored Jun 29, 2009
61 # leaderboard api
62 get '/leaderboard' do
63 @entries = ContestEntry.all(:order => [:highscore.desc])
8f53af9 @schacon test
authored Jun 29, 2009
64 erb :leaderboard
7a1645e @schacon added leaderboard
authored Jun 29, 2009
65 end
66
67 # individual project data
68 get '/p/:user/:repo' do
08debe1 @schacon fix relative paths
authored Jun 29, 2009
69 @entry = ContestEntry.first({:name => params[:repo], :owner => params[:user]})
83c2d96 @schacon project page
authored Jun 29, 2009
70 if @entry
71 erb :project
72 else
73 erb :notfound
74 end
7a1645e @schacon added leaderboard
authored Jun 29, 2009
75 end
76
fbe9274 @schacon add action page
authored Jul 29, 2009
77 get '/action' do
78 @entries = ContestEntry.all(:order => [:id.desc], :limit => 10)
79 @scores = Score.all(:order => [:id.desc], :limit => 10)
80 @pushes = Push.all(:order => [:id.desc], :limit => 25)
81 erb :action
add9961 @schacon remove testing
authored Jun 29, 2009
82 end
83
334f3d5 @schacon testing
authored Jun 29, 2009
84 # post receive handler
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
85 post '/' do
86 push = JSON.parse(params[:payload])
87
88 repo = push['repository']
89 repo_name = repo['name']
90 owner = repo['owner']['name']
91 after = push['after']
92
93 # get or create the entry
e0b8ac1 @schacon dm error
authored Jun 29, 2009
94 entry = ContestEntry.first(:name => repo_name, :owner => owner)
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
95 if !entry
96 entry = ContestEntry.new
1a37242 @schacon entry info
authored Jun 29, 2009
97 entry.attributes = {:name => repo_name, :owner => owner, :entered => Time.now()}
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
98 entry.save
99 # email to congratulate for joining?
100 end
1a37242 @schacon entry info
authored Jun 29, 2009
101 entry.homepage = repo['homepage']
102 entry.description = repo['description']
103 entry.email = repo['owner']['email']
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
104
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
105 # save the push information
106 pu = Push.new
107 pu.sha = after
108 pu.ref = push['ref']
109 pu.entered = Time.now()
110 pu.save
111 entry.pushes << pu
112 entry.save
113
114 # look for a changed file
115 new_results = false
116 tree_url = "http://github.com/api/v2/json/tree/show/#{owner}/#{repo_name}/#{after}"
117 tree = open(tree_url) do |f|
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
118 f.read
119 end
120
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
121 new_tree = JSON.parse(tree)
a42f554 @schacon checking the tree info properly
authored Jul 28, 2009
122 new_tree['tree'].each do |f|
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
123 if f['name'] == 'results.txt'
59ec1cd @schacon fix debug page, dont process unless its new
authored Jul 29, 2009
124 if !Push.first(:results_sha => f['sha'])
125 new_results = true
126 pu.message = 'processing new results.txt file'
127 else
128 pu.message = 'no new results.txt file'
129 end
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
130 pu.results_sha = f['sha']
131 pu.save
132 end
133 end
134
135 if new_results
136 # read the results
137 raw = "http://github.com/#{owner}/#{repo_name}/raw/#{after}/results.txt"
138 results = open(raw) do |f|
139 f.read
140 end
141
142 if results
143 key = JSON.parse(File.read('key.json'))
144 score = 0
3fc2979 @schacon fixup project page
authored Jun 29, 2009
145
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
146 # verify that it is the right format (add to error if not)
147 results.split("\n").each do |guess|
148 (uid, rids) = guess.split(':')
149 next if !key[uid]
150 next if !rids
151 rids = rids.split(',')[0, 10] # verify that each entry only has up to 10 guesses
152 if rids.include? key[uid]
153 key.delete uid # verify that each entry is only there once
154 score += 1
155 end
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
156 end
a5882ed @schacon front page?
authored Jun 29, 2009
157
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
158 if score > 0
159 sc = Score.new
160 sc.score = score
161 sc.push = pu
162 sc.save
163 entry.scores << sc
164 hs = entry.highscore || 0
165 if score > hs
166 entry.highscore = score
167 end
168 entry.save
e8888e6 @schacon highscore
authored Jun 29, 2009
169 end
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
170 end
db53532 @schacon saving push data and looking for new results
authored Jul 28, 2009
171
1e15f20 @schacon beginning of post receive
authored Jun 29, 2009
172 end
ec58c49 @schacon test sinatra app
authored Jun 29, 2009
173 end
Something went wrong with that request. Please try again.