Skip to content

Commit

Permalink
Recognise new tourney with t or t11a.
Browse files Browse the repository at this point in the history
  • Loading branch information
greensnark committed Jun 3, 2011
1 parent 0a70b5b commit 5287e63
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 38 deletions.
27 changes: 20 additions & 7 deletions commands/crawl-data.yml
Expand Up @@ -465,13 +465,26 @@ tournament-prefixes:
- tourney
- tournament

tournament-versions:
2008: '0.4'
2009: '0.5'
2010: '0.7'

tournament-sprint-map:
2010: dungeon sprint mu
tournament-data:
default-tourney: '2011a'
crawl:
'2008':
version: "0.4"
time: [20080801, 20080901]
'2009':
version: "0.5"
time: [20090801, 20090901]
'2010':
version: "0.7"
time: [20100801, 20100901]
'2011a':
version: "0.8"
time: [20110514, 20110530]
sprint:
'2010':
version: "0.7"
time: [20100815, 20100901]
map: dungeon sprint mu

sql-field-names:
name: pname
Expand Down
49 changes: 18 additions & 31 deletions commands/sqlhelper.rb
Expand Up @@ -2,17 +2,20 @@

exit(0) if !ENV['HENZELL_SQL_QUERIES']

LG_CONFIG_FILE = 'commands/crawl-data.yml'

require 'dbi'
require 'commands/helper'
require 'commands/tourney'
require 'set'
require 'yaml'

include Tourney

DBNAME = ENV['HENZELL_DBNAME'] || 'henzell'
DBUSER = ENV['HENZELL_DBUSER'] || 'henzell'
DBPASS = ENV['HENZELL_DBPASS'] || ''

LG_CONFIG_FILE = 'commands/crawl-data.yml'

CFG = YAML.load_file(LG_CONFIG_FILE)

# Don't use more than this much memory (bytes)
Expand All @@ -23,7 +26,6 @@
GAME_SPRINT = 'sprint'
GAMES = CFG['game-type-prefixes'].keys
GAME_PREFIXES = CFG['game-type-prefixes']
TOURNEY_SPRINT_MAP = CFG['tournament-sprint-map']

OPERATORS = {
'==' => '=', '!==' => '!=',
Expand Down Expand Up @@ -98,11 +100,6 @@

MILE_TYPES = CFG['milestone-types']

TOURNEY_PREFIXES = CFG['tournament-prefixes']
TOURNEY_VERSIONS = CFG['tournament-versions']

TOURNEY_REGEXES = TOURNEY_PREFIXES.map { |p| %r/^(#{p})(\d*)$/i }

SORTEDOPS = OPERATORS.keys.sort { |a,b| b.length <=> a.length }
OPMATCH = Regexp.new(SORTEDOPS.map { |o| Regexp.quote(o) }.join('|'))
ARGSPLITTER = Regexp.new('^-?([a-z.:_]+)\s*(' +
Expand Down Expand Up @@ -1625,7 +1622,7 @@ def fixup_listgame_arg(preds, sorts, arg)
return reproc.call('place', arg)
end

return reproc.call('when', arg) if TOURNEY_REGEXES.find { |r| arg =~ r }
return reproc.call('when', arg) if tourney_keyword?(arg)

for s in LISTGAME_SHORTCUTS
res = s.call(arg, reproc)
Expand Down Expand Up @@ -1875,28 +1872,19 @@ def query_field(selector, field, op, sqlop, val)
end

if selfield == 'when'
year = 2010
for reg in TOURNEY_REGEXES
if val =~ reg && $2 && !$2.empty?
year = $2.to_i
val.sub!(reg, '\1')
year += 2000 if year < 100
break
end
end
tourney = tourney_info(val, GameContext.game)

if TOURNEY_PREFIXES.index(val.downcase) and [ '=', '!=' ].index(op)
cv = TOURNEY_VERSIONS[year]
if [ '=', '!=' ].index(op)
cv = tourney.version

tourney = op == '='
clause = [ tourney ? 'AND' : 'OR' ]
lop = tourney ? '>' : '<'
rop = tourney ? '<' : '>'
eqop = tourney ? '=' : '!='
in_tourney = op == '='
clause = [ in_tourney ? 'AND' : 'OR' ]
lop = in_tourney ? '>' : '<'
rop = in_tourney ? '<' : '>'
eqop = in_tourney ? '=' : '!='

tstart = "#{year}0701"
tend = "#{year}0801"
tstart = "#{year}0715" if GameContext.game == GAME_SPRINT
tstart = tourney.tstart
tend = tourney.tend

if $CTX == CTX_LOG
clause << query_field('rstart', 'rstart', lop, lop, tstart)
Expand All @@ -1906,9 +1894,8 @@ def query_field(selector, field, op, sqlop, val)
clause << query_field('rtime', 'rtime', rop, rop, tend)
end
clause << query_field('cv', 'cv', eqop, eqop, cv)
if GameContext.game == GAME_SPRINT and TOURNEY_SPRINT_MAP[year]
clause << query_field('map', 'map', eqop, eqop,
TOURNEY_SPRINT_MAP[year])
if tourney.tmap
clause << query_field('map', 'map', eqop, eqop, tourney.tmap)
end
return clause
else
Expand Down
80 changes: 80 additions & 0 deletions commands/tourney.rb
@@ -0,0 +1,80 @@
require 'yaml'

module Tourney
CFG = YAML.load_file(LG_CONFIG_FILE)

TOURNEY_SPRINT_MAP = CFG['tournament-sprint-map']
TOURNEY_PREFIXES = CFG['tournament-prefixes']
TOURNEY_VERSIONS = CFG['tournament-versions']
TOURNEY_DATA = CFG['tournament-data']
SPRINT_TOURNEY_DATES =

TOURNEY_REGEXES = TOURNEY_PREFIXES.map do |p|
%r/^(#{p})(\d*)([a-z]?)$/i
end

def tourney_keyword?(argument)
TOURNEY_REGEXES.find { |r| argument =~ r }
end

def tourney_info(argument, game = GAME_TYPE_DEFAULT)
TourneyInfo.new(argument, game)
end

class TourneyInfo
attr_reader :tstart, :tend, :tmap, :version

def initialize(tournament_key, game_type)
@key = tournament_key
@game_type = game_type
@year = resolve_year(@key)
unless tourney_data[@year]
raise Exception.new("Unknown tournament: #{tournament_key}")
end
@tmap = nil

@version = tourney_data[@year]['version'].strip
resolve_time!
resolve_map!
end

def tourney_data
(TOURNEY_DATA[@game_type] or
raise Exception.new("No tournament data for #{@game_type}"))
end

def resolve_map!
@tmap = tourney_data[@year]['map']
@tmap.strip! if @tmap
end

def resolve_time!
range = tourney_data[@year]['time']
raise Exception.new("No tourney information for #{@year}") unless range
@tstart = raw_date(range[0])
@tend = raw_date(range[1])
end

##
# Convert a regular YYYYMMDD date into a POSIX date with 0-indexed months.
def raw_date(date)
date.to_s.sub(/^(\d{4})(\d{2})(\d{2})/) { |m|
"#$1#{sprintf('%02d', $2.to_i - 1)}#$3"
}
end

def resolve_year(val)
year = TOURNEY_DATA['default-tourney']
for reg in TOURNEY_REGEXES
if val =~ reg && $2 && !$2.empty?
year = $2.to_i
suffix = $3
year += 2000 if year < 100
year = "#{year}#{suffix}"
break
end
end
year
end
end
end

0 comments on commit 5287e63

Please sign in to comment.