Permalink
Browse files

added users and save and load

  • Loading branch information...
1 parent 70b3875 commit 8406c7f7de7f940162abbbd8c6a749ae2363ffc0 @Dan12 committed Nov 1, 2015
View
@@ -31,7 +31,7 @@ gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
-# gem 'bcrypt', '~> 3.1.7'
+gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
View
@@ -37,6 +37,7 @@ GEM
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
+ bcrypt (3.1.10)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
@@ -147,6 +148,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ bcrypt (~> 3.1.7)
byebug
coffee-rails (~> 4.1.0)
jbuilder (~> 2.0)
@@ -42,6 +42,7 @@ elementOver = null;
current_song = [];
loaded_song_id = -1;
+loaded_song_name = "";
recordResolution = 100;
recordStartTime = null;
@@ -13,6 +13,27 @@ function setupMouseInputs(){
});
});
+ $("#submit_login").click(function() {
+ $.ajax({
+ type: "POST",
+ url: "/login",
+ data: {username: $("#login_username").val(), password: $("#login_password").val()},
+ success: function(data, textStatus, jqXHR) {
+ console.log(data);
+ // console.log(textStatus);
+ // console.log(jqXHR);
+ if(data.message == "success")
+ $("#login_form").css("display", "none");
+ else
+ alert("There was an error");
+ },
+ error: function(jqXHR, textStatus, errorThrown) {
+ console.log("Error=" + errorThrown);
+ alert("There was an error");
+ }
+ });
+ });
+
$("#editor_canvas").mousemove(function(e){
elementOver = null;
mouseX = e.pageX - $("#editor_canvas").offset().left;
@@ -1,14 +1,15 @@
<!--guitar hero launchpad/DDR launchpad-->
$(document).ready(function(){
-
- loadSounds(sound1Srcs, sounds1, 1);
- loadSounds(sound4Srcs, sounds4, 4);
- loadSounds(sound2Srcs, sounds2, 2);
- loadSounds(sound3Srcs, sounds3, 3);
-
- $(window).resize(function(){
- reformat();
- });
+ if(document.getElementById("editor_canvas") != null){
+ loadSounds(sound1Srcs, sounds1, 1);
+ loadSounds(sound4Srcs, sounds4, 4);
+ loadSounds(sound2Srcs, sounds2, 2);
+ loadSounds(sound3Srcs, sounds3, 3);
+
+ $(window).resize(function(){
+ reformat();
+ });
+ }
});
function loadSounds(srcArr, soundArr, chain){
@@ -97,19 +97,28 @@ function playbackOffsetUpdate(){
}
function saveSong(){
+ if(loaded_song_id == -1)
+ loaded_song_name = prompt("Enter song name");
$.ajax({
type: "POST",
url: "/create_song",
- data: {song_data: current_song},
+ data: {song_data: current_song, id: loaded_song_id, name: loaded_song_name},
success: function(data, textStatus, jqXHR) {
// console.log(data);
// console.log(textStatus);
// console.log(jqXHR);
// console.log(data.data.song_data);
- if(data.data.song_data == "null")
+ if(data.data == "null")
alert("Not Saved, try again");
+ if(data.data == "nli"){
+ alert("You must be logged in");
+ if(confirm("Go to login page?"))
+ $("#login_form").css("display", "block")
+ }
else{
song_data = data.data.song_data;
+ loaded_song_id = data.data.id
+ loaded_song_name = data.data.name
drawLayout();
}
},
@@ -156,6 +165,23 @@ function moveMouseMove(){
selectedPrevY = mouseY;
}
+function loadSongs(){
+ $.ajax({
+ type: "POST",
+ url: "/view_all_songs",
+ data: {},
+ success: function(data, textStatus, jqXHR) {
+ console.log(data);
+ // console.log(textStatus);
+ // console.log(jqXHR);
+ },
+ error: function(jqXHR, textStatus, errorThrown) {
+ console.log("Error=" + errorThrown);
+ alert("There was an error");
+ }
+ });
+}
+
function moveMouseUp(){
singleSelected = null;
}
@@ -165,8 +191,14 @@ function toolFunctionManager(){
startRecording();
if(current_tool == 8 && playingStartTime == null)
startPlaying();
- if(current_tool == 12)
+ if(current_tool == 12){
saveSong();
+ current_tool = 2;
+ }
+ if(current_tool == 13){
+ loadSongs();
+ current_tool = 2;
+ }
}
function toolMouseDownManager(mx, my){
@@ -45,4 +45,21 @@ h1{
font-size: 20px;
display: inline-block;
padding: 4px 8px;
+}
+
+#login_form{
+ display: none;
+ width: 100%;
+ position: fixed;
+ text-align: center;
+ z-index: 20;
+}
+
+#gray_background{
+ display: none;
+ width: 100%;
+ height: 100%;
+ z-index: 10;
+ background-color: rgba(150,150,150,150);
+ position: fixed;
}
@@ -6,4 +6,23 @@ class ApplicationController < ActionController::Base
def getAssetUrl
render :json => {"asset_path" => ActionController::Base.helpers.asset_path("chain#{params["chain"]}/#{params["file_name"]}.mp3"), "sindex" => params["sindex"]}
end
+
+ def index
+ render "index"
+ end
+
+ def logout
+ session[:user_id] = nil
+ render :json => {"message" => "logged out"}
+ end
+
+ def login
+ @user = User.find_by(username: params[:username])
+ if @user and @user.authenticate(params[:password])
+ session[:user_id] = @user.id
+ render :json => {"message" => "success"}
+ else
+ render :json => {"message" => "failed"}
+ end
+ end
end
@@ -1,20 +1,28 @@
class SongController < ApplicationController
def create
- s = Song.new
- s.song_data = params[:song_data]
- if s.save
- render :json => {"data" => s}
+ if session[:user_id] != nil
+ @song = Song.find_by(id: params[:id])
+ if @song
+ @song.song_data = params[:song_data]
+ @song.save
+ render :json => {"data" => @song}
+ else
+ s = Song.new
+ s.song_data = params[:song_data]
+ s.name = params[:name]
+ if s.save
+ render :json => {"data" => s}
+ else
+ render :json => {"data" => "null"}
+ end
+ end
else
- render :json => {"data" => "null"}
+ render :json => {"data" => "nli"}
end
end
- def update
-
- end
-
- def load
-
+ def view_all
+ render :json => {"data" => Song.all}
end
end
View
@@ -0,0 +1,4 @@
+class User < ActiveRecord::Base
+ has_secure_password
+ validates_uniqueness_of :username
+end
@@ -11,4 +11,16 @@
</div>
<div>
<div id="link">Here is the link to the tutorial video which has the project file that I recorded all of the sounds from: <a href="https://www.youtube.com/watch?v=PUfQXaxGNNA" target="_blank">Nev Teaches: How to Play Skrillex - First of the Year (Equinox) Launchpad Tutorial</a></div>
+</div>
+<div id="gray_background"></div>
+<div id="login_form">
+ <div>
+ <label>Username</label>
+ <input type="text" name="username" id="login_username"/>
+ </div>
+ <div>
+ <label>Password</label>
+ <input type="password" name="password" id="login_password"/>
+ </div>
+ <div id="submit_login">Login</div>
</div>
View
@@ -3,8 +3,10 @@
post "/get_asset_path" => "application#getAssetUrl"
+ post "/login" => "application#login"
+ post "/logout" => "application#logout"
+
#song
post "/create_song" => "song#create"
- post "/load_song" => "song#load"
- post "/update_song" => "song#update"
+ post "/view_all_songs" => "song#view_all"
end
@@ -0,0 +1,10 @@
+class CreateUsers < ActiveRecord::Migration
+ def change
+ create_table :users do |t|
+ t.string :username
+ t.string :password_digest
+
+ t.timestamps null: false
+ end
+ end
+end
@@ -0,0 +1,5 @@
+class AddNameSong < ActiveRecord::Migration
+ def change
+ add_column :songs, :name, :string
+ end
+end
View
@@ -11,12 +11,20 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20151031042850) do
+ActiveRecord::Schema.define(version: 20151031232523) do
create_table "songs", force: :cascade do |t|
t.text "song_data"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.string "name"
+ end
+
+ create_table "users", force: :cascade do |t|
+ t.string "username"
+ t.string "password_digest"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
end
end
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ username: MyString
+ password_digest: MyString
+
+two:
+ username: MyString
+ password_digest: MyString
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class UserTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 8406c7f

Please sign in to comment.