Added functionality to add a benchmark result

This commit is contained in:
Gregory Ballantine 2023-07-05 23:39:08 -04:00
parent 691e2e9b1e
commit 7655b75410
6 changed files with 142 additions and 2 deletions

View File

@ -20,7 +20,6 @@ Sequel.migration do
create_table(:results) do
primary_key :id
String :name, null: false
String :score, null: false
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP

View File

@ -1,6 +1,6 @@
class Result < Sequel::Model
many_to_one :hardware
many_to_one :benchmarks
many_to_one :benchmark
end

View File

@ -1,3 +1,4 @@
require_relative 'index'
require_relative 'hardware'
require_relative 'benchmark'
require_relative 'result'

36
src/routes/result.rb Normal file
View File

@ -0,0 +1,36 @@
class GameData < Sinatra::Base
get '/result' do
results = Result.reverse(:updated_at).limit(10).all()
erb :'result/index', locals: {
title: 'List of Results',
results: results
}
end
get '/result/add' do
hardware = Hardware.all()
benchmarks = Benchmark.all()
erb :'result/add', locals: {
title: 'Add Result',
hardware: hardware,
benchmarks: benchmarks
}
end
post '/result/add' do
benchmark = Benchmark.where(:id => params[:result_benchmark]).first()
formatted_score = params[:result_average]
if benchmark.scoring == 'fps'
formatted_score = params[:result_average] + ':' + params[:result_minimum] + ':' + params[:result_maximum]
end
result = Result.create(
hardware_id: params[:result_hardware],
benchmark_id: params[:result_benchmark],
score: formatted_score
)
redirect "/result"
end
end

60
views/result/add.erb Normal file
View File

@ -0,0 +1,60 @@
<div class="grid-x grid-margin-x">
<div class="cell small-12">
<h1>Add new result</h1>
</div>
</div>
<div class="grid-x grid-margin-x">
<form class="cell small-12" action="/result/add" method="post">
<div class="grid-x grid-padding-x">
<div class="cell medium-6">
<label>
Hardware:
<select name="result_hardware">
<% hardware.each do |h| %>
<option value="<%= h.id %>"><%= h.name %></option>
<% end %>
</select>
</label>
</div>
<div class="cell medium-6">
<label>
Benchmark:
<select name="result_benchmark">
<% benchmarks.each do |b| %>
<option value="<%= b.id %>"><%= b.name %></option>
<% end %>
</select>
</label>
</div>
</div>
<div class="grid-x grid-padding-x">
<div class="cell medium-4">
<label>
Average score:
<input type="number" name="result_average" value="0.0" step="0.01">
</label>
</div>
<div class="cell medium-4">
<label>
Minimum score:
<input type="number" name="result_minimum" value="0.0" step="0.01">
</label>
</div>
<div class="cell medium-4">
<label>
Maximum score:
<input type="number" name="result_maximum" value="0.0" step="0.01">
</label>
</div>
</div>
<input type="submit" class="button" value="Submit">
</form>
</div>

44
views/result/index.erb Normal file
View File

@ -0,0 +1,44 @@
<div class="grid-x grid-margin-x">
<div class="cell small-12">
<h1>List of results</h1>
</div>
<div class="cell small-12">
<p>
<a href="/result/add">Add new result</a>
</p>
</div>
</div>
<div class="grid-x grid-margin-x">
<% if results.length > 0 %>
<div class="cell small-12">
<table>
<thead>
<tr>
<th>Hardware</th>
<th>Benchmark</th>
<th>Score (type)</th>
<th>Date added</th>
<th>Date modified</th>
</tr>
</thead>
<tbody>
<% results.each do |r| %>
<tr>
<td><%= r.hardware.name %></td>
<td><%= r.benchmark.name %></td>
<td><%= r.score %> (<%= r.benchmark.scoring %>)</td>
<td><%= date_format(r.created_at) %></td>
<td><%= date_format(r.updated_at) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="cell small-12">
<p>I'm sorry, there don't appear to be any results added yet. Check again later!</p>
</div>
<% end %>
</div>