-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.rb
More file actions
144 lines (120 loc) · 3.73 KB
/
Copy pathsession.rb
File metadata and controls
144 lines (120 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
configure :development do
#setup sqlite database
# DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default,"sqlite3://#{Dir.pwd}/login.db")
end
configure :production do
DataMapper.setup(:default, ENV['DATABASE_URL'])
end
#creating the model
class Credential
include DataMapper::Resource
# property :id, Serial
#discarding the id, so that accessing the username is easier
property :username, String,:unique_index=> true,:required => true,:key=> true
property :password, String ,:required => true
property :total_lost, Integer
property :total_won, Integer
property :total_profit, Integer
end
DataMapper.finalize
=begin to be used when the table is recreated
DataMapper.auto_migrate!
c = Credential.new;c.username = "ruby";c.password = "password";c.total_lost = 0;c.total_won = 0;c.total_profit = 0;
c.save
c = Credential.new;c.username = "user";c.password = "pass";c.total_lost = 0;c.total_won = 0;c.total_profit = 0;
c.save
=end
############################################
#routing
enable :sessions
#global variables
usernameDB = ""
passwordDB = ""
get '/' do
redirect to('/login')
end
get '/login' do
erb:login
end
post '/login' do
usn = params[:username]
pwd = params[:password]
if Credential.get(params[:username]) == nil
session[:credMsg] = "Username not found!"
redirect to './login'
else
usernameDB = Credential.get(params[:username]).username
passwordDB = Credential.get(params[:username]).password
end
if(usn == usernameDB && pwd == passwordDB )
session[:login] = true
session[:tot_won_sess] = 0
session[:tot_lost_sess] = 0
session[:tot_profit_sess] = 0
redirect to '/dashboard'
else
session[:username] = usn
session[:password] = pwd
session[:credMsg] = "Wrong username/password!"
redirect to '/login'
end
end
get '/dashboard' do
session[:tot_won_acc] = Credential.get(usernameDB.to_s).total_won.to_i
session[:tot_lost_acc] = Credential.get(usernameDB.to_s).total_lost.to_i
session[:tot_profit_acc] = Credential.get(usernameDB.to_s).total_profit.to_i
erb:dashboard
end
post '/dashboard' do
betAmt = params[:betAmount].to_i # identical to name attribute of input
betNum = params[:betNumber].to_i
session[:betNum] = betNum
session[:betAmt] = betAmt
roll = rand(6)+1
if betNum == 0 || betAmt == 0
@betMessage = "Please choose a proper bet!"
erb "./dashboard"
elsif betNum == roll
@betMessage = "Yay!, you WON this bet!"
save_session(:won,betAmt)
else
save_session(:lost,betAmt)
@betMessage = "Oh!, you LOST this bet!"
end
session[:tot_won_sess] = session[:won].to_i*10
session[:tot_lost_sess] = session[:lost].to_i
session[:tot_profit_sess] = session[:tot_won_sess] - session[:tot_lost_sess]
erb:dashboard
end
get '/logout' do
redirect to '/login'
end
post '/logout' do
@tot_won_acc = save_account(session[:tot_won_acc],session[:tot_won_sess])
@tot_lost_acc = save_account(session[:tot_lost_acc],session[:tot_lost_sess])
@tot_profit_acc = save_account(session[:tot_profit_acc], session[:tot_profit_sess])
Credential.get(usernameDB.to_s).update(total_won: @tot_won_acc)
Credential.get(usernameDB.to_s).update(total_lost: @tot_lost_acc)
Credential.get(usernameDB.to_s).update(total_profit: @tot_profit_acc)
session.clear
session[:credMsg] = "You have been succesfully logged out!"
redirect to '/login'
end
not_found do
"<h3>Sorry, we couldn't find any page to the URL you requested!! :/ </h3>"
end
#save session data
def save_session(won_lost,money)
total = (session[won_lost] || 0)
total = total + money
session[won_lost] = total
end
#save account data
def save_account(new,old)
return old.to_i + new.to_i
end
###########################