Old high school files. Lessson notes/codes/projects etc.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.5 KiB

#!/usr/bin/ruby
require "sinatra"
require "slim"
load "db_parse.rb"
DEFAULT_AVATAR = "https://robohash.org/innonaut.png?size=50x50&set=set1"
3 years ago
enable :sessions
3 years ago
db_cache = get_db() # get all the parsed data
3 years ago
user_creds = {}
user_avatars = Hash.new(DEFAULT_AVATAR)
3 years ago
3 years ago
get "/" do
session[:msg] = nil
3 years ago
slim(:person, locals: {persondata: db_cache})
end
3 years ago
3 years ago
get "/loginpage" do
3 years ago
slim(:login)
end
3 years ago
get "/registerpage" do
slim(:register)
end
post "/register" do
userstring = params[:username].chomp.gsub(" ", "")
if( userstring.length < 3 ) then
session[:msg] = "Invalid username. Minimum chars=3"
redirect "/registerpage"
end
username = userstring.to_sym
3 years ago
if( user_creds[username] == nil ) then
if( params[:password] == params[:password2] ) then
user_creds[username] = params[:password]
# user_avatars[username] = DEFAULT_AVATAR
session[:msg] = "Account created."
redirect "/loginpage"
else
session[:msg] = "Password does not match, please rewrite password."
redirect "/registerpage"
end
3 years ago
else
session[:msg] = "Username in use. Please pick another."
redirect "/registerpage"
3 years ago
end
end
post "/login" do
username = params[:username]
password = params[:password]
if( user_creds[username.to_sym] == password ) then
session[:username] = username
session[:avatar] = user_avatars[username]
3 years ago
session[:msg] = "Login success."
redirect "/"
else
session[:msg] = "Login failed."
redirect "/loginpage"
end
end
get "/profile" do
session[:msg] = nil
if(session[:username] != nil) then
slim(:profile)
else
session[:msg] = "Please login to view your profile."
3 years ago
redirect "/login"
end
3 years ago
end
post "/updateavatar" do
if( session[:username] != nil ) then
if(params[:avatar_url] != "" && params[:avatar_url] != nil) then
user_avatars[session[:username].to_s] = params[:avatar_url]
session[:avatar] = params[:avatar_url]
redirect "/profile"
end
else
session[:msg] = "You need to login to update your avatar."
redirect "/"
end
end
post "/logout" do
session[:username] = nil
redirect "/"
end
get "/calc" do
slim(:calc)
end
post "/calculate" do
result = 0
num1 = params[:number1].to_f
num2 = params[:number2].to_f
op = params[:op]
begin
case op
when "+"
result = num1 + num2
when "-"
result = num1 - num2
when "*"
result = num1 * num2
when "/"
result = num1 / num2
end
rescue ZeroDivisionError
result = "inf"
end
session[:calc_result] = result
session[:history] = session[:history] || []
session[:history] << {
number1: num1,
number2: num2,
op: op,
result: result
}
redirect "/calc"
end