Files
space_game_server/core/server.rb
T

56 lines
1.3 KiB
Ruby

# Nathan Hinton 19 Dec 2025
# Runs the server for the game
require 'sinatra'
require 'sinatra/cross_origin'
require 'json'
require './game'
configure do
enable :cross_origin
end
# CORS headers for all routes
before do
headers 'Access-Control-Allow-Origin' => '*', # Allow all domains (you can change '*' to a specific domain for security)
'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS', # Allow only specific methods
'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
end
games = {}
# Get the server status
get '/status' do
return 'UP'
end
# Creates a new game
#
# @param name [String] Required. The name of the game
# @param width [Integer] The width of the game space
# @param height [Integer] The height of the game space
# @param planets [Integer] The number of planets to have in the game
get '/create_game' do
puts "Creating game named #{params['name']}"
name = params['name']
width = params['width'] || 10
width = width.to_i
height = params['height'] || 10
height = height.to_i
planets = params['planets'] || 20
planets = planets.to_i
games.update({name => Game.new(width: width, height: height, planets: planets, name: name)})
return 'OK'
end
# Gets games available
#
# @return [Array] list of game names
get '/list_games' do
return games.keys.to_json
end