Skip to content

Commit

Permalink
Fugly but it works
Browse files Browse the repository at this point in the history
  • Loading branch information
leereilly committed Jan 29, 2012
1 parent ae61fa8 commit 00cad0d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
12 changes: 10 additions & 2 deletions README.md
Expand Up @@ -84,9 +84,15 @@

Takes the following parameters:

* username
* levelId
* username (optional)
* email (optional)
* score
* score (optional)
* explosions
* platforms
* spawn_points
* trees
* pickups

**NOTE:** Every other key/value pair provided is expected to be a recorded stat. E.g. the following request does the following:

Expand All @@ -98,6 +104,8 @@ Takes the following parameters:
Example request:

curl -d "username=leereilly&email=lee@leereilly.net&score=10&explosions=4&deaths=5&trees=4" http://falconhood.heroku.com/api/v1/report
curl -d "levelId=1&explosions=1&platforms=2&spawn_points=3&trees=4&pickups=5" http://falconhood.heroku.com/api/v1/report


Example response:

Expand Down
29 changes: 15 additions & 14 deletions lib/api/v1.rb
Expand Up @@ -38,7 +38,7 @@ def statistics_report(user)

# If a score is provided, update the users' score
if params.has_key? 'score'
@score = Score.create(:user_id => @user.id, :score => params[:score])
score = Score.create(:user_id => @user.id, :score => params[:score])
params.delete('score')
end
end
Expand All @@ -52,29 +52,30 @@ def statistics_report(user)
params.each do |key, val|
if @user
# update users stats
@user_stat = UserStatistic.find_or_create(:user_id => @user.id, :name => key_prep + key)
@user_stat.counter = @user_stat.counter + val.to_i
@user_stat.save
user_stat = UserStatistic.find_or_create(:user_id => @user.id, :name => (key_prep + key))
user_stat.counter = user_stat.counter + val.to_i
user_stat.save
end

# update global stats
@stat = Statistic.find_or_create(:name => key_prep + key)
@stat.counter = @stat.counter + val.to_i
@stat.save
stat = Statistic.find_or_create(:name => (key_prep + key))
stat.counter = stat.counter + val.to_i
stat.save
end

# Increment game counters
@stat = Statistic.find_or_create(:name => 'total_games')
@stat.counter = @stat.counter + 1
@stat.save
games = Statistic.find_or_create(:name => 'total_games')
games.counter = games.counter + 1
games.save

if @user
@user_stat = UserStatistic.find_or_create(:user_id => @user.id, :name => 'total_games')
@user_stat.counter = @user_stat.counter + 1
@user_stat.save
puts "We got another user"
user_stat = UserStatistic.find_or_create(:user_id => @user.id, :name => 'total_games')
user_stat.counter = @user_stat.counter + 1
user_stat.save
end

statistics_report(@user).to_json
Statistic.all_of_the_things.to_json
end

# This retrieves all the things
Expand Down

0 comments on commit 00cad0d

Please sign in to comment.