KeeperChallenge 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -4,7 +4,7 @@ require "keeperchallenge/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "KeeperChallenge"
7
- s.version = '0.0.3'
7
+ s.version = '0.0.4'
8
8
  s.authors = ["Benjamin Combe"]
9
9
  s.email = ["benjamin.combe@gmail.com"]
10
10
  s.homepage = "https://github.com/kamiben/KeeperChallenge"
@@ -1,9 +1,3 @@
1
- #require './keeperchallenge/player'
2
- #require './keeperchallenge/score'
3
- #require './keeperchallenge/activity'
4
- #require './keeperchallenge/database'
5
- #TODO
6
- #import activity and players from text file ?
7
1
  require File.dirname(__FILE__) +'/keeperchallenge/player'
8
2
  require File.dirname(__FILE__) +'/keeperchallenge/score'
9
3
  require File.dirname(__FILE__) +'/keeperchallenge/activity'
@@ -11,14 +5,15 @@ require File.dirname(__FILE__) +'/keeperchallenge/database'
11
5
 
12
6
  # will handle user input - will to quit the program, input from the console
13
7
  class UserInput
14
- attr_accessor :exit
8
+ attr_accessor :exit, :input
15
9
  def initialize
16
10
  exit = false
11
+ self.input = $stdin
17
12
  end
18
13
 
19
14
  def get_input
20
15
  print '> '
21
- result = gets.chomp()
16
+ result = input.gets.chomp()
22
17
  return result
23
18
  end
24
19
 
@@ -51,27 +46,30 @@ class Display
51
46
  end
52
47
  end
53
48
 
49
+ #will return an index to use in the [@players]
54
50
  def select_player(players)
55
51
  puts "Please select your player"
56
52
  i=1
57
53
  if !players.empty?
58
- players.each do |player|
59
- puts "#{i}. #{player.name}"
54
+ players.each do |key,player|
55
+ puts "#{player.name}"
60
56
  end
61
57
  choice = @input.get_input
62
- return choice.to_i-1
58
+ return choice
63
59
  else
64
60
  puts "There are no players in the database"
65
61
  return nil
66
62
  end
67
63
  end
68
64
 
65
+ # Will return the new player name
69
66
  def add_player
70
67
  puts "Enter the new player name :"
71
68
  name = @input.get_input
72
69
  return name
73
70
  end
74
71
 
72
+ # will return the components of a new activity
75
73
  def add_activity(activities_type)
76
74
 
77
75
  puts "Enter activity type"
@@ -91,13 +89,23 @@ class Display
91
89
  return type, time, cal, km
92
90
  end
93
91
 
92
+ # Will print the scores
94
93
  def display_scores(players)
95
94
  puts "Scores :"
96
- players.each do |player|
95
+ players.each do |key,player|
97
96
  puts "#{player.name} : #{player.score}"
98
97
  end
99
98
  end
100
99
 
100
+ def confirm_delete
101
+ puts "Are you sure you want to erase all data? (type YES)"
102
+ return @input.get_input
103
+ end
104
+
105
+ def abort
106
+ puts "Abort"
107
+ end
108
+
101
109
  end
102
110
 
103
111
 
@@ -107,9 +115,9 @@ class Main
107
115
  def initialize
108
116
  # load players
109
117
  # load activities
110
- @players = []
118
+ @players = {}
111
119
  @activities_type = ["velo", "course" , "marche", "natation"]
112
- #setup_test_users
120
+ #setup_test_users
113
121
 
114
122
  end
115
123
  def launch
@@ -130,15 +138,19 @@ class Main
130
138
  when :add_player then
131
139
  name = screen.add_player
132
140
  new_player = Player.new(name)
133
- @players.push(new_player)
134
- #add player
141
+ @players.update(name => new_player)
135
142
  when :launch_scoreboard then
136
143
  score = Score.new(@players)
137
144
  score.compute(@activities_type)
138
145
  screen.display_scores(@players)
139
146
  when :clean_database then
140
- db.clear
141
- @players.clear
147
+ response = screen.confirm_delete
148
+ if response == "YES"
149
+ db.clear
150
+ @players.clear
151
+ else
152
+ screen.abort
153
+ end
142
154
 
143
155
  end
144
156
  db.save(@players)
@@ -146,14 +158,14 @@ class Main
146
158
  end
147
159
 
148
160
  def setup_test_users
149
- @players.push(Player.new("Ben"))
150
- @players.push(Player.new("Claire"))
161
+ @players.update({"Ben" => Player.new("Ben")})
162
+ @players.update({"Claire" => Player.new("Claire")})
151
163
 
152
- @players[0].add_activity("velo", 15, 15, 15)
153
- @players[0].add_activity("course", 10, 10, 10)
154
- @players[0].add_activity("course", 20, 20, 20)
155
- @players[1].add_activity("velo", 12, 12, 12)
156
- @players[1].add_activity("natation", 30, 20, 10)
164
+ @players["Ben"].add_activity("velo", 15, 15, 15)
165
+ @players["Ben"].add_activity("course", 10, 10, 10)
166
+ @players["Claire"].add_activity("course", 20, 20, 20)
167
+ @players["Claire"].add_activity("velo", 12, 12, 12)
168
+ @players["Claire"].add_activity("natation", 30, 20, 10)
157
169
  end
158
170
 
159
171
 
@@ -1,9 +1,10 @@
1
1
  class FileDatabase
2
-
2
+ attr_reader :folder_path
3
3
  # will create the db folder if not present on system
4
4
  def initialize
5
5
  #if db folder does not exist, create it
6
6
  @folder_path = File.dirname(__FILE__) + "/db/"
7
+ puts @folder_path
7
8
  unless File.directory?(@folder_path)
8
9
  Dir.mkdir(@folder_path)
9
10
  end
@@ -12,20 +13,20 @@ class FileDatabase
12
13
 
13
14
  #will load all files from db directory and load them as player and activities
14
15
  def load(players)
15
- index = 0
16
+
16
17
  # read all files in database
17
18
 
18
19
  Dir.foreach(@folder_path) do |file|
19
20
  if !(file =='.' || file == '..')
20
21
  # create an object per file (=player)
21
- players.push(Player.new(file))
22
+ players.update({file => Player.new(file)})
22
23
  # populate with activities
23
24
  content = File.open("#{@folder_path}#{file}")
24
25
  content.each_line do |line|
25
26
  activity = line.split(' ')
26
- players[index].add_activity(activity[0], activity[1], activity[2], activity[3])
27
+ players[file].add_activity(activity[0], activity[1], activity[2], activity[3])
27
28
  end
28
- index += 1
29
+
29
30
  content.close
30
31
  end
31
32
  end
@@ -34,7 +35,7 @@ class FileDatabase
34
35
  #will save player to static files in db directory
35
36
  def save(players)
36
37
  # create a file per player
37
- players.each do |player|
38
+ players.each do |key,player|
38
39
  file_name = "#{@folder_path}#{player.name}"
39
40
  player_file = File.open(file_name,'w')
40
41
  # in this file : one activity per line, each attribute separated by a space
@@ -2,18 +2,21 @@ class Player
2
2
  attr_reader :name
3
3
  attr_accessor :activities, :score
4
4
 
5
+ #only need a name to initialize a player
5
6
  def initialize(name)
6
7
  @name = name
7
8
  @activities = []
8
9
  @score = 0
9
10
  end
10
11
 
12
+ # to add an activity, need all the parameters of an activity
11
13
  def add_activity(type, time, cal, km)
12
14
  new_activity = Activity.new(type, time, cal, km)
13
15
  @activities.push(new_activity)
14
16
  end
15
17
 
16
- def list_by_type(type)
18
+ # will return all activities taken for a type of activity in an array
19
+ def activities_taken(type)
17
20
  activity_by_type = []
18
21
  @activities.each do |activity|
19
22
  if activity.type == type
@@ -23,6 +26,7 @@ class Player
23
26
  return activity_by_type
24
27
  end
25
28
 
29
+ # Will count all sports the player practised.
26
30
  def count_activity_type
27
31
  total_activities_type = 0
28
32
  previous_activity = ''
@@ -14,12 +14,12 @@ class Score
14
14
  total_time = {}
15
15
  total_cal = {}
16
16
  total_km = {}
17
- @players.each do |player|
17
+ @players.each do |key,player|
18
18
  total_time[player.name] =0
19
19
  total_cal[player.name] =0
20
20
  total_km[player.name] =0
21
21
 
22
- player.list_by_type(activity).each do |player_activity|
22
+ player.activities_taken(activity).each do |player_activity|
23
23
  total_time[player.name] += player_activity.time.to_i
24
24
  total_cal[player.name] += player_activity.cal.to_i
25
25
  total_km[player.name] += player_activity.km.to_i
@@ -27,7 +27,7 @@ class Score
27
27
  end
28
28
 
29
29
  #d�termine le meilleur de chaque discipline
30
- @players.each do |player|
30
+ @players.each do |key,player|
31
31
  if player.name == find_best_score(total_time)
32
32
  puts "Best total time in #{activity} for #{player.name}, adding 3 points"
33
33
  player.score += 3
@@ -46,13 +46,14 @@ class Score
46
46
  end
47
47
 
48
48
  #Incr�mentation du compteur pour le nombre d'activit�s diff�rentes effectu�es
49
- @players.each do |player|
49
+ @players.each do |key,player|
50
50
  puts "Adding numbers of activity for #{player.name} = #{player.count_activity_type}"
51
51
  player.score += player.count_activity_type
52
52
  end
53
53
 
54
54
  end
55
55
 
56
+ # returns the player name of the best total from the given hash.
56
57
  def find_best_score(total_per_player)
57
58
  best_score = 0
58
59
  winner = ''
@@ -66,9 +67,9 @@ class Score
66
67
  return winner
67
68
  end
68
69
 
69
-
70
+ # Reset all scores to prevent abuse from the save file
70
71
  def reset_player_scores()
71
- @players.each do |player|
72
+ @players.each do |key,player|
72
73
  player.score = 0
73
74
  end
74
75
  end
data/readme.md CHANGED
@@ -5,4 +5,12 @@ It will allow :
5
5
  - adding an activity for a player
6
6
  - compute the score for each player in the system
7
7
  - save and load from disk
8
- - erase all data
8
+ - erase all data
9
+
10
+ To install :
11
+ gem install KeeperChallenge
12
+
13
+ To use :
14
+ KeeperChallenge
15
+
16
+ The application is console based for now.
@@ -1,17 +1,161 @@
1
1
  require 'test/unit'
2
+ require 'stringio'
3
+ require_relative '../lib/keeperchallenge'
2
4
 
3
5
  class MyUnitTest < Test::Unit::TestCase
4
6
 
5
- def setup
6
- puts "setup"
7
+ def test_user_input
8
+ # STDIN should be returned by get_input
9
+ user_input = UserInput.new()
10
+ text_input = StringIO.new("velo\n")
11
+ user_input.input = text_input
12
+ assert_equal(user_input.get_input,"velo")
13
+
7
14
  end
8
15
 
9
- def teardown
10
- puts "teardown"
16
+ def test_display
17
+ # test select_player
18
+ user_input = UserInput.new()
19
+ user_input.input = StringIO.new("Ben\n")
20
+ display = Display.new(user_input)
21
+ player1 = Player.new("Ben")
22
+ player2 = Player.new("Claire")
23
+ players = {"Ben" =>player1,"Claire" =>player2}
24
+
25
+ assert_equal(display.select_player(players),"Ben")
26
+
27
+ # test add player
28
+ user_input.input = StringIO.new("Cyril")
29
+ assert_equal(display.add_player(),"Cyril")
30
+
31
+
11
32
  end
12
33
 
13
- def test_basic
14
- puts "I ran"
34
+ def test_main
35
+ game = Main.new()
36
+ game.setup_test_users
37
+ # quite hard to test a console based program that loops through the main.
38
+ # this test is on hold as not vital to the aim to the app
15
39
  end
16
40
 
41
+ def test_activity
42
+ #Only accessors here ! Nothing to test
43
+ end
44
+
45
+ def test_add_activity
46
+ player = Player.new("Ben")
47
+
48
+ # test new activity
49
+ player.add_activity("Velo", 1, 1, 1)
50
+
51
+ assert_equal(player.activities[0].type,"Velo")
52
+ assert_equal(player.activities[0].km,1)
53
+ assert_equal(player.activities[0].cal,1)
54
+ assert_equal(player.activities[0].time,1)
55
+
56
+ end
57
+
58
+ def test_list_by_type
59
+ player = Player.new("Ben")
60
+ player.add_activity("Velo", 1, 1, 1)
61
+
62
+ assert_equal(player.activities_taken("Velo"),[player.activities[0]])
63
+
64
+ end
65
+
66
+ def test_count_activities_type
67
+ player = Player.new("Ben")
68
+ player.add_activity("Velo", 1, 1, 1)
69
+
70
+ assert_equal(player.count_activity_type,1)
71
+
72
+ end
73
+
74
+ def test_score
75
+ setup_test_users
76
+ @activities_type = ["velo", "course" , "marche", "natation"]
77
+ score = Score.new(@players)
78
+
79
+ score.compute(@activities_type)
80
+ assert_equal(@players["Ben"].score,11)
81
+ assert_equal(@players["Claire"].score,21)
82
+
83
+ @players.clear
84
+ end
85
+
86
+ def test_find_best_score
87
+
88
+ score = Score.new(@players)
89
+ total_time = {}
90
+ total_time['Ben'] = 10
91
+ total_time['Claire'] = 5
92
+ assert_equal(score.find_best_score(total_time),'Ben')
93
+
94
+ end
95
+
96
+ def reset_player_score
97
+ player = Player.new("Ben")
98
+ player.score = 10
99
+ players=[player]
100
+ assert_equal(player.score,10)
101
+ score = Score.new(players)
102
+ score.reset_player_score
103
+ assert_equal(player.score,0)
104
+
105
+ end
106
+
107
+
108
+ def setup_test_users
109
+ @players ={}
110
+ @players.update({"Ben" => Player.new("Ben")})
111
+ @players.update({"Claire" => Player.new("Claire")})
112
+
113
+ @players["Ben"].add_activity("velo", 15, 15, 15)
114
+ @players["Ben"].add_activity("course", 10, 10, 10)
115
+ @players["Claire"].add_activity("course", 20, 20, 20)
116
+ @players["Claire"].add_activity("velo", 12, 12, 12)
117
+ @players["Claire"].add_activity("natation", 30, 20, 10)
118
+ end
119
+
120
+
121
+ def test_load
122
+ players = {}
123
+ @folder_path = "../lib/keeperchallenge/db/"
124
+ puts @folder_path
125
+ player_file = File.open("#{@folder_path}Ben",'w')
126
+ activity_string = "Velo 1 1 1\n"
127
+ player_file.write(activity_string)
128
+ player_file.close
129
+
130
+ db = FileDatabase.new
131
+
132
+ db.load(players)
133
+
134
+ assert_equal(players["Ben"].name,"Ben")
135
+ assert_equal(players["Ben"].activities[0].type,"Velo")
136
+
137
+ db.clear
138
+
139
+ end
140
+
141
+
142
+ def test_save
143
+ setup_test_users
144
+ db = FileDatabase.new
145
+ db.save(@players)
146
+ index = 0
147
+ Dir.foreach(db.folder_path) do |file|
148
+ if !(file =='.' || file == '..')
149
+ if index == 0
150
+ assert_equal(file,'Ben')
151
+ elsif index ==1
152
+ assert_equal(file,'Claire')
153
+ end
154
+ end
155
+ index += 1
156
+ end
157
+
158
+ db.clear
159
+ end
160
+
17
161
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: KeeperChallenge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-24 00:00:00.000000000 Z
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: KeeperChallenge is a console based application that will allow the user
15
15
  to add participants, add activities and then determine the winner of the contest.
@@ -21,6 +21,7 @@ extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
23
  - KeeperChallenge-0.0.2.gem
24
+ - KeeperChallenge-0.0.3.gem
24
25
  - KeeperChallenge.gemspec
25
26
  - bin/keeperchallenge
26
27
  - lib/KeeperChallenge.rb