end
end
+ attr_reader :records
+
# file_path_name is a Pathname object for this storage
#
def initialize(file_path_name)
end
def last_win?(player_id)
- rc = last_valid_record(player_id)
+ rc = last_valid_game(player_id)
return false unless rc
return rc[:winner] == player_id
end
def last_lose?(player_id)
- rc = last_valid_record(player_id)
+ rc = last_valid_game(player_id)
return false unless rc
return rc[:loser] == player_id
end
- def last_valid_record(player_id)
+ def last_valid_game(player_id)
records = nil
- @mutex.synchronize do
+ @@mutex.synchronize do
records = @records.reverse
end
rc = records.find do |rc|
rc[:winner] &&
rc[:loser] &&
- (rc[:black] == player_id || rc[:while] == player_id)
+ (rc[:black] == player_id || rc[:white] == player_id)
end
return rc
end
@history = ShogiServer::League::Floodgate::History.new @file
end
- def teaup
+ def teardown
@file.delete if @file.exist?
end
assert file.exist?
file.delete if file.exist?
end
+
+ def test_update
+ dummy = nil
+ def @history.make_record(game_result)
+ {:game_id => "wdoor+floodgate-900-0-hoge-foo-1",
+ :black => "hoge", :white => "foo",
+ :winner => "foo", :loser => "hoge"}
+ end
+ @history.update(dummy)
+
+ def @history.make_record(game_result)
+ {:game_id => "wdoor+floodgate-900-0-hoge-foo-2",
+ :black => "hoge", :white => "foo",
+ :winner => "hoge", :loser => "foo"}
+ end
+ @history.update(dummy)
+
+ def @history.make_record(game_result)
+ {:game_id => "wdoor+floodgate-900-0-hoge-foo-3",
+ :black => "hoge", :white => "foo",
+ :winner => nil, :loser => nil}
+ end
+ @history.update(dummy)
+
+ @history.load
+ assert_equal 3, @history.records.size
+ assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
+ assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
+ assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
+ assert_equal "hoge", @history.records[1][:black]
+ assert_equal "foo", @history.records[1][:white]
+ assert_equal "hoge", @history.records[1][:winner]
+ assert_equal "foo", @history.records[1][:loser]
+
+ assert @history.last_win? "hoge"
+ assert !@history.last_win?("foo")
+ assert !@history.last_lose?("hoge")
+ assert @history.last_lose?("foo")
+ end
end
+
+