web/app.rb in game_machine-0.0.11 vs web/app.rb in game_machine-1.0.2
- old
+ new
@@ -1,131 +1,153 @@
require 'json'
require 'sinatra/base'
-require 'sinatra/multi_route'
+require 'rack-flash'
-require_relative 'controllers/base_controller'
-require_relative 'controllers/index_controller'
-require_relative 'controllers/messages_controller'
-require_relative 'controllers/auth_controller'
-require_relative 'controllers/log_controller'
-require_relative 'controllers/player_register_controller'
-
class WebApp < Sinatra::Base
- CONTROLLERS = {}
- set :bind, GameMachine::Application.config.http_host
- set :port, GameMachine::Application.config.http_port
+ set :bind, GameMachine::Application.config.http.host
+ set :port, GameMachine::Application.config.http.port
set :root, File.expand_path(File.dirname(__FILE__))
set :environment, :development
mime_type :proto, 'application/octet-stream'
+ set :server, :puma
- register Sinatra::MultiRoute
+ enable :sessions
+ set :session_secret, 'dsf5785sadf86876asdf6'
+ use Rack::Flash
- def base_uri
- host = GameMachine::Application.config.http_host
- port = GameMachine::Application.config.http_port
- "http://#{host}:#{port}"
- end
+ helpers do
- def controller(name)
- case name
- when :index
- CONTROLLERS[name] ||= Web::Controllers::IndexController.new
- when :auth
- CONTROLLERS[name] ||= Web::Controllers::AuthController.new
- when :messages
- CONTROLLERS[name] ||= Web::Controllers::MessagesController.new
- when :log
- CONTROLLERS[name] ||= Web::Controllers::LogController.new
- when :player_register
- CONTROLLERS[name] ||= Web::Controllers::PlayerRegisterController.new
+ def config
+ GameMachine::Application.config
end
- end
+ def logged_in?
+ session['user']
+ end
- get '/player_register' do
- @content = {}
- haml :player_register, :layout => :register_layout
- end
+ def admin_user
+ config.admin.user
+ end
- post '/player_register.html' do
- @content = controller(:player_register).set_request(request,params).create
- if @content['error']
- haml :player_register, :layout => :register_layout
- else
- haml :player_registered, :layout => :register_layout
+ def admin_pass
+ config.admin.pass
end
+
+ def auth_response(token)
+ response = {
+ :authtoken => token,
+ :protocol => config.client.protocol
+ }
+
+ if config.client.protocol == 'TCP'
+ response['tcp_host'] = config.tcp.host
+ response['tcp_port'] = config.tcp.port
+ elsif config.client.protocol == 'UDP'
+ response['udp_host'] = config.udp.host
+ response['udp_port'] = config.udp.port
+ end
+ response
+ end
+
end
- post '/player_register.json' do
- content = controller(:player_register).set_request(request,params).create
- JSON.generate(content)
+ before do
+ allow = false
+ public = ['/client','/login','/clusterinfo']
+ public.each do |path|
+ if request.path_info.match(path)
+ allow = true
+ end
+ end
+
+ if !allow && !logged_in?
+ redirect to('/login')
+ end
end
get '/' do
- if request.params['restarted']
- @restarted = true
+ erb :index
+ end
+
+ get '/login' do
+ erb :login
+ end
+
+ post '/login' do
+ if params['username'] == admin_user && params['password'] == admin_pass
+ flash[:notice] = "Logged in"
+ session['user'] = true
+ redirect to('/')
+ else
+ flash[:error] = "Invalid username or password"
+ redirect to("/login")
end
- haml :index
end
- get '/alive' do
- JSON.generate({})
+ get '/players/:id/delete' do
+ GameMachine::MessageLib::Player.store_delete('players',params['id'])
+ redirect to('/players')
end
- get '/restart' do
- haml :restart
+ post '/players' do
+ scope,query_string = params['query_string'].split('##')
+ if query_string.nil?
+ query_string = ''
+ end
+ @players = GameMachine::DataStore.instance.query(scope,query_string,200,'Player')
+ @form_query = scope + '##' + query_string
+ erb :players
end
- get '/logs' do
- @logtypes = {
- :app => 'Application',
- :stdout => 'Standard out',
- :stderr => 'Standard error'
- }
- @logtype = (params['logtype'] || 'app').to_sym
- @content = controller(:log).set_request(request,params).logs(@logtype)
- haml :logs
+ get '/players' do
+ @players = GameMachine::DataStore.instance.query("players",'',200,'Player')
+ erb :players
end
- get '/messages/game' do
- @content = controller(:messages).set_request(request,params).game
- @messages = :game
- haml :game_messages
+ get '/add_player' do
+ erb :add_player
end
- post '/messages/game' do
- @content = controller(:messages).set_request(request,params).update
- if @content == 'restart'
- haml :restart
+ post '/add_player' do
+ if player = GameMachine::MessageLib::Player.store_get('players',params['username'],500)
+ flash[:error] = "Error creating player (already exists?)"
else
- @messages = :game
- haml :game_messages
+ player = GameMachine::MessageLib::Player.new.set_id(params['username'])
+ authenticator = GameMachine::JavaLib::DefaultAuthenticator.new(player)
+ authenticator.set_password(params['password'])
+ flash[:notice] = "Player created"
end
+ redirect to('/')
end
- get '/messages/all' do
- @content = controller(:messages).set_request(request,params).all
- @messages = :all
- @format = params['format']
- if @format == 'proto'
- content_type :proto
- attachment 'messages.proto'
- @content
- else
- haml :game_messages
+ get '/clusterinfo' do
+ info = {}
+ info[:members] = {}
+ GameMachine::ClusterMonitor.cluster_members.keys.each do |key|
+ member = GameMachine::ClusterMonitor.cluster_members[key]
+ info[:members][key] = {:address => member.address, :status => member.status}
end
+ info[:self_address] = GameMachine::Akka.instance.address
+
+ JSON.generate(info)
end
- route :get, :post, '/auth' do
- res = controller(:auth).set_request(request,params).get
- if res == 'error'
- status 403
- body res
+ post '/api/client/login/:game_id' do
+ if authtoken = GameMachine::Handlers::PlayerAuthentication.instance.authorize(params['username'],params['password'])
+ content_type 'text/plain'
+ JSON.generate(auth_response(authtoken))
else
- res
+ status 403
+ 'error'
end
end
+
+ get '/stats' do
+ @stats = {}
+ @stats[:local_player_count] = GameMachine::ClientManager.local_players.size
+ @stats[:local_players] = GameMachine::ClientManager.local_players
+ end
+
end
WebApp.run!
-