From 06180bc2b95f9fd5493c1f86a5e7b49a15c8234a Mon Sep 17 00:00:00 2001 From: Jose Chavez Aparicio Date: Wed, 12 Jan 2022 21:05:34 -0600 Subject: [PATCH 1/2] feat: error_test file added with required methods for the practice --- environment.rb | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ error_test.rb | 56 +++++++++++++++++++++++++++ game.rb | 37 ++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 environment.rb create mode 100644 error_test.rb create mode 100644 game.rb diff --git a/environment.rb b/environment.rb new file mode 100644 index 000000000..48f14e468 --- /dev/null +++ b/environment.rb @@ -0,0 +1,101 @@ +module Environment + class Depot + attr_accessor :packs + def initialize + @packs = { + simple_transportation_pack: { + intelligence: [:cellphone], + items: [:medipack, :chevy_versa] + }, + standard_transportation_pack: { + intelligence: [:cellphone, :antenna], + arsenal: [:colt_1911], + items: [:handcuffs, :medipack, :chemistry, :chevy_versa] + }, + simple_mission_pack: { + intelligence: [:infopack, :laptop, :cellphone, :antenna], + arsenal: [[:colt_1911], [:handcuffs, :medipack, :chemistry, :financial]] + }, + standar_mission_pack: { + intelligence: [:infopack, :laptop, :cellphone, :antenna], + arsenal: [:remington_870, :colt_1911, :machete, :hatchet], + items: [:handcuffs, :medipack, :chemistry, :financial] + } + } + end + end + + class Control + attr_accessor :missions + @@modes = [:paused, :aborted, :accomplished, :failed] + def initialize + @missions = {} + end + + def new_mission name:, objective:, pack: + @missions[name] = { + objective: objective, + status: :active, + pack: pack + } + end + + @@modes.each do |mode| + define_method("set_mission_to_#{mode}") do |mission| + @missions[mission][:status] = mode + end + end + end + + + class Human + attr_accessor :id, :name, :personal_data, :professional_data + + def initialize name: nil + @name = name + @id = object_id + end + + define_method("set_personal_data") do |surname:, age:, country:, language:, marital_status:, children:| + @personal_data = { + surname: surname, + age: age, + country: country, + language: language, + marital_status: marital_status, + children: children + } + end + + define_method("set_professional_data") do |position:, occupation:, skills: , observations: | + @professional_data = { + position: position, + occupation: occupation, + skills: skills, + observations: observations + } + end + + class Worker < Human + attr_accessor :standard_shift, :extra_shift + + def initialize name + super name: name + @standard_shift = { + id: @id, + hours: 8, + payment: 0.0, + facility: "", #(or nil) + status: nil #(will be of type ) + } + @extra_shift = { + id: @id, #Id of the object when created. + hours: 8, + payment:0.0, + facility: "", #(or nil) + status: nil #(will be of type Symbol) + } + end + end + end +end \ No newline at end of file diff --git a/error_test.rb b/error_test.rb new file mode 100644 index 000000000..51ef9b45e --- /dev/null +++ b/error_test.rb @@ -0,0 +1,56 @@ +require_relative './environment.rb' + +class ErrorTest + include Environment + attr_accessor :board, :depot, :control + + def initialize + @board = { + players: {}, + control: {}, + status: nil + } + @depot = Environment::Depot.new + @control = Environment::Control.new + end + + def new_worker name: nil + begin + raise NameError, 'Name cannot be empty' if name.nil? + + @board[:players].store name, Environment::Human::Worker.new(name) + rescue NameError => e + puts "Error: #{e}" + end + end + + + + def new_mission name: nil, objective: nil, pack: nil + begin + raise NameError, 'name cannot be empty' if name.nil? + raise NameError, 'Objetive cannot be empty' if objective.nil? + raise NameError, 'Pack cannot be empty' if pack.nil? + + @control.new_mission(name: name, objective: objective, pack: pack) + @board[:control][:missions] = @control.missions + rescue NameError => e + puts "Error: #{e}" + end + end +end + +error_test = ErrorTest.new +player = error_test.new_worker + +player2 = error_test.new_mission name: "Fer", objective: "Conquer the zone" + +# player1 = error_test.new_worker name: 'Diego' +# diego.set_personal_data(surname: 'Mota', age: 40, marital_status: :single, children: 0, country: :mx, language: :es) +# diego.set_professional_data(position: 'SE', occupation: 'IT', skills: [:ruby, :blender], observations: 'none') +# puts "#{diego.name}, #{diego.class}", diego.personal_data, diego.professional_data + +# name = :alpha; objective = 'Get Alpha to the base'; pack = :simple_transportation_pack +# error_test.new_mission(name: name, objective: objective, pack: error_test.depot.packs[pack]) +# puts error_test.control.missions[:alpha] +# error_test.control.set_mission_to_accomplished(:alpha) and puts error_test.control.missions[:alpha] \ No newline at end of file diff --git a/game.rb b/game.rb new file mode 100644 index 000000000..fffa8d5d3 --- /dev/null +++ b/game.rb @@ -0,0 +1,37 @@ +require './environment.rb' + +class Game + attr_accessor :board, :depot, :control + + def initialize + @board = { + players: {}, + control: {}, + status: nil + } + @depot = Environment::Depot.new + @control = Environment::Control.new + end + + + def new_worker name: nil + worker = Environment::Human::Worker.new(name) + @board[:players][name] = worker + end + + def new_mission name:, objective:, pack: + @control.new_mission(name: name, objective: objective, pack: pack) + @board[:control][:missions] = @control.missions + end +end + +game = Game.new +diego = game.new_worker name: 'Diego' +diego.set_personal_data(surname: 'Mota', age: 40, marital_status: :single, children: 0, country: :mx, language: :es) +diego.set_professional_data(position: 'SE', occupation: 'IT', skills: [:ruby, :blender], observations: 'none') +puts "#{diego.name}, #{diego.class}", diego.personal_data, diego.professional_data + +name = :alpha; objective = 'Get Alpha to the base'; pack = :simple_transportation_pack +game.new_mission(name: name, objective: objective, pack: game.depot.packs[pack]) +puts game.control.missions[:alpha] +game.control.set_mission_to_accomplished(:alpha) and puts game.control.missions[:alpha] \ No newline at end of file From 734f688690e0e08a28bc75e59df4fe10390d3a36 Mon Sep 17 00:00:00 2001 From: Jose Chavez Aparicio Date: Mon, 17 Jan 2022 09:15:16 -0600 Subject: [PATCH 2/2] fix: identation and hashes fix --- environment.rb | 157 +++++++++++++++++++++---------------------------- error_test.rb | 72 ++++++++++------------- game.rb | 45 +++++++------- 3 files changed, 121 insertions(+), 153 deletions(-) diff --git a/environment.rb b/environment.rb index 48f14e468..4b8731045 100644 --- a/environment.rb +++ b/environment.rb @@ -1,101 +1,78 @@ +# frozen_string_literal: true + module Environment - class Depot - attr_accessor :packs - def initialize - @packs = { - simple_transportation_pack: { - intelligence: [:cellphone], - items: [:medipack, :chevy_versa] - }, - standard_transportation_pack: { - intelligence: [:cellphone, :antenna], - arsenal: [:colt_1911], - items: [:handcuffs, :medipack, :chemistry, :chevy_versa] - }, - simple_mission_pack: { - intelligence: [:infopack, :laptop, :cellphone, :antenna], - arsenal: [[:colt_1911], [:handcuffs, :medipack, :chemistry, :financial]] - }, - standar_mission_pack: { - intelligence: [:infopack, :laptop, :cellphone, :antenna], - arsenal: [:remington_870, :colt_1911, :machete, :hatchet], - items: [:handcuffs, :medipack, :chemistry, :financial] - } - } - end + class Depot + attr_accessor :packs + + def initialize + @packs = { + simple_transportation_pack: { + intelligence: [:cellphone], + items: [:medipack :chevy_versa] + }, + standard_transportation_pack: { + intelligence: [:cellphone, :antenna], + arsenal: [:colt_1911], + items: [:handcuffs, :medipack, :chemistry, :chevy_versa] + }, + simple_mission_pack: { + intelligence: [:infopack, :laptop, :cellphone, :antenna], + arsenal: [[:colt_1911], [:handcuffs, :medipack, :chemistry, :financial]] + }, + standar_mission_pack: { + intelligence: [:infopack, :laptop, :cellphone, :antenna], + arsenal: [:remington_870, :colt_1911, :machete, :hatchet], + items: [:handcuffs, :medipack, :chemistry, :financial] + } + } + end + end + + class Control + attr_accessor :missions + + @@modes = [:paused, :aborted, :accomplished, :failed] + def initialize + @missions = {} end - class Control - attr_accessor :missions - @@modes = [:paused, :aborted, :accomplished, :failed] - def initialize - @missions = {} - end + def new_mission(name:, objective:, pack:) + @missions[name] = {objective: objective, status: :active, pack: pack} + end - def new_mission name:, objective:, pack: - @missions[name] = { - objective: objective, - status: :active, - pack: pack - } - end - - @@modes.each do |mode| - define_method("set_mission_to_#{mode}") do |mission| - @missions[mission][:status] = mode - end - end + @@modes.each do |mode| + define_method("set_mission_to_#{mode}") do |mission| + @missions[mission][:status] = mode + end end + end + class Human + attr_accessor :id, :name, :personal_data, :professional_data - class Human - attr_accessor :id, :name, :personal_data, :professional_data + def initialize(name: nil) + @name = name + @id = object_id + end - def initialize name: nil - @name = name - @id = object_id - end + define_method('set_personal_data') do |surname:, age:, country:, language:, marital_status:, children:| + @personal_data = { surname: surname, age: age, country: country, language: language, + marital_status: marital_status, children: children} + end - define_method("set_personal_data") do |surname:, age:, country:, language:, marital_status:, children:| - @personal_data = { - surname: surname, - age: age, - country: country, - language: language, - marital_status: marital_status, - children: children - } - end + define_method('set_professional_data') do |position:, occupation:, skills:, observations:| + @professional_data = { position: position, occupation: occupation, skills: skills, + observations: observations} + end - define_method("set_professional_data") do |position:, occupation:, skills: , observations: | - @professional_data = { - position: position, - occupation: occupation, - skills: skills, - observations: observations - } - end - - class Worker < Human - attr_accessor :standard_shift, :extra_shift + class Worker < Human + attr_accessor :standard_shift, :extra_shift - def initialize name - super name: name - @standard_shift = { - id: @id, - hours: 8, - payment: 0.0, - facility: "", #(or nil) - status: nil #(will be of type ) - } - @extra_shift = { - id: @id, #Id of the object when created. - hours: 8, - payment:0.0, - facility: "", #(or nil) - status: nil #(will be of type Symbol) - } - end - end - end -end \ No newline at end of file + def initialize(name) + super name: name + @standard_shift = { id: @id, hours: 8, payment: 0.0, facility: '', status: nil} + @extra_shift = { id: @id, hours: 8, payment: 0.0, facility: '', status: nil} + end + end + end +end diff --git a/error_test.rb b/error_test.rb index 51ef9b45e..54aa6f040 100644 --- a/error_test.rb +++ b/error_test.rb @@ -1,49 +1,41 @@ -require_relative './environment.rb' +# frozen_string_literal: true + +require_relative './environment' class ErrorTest - include Environment - attr_accessor :board, :depot, :control - - def initialize - @board = { - players: {}, - control: {}, - status: nil - } - @depot = Environment::Depot.new - @control = Environment::Control.new - end - - def new_worker name: nil - begin - raise NameError, 'Name cannot be empty' if name.nil? - - @board[:players].store name, Environment::Human::Worker.new(name) - rescue NameError => e - puts "Error: #{e}" - end - end - - - - def new_mission name: nil, objective: nil, pack: nil - begin - raise NameError, 'name cannot be empty' if name.nil? - raise NameError, 'Objetive cannot be empty' if objective.nil? - raise NameError, 'Pack cannot be empty' if pack.nil? - - @control.new_mission(name: name, objective: objective, pack: pack) - @board[:control][:missions] = @control.missions - rescue NameError => e - puts "Error: #{e}" - end - end + include Environment + attr_accessor :board, :depot, :control + + def initialize + @board = {players: {}, control: {}, status: nil} + @depot = Environment::Depot.new + @control = Environment::Control.new + end + + def new_worker(name: nil) + raise NameError, 'Name cannot be empty' if name.nil? + + @board[:players].store name, Environment::Human::Worker.new(name) + rescue NameError => e + puts "Error: #{e}" + end + + def new_mission(name: nil, objective: nil, pack: nil) + raise NameError, 'name cannot be empty' if name.nil? + raise NameError, 'Objetive cannot be empty' if objective.nil? + raise NameError, 'Pack cannot be empty' if pack.nil? + + @control.new_mission(name: name, objective: objective, pack: pack) + @board[:control][:missions] = @control.missions + rescue NameError => e + puts "Error: #{e}" + end end error_test = ErrorTest.new player = error_test.new_worker -player2 = error_test.new_mission name: "Fer", objective: "Conquer the zone" +player2 = error_test.new_mission name: 'Fer', objective: 'Conquer the zone' # player1 = error_test.new_worker name: 'Diego' # diego.set_personal_data(surname: 'Mota', age: 40, marital_status: :single, children: 0, country: :mx, language: :es) @@ -53,4 +45,4 @@ def new_mission name: nil, objective: nil, pack: nil # name = :alpha; objective = 'Get Alpha to the base'; pack = :simple_transportation_pack # error_test.new_mission(name: name, objective: objective, pack: error_test.depot.packs[pack]) # puts error_test.control.missions[:alpha] -# error_test.control.set_mission_to_accomplished(:alpha) and puts error_test.control.missions[:alpha] \ No newline at end of file +# error_test.control.set_mission_to_accomplished(:alpha) and puts error_test.control.missions[:alpha] diff --git a/game.rb b/game.rb index fffa8d5d3..864ce66bb 100644 --- a/game.rb +++ b/game.rb @@ -1,37 +1,36 @@ -require './environment.rb' +# frozen_string_literal: true -class Game - attr_accessor :board, :depot, :control +require './environment' - def initialize - @board = { - players: {}, - control: {}, - status: nil - } - @depot = Environment::Depot.new - @control = Environment::Control.new - end +class Game + attr_accessor :board, :depot, :control + def initialize + @board = {players: {}, control: {}, status: nil} + @depot = Environment::Depot.new + @control = Environment::Control.new + end - def new_worker name: nil - worker = Environment::Human::Worker.new(name) - @board[:players][name] = worker - end + def new_worker(name: nil) + worker = Environment::Human::Worker.new(name) + @board[:players][name] = worker + end - def new_mission name:, objective:, pack: - @control.new_mission(name: name, objective: objective, pack: pack) - @board[:control][:missions] = @control.missions - end + def new_mission(name:, objective:, pack:) + @control.new_mission(name: name, objective: objective, pack: pack) + @board[:control][:missions] = @control.missions + end end game = Game.new diego = game.new_worker name: 'Diego' diego.set_personal_data(surname: 'Mota', age: 40, marital_status: :single, children: 0, country: :mx, language: :es) -diego.set_professional_data(position: 'SE', occupation: 'IT', skills: [:ruby, :blender], observations: 'none') +diego.set_professional_data(position: 'SE', occupation: 'IT', skills: %i[ruby blender], observations: 'none') puts "#{diego.name}, #{diego.class}", diego.personal_data, diego.professional_data -name = :alpha; objective = 'Get Alpha to the base'; pack = :simple_transportation_pack +name = :alpha +objective = 'Get Alpha to the base' +pack = :simple_transportation_pack game.new_mission(name: name, objective: objective, pack: game.depot.packs[pack]) puts game.control.missions[:alpha] -game.control.set_mission_to_accomplished(:alpha) and puts game.control.missions[:alpha] \ No newline at end of file +game.control.set_mission_to_accomplished(:alpha) and puts game.control.missions[:alpha]