Fork of https://github.com/modality/charred-black. Short term, has some fixes. Long term, may include a tool to create and edit stock/lifepath/skill/trait data. http://charred.obscuritus.ca:8080/#/
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.
 
 
 
 
charred-gold/src/app.rb

139 lines
3.1 KiB

require 'sinatra'
require 'sinatra/json'
require 'json'
require_relative 'lib/cache'
require_relative 'lib/pdf'
require_relative 'lib/data'
require_relative 'lib/wiki'
use Rack::Logger
set :bind, ENV['HOST']
set :port, ENV['PORT']
CACHE = Mu::Cache.new :max_size => 128, :max_time => 30.0
DATA = Charred::Data.new.data
helpers do
def logger
request.logger
end
end
get '/' do
erb :index
end
get '/list_chars/:user' do
"{}"
end
get /\/([\w]+)_partial/ do
partial = params['captures'].first
erb "partials/#{partial}".to_sym
end
get '/namegen/:gender' do
if params['gender'] == 'female'
['Ada', 'Belle', 'Carmen', 'Desdemona', 'Edie'].sample
elsif params['gender'] == 'male'
['Agamemnon', 'Beren', 'Cadwalader', 'Dro', 'Edgar'].sample
end
end
get '/skills' do
json DATA[:skills]
end
get '/traits' do
json DATA[:traits]
end
get '/stocks' do
json DATA[:stocks]
end
get '/lifepaths/:stock' do
if DATA[:stocks].keys.include? params['stock']
json DATA[:lifepaths][params['stock']]
else
404
end
end
get '/resources/:stock' do
if DATA[:stocks].keys.include? params['stock']
json DATA[:resources][params['stock']]
else
404
end
end
post '/charsheet' do
request.body.rewind
raw = request.body.readpartial(16 * 1024)
data = JSON.parse(raw)
key = "char-#{Time.now.strftime('%Y%m%d%H%M%S%L')}-#{rand(1...10000)}"
CACHE.store key, data
"/get_file?file=#{key}&download_name=#{data['name']} Character Sheet.pdf"
end
post '/upload_charfile' do
data = params['charfile']['tempfile'].read
erb '<html><body><pre><%= char %></pre></body></html>', :locals => {:char => data}
end
post '/download_charfile' do
request.body.rewind # in case someone already read it
raw = request.body.readpartial(16 * 1024)
data = JSON.parse(raw)
key = "char-#{Time.now.strftime('%Y%m%d%H%M%S%L')}-#{rand(1...10000)}"
CACHE.store key, data
"/get_file?file=#{key}&download_name=#{data['name']} Character Sheet.char"
end
post '/model' do
request.body.rewind
raw = request.body.readpartial(16 * 1024)
data = JSON.parse(raw)
key = "char-#{Time.now.strftime('%Y%m%d%H%M%S%L')}-#{rand(1...10000)}"
CACHE.store key, data
"/get_file?file=#{key}&download_name=#{data['name']} Character Sheet.model"
end
post '/wiki' do
request.body.rewind
raw = request.body.readpartial(16 * 1024)
data = JSON.parse(raw)
key = "char-#{Time.now.strftime('%Y%m%d%H%M%S%L')}-#{rand(1...10000)}"
CACHE.store key, data
"/get_file?file=#{key}&download_name=#{data['name']} Character Sheet.wiki"
end
get '/get_file' do
data = nil
if params['download_name'].match(/\.pdf$/)
content_type 'application/pdf'
data = CACHE.delete(params['file'])
if data
cs = CharSheet.new(data)
data = cs.render(logger, DATA)
end
elsif params['download_name'].match(/\.wiki$/)
content_type 'text/plain'
data = CACHE.delete(params['file'])
if data
wiki_sheet = WikiSheet.new(data)
data = wiki_sheet.render
end
else
content_type 'application/octet-stream'
data = JSON.dump CACHE.delete(params['file'])
end
attachment params['download_name']
data
end