wrote basic building stuff, need to impliment more in the player.
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
# Nathan Hinton Jan 8 2026
|
||||
# Test file for the buildings in the game
|
||||
|
||||
require 'game/building'
|
||||
|
||||
RSpec.describe Building do
|
||||
let(:player) {instance_double(
|
||||
'Player',
|
||||
ship_build_power: 0,
|
||||
research_tech_build_power: 0,
|
||||
military_tech_build_power: 0,
|
||||
player_resources: {
|
||||
'metal' => 20_000,
|
||||
'crystals' => 20_000,
|
||||
'fuel' => 20_000,
|
||||
'credits' => 20_000},
|
||||
all_buildings: [],
|
||||
all_tech: []
|
||||
)}
|
||||
|
||||
let(:planet) {instance_double(
|
||||
'Planet',
|
||||
owner: player
|
||||
)}
|
||||
|
||||
let(:anti_gravity_thrusters_1) {instance_double('Tech',
|
||||
name: 'Anti Gravity Thrusters 1'
|
||||
)}
|
||||
let(:anti_gravity_thrusters_2) {instance_double('Tech',
|
||||
name: 'Anti Gravity Thrusters 2'
|
||||
)}
|
||||
|
||||
|
||||
before(:all) do
|
||||
end
|
||||
|
||||
describe '#initialize & #validate_building' do
|
||||
it 'Can construct any of level one mining buildings' do
|
||||
expect{Building.new(player, planet, 'Metal Mines 1')}.to_not raise_error
|
||||
end
|
||||
|
||||
it 'Can construct basic factory buildings' do
|
||||
expect{Building.new(player, planet, 'Shipyard Level 1')}.to_not raise_error
|
||||
end
|
||||
|
||||
it 'Can construct second level factory buildings' do
|
||||
allow(player).to receive(:all_buildings).and_return([Building.new(player, planet, 'Shipyard Level 1')])
|
||||
expect{Building.new(player, planet, 'Shipyard Level 2')}.to_not raise_error
|
||||
end
|
||||
|
||||
it 'Can construct third level (dependency check) buildings' do
|
||||
allow(player).to receive(:all_tech).and_return([anti_gravity_thrusters_1, anti_gravity_thrusters_2]) # Required tech for buildings
|
||||
allow(player).to receive(:all_buildings).and_return([Building.new(player, planet, 'Shipyard Level 1')]) # Building the second level requires the first first :)
|
||||
allow(player).to receive(:all_buildings).and_return([Building.new(player, planet, 'Shipyard Level 2'), Building.new(player, planet, 'Space Construction Station Level 1')])
|
||||
expect{Building.new(player, planet, 'Space Construction Station Level 2')}.to_not raise_error
|
||||
end
|
||||
|
||||
it 'Raises an error when trying to build a building that does not have the required buildings' do
|
||||
expect{Building.new(player, planet, 'Shipyard Level 2')}.to raise_error(BuildingError, 'Requires `Shipyard Level 1` first')
|
||||
end
|
||||
|
||||
it 'Raises an error when trying to build a building that does not have the required tech' do
|
||||
allow(player).to receive(:all_buildings).and_return([Building.new(player, planet, 'Shipyard Level 1')]) # Building the second level requires the first first :)
|
||||
expect{Building.new(player, planet, 'Space Construction Station Level 1')}.to raise_error(BuildingError, 'Requires the tech of `Anti Gravity Thrusters 1` first')
|
||||
end
|
||||
|
||||
it 'Raises an error when the player does not own the planet' do
|
||||
allow(planet).to receive(:owner).and_return(nil)
|
||||
expect{Building.new(player, planet, 'Shipyard Level 1')}.to raise_error(BuildingError, 'Player does not own planet!')
|
||||
end
|
||||
|
||||
it 'Raises an error if the player does not have the required credits' do
|
||||
allow(player).to receive(:player_resources).and_return(
|
||||
{
|
||||
'metal' => 20_000,
|
||||
'crystals' => 20_000,
|
||||
'fuel' => 20_000,
|
||||
'credits' => 0}
|
||||
)
|
||||
expect{Building.new(player, planet, 'Shipyard Level 1')}.to raise_error(BuildingError, 'Player does not have enough credits!')
|
||||
end
|
||||
|
||||
it 'Raises an error if the player does not have enough metal' do
|
||||
allow(player).to receive(:player_resources).and_return(
|
||||
{
|
||||
'metal' => 0,
|
||||
'crystals' => 20_000,
|
||||
'fuel' => 20_000,
|
||||
'credits' => 20_000}
|
||||
)
|
||||
expect{Building.new(player, planet, 'Shipyard Level 1')}.to raise_error(BuildingError, 'Player does not have enough metal!')
|
||||
end
|
||||
|
||||
it 'Raises an error if the player does not have enough fuel' do
|
||||
allow(player).to receive(:player_resources).and_return(
|
||||
{
|
||||
'metal' => 20_000,
|
||||
'crystals' => 20_000,
|
||||
'fuel' => 0,
|
||||
'credits' => 20_000}
|
||||
)
|
||||
expect{Building.new(player, planet, 'Shipyard Level 1')}.to raise_error(BuildingError, 'Player does not have enough fuel!')
|
||||
end
|
||||
|
||||
it 'Raises an error if the player does not have enough crystals' do
|
||||
allow(player).to receive(:player_resources).and_return(
|
||||
{
|
||||
'metal' => 20_000,
|
||||
'crystals' => 0,
|
||||
'fuel' => 20_000,
|
||||
'credits' => 20_000}
|
||||
)
|
||||
expect{Building.new(player, planet, 'Shipyard Level 1')}.to raise_error(BuildingError, 'Player does not have enough crystals!')
|
||||
end
|
||||
|
||||
it 'Raises an error if the building does not exist' do
|
||||
expect{Building.new(player, planet, 'billy bob joe')}.to raise_error(BuildingError, 'Invalid building name `billy bob joe`')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#update/#produce_resources' do
|
||||
|
||||
before(:each) do
|
||||
allow(player).to receive(:ship_build_power=)
|
||||
allow(player).to receive(:research_tech_build_power=)
|
||||
allow(player).to receive(:military_tech_build_power=)
|
||||
end
|
||||
|
||||
describe 'Harvesting building produces resources' do
|
||||
it 'Metal Mines' do
|
||||
building = Building.new(player, planet, 'Metal Mines 1')
|
||||
# Ensure the building is built:
|
||||
building.instance_variable_set(:@building_exist_time, 10000)
|
||||
building.update(1.0)
|
||||
expect(player.player_resources).to eq(
|
||||
{
|
||||
'metal' => 19_500,
|
||||
'crystals' => 19_140,
|
||||
'fuel' => 19_000,
|
||||
'credits' => 18_960
|
||||
})
|
||||
end
|
||||
it 'Crystal Collector' do
|
||||
building = Building.new(player, planet, 'Crystal Collector 1')
|
||||
# Ensure the building is built:
|
||||
building.instance_variable_set(:@building_exist_time, 10000)
|
||||
building.update(1.0)
|
||||
expect(player.player_resources).to eq(
|
||||
{
|
||||
'crystals' => 19_500,
|
||||
'fuel' => 19_140,
|
||||
'credits' => 19_000,
|
||||
'metal' => 18_960
|
||||
})
|
||||
end
|
||||
it 'Fuel Refinery' do
|
||||
building = Building.new(player, planet, 'Fuel Refinery 1')
|
||||
# Ensure the building is built:
|
||||
building.instance_variable_set(:@building_exist_time, 10000)
|
||||
building.update(1.0)
|
||||
expect(player.player_resources).to eq(
|
||||
{
|
||||
'fuel' => 19_500,
|
||||
'credits' => 19_140,
|
||||
'metal' => 19_000,
|
||||
'crystals' => 18_960
|
||||
})
|
||||
end
|
||||
it 'Credit Center' do
|
||||
building = Building.new(player, planet, 'Credit Center 1')
|
||||
# Ensure the building is built:
|
||||
building.instance_variable_set(:@building_exist_time, 10000)
|
||||
building.update(1.0)
|
||||
expect(player.player_resources).to eq(
|
||||
{
|
||||
'credits' => 19_500,
|
||||
'metal' => 19_140,
|
||||
'crystals' => 19_000,
|
||||
'fuel' => 18_960
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
it 'Does not produce when there are not enough resources for upkeep' do
|
||||
start_resources = {
|
||||
'metal' => 1_010,
|
||||
'crystals' => 1_020,
|
||||
'fuel' => 1_030,
|
||||
'credits' => 1_040}
|
||||
|
||||
allow(player).to receive(:player_resources).and_return(start_resources)
|
||||
building = Building.new(player, planet, 'Credit Center 1')
|
||||
building.instance_variable_set(:@building_exist_time, 10000)
|
||||
expect{building.update(1.0)}.to raise_error(BuildingWarning, 'Player does not have enough metal to produce from Credit Center 1!')
|
||||
expect(player.player_resources).to eq(start_resources)
|
||||
end
|
||||
|
||||
it 'does not produce when it is not yet finished constructing' do
|
||||
building = Building.new(player, planet, 'Metal Mines 1')
|
||||
building.update(1.0)
|
||||
expect(player.player_resources).to eq(
|
||||
{
|
||||
'metal' => 19_000,
|
||||
'crystals' => 19_000,
|
||||
'fuel' => 19_000,
|
||||
'credits' => 19_000
|
||||
})
|
||||
end
|
||||
|
||||
it 'Having shipyard buildings allows the production of ships' do
|
||||
building = Building.new(player, planet, 'Shipyard Level 1')
|
||||
building.instance_variable_set(:@building_exist_time, 10000)
|
||||
building.update(1.0)
|
||||
expect(player).to have_received(:ship_build_power=).with(100)
|
||||
end
|
||||
|
||||
it 'Having research tech buildings allows the production of tech' do
|
||||
building = Building.new(player, planet, 'Research Factory Level 1')
|
||||
building.instance_variable_set(:@building_exist_time, 10000)
|
||||
building.update(1.0)
|
||||
expect(player).to have_received(:research_tech_build_power=).with(100)
|
||||
end
|
||||
|
||||
it 'Having military tech buildings allows the production of military tech' do
|
||||
allow(player).to receive(:all_buildings).and_return([Building.new(player, planet, 'Research Factory Level 1')])
|
||||
building = Building.new(player, planet, 'Military Factory Level 1')
|
||||
building.instance_variable_set(:@building_exist_time, 10000)
|
||||
building.update(1.0)
|
||||
expect(player).to have_received(:military_tech_build_power=).with(100)
|
||||
end
|
||||
|
||||
it 'Is in production for the right amount of time' do
|
||||
building = Building.new(player, planet, 'Metal Mines 1')
|
||||
building.update(1000.0)
|
||||
expect(player.player_resources).to eq(
|
||||
{
|
||||
'metal' => 19_000,
|
||||
'crystals' => 19_000,
|
||||
'fuel' => 19_000,
|
||||
'credits' => 19_000
|
||||
})
|
||||
building.update(1.0)
|
||||
expect(player.player_resources).to eq(
|
||||
{
|
||||
'metal' => 19_500,
|
||||
'crystals' => 19_140,
|
||||
'fuel' => 19_000,
|
||||
'credits' => 18_960
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -70,7 +70,7 @@ module Game
|
||||
|
||||
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(game.player_connect(client_socket, cmd)).to eq({"payload"=>"success", "type"=>"update"})
|
||||
expect(existing_player).to have_received(:connect).with(client_socket)
|
||||
end
|
||||
end
|
||||
@@ -103,7 +103,7 @@ module Game
|
||||
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(response).to eq({"payload"=>"success", "type"=>"update"})
|
||||
expect(game.players).to include(an_object_having_attributes(player_id:))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -61,6 +61,13 @@ RSpec.describe Planet do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#info_update' do
|
||||
it 'returns the correct data about the planet to the player' do
|
||||
data = planet.info_update
|
||||
expect(data.keys).to eq(['id', 'position'])
|
||||
end
|
||||
end
|
||||
|
||||
describe '#save_to_file' do
|
||||
it 'saves the planet to a file' do
|
||||
expect{planet.save_to_file}.to_not raise_error
|
||||
|
||||
@@ -166,7 +166,7 @@ RSpec.describe Player do
|
||||
|
||||
before do
|
||||
subject.create_new(credits, metal, crystals, fuel)
|
||||
subject.player_planets << double('planet')
|
||||
subject.player_planets << double('planet', info_update: 'test_data')
|
||||
subject.player_ships << double('ship')
|
||||
end
|
||||
|
||||
@@ -177,7 +177,7 @@ RSpec.describe Player do
|
||||
'request_id' => request_id,
|
||||
'payload' => {
|
||||
'resources' => subject.player_resources,
|
||||
'planets' => subject.player_planets,
|
||||
'planets' => ['test_data'],
|
||||
'ships' => subject.player_ships
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,9 @@ RSpec.describe Ship do
|
||||
expect(ship.owner).to eq 'nathan'
|
||||
expect(ship.position).to eq [0, 2, 4]
|
||||
end
|
||||
end
|
||||
|
||||
describe '#validate_creation' do
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user