Basic working with ships partly implimented

This commit is contained in:
2026-01-07 23:11:46 -07:00
parent 1ec24fbf12
commit 6e914467ed
49 changed files with 2079 additions and 220 deletions
+172
View File
@@ -0,0 +1,172 @@
# frozen_string_literal: true
# Nathan Hinton, 24 Dec 2025
# Test file for the game.
require 'rspec'
require 'socket'
require 'json'
require_relative '../../lib/game/game' # adjust the path if you keep the lib in another folder
require_relative '../../lib/game/planet' # adjust the path if you keep the lib in another folder
module Game
RSpec.describe Game do
let(:player_defaults) do
{
'credits' => 1000,
'metal' => 200,
'crystals' => 50,
'fuel' => 300
}
end
let(:conf) do
{
'name' => 'TestGalaxy',
'width' => 1000,
'height' => 800,
'depth' => 500,
'time' => 0.0,
'player_defaults' => player_defaults,
'players' => [],
'planets' => [],
'ships' => []
}
end
describe '#initialize' do
it 'creates a game instance with the supplied config' do
game = Game.new(conf)
expect(game.name).to eq('TestGalaxy')
expect(game.width).to eq(1000)
expect(game.height).to eq(800)
expect(game.depth).to eq(500)
expect(game.players).to be_empty
end
it 'raises when a required key is missing' do
bad_conf = conf.dup.delete('name')
expect { Game.new(conf) }.not_to raise_error
# Missing the mandatory “name” key
conf_without_name = conf.merge('name' => nil)
expect { Game.new(conf_without_name) }.to raise_error(ArgumentError)
end
end
describe '#player_connect' do
let(:game) { Game.new(conf) }
let(:client_socket) { instance_double(TCPSocket) }
context 'when the player already exists and is disconnected' do
let(:player_id) { 'player123' }
let(:existing_player) { instance_double('Player', player_id: player_id, connected?: false, connect: true) }
before do
allow(game).to receive(:player_registered?).with(player_id).and_return(existing_player)
allow(existing_player).to receive(:connected?).and_return(false)
allow(existing_player).to receive(:connect).with(client_socket)
end
it 're-connects the player and returns success' do
cmd = { 'payload' => { 'player_id' => player_id, 'faction' => 'good' } }
expect(game.player_connect(client_socket, cmd)).to eq('status' => 'success')
expect(existing_player).to have_received(:connect).with(client_socket)
end
end
context 'when the player is already connected' do
let(:player_id) { 'dup' }
let(:existing_player) { instance_double('Player', player_id:, connected?: true) }
before do
allow(game).to receive(:player_registered?).with(player_id).and_return(existing_player)
end
it 'raises a GameError' do
cmd = { 'payload' => { 'player_id' => player_id, 'faction' => 'good' } }
expect { game.player_connect(client_socket, cmd) }
.to raise_error(GameError, 'Player already connected!')
end
end
context 'when the player is new' do
let(:player_id) { 'newplayer' }
before do
allow(game).to receive(:player_registered?).with(player_id).and_return(false)
allow(game).to receive(:create_player).with(player_id, 'evil').and_return(
instance_double('Player', player_id:, connect: true)
)
end
it 'creates a new player, connects them, and adds them to #players' do
cmd = { 'payload' => { 'player_id' => player_id, 'faction' => 'evil'} }
response = game.player_connect(client_socket, cmd)
expect(response).to eq('status' => 'success')
expect(game.players).to include(an_object_having_attributes(player_id:))
end
end
context 'when the command is missing a player_id' do
it 'raises an ArgumentError' do
expect { game.player_connect(client_socket, {}) }
.to raise_error(ArgumentError, /player_id missing/)
end
end
end
describe '#create_player' do
let(:game) { Game.new(conf) }
let(:planet) { Planet.new(1, 1, 0) }
let(:planet2) { Planet.new(1, 1, 0) }
before do
game.instance_variable_set('@planets', [planet, planet2])
end
it 'returns a Player instance with the requested id' do
expect(game.create_player('foo', 'good')).to be_a(Player)
end
end
describe '#player_registered?' do
let(:game) { Game.new(conf) }
it 'returns false if no players exist' do
expect(game.player_registered?('foo')).to be false
end
it 'returns the Player when id matches' do
new_player = instance_double('Player', player_id: 'match')
game.instance_variable_set('@players', [new_player])
allow(game).to receive(:players).and_return([new_player])
expect(game.player_registered?('match')).to eq(new_player)
end
end
describe '#info' do
it 'returns the game time in JSON format' do
game = Game.new(conf)
expect(JSON.parse(game.info(0))['payload']['time']).to eq(0.0)
end
end
describe '#save_game_state' do
let(:game) { Game.new(conf) }
let(:file_content) { StringIO.new }
before do
allow(File).to receive(:open).and_yield(file_content)
end
it 'writes a YAML configuration containing the game time and attributes' do
game.save_game_state
yaml_output = YAML.safe_load(file_content.string)
expect(yaml_output['name']).to eq('TestGalaxy')
expect(yaml_output['time']).to eq(0.0)
end
end
end
end
+79
View File
@@ -0,0 +1,79 @@
# Nathan Hinton
# Test file for the planets
require 'game/planet'
RSpec.describe Planet do
let(:planet) {Planet.new(5, 5, 5)}
before(:all) do
Dir.mkdir('game_data') unless Dir.exist?('game_data')
end
after(:each) do
# Clean up any save file that might have been created
save_file = File.join('game_data', "#{planet.planet_id}.save")
File.delete(save_file) if File.exist?(save_file)
end
describe '#initialize' do
it 'starts with an initial position inside the game board' do
planet.instance_variable_get(:@position).each do |pos|
expect(pos < 5).to be true
end
end
end
describe '#claimable?' do
it 'is claimable when no owner' do
expect(planet.claimable?).to be true
end
it 'is claimable if owned by another player and has no buildings' do
planet.instance_variable_set(:@owner, 'nathan')
expect(planet.claimable?).to be true
end
it 'is not claimable if owned by another player and has buildings' do
planet.instance_variable_set(:@owner, 'nathan')
planet.instance_variable_set(:@buildings, ['command post'])
expect(planet.claimable?).to be false
end
end
describe '#claim' do
it 'sets the owner of the planet to a given player' do
planet.claim('nathan')
expect(planet.owner).to eq 'nathan'
end
it 'overwrites the player even if it was already set' do
planet.claim('nathan')
expect(planet.owner).to eq 'nathan'
planet.claim('leah')
expect(planet.owner).to eq 'leah'
end
end
describe '#update' do
it 'process updates for the planet' do
end
end
describe '#save_to_file' do
it 'saves the planet to a file' do
expect{planet.save_to_file}.to_not raise_error
# Check that save file was created.
expect(File.exist?(File.join('game_data', "#{planet.planet_id}_planet_.save"))).to be true
end
end
describe '#load_from_save' do
it 'can load a planet from a save' do
planet.save_to_file
expect(File.exist?(File.join('game_data', "#{planet.planet_id}_planet_.save"))).to be true
expect(Planet.load_from_save(planet.planet_id)).to be_kind_of(Planet)
end
end
end
+295
View File
@@ -0,0 +1,295 @@
# Nathan Hinton 24 Dec 2025
# spec/player_spec.rb
require 'rspec'
require 'json'
require 'game/player' # adjust the path if needed
RSpec.describe Player do
let(:player_id) { SecureRandom.uuid }
subject(:player) { Player.new(player_id, 'good') }
# ------------------------------------------------------------------
# Helpers / setup
# ------------------------------------------------------------------
before(:all) do
Dir.mkdir('game_data') unless Dir.exist?('game_data')
end
after(:each) do
# Clean up any save file that might have been created
save_file = File.join('game_data', "#{player_id}.save")
File.delete(save_file) if File.exist?(save_file)
end
# ------------------------------------------------------------------
# initialize
# ------------------------------------------------------------------
describe '#initialize' do
it 'sets the id and empty arrays for planets and ships' do
expect(subject.player_id).to eq(player_id)
expect(subject.player_planets).to be_empty
expect(subject.player_ships).to be_empty
expect(subject.instance_variable_get(:@home_planet)).to be_nil
end
it 'sets the faction correctly' do
subject = Player.new(player_id, 'good')
expect(subject.faction).to eq('good')
subject = Player.new(player_id, 'evil')
expect(subject.faction).to eq('evil')
end
it 'raises errors when the faction does not match' do
expect{subject = Player.new(player_id, 'billy_bob_jo')}.to raise_error(PlayerError, 'Faction must be `good` or `evil`')
end
end
# ------------------------------------------------------------------
# create_new
# ------------------------------------------------------------------
describe '#create_new' do
let(:credits) { 1000 }
let(:metal) { 500 }
let(:crystals) { 200 }
let(:fuel) { 300 }
it 'stores the resources hash correctly' do
subject.create_new(credits, metal, crystals, fuel)
expect(subject.player_resources).to eq(
'credits' => credits,
'metal' => metal,
'crystals' => crystals,
'fuel' => fuel
)
end
end
# ------------------------------------------------------------------
# connect / connected?
# ------------------------------------------------------------------
describe '#connect and #connected?' do
let(:socket) { instance_double('TCPSocket', closed?: false, puts: nil) }
it 'stores the socket' do
subject.connect(socket)
expect(subject.instance_variable_get(:@socket)).to eq(socket)
end
context 'when the socket is nil' do
it 'returns false' do
subject.instance_variable_set(:@socket, nil)
expect(subject.connected?).to be false
end
end
context 'when the socket is closed' do
before do
allow(socket).to receive(:closed?).and_return(true)
subject.connect(socket)
end
it 'returns false' do
expect(subject.connected?).to be false
end
end
context 'when the socket is open' do
before { subject.connect(socket) }
it 'returns true' do
expect(subject.connected?).to be true
end
end
end
# ------------------------------------------------------------------
# claim_planet
# ------------------------------------------------------------------
describe '#claim_planet' do
let(:planet) { double('planet', claimable?: true, owner: nil) }
context 'when the planet is claimable' do
it 'adds the planet to the player and calls claim on the planet' do
expect(planet).to receive(:claim).with(subject)
subject.claim_planet(planet)
expect(subject.player_planets).to include(planet)
end
end
context 'when the planet is not claimable' do
before { allow(planet).to receive(:claimable?).and_return(false) }
it 'does nothing' do
expect(planet).not_to receive(:claim)
subject.claim_planet(planet)
expect(subject.player_planets).not_to include(planet)
end
end
end
# ------------------------------------------------------------------
# make_home_planet
# ------------------------------------------------------------------
describe '#make_home_planet' do
context 'when the player owns the planet' do
let(:planet) { double('planet', owner: subject) }
it 'sets the home planet' do
subject.make_home_planet(planet)
expect(subject.instance_variable_get(:@home_planet)).to eq(planet)
end
end
context 'when the player does NOT own the planet' do
let(:planet) { double('planet', owner: nil) }
it 'returns an error hash' do
result = subject.make_home_planet(planet)
expect(result).to eq(
'type' => 'error',
'payload' => 'Planet must be owned by the player'
)
end
end
end
# ------------------------------------------------------------------
# update
# ------------------------------------------------------------------
describe '#update' do
let(:credits) { 1000 }
let(:metal) { 500 }
let(:crystals) { 200 }
let(:fuel) { 300 }
let(:request_id) { 42 }
before do
subject.create_new(credits, metal, crystals, fuel)
subject.player_planets << double('planet')
subject.player_ships << double('ship')
end
it 'returns a hash in the expected format' do
expected = {
'type' => 'update',
'domain' => 'player',
'request_id' => request_id,
'payload' => {
'resources' => subject.player_resources,
'planets' => subject.player_planets,
'ships' => subject.player_ships
}
}
expect(subject.update(request_id)).to eq(expected)
end
end
# ------------------------------------------------------------------
# send_update
# ------------------------------------------------------------------
describe '#send_update' do
let(:socket) { instance_double('TCPSocket', closed?: false, puts: nil) }
before { subject.connect(socket) }
context 'when connected' do
it 'sends the update JSON' do
expect(socket).to receive(:puts).with(kind_of(String))
subject.send_update
end
end
context 'when not connected' do
before { subject.instance_variable_set(:@socket, nil) }
it 'does nothing' do
expect { subject.send_update }.not_to raise_error
end
end
context 'when socket.puts raises an exception' do
before do
allow(socket).to receive(:puts).and_raise(StandardError)
allow(socket).to receive(:close)
end
it 'closes the socket' do
expect(socket).to receive(:close)
subject.send_update
end
end
end
# ------------------------------------------------------------------
# save_to_file / load_from_save
# ------------------------------------------------------------------
describe 'file persistence' do
let(:credits) { 1000 }
let(:metal) { 500 }
let(:crystals) { 200 }
let(:fuel) { 300 }
before do
subject.create_new(credits, metal, crystals, fuel)
end
it 'writes a marshaled file and can read it back' do
file_id = subject.save_to_file
expect(file_id).to eq(player_id)
loaded_player = Player.load_from_save(player_id)
expect(loaded_player).to be_a(Player)
expect(loaded_player.player_id).to eq(player_id)
expect(loaded_player.player_resources).to eq(
'credits' => credits,
'metal' => metal,
'crystals' => crystals,
'fuel' => fuel
)
end
end
# ------------------------------------------------------------------
# marshal_dump / marshal_load
# ------------------------------------------------------------------
describe '#marshal_dump and #marshal_load' do
let(:credits) { 1000 }
let(:metal) { 500 }
let(:crystals) { 200 }
let(:fuel) { 300 }
before do
subject.create_new(credits, metal, crystals, fuel)
end
it 'returns a hash with the expected keys' do
dump = subject.marshal_dump
expect(dump).to include(
player_id: subject.player_id,
player_resources: subject.player_resources
)
end
it 'restores the player via marshal_load' do
dump = subject.marshal_dump
# Create a brandnew instance and load the dumped data
new_player = Player.new('dummy-id', 'good')
new_player.marshal_load(dump)
expect(new_player.player_id).to eq(subject.player_id)
expect(new_player.player_resources).to eq(subject.player_resources)
end
end
end# Nathan Hinton 24 Dec 2025
# Test file for the player
require 'game/player'
RSpec.describe Player do
describe '#initialize' do
it 'initializes with a player id' do
end
end
end
+106
View File
@@ -0,0 +1,106 @@
# Nathan Hinton 30 Dec 2025
# Test file for ships
require 'game/ship'
RSpec.describe Ship do
let(:ship) {Ship.new('nathan', 0, 2, 4, 'Starlight')}
describe '#initialize' do
it 'Creates a ship with a player and a position' do
expect(ship.owner).to eq 'nathan'
expect(ship.position).to eq [0, 2, 4]
end
it 'Fails to create a ship when the name is invalid' do
expect{Ship.new('nathan', 0, 2, 4, 'billy bob joe')}.to raise_error ShipError, "invalid ship name 'billy bob joe'"
end
it 'Fails when the player does not have the required tech' do
nathan = instance_double('Player', researched_tech: ['Light Shield', 'Basic Engine'])
expect{Ship.new(nathan, 0, 2, 4, 'Nebula Dominion')}.to raise_error ShipError, "Player needs the tech of 'Heavy Shield'"
end
it 'Fails when the player does not have the required buildings' do
nathan = instance_double('Player', researched_tech: ['Heavy Shield', 'Large Engine', 'Laser Cannons', 'Neoplated Armor'], buildings: [])
expect{Ship.new(nathan, 0, 2, 4, 'Nebula Dominion')}.to raise_error ShipError, "Player needs the building of 'Space Construction Station' to build this ship"
end
end
describe '#teleport' do
it 'Changes the coordinates of the ship immediately' do
expect(ship.position).to eq [0, 2, 4]
ship.teleport(4, 2, 0)
expect(ship.position).to eq [4, 2, 0]
end
end
describe '#move' do
it 'Sets the target of the ship movement' do
ship.move(1, 3, 5)
expect(ship.instance_variable_get(:@target)).to eq [1, 3, 5]
end
end
describe '#update' do
it 'Does not move when target is eq to position' do
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [0, 2, 4]
end
describe 'Moves a single ship at the correct speed when target is different from position' do
it 'in positive x' do
ship.move(1, 2, 4)
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [1.0, 2.0, 4.0]
end
it 'in negative x' do
ship.move(-1, 2, 4)
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [-1.0, 2.0, 4.0]
end
it 'in positive y' do
ship.move(0, 3, 4)
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [0.0, 3.0, 4.0]
end
it 'in negative y' do
ship.move(0, 1, 4)
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [0.0, 1.0, 4.0]
end
it 'in positive z' do
ship.move(0, 2, 5)
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [0.0, 2.0, 5.0]
end
it 'in negative z' do
ship.move(0, 2, 3)
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [0.0, 2.0, 3.0]
end
it 'in multiple dimensions' do
ship.move(-1, 3, 3)
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [-0.5773502691896258, 2.5773502691896257, 3.4226497308103743]
expect{ship.update(1)}.to_not raise_error
expect(ship.position).to eq [-1, 3, 3]
end
end
it 'Moves a group of ships at the slowest ship speed' do
end
end
describe '#save_to_file' do
end
describe '#load_from_save' do
end
end