Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Most of the match logic is finished and working
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieCraig committed Feb 23, 2011
1 parent 7bc5be8 commit 591fa1b
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 151 deletions.
73 changes: 73 additions & 0 deletions app/controllers/matches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def create
if cannot? :create, @match
redirect_to "/hax.html"
end
@match.round = 1
@match.first_bot_round1_score = 0
@match.first_bot_round2_score = 0
@match.first_bot_round3_score = 0
@match.second_bot_round1_score = 0
@match.second_bot_round2_score = 0
@match.second_bot_round3_score = 0

respond_to do |format|
if @match.save
Expand Down Expand Up @@ -92,4 +99,70 @@ def destroy
format.xml { head :ok }
end
end

def start_round
@match = Match.find(params[:id])

if cannot? :manage, @match then
redirect_to "/hax.html"
end

@match.update_attributes(:start => Time.now.utc)

respond_to do |format|
format.html { redirect_to(@match) }
format.xml { head :ok }
end
end

def grant_point
@bot = Sumobot.find(params[:bot_id])
@match = Match.find(params[:id])

if cannot? :manage, @match then
redirect_to "/hax.html"
end

if not @match.start
respond_to do |format|
format.html { redirect_to(@match, :notice => 'You must first start the round!') }
format.xml { render :xml => "error" , :status => "Round not started" }
end
return
end

round = @match.round
if @match.first_bot_id == @bot.id then
case round
when 1 then
@match.update_attributes(:first_bot_round1_score => 1)
when 2 then
@match.update_attributes(:first_bot_round2_score => 1)
when 3 then
@match.update_attributes(:first_bot_round3_score => 1)
end
elsif @match.second_bot_id == @bot.id then
case round
when 1 then
@match.update_attributes(:second_bot_round1_score => 1)
when 2 then
@match.update_attributes(:second_bot_round2_score => 1)
when 3 then
@match.update_attributes(:second_bot_round3_score => 1)
end
end

if @match.bot1_final_score == 2 or
@match.bot2_final_score == 2 then
@match.update_attributes(:winning_bot => @bot.id)
else
@match.update_attributes(:round => round + 1, :start => nil)
end

respond_to do |format|
format.html { redirect_to(@match) }
format.xml { head :ok }
end

end
end
12 changes: 10 additions & 2 deletions app/controllers/sumobots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ class SumobotsController < ApplicationController
# GET /sumobots
# GET /sumobots.xml
def index
@sumobots = Sumobot.all
owner_id = params[:owner_id]
if owner_id then
@sumobots = Sumobot.find(owner_id)
else
@sumobots = Sumobot.all
end

respond_to do |format|
format.html # index.html.erb
Expand All @@ -26,7 +31,6 @@ def show
# GET /sumobots/new
# GET /sumobots/new.xml
def new
@contender =
@sumobot = Sumobot.new

respond_to do |format|
Expand All @@ -48,6 +52,10 @@ def edit
def create
@sumobot = Sumobot.new(params[:sumobot])
@sumobot.contender_id = current_contender.id
@sumobot.wins = 0
@sumobot.loses = 0
@sumobot.ties = 0
@sumobot.matches = 0

respond_to do |format|
if @sumobot.save
Expand Down
14 changes: 14 additions & 0 deletions app/models/match.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
class Match < ActiveRecord::Base

def validate
errors.add_to_base "You must specify different robots to battle!" if first_bot_id == second_bot_id
end

def bot1_final_score
final = first_bot_round1_score + first_bot_round2_score + first_bot_round3_score
return final
end

def bot2_final_score
final = second_bot_round1_score + second_bot_round2_score + second_bot_round3_score
return final
end
end
3 changes: 3 additions & 0 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<p><%= f.submit "Update" %></p>
<% end %>

<p>
<%= link_to 'My Sumobots', sumobots_path, :owner_id => current_contender.id %></p>

<h3>Cancel my account</h3>

<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p>
Expand Down
9 changes: 2 additions & 7 deletions app/views/home/admin.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@
- @sumobots = Sumobot.find(:all)
= "A total of #{@sumobots.size} sumobots are registered."
%br
%b= "Quick Match"
- form_for :match, :url => new_match_path, :html => {:method => 'post'} do |f|
First Bot:
= f.select("first_bot_id", @sumobots.collect {|bot| [bot.botname, bot.id] })
Second Bot:
= f.select("first_bot_id", @sumobots.collect {|bot| [bot.botname, bot.id] })
= submit_tag "Start Match"
= link_to "Quick Match", new_match_path

49 changes: 0 additions & 49 deletions app/views/matches/_form.html.erb

This file was deleted.

18 changes: 18 additions & 0 deletions app/views/matches/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- @sumobots = Sumobot.find(:all)
= form_for(@match) do |f|
- if @match.errors.any?
#error_explanation
%h2
= pluralize(@match.errors.count, "error")
prohibited this match from being saved:

%ul
- @match.errors.full_messages.each do |msg|
%li= msg

.field
= f.select("first_bot_id", @sumobots.collect {|bot| [bot.botname, bot.id] })
%b= " Vs. "
= f.select("second_bot_id", @sumobots.collect {|bot| [bot.botname, bot.id] })
.actions
= f.submit
37 changes: 0 additions & 37 deletions app/views/matches/index.html.erb

This file was deleted.

36 changes: 36 additions & 0 deletions app/views/matches/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%h1 Listing matches

%table
%tr
%th First bot
%th Second bot
%th Status
%th
%th
%th

- @matches.each do |match|
- first_bot = Sumobot.find(match.first_bot_id)
- second_bot = Sumobot.find(match.second_bot_id)
%tr
%td= first_bot.botname
%td= second_bot.botname
%td
- if match.winning_bot then
= "#{Sumobot.find(match.winning_bot).botname} Won"
- if match.winning_bot == match.first_bot_id then
= "#{match.bot1_final_score} - #{match.bot2_final_score}"
- else
= "#{match.bot2_final_score} - #{match.bot1_final_score}"
- elsif match.start then
In Progress
- else
= "Round #{match.round} Not Started"
%td= link_to 'Show', match
- if can? :manage, match then
%td= link_to 'Edit', edit_match_path(match)
%td= link_to 'Destroy', match, :confirm => 'Are you sure?', :method => :delete

%br

= link_to 'New Match', new_match_path
5 changes: 0 additions & 5 deletions app/views/matches/new.html.erb

This file was deleted.

5 changes: 5 additions & 0 deletions app/views/matches/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%h1 Create a quick match

= render :partial => 'form'

= link_to 'Back', matches_path
45 changes: 0 additions & 45 deletions app/views/matches/show.html.erb

This file was deleted.

Loading

0 comments on commit 591fa1b

Please sign in to comment.