from __future__ import print_function import json, os, sys config = json.load(open(os.path.join( os.path.dirname(__file__), 'config.json'))) butler_set = sys.argv[1] if butler_set not in config: sys.exit() groups = config.get('__groups', {}) queries = config.get('__queries', {}) config = config[butler_set] TABL_STEP = 8 TEAM_STEP = 16 PLAYER_STEP = 400 tabl_offset = 0 team_offset = 0 player_offset = 0 for s_db in config['source']: print('USE %s;' % (s_db)) for table in ['matches', 'players', 'scores', 'teams', 'segments']: print('DROP TABLE IF EXISTS _%s;' % (table)) print('CREATE TABLE _%s AS SELECT * FROM %s;' % (table, table)) if s_db in queries: for q in queries[s_db]: print(q) print( 'UPDATE _matches SET tabl=tabl+%d, homet=homet+%d, visit=visit+%d;' % ( tabl_offset, team_offset, team_offset) ) print( 'UPDATE _players SET id=id+%d, team=team+%d;' % ( player_offset, team_offset) ) print ( 'UPDATE _scores SET tabl=tabl+%d;' % ( tabl_offset) ) print ( 'UPDATE _teams SET id=id+%d;' % ( team_offset) ) print( '''UPDATE _segments SET tabl=tabl+%d, homet=homet+%d, visit=visit+%d, openN=openN+%d, openE=openE+%d, openS=openS+%d, openW=openW+%d, closeN=closeN+%d, closeE=closeE+%d, closeS=closeS+%d, closeW=closeW+%d;''' % ( tabl_offset, team_offset, team_offset, player_offset, player_offset, player_offset, player_offset, player_offset, player_offset, player_offset, player_offset) ) tabl_offset += TABL_STEP team_offset += TEAM_STEP player_offset += PLAYER_STEP print('USE %s;' % (config['output'])) print('UPDATE admin SET teamcnt=%d, butler=%d;' % ( 16*len(config['source']), 4*len(config['source']) )) print('UPDATE teams SET bye=2;') print('DELETE FROM butler;') for table in ['boards', 'rounds']: print('DELETE FROM %s;' % (table)) print('INSERT INTO %s SELECT * FROM %s.%s;' % ( table, config['source'][0], table)) for table in ['matches', 'players', 'scores', 'segments']: print('DELETE FROM %s;' % (table)) for s_db in config['source']: print('INSERT INTO %s SELECT * FROM %s._%s;' % ( table, s_db, table)) print('DELETE FROM teams;') for idx, s_db in enumerate(config['source']): print("INSERT INTO teams (id,fullname,shortname,grupa,bye) SELECT id,fullname,CONCAT('%s: ',shortname),%d,bye FROM %s._teams;" % ( groups[s_db], idx+1, s_db)) print('UPDATE admin SET rnd=%d, segm=%d;' % ( int(os.environ.get('LIGA_BUTLER_FINISHED_ROUND', 0)), int(os.environ.get('LIGA_BUTLER_FINISHED_SEGMENT', 0)) ))