-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_methods.rb
131 lines (102 loc) · 2.95 KB
/
game_methods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require './game'
require './author'
class GameMethods
attr_accessor :games, :authors
def initialize
@games = []
@authors = []
end
def list_authors
if @authors.empty?
puts 'No authors available.'
else
puts 'List of authors: '
@authors.each do |author|
puts "First Name: #{author.first_name}"
puts "Last Name: #{author.last_name}"
puts '-----------------'
end
end
end
def list_games
if @games.empty?
puts 'No games available'
else
puts 'List of game: '
@games.each do |game|
puts "Multiplayer: #{game.multiplayer}"
puts "Last played At: #{game.last_played_at}"
puts "Publish Date: #{game.publish_date}"
puts '------------------'
end
end
end
def add_game
puts 'Enter First name of author'
first_name = gets.chomp
puts 'Enter Last name author'
last_name = gets.chomp
puts 'Enter the publish date of the game (YYYY-MM-DD)'
publish_date = gets.chomp
Time.now.year
puts 'Is it a multiplayer game? [Y/N]'
multiplayer = gets.chomp.upcase
case multiplayer
when 'Y'
multiplayer = true
when 'N'
multiplayer = false
end
puts 'Enter the last played date of the game [YYYY-MM-DD]'
last_played_at = gets.chomp
game = Game.new(publish_date, multiplayer, last_played_at)
@games << game
author = Author.new(first_name, last_name)
@authors << author
author.add_items(game)
puts "Game '#{game.publish_date}', '#{game.multiplayer}, '#{game.last_played_at} added successfully"
end
def save_game
game_to_hash = games.map do |hash|
{
publish_date: hash.publish_date,
multiplayer: hash.multiplayer,
last_played_at: hash.last_played_at
}
end
author_to_hash = authors.map do |hash|
{
first_name: hash.first_name,
last_name: hash.last_name
}
end
game_json = JSON.pretty_generate(game_to_hash)
author_json = JSON.pretty_generate(author_to_hash)
File.write('./database/games.json', game_json)
File.write('./database/authors.json', author_json)
end
def load_game
return [] unless File.exist?('./database/games.json')
game_data = JSON.parse(File.read('./database/games.json'))
@games.clear
new_game = ''
game_data.each do |game|
publish_date = game['publish_date']
multiplayer = game['multiplayer']
last_played_at = game['last_played_at']
game = Game.new(publish_date, multiplayer, last_played_at)
new_game = game
@games << game
end
return [] unless File.exist?('./database/authors.json')
author_data = JSON.parse(File.read('./database/authors.json'))
@authors.clear
author_data.each do |author|
first_name = author['first_name']
last_name = author['last_name']
author = Author.new(first_name, last_name)
author.add_items(new_game)
@authors << author
end
end
end