Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
/log/*
!/log/.keep
/tmp

*.DS_Store
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

gem 'bootstrap-sass'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ GEM
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
autoprefixer-rails (6.4.0.1)
execjs
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
builder (3.2.2)
byebug (9.0.5)
coffee-rails (4.1.1)
Expand Down Expand Up @@ -145,6 +150,7 @@ PLATFORMS
ruby

DEPENDENCIES
bootstrap-sass
byebug
coffee-rails (~> 4.1.0)
jbuilder (~> 2.0)
Expand Down
Binary file added app/assets/images/button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .
27 changes: 27 additions & 0 deletions app/assets/javascripts/button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// we want this global timer to be accessed by all user threads

var userOwns = false;

$(document).on('click', '#button', function() {
if(userOwns == false) {
startClock();
userOwns = true;
}
else {
// when someone else clicks button, you lose it and get set to zero
// store session name and time in db

var stopTime = $time.innerHTML;
$.post("/users",
{
time: stopTime
},
function() {
alert("congratz u got a time of: " + stopTime);
}
);
stopClock();
resetClock();
userOwns = false;
}
});
81 changes: 81 additions & 0 deletions app/assets/javascripts/clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
var clsStopwatch = function() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obvi this is copypasta sry we can take out what we dont need later

// Private vars
var startAt = 0; // Time of last start / resume. (0 if not running)
var lapTime = 0; // Time on the clock when last stopped in milliseconds

var now = function() {
return (new Date()).getTime();
};

// Public methods
// Start or resume
this.startClock = function() {
startAt = startAt ? startAt : now();
};

// Stop or pause
this.stopClock = function() {
// If running, update elapsed time otherwise keep it
lapTime = startAt ? lapTime + now() - startAt : lapTime;
startAt = 0; // Paused
};

// Reset
this.resetClock = function() {
lapTime = startAt = 0;
};

// Duration
this.time = function() {
return lapTime + (startAt ? now() - startAt : 0);
};
};

var x = new clsStopwatch();
var $time;
var clocktimer;

function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}

function formatTime(time) {
var h = m = s = ms = 0;
var newTime = '';

h = Math.floor( time / (60 * 60 * 1000) );
time = time % (60 * 60 * 1000);
m = Math.floor( time / (60 * 1000) );
time = time % (60 * 1000);
s = Math.floor( time / 1000 );
ms = time % 1000;

newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3);
return newTime;
}

function showClock() {
$time = document.getElementById('time');
updateClock();
}

function updateClock() {
$time.innerHTML = formatTime(x.time());
}

function startClock() {
clocktimer = setInterval("updateClock()", 1);
x.startClock();
}

function stopClock() {
x.stopClock();
clearInterval(clocktimer);
}

function resetClock() {
stopClock();
x.resetClock();
updateClock();
}
3 changes: 3 additions & 0 deletions app/assets/javascripts/home.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
7 changes: 7 additions & 0 deletions app/assets/javascripts/modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$(window).load(function(){
$('#signinModal').modal('show');
});

function closeModal() {
$('#signinModal').modal('hide');
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
*= require_tree .
*= require_self
*/

@import "bootstrap";
3 changes: 3 additions & 0 deletions app/assets/stylesheets/home.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the home controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
5 changes: 5 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HomeController < ApplicationController
def index
@user = User.new
end
end
19 changes: 18 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
class UsersController < ApplicationController
def new
def create
User.create(
name: session[:name],
time: params[:time]
)
redirect_to root_path
end

def store_user
# so user can stay on same page and reuse session name
session[:name] = user_params[:name]
redirect_to root_path
end

private

def user_params
params.require(:user).permit(:name)
end
end
2 changes: 2 additions & 0 deletions app/helpers/home_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module HomeHelper
end
5 changes: 5 additions & 0 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= image_tag "button.png", id: "button" %>

<div>Time: <span id="time"></span></div>

<%= render partial: 'users/new' %>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<body onload="showClock();">

<%= yield %>

Expand Down
18 changes: 18 additions & 0 deletions app/views/users/_new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="modal fade" id="signinModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Sign in</h4>
</div>
<div class="modal-body">
<%= form_for @user, remote: true, url: store_user_path, method: "post", class: "form-inline" do |f| %>
<%= f.label "Hi! What's your name?" %>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="modal-footer">
<%= f.submit "Play!", class: "btn btn-primary", onclick: "closeModal()" %>
<% end %>
</div>
</div>
</div>
</div>
2 changes: 0 additions & 2 deletions app/views/users/new.html.erb

This file was deleted.

59 changes: 3 additions & 56 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,5 @@
Rails.application.routes.draw do
get 'users/new'

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
root 'home#index'
resources :users
post 'store_user', to: 'users#store_user', as: 'store_user'
end
3 changes: 1 addition & 2 deletions db/migrate/20160615013423_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.integer :score
t.string :time

t.timestamps null: false
end
Expand Down
23 changes: 23 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160615013423) do

create_table "users", force: :cascade do |t|
t.string "name"
t.string "time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
7 changes: 7 additions & 0 deletions test/controllers/home_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class HomeControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end