From 3d66bd1884e3cd4419447a9df2da68c066cca7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Thu, 30 Nov 2023 21:10:59 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + Gemfile | 13 ++++++++ Gemfile.lock | 30 ++++++++++++++++++ config.json.example | 8 +++++ server.rb | 75 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 config.json.example create mode 100644 server.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d344ba6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b72003d --- /dev/null +++ b/Gemfile @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# gem "rails" + +gem "sinatra", "~> 3.1" + +gem "pg", "~> 1.5" + +gem "json", "~> 2.6" + +gem "webrick", "~> 1.8" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..cb63758 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,30 @@ +GEM + remote: https://rubygems.org/ + specs: + json (2.6.3) + mustermann (3.0.0) + ruby2_keywords (~> 0.0.1) + pg (1.5.4) + rack (2.2.8) + rack-protection (3.1.0) + rack (~> 2.2, >= 2.2.4) + ruby2_keywords (0.0.5) + sinatra (3.1.0) + mustermann (~> 3.0) + rack (~> 2.2, >= 2.2.4) + rack-protection (= 3.1.0) + tilt (~> 2.0) + tilt (2.3.0) + webrick (1.8.1) + +PLATFORMS + x86_64-linux + +DEPENDENCIES + json (~> 2.6) + pg (~> 1.5) + sinatra (~> 3.1) + webrick (~> 1.8) + +BUNDLED WITH + 2.4.10 diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..538094c --- /dev/null +++ b/config.json.example @@ -0,0 +1,8 @@ +{ + "db": { + "host": "127.0.0.1", + "dbname": "postgres", + "user": "postgres", + "password": "secretpassword" + } +} diff --git a/server.rb b/server.rb new file mode 100644 index 0000000..f4db3e7 --- /dev/null +++ b/server.rb @@ -0,0 +1,75 @@ +# 2023-01-01 13:33 +# 2023-07-24 10:41 +# 2023-10-20 17:24 + +require 'sinatra' +require 'json' +require 'pg' +require 'io/console' + +set :bind => (ENV['SERVER_BIND_ADDRESS'] or '0.0.0.0') +set :port => (ENV['SERVER_BIND_PORT'] or '3333') + +config_file_path = (ENV['SERVER_CONFIG_PATH'] or '/data/config.json') +config = JSON.parse(File.read(config_file_path)) + +#SECRET_TOKEN = config['secret_token'] +#SECRET_USERNAME = config['secret_username'] + +db = PG.connect(host: config['db']['host'], dbname: config['db']['dbname'], user: config['db']['user'], password: config['db']['password']) + +QUERY_INSERT_SCENARIO_RECORD = <<-SQL + INSERT INTO scenario_record(username, format_version, source, user_initials, total_score, final_multiplier, final_clock, enemies_killed, player_move_count, scenario_id, json_data, uploaded_at) VALUES + ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, current_timestamp) RETURNING id; +SQL + +PREP_QUERY_INSERT_SCENARIO_RECORD = db.prepare('insert_scenario_record', QUERY_INSERT_SCENARIO_RECORD) + +def handle_scenario_record db, data, req_data + data_parsed = JSON.parse data + postulations = {:total_score => data_parsed["counts"]["score"].to_i.floor, + :final_multiplier => data_parsed["counts"]["multiplier"], + :final_clock => data_parsed["counts"]["clock"], + :enemies_killed => data_parsed["counts"]["enemies_killed"], + :player_move_count => data_parsed["counts"]["player_move_count"] + } + pp postulations + res = db.exec_prepared('insert_scenario_record', + [ + req_data[:username], + data_parsed['format_version'], + req_data[:client_ip], + data_parsed["counts"]["user_initials"], + postulations[:total_score], + postulations[:final_multiplier], + postulations[:final_clock], + postulations[:enemies_killed], + postulations[:player_move_count], + data_parsed["counts"]["scenario_id"], + data + ]) + "OK" +end + +before '/api/1/replay/scenario' do + pass unless request.env['CONTENT_TYPE'] == 'application/json' + + if request.content_length.nil? or request.content_length == 0 then + halt 200 + end +end + +#use Rack::Auth::Basic, "Protected Area" do |username, password| +# username == SECRET_USERNAME && password = SECRET_TOKEN +#end + +post '/api/1/replay/scenario' do + username = request.env['HTTP_X_USERNAME'] + client_ip = request.ip + + request.body.rewind + data = request.body.read + req_data = { :username => username, + :client_ip => client_ip } + handle_scenario_record db, data, req_data +end