Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 125 lines (101 sloc) 5.49 KB
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application "mina" is installed as part of a gem, and
# this file is here to facilitate running it.
#
require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require "rubygems"
require "bundler/setup"
require "dotenv"
require "sequel"
require "json"
Dotenv.load(".env.#{ENV.fetch("HANAMI_ENV", "development")}")
@event_successor_map = Hash.new([])
@event_successor_map["MatchConfigured"] = ["PlayerAdded"]
@event_successor_map["PlayerAdded"] = ["PlayerAdded", "MatchStarted"]
@event_successor_map["MatchStarted"] = ["LegAdded"]
@event_successor_map["LegAdded"] = ["DartEntered", "LegRemoved"]
@event_successor_map["DartEntered"] = ["DartEntered", "PlayerScored", "PlayerBusted", "DartRemoved", "SingleCheckoutHit", "SingleCheckoutMissed", "DoubleCheckoutHit", "DoubleCheckoutMissed", "TripleCheckoutHit", "TripleCheckoutMissed"]
@event_successor_map["SingleCheckoutHit"] = ["DartEntered", "PlayerScored", "DoubleCheckoutHit", "TripleCheckoutHit", "DoubleCheckoutMissed", "TripleCheckoutMissed"]
@event_successor_map["DoubleCheckoutHit"] = ["DartEntered", "PlayerScored", "TripleCheckoutHit", "TripleCheckoutMissed"]
@event_successor_map["TripleCheckoutHit"] = ["DartEntered", "PlayerScored"]
@event_successor_map["SingleCheckoutMissed"] = ["DartEntered", "PlayerScored", "PlayerBusted", "DoubleCheckoutMissed", "TripleCheckoutMissed", "DoubleCheckoutHit", "TripleCheckoutHit", "ReverseSingleCheckoutMissed"]
@event_successor_map["DoubleCheckoutMissed"] = ["DartEntered", "PlayerScored", "PlayerBusted", "TripleCheckoutMissed", "TripleCheckoutHit", "ReverseSingleCheckoutMissed", "ReverseDoubleCheckoutMissed"]
@event_successor_map["TripleCheckoutMissed"] = ["DartEntered", "PlayerScored", "PlayerBusted", "ReverseDoubleCheckoutMissed", "ReverseTripleCheckoutMissed", "ReverseSingleCheckoutMissed"]
@event_successor_map["PlayerScored"] = ["PlayerWonLeg", "TurnChanged"]
@event_successor_map["PlayerBusted"] = ["TurnChanged"]
@event_successor_map["PlayerWonLeg"] = ["PlayerWonMatch", "LegAdded", "MatchTied"]
@event_successor_map["TurnChanged"] = ["DartEntered", "TurnChanged", "ReversePlayerBusted", "ReversePlayerScored"]
@event_successor_map["LegRemoved"] = ["ReversePlayerWonLeg"]
@event_successor_map["ReversePlayerWonLeg"] = ["ReversePlayerScored"]
@event_successor_map["ReversePlayerScored"] = ["DartRemoved", "ReverseSingleCheckoutHit", "ReverseDoubleCheckoutHit", "ReverseTripleCheckoutHit", "ReverseSingleCheckoutMissed", "ReverseDoubleCheckoutMissed", "ReverseTripleCheckoutMissed"]
@event_successor_map["ReverseSingleCheckoutHit"] = ["DartRemoved", "ReverseDoubleCheckoutMissed", "ReverseTripleCheckoutMissed"]
@event_successor_map["ReverseDoubleCheckoutHit"] = ["DartRemoved", "ReverseSingleCheckoutMissed", "ReverseTripleCheckoutMissed"]
@event_successor_map["ReverseTripleCheckoutHit"] = ["DartRemoved", "ReverseSingleCheckoutMissed", "ReverseDoubleCheckoutMissed"]
@event_successor_map["ReverseSingleCheckoutMissed"] = ["DartRemoved", "ReverseDoubleCheckoutHit", "ReverseTripleCheckoutHit", "ReverseDoubleCheckoutMissed", "ReverseTripleCheckoutMissed"]
@event_successor_map["ReverseDoubleCheckoutMissed"] = ["DartRemoved", "ReverseTripleCheckoutHit", "ReverseTripleCheckoutMissed"]
@event_successor_map["ReverseTripleCheckoutMissed"] = ["DartRemoved"]
@event_successor_map["DartRemoved"] = ["DartEntered", "DartRemoved", "LegRemoved", "TurnChanged", "ReverseSingleCheckoutMissed", "ReverseDoubleCheckoutMissed", "ReverseTripleCheckoutMissed"]
@event_successor_map["PlayerWonMatch"] = ["ReversePlayerWonMatch"]
@event_successor_map["ReversePlayerWonMatch"] = ["ReversePlayerWonLeg"]
@event_successor_map["ReversePlayerBusted"] = ["DartRemoved", "ReverseSingleCheckoutMissed", "ReverseDoubleCheckoutMissed", "ReverseTripleCheckoutMissed"]
@event_successor_map["MatchTied"] = ["ReverseMatchTied"]
@event_successor_map["ReverseMatchTied"] = ["ReversePlayerWonLeg"]
def sanity_check_match(match)
result = sanity_check_events(JSON.parse(match[:events] || "[]"))
if result[0] == :error
[:error, match, result[1], result[2], result[3]]
else
[:ok]
end
end
def sanity_check_events(events)
events.each_with_index do |event, index|
successor = events.at(index + 1)
next if successor.nil?
current_name = event["name"]
successor_name = successor["name"]
unless @event_successor_map[current_name].include?(successor_name)
return [:error, current_name, successor_name, @event_successor_map[current_name]]
end
end
[:ok]
end
failed_checks = []
Sequel.connect(ENV["DATSU_DATABASE_URL"]) do |db|
db[:matches].order(:created_at).all.each do |match|
result = sanity_check_match(match)
if result[0] == :error
failed_checks << result
end
end
end
puts "Matches that failed sanity check."
puts ""
failed_checks.each do |check|
puts "Match ##{check[1][:id]}"
puts "Previous Event: #{check[2]}"
puts "Got Event: #{check[3]}"
puts "Expected Event: #{check[4].join(", ")}"
puts ""
end
grouped_failures = Hash.new
failed_checks.each do |check|
key = "#{check[2]} -> #{check[3]}"
grouped_failures[key] = { count: 0, matches: [] } unless grouped_failures.key?(key)
grouped_failures[key][:count] += 1
grouped_failures[key][:matches] << check[1][:id]
end
puts "Grouped failures."
puts ""
grouped_failures.each do |key, value|
puts "Scenario: #{key}"
puts "Count: #{value[:count]}"
puts "Matches: #{value[:matches].join(", ")}"
puts ""
end
puts "#{failed_checks.size} matches failed sanity check."