Skip to content

Commit c596826

Browse files
committed
Added score function, reworked join_team url
1 parent 02d4102 commit c596826

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

app/controllers/players_controller.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ def join_team
8080
end
8181
end
8282

83+
def score
84+
@player = Player.find(params[:id])
85+
@leader = Player.find(params[:leader_id])
86+
if @player.team && @player.team.leader_id == @leader.id then
87+
@playerscore = PlayerScore.new
88+
@playerscore.points = params[:points]
89+
@playerscore.team_id = @player.team_id
90+
@playerscore.player_id = @player.id
91+
respond_to do |format|
92+
if @playerscore.save
93+
format.xml { head :ok }
94+
format.json { head :ok }
95+
else
96+
format.xml { render :xml => @playerscore.errors, :status => :unprocessable_entity }
97+
format.json { render :json => @playerscore.errors, :status => :unprocessable_entity }
98+
end
99+
end
100+
else
101+
head :unauthorized
102+
end
103+
end
104+
83105
# DELETE /players/1
84106
# DELETE /players/1.xml
85107
def destroy

app/models/player_score.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class PlayerScore < ActiveRecord::Base
22
belongs_to :team
33
belongs_to :player
4+
5+
validates_numericality_of :points
46
end

app/views/web/api.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<h2>GET to http://joysti.cc/players/{id}.json</h2>
1818
<script src="https://gist.github.com/985087.js?file=gistfile1.json"></script>
1919
<h1>Add a Player to a Team</h1>
20-
<h2>POST to http://joysti.cc/players/join_team/{player id}.json - <b>access_key</b>, <b>game_password</b>, and <b>team_id</b> are required</h2>
20+
<h2>POST to http://joysti.cc/players/{player id}/join_team.json - <b>access_key</b>, <b>game_password</b>, and <b>team_id</b> are required</h2>
2121
<script src="https://gist.github.com/985116.js?file=gistfile1.json"></script>
2222
<h2>Response</h2>
2323
<script src="https://gist.github.com/985117.js?file=gistfile1.json"></script>

config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
match 'api' => 'web#api'
1111

12-
match 'players/join_team/:id(.:format)' => 'players#join_team', :via => :post
13-
12+
match 'players/:id/join_team(.:format)' => 'players#join_team', :via => :post
13+
match 'players/:id/score(.:format)' => 'players#score', :via => :post
1414
# The priority is based upon order of creation:
1515
# first created -> highest priority.
1616

0 commit comments

Comments
 (0)