Basic working with ships partly implimented
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user