Skip to content
Browse files

Lots of stuff

  • Loading branch information...
1 parent f51cb33 commit bb5301dc9f83dbefa9172b92a60f968c79cdf39a @rogerbraun committed Sep 6, 2010
Showing with 101 additions and 12 deletions.
  1. +47 −2 app.rb
  2. BIN db/dev.db
  3. +39 −8 models/Kana.rb
  4. +2 −2 views/index.haml
  5. +8 −0 views/login.haml
  6. +5 −0 views/register.haml
View
49 app.rb
@@ -3,6 +3,8 @@
require "sinatra"
require "haml"
require "datamapper"
+require "rack-flash"
+require "dm-validations"
require "models/Kana.rb"
@@ -11,7 +13,9 @@
DataMapper.auto_upgrade!
-enable :settings
+enable :sessions
+
+use Rack::Flash
facebook do
api_key "095013a6174927028e52bc5c6652be1e"
@@ -24,7 +28,7 @@
helpers do
def logged_in?
- if session[:uid] then true end
+ return true if session[:uid]
if fb[:user] then
session[:uid] = User.get_by_fb(fb[:user]).id
@@ -48,6 +52,47 @@ def current_user
end
+get "/login" do
+ haml :login
+end
+
+get "/learn" do
+ if logged_in? then
+ haml :learn
+ else
+ haml :login
+ end
+end
+
+post "/login" do
+ user = User.get_by_params(params)
+ session[:uid] = user.id if user
+ puts user.id
+ if user then
+ redirect "/"
+ else
+ flash[:error] = "Mhh, das hat nicht funktioniert. Versuch es nochmal."
+ redirect "/login"
+ end
+end
+
+get "/logout" do
+ haml :logout
+end
+
+post "/logout" do
+ session[:uid] = nil
+ redirect "/"
+end
+
+get "/register" do
+ haml :register
+end
+
+post "/register" do
+ User.new_from_params(params)
+ redirect "/login"
+end
get '/receiver' do
%[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
View
BIN db/dev.db
Binary file not shown.
View
47 models/Kana.rb
@@ -5,7 +5,7 @@ class Kana
property :utf8, String
property :url, String
- has n, :flipcards, :through => Resource
+ has n, :flipcards
end
class Flipcard
@@ -14,7 +14,7 @@ class Flipcard
property :id, Serial
property :last_learned_on, Date
property :repetition, Integer, :default => 0
- property :easiness, Decimal, :default => 2.5
+ property :easiness, Float, :default => 2.5
def interval(n, ef)
return 1 if n == 1
@@ -23,22 +23,30 @@ def interval(n, ef)
end
def ef(oldef,q)
- ef = ef+(0.1-(5-q)*(0.08+(4-q)*0.02))
+ puts "oldef: #{oldef}"
+ puts "q: #{q}"
+ ef = oldef+(0.1-(5-q)*(0.08+(4-q)*0.02))
[ef,1.3].max
end
def learn_today?
- interval(repetition) < (Date.now - last_learned_on)
+ return true unless last_learned_on
+ interval(repetition,easiness) < (Date.today - last_learned_on)
end
def learn(q)
- easiness = ef(easiness,q)
- last_learned_on = Date.today
- q < 3 ? repetition = 0 : repetition +=1
+ self.easiness = ef(self.easiness,q)
+ self.last_learned_on = Date.today
+ q < 3 ? self.repetition = 0 : self.repetition +=1
save
end
- has n, :kana, :through => Resource
+ def self.next(user)
+ flipcards = user.flipcards.all.select{|f| f.learn_today?}
+ flipcards.first
+ end
+
+ belongs_to :kana
belongs_to :user
end
@@ -63,6 +71,15 @@ def reset
end
end
+ def self.new_from_params(params)
+ new_user = self.new
+ new_user.email = params[:email].downcase
+ new_user.pwhash = Digest::SHA1.hexdigest(params[:password])
+ new_user.reset
+ new_user.save
+ new_user
+ end
+
def picture
if facebook_uid then
"http://graph.facebook.com/#{facebook_uid}/picture?type=square"
@@ -71,6 +88,20 @@ def picture
end
end
+ def name
+
+ if facebook_uid then
+ # do facebook stuff
+ #fb:name{:uid => fb[:user], :useyou => 'false', :firstnameonly => 'true'}
+ else
+ email.split("@")[0]
+ end
+ end
+
+ def self.get_by_params(params)
+ self.first(:email => params[:email].downcase, :pwhash => Digest::SHA1.hexdigest(params[:password]))
+ end
+
def self.get_by_fb(fb_uid)
fb_user = first(:facebook_uid => fb_uid)
if fb_user then
View
4 views/index.haml
@@ -2,6 +2,6 @@
%img{:src => current_user.picture}
%br
%p
- Hallo,
- %fb:name{:uid => fb[:user], :useyou => 'false', :firstnameonly => 'true'}
+ Hallo,
+ =current_user.name.capitalize
bald kannst du hier Kana lernen!
View
8 views/login.haml
@@ -0,0 +1,8 @@
+%p Login
+-if flash[:error] then
+ %p=flash[:error]
+%form{:action => "/login", :method => "post"}
+ %input{:name => "email"} Email:
+ %input{:type => "password", :name => "password"} Password:
+ %button Anmelden
+
View
5 views/register.haml
@@ -0,0 +1,5 @@
+%p Registrierung
+%form{:action => "/register", :method => "post"}
+ %input{:name => "email"} Email:
+ %input{:type => "password", :name => "password"} Password:
+ %button Registrieren

0 comments on commit bb5301d

Please sign in to comment.
Something went wrong with that request. Please try again.