def remove(cls, thrower, favourite): """Remove Removes a favourite from a thrower Arguments: thrower {str} -- The UUID of the thrower favourite {str} -- The UUID of the favourite Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(thrower) \ .update(lambda t: { "ids": t['ids'].filter(lambda f: f != favourite) }).run(oCon) # Return True if a record was changed return dRes['replaced'] == 1
def calculated(cls, _id, state=True): """Calculated Marks the match as calculated Arguments: _id {str} -- The UUID of the match state {bool} -- The value to set finished to Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query to update the games_finished field # atomically dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({ "calculated": state }) \ .run(oCon) # Return based on whether anything was replaced return dRes['replaced'] == 1
def addOvertime(cls, _id): """Add Overtime Adds a section to the existing match Arguments: _id {str} -- The UUID of the match Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query to add the big axe section dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({ "overtime": { "finished": {"i": False, "o": False}, "i": [], "o": [] } }) \ .run(oCon) # Return based on whether anything was replaced return dRes['replaced'] == 1
def updateOvertime(cls, _id, _is, data): """Update Overtime Updates or adds a throw to the overtime section Arguments: _id {str} -- The UUID of the match _is {str} -- The thrower to update, i or o data {dict} -- The current data in the big axe section for the type Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query to add or update an overtime throw dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({ "overtime": { "finished": {"i": False, "o": False}, _is: data[_is] } }) \ .run(oCon) # Return based on whether anything was replaced return dRes['replaced'] == 1
def updateThrow(cls, _id, _is, throw, value): """Update Throw Updates a single data point in the game record Arguments: _id {str} -- The UUID of the match to update _is {str} -- The thrower to update, i or o throw {str} -- The throw to update, 1 to 5 value {mixed} -- The value to set for the throw Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query to update the throw dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({"game": { _is: { throw: value } }}) \ .run(oCon) # Return true if something was updated return dRes['replaced'] == 1
def finishGame(cls, _id, _is): """Finish Game Updates the finished bool Arguments: _id {str} -- The UUID of the match _is {str} -- The thrower, either 'i' or 'o' Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query to update the games_finished field # atomically dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({ "game_finished": { _is: True } }) \ .run(oCon) # Return based on whether anything was replaced return dRes['replaced'] == 1
def unfinished(cls, thrower): """Unfinished Fetches all unfinished (`finished` = False) matches where thrower is initiator or opponent Arguments: thrower {str} -- The UUID of the thrower Returns: list """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query to find all unfinished matches and # then filter them by thrower itRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get_all(False, index='finished') \ .filter((Record_ReDB.r.row['initiator'] == thrower) | \ (Record_ReDB.r.row['opponent'] == thrower)) \ .pluck('_id', 'initiator', 'opponent') \ .default(None) \ .run(oCon) # Return the records found return [d for d in itRes]
def add(cls, thrower, favourite): """Add Adds a favourite to a thrower Arguments: thrower {str} -- The UUID of the thrower favourite {str} -- The UUID of the favourite Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Up to the table t = Record_ReDB.r.db(dStruct['db']).table(dStruct['table']) # Generate the rethink query dRes = Record_ReDB.r.branch( t.get(thrower).ne(None), t.get(thrower).update( {"ids": Record_ReDB.r.row['ids'].set_insert(favourite)}), t.insert({ "_thrower": thrower, "ids": [favourite] })).run(oCon) # Return True if a record was changed return dRes['replaced'] == 1 or dRes['inserted'] == 1
def email(cls, _id, email): """Email Updates the email associated with the thrower Arguments: _id {str} -- The UUID of the thrower email {str} -- The new email Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({"email": email}) \ .run(oCon) # Return based on whether anything was replaced return dRes['replaced'] == 1
def search(cls, q): """Search Searches for throwers based on alias Arguments: q string -- The query to search Returns: list """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query itRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .filter(lambda thrower: (thrower['_id'] == q) | (thrower['alias'].match("(?i)" + q)) ) \ .pluck("_id", "alias") \ .default(None) \ .run(oCon) return [d for d in itRes]
def alias(cls, _id, alias): """Alias Updates the alias associated with the thrower Arguments: _id {str} -- The UUID of the thrower alias {str} -- The new alias Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({"alias": alias}) \ .run(oCon) # Return based on whether anything was replaced return dRes['replaced'] == 1
def run(): # Create the new stats table print('Creating `natf_match_stats` table') if not MatchStats.tableCreate(): return False # Get a connection to the host dStruct = Match.struct() with Record_ReDB._with(dStruct['host']) as oCon: # Add the calculated field to matches print('Adding `calculated` field to `natf_match` table') dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .update({"calculated": False}) \ .run(oCon) print('Adding `calculate` index to `natf_match` table') dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .index_create('calculate', [ Record_ReDB.r.row['finished'], Record_ReDB.r.row['calculated'] ]) \ .run(oCon) # Return OK return True
def run(): # Get the thrower struct dStruct = Thrower.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Add NATF as prefered org to all throwers dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .update({"org": 'natf'}) \ .run(oCon) # Return based on update result return dRes['replaced'] != 0
def finishBigAxeReset(cls, _type, _id): """Finish Big Axe reset Updates the bigaxe.[_type].finished int to zero Arguments: _type {str} -- The type to reset, 'points' or 'target' _id {str} -- The UUID of the match Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query to update the games_finished field # atomically dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({ "bigaxe": { _type: { "finished": { "i": False, "o": False } } } }) \ .run(oCon) # Return based on whether anything was replaced return dRes['replaced'] == 1
def finishOvertimeReset(cls, _id): """Finish Overtime reset Updates the bigaxe.finished object to false/false Arguments: _id {str} -- The UUID of the match Returns: bool """ # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Generate the rethink query to update the games_finished field # atomically dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(_id) \ .update({ "overtime": { "finished": { "i": False, "o": False } } }) \ .run(oCon) # Return based on whether anything was replaced return dRes['replaced'] == 1
def add(cls, thrower, stats): """Add Adds stats from a match to the totals in this table Arguments: thrower {str} -- The UUID of the thrower stats {dict} -- The stats to add to the existing stats Returns: bool """ # Add the thrower to the stats stats['_thrower'] = thrower # Get the structure dStruct = cls.struct() # Simplify r = Record_ReDB.r # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Up to the table t = r.db(dStruct['db']).table(dStruct['table']) # Generate the rethink query dRes = r.branch( t.get(thrower).ne(None), t.get(thrower).update({ "matches": r.row['matches'] + stats['matches'], "points": r.row['points'] + stats['points'], "wins": r.row['wins'] + stats['wins'], "losses": r.row['losses'] + stats['losses'], "eightyones": r.row['eightyones'] + stats['eightyones'], "supernaturals": r.row['supernaturals'] + stats['supernaturals'], "naturals": r.row['naturals'] + stats['naturals'], "unnaturals": r.row['unnaturals'] + stats['unnaturals'], "clutches": { "attempts": r.row['clutches']['attempts'] + stats['clutches']['attempts'], "drops": r.row['clutches']['drops'] + stats['clutches']['drops'], "hits": r.row['clutches']['hits'] + stats['clutches']['hits'] }, "regular": { "attempts": r.row['regular']['attempts'] + stats['regular']['attempts'], "drops": r.row['regular']['drops'] + stats['regular']['drops'], "fives": r.row['regular']['fives'] + stats['regular']['fives'], "threes": r.row['regular']['threes'] + stats['regular']['threes'], "ones": r.row['regular']['ones'] + stats['regular']['ones'], "zeros": r.row['regular']['zeros'] + stats['regular']['zeros'] }, "bigaxe": { "matches": r.row['bigaxe']['matches'] + stats['bigaxe']['matches'], "wins": r.row['bigaxe']['wins'] + stats['bigaxe']['wins'], "losses": r.row['bigaxe']['losses'] + stats['bigaxe']['losses'], "paint": { "attempts": r.row['bigaxe']['paint']['attempts'] + stats['bigaxe']['paint']['attempts'], "drops": r.row['bigaxe']['paint']['drops'] + stats['bigaxe']['paint']['drops'], "hits": r.row['bigaxe']['paint']['hits'] + stats['bigaxe']['paint']['hits'] }, "points": { "clutches": { "attempts": r.row['bigaxe']['points']['clutches']['attempts'] + stats['bigaxe']['points']['clutches']['attempts'], "drops": r.row['bigaxe']['points']['clutches']['drops'] + stats['bigaxe']['points']['clutches']['drops'], "hits": r.row['bigaxe']['points']['clutches']['hits'] + stats['bigaxe']['points']['clutches']['hits'] }, "regular": { "attempts": r.row['bigaxe']['points']['regular']['attempts'] + stats['bigaxe']['points']['regular']['attempts'], "drops": r.row['bigaxe']['points']['regular']['drops'] + stats['bigaxe']['points']['regular']['drops'], "fives": r.row['bigaxe']['points']['regular']['fives'] + stats['bigaxe']['points']['regular']['fives'], "threes": r.row['bigaxe']['points']['regular']['threes'] + stats['bigaxe']['points']['regular']['threes'], "ones": r.row['bigaxe']['points']['regular']['ones'] + stats['bigaxe']['points']['regular']['ones'], "zeros": r.row['bigaxe']['points']['regular']['zeros'] + stats['bigaxe']['points']['regular']['zeros'] } } } }), t.insert(stats) ).run(oCon) # Return true if something was updated return dRes['replaced'] == 1 or dRes['inserted'] == 1
def run(): # Create the new stats table print('Creating `natf_practice_stats` table') PracticeStats.tableCreate() # Get a connection to the host dStruct = Practice.struct() with Record_ReDB._with(dStruct['host']) as oCon: # Fetch unique throwers in natf practice table print('Fetching unique throwers in `natf_practice`') itRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .pluck('thrower') \ .distinct() \ .run(oCon) lThrowers = [d['thrower'] for d in itRes] # Go through each thrower and get all practices print('Adding stats to each thrower', end='') for sThrower in lThrowers: # Init stats dStats = {} # Fetch all practices for this thrower lStats = Practice.get(filter={"thrower": sThrower}, raw=['clutches', 'points', 'throws']) # Go through each practice for d in lStats: # If we have no current stats if not dStats: dStats = d # Else, add to the existing else: dStats['clutches']['attempts'] += d['clutches']['attempts'] dStats['clutches']['hits'] += d['clutches']['hits'] dStats['points']['target'] += d['points']['target'] dStats['points']['total'] += d['points']['total'] dStats['throws']['attempts'] += d['throws']['attempts'] dStats['throws']['drops'] += d['throws']['drops'] dStats['throws']['hits'] += d['throws']['hits'] # Add the thrower ID dStats['_thrower'] = sThrower # Create the stats for the thrower oPS = PracticeStats(dStats) oPS.create() print('.', end='') print('') # Return OK return True
def add(cls, thrower, stats): """Add Adds stats from a practice to the totals in this table Arguments: thrower {str} -- The UUID of the thrower stats {dict} -- The stats to add to the existing stats Returns: bool """ # Add the version and thrower stats['_thrower'] = thrower # Get the structure dStruct = cls.struct() # Get a connection to the host with Record_ReDB._with(dStruct['host']) as oCon: # Up to the table t = Record_ReDB.r.db(dStruct['db']).table(dStruct['table']) # Generate the rethink query dRes = Record_ReDB.r.branch( t.get(thrower).ne(None), t.get(thrower).update({ "ksLeft": { "attempts": Record_ReDB.r.row['ksLeft']['attempts'] + stats['ksLeft']['attempts'], "drops": Record_ReDB.r.row['ksLeft']['drops'] + stats['ksLeft']['drops'], "hits": Record_ReDB.r.row['ksLeft']['hits'] + stats['ksLeft']['hits'], "points": Record_ReDB.r.row['ksLeft']['points'] + stats['ksLeft']['points'] }, "ksRight": { "attempts": Record_ReDB.r.row['ksRight']['attempts'] + stats['ksRight']['attempts'], "drops": Record_ReDB.r.row['ksRight']['drops'] + stats['ksRight']['drops'], "hits": Record_ReDB.r.row['ksRight']['hits'] + stats['ksRight']['hits'], "points": Record_ReDB.r.row['ksRight']['points'] + stats['ksRight']['points'] }, "regular": { "attempts": Record_ReDB.r.row['regular']['attempts'] + stats['regular']['attempts'], "drops": Record_ReDB.r.row['regular']['drops'] + stats['regular']['drops'], "sixes": Record_ReDB.r.row['regular']['sixes'] + stats['regular']['sixes'], "fours": Record_ReDB.r.row['regular']['fours'] + stats['regular']['fours'], "threes": Record_ReDB.r.row['regular']['threes'] + stats['regular']['threes'], "twos": Record_ReDB.r.row['regular']['twos'] + stats['regular']['twos'], "ones": Record_ReDB.r.row['regular']['ones'] + stats['regular']['ones'], "zeros": Record_ReDB.r.row['regular']['zeros'] + stats['regular']['zeros'], "hits": Record_ReDB.r.row['regular']['hits'] + stats['regular']['hits'], "points": Record_ReDB.r.row['regular']['points'] + stats['regular']['points'] } }), t.insert(stats)).run(oCon) # Return true if something was updated return dRes['replaced'] == 1 or dRes['inserted'] == 1
def run(): # Notify print("Updating NATF practice stats to version 2") # Get the structures for the tables dPractStruct = Practice.struct() dStatsStruct = PracticeStats.struct() # Get a connection to the host with Record_ReDB._with(dPractStruct['host']) as oCon: # Fetch unique throwers in natf practice table print('Fetching unique throwers in `natf_practice`') itRes = Record_ReDB.r \ .db(dPractStruct['db']) \ .table(dPractStruct['table']) \ .pluck('thrower') \ .distinct() \ .run(oCon) lThrowers = [d['thrower'] for d in itRes] # Go through each thrower and get all practices print('Compiling stats for each thrower', end='') for sThrower in lThrowers: # Init stats dStats = {} # Fetch all practices for this thrower lStats = [ d['stats'] for d in Practice.get(sThrower, index="thrower", raw=['stats']) ] # Go through each practice for d in lStats: # If we have no current stats if not dStats: dStats = d # Else, add to the existing else: dStats['bigaxe']['clutches']['attempts'] += d['bigaxe'][ 'clutches']['attempts'] dStats['bigaxe']['clutches']['drops'] += d['bigaxe'][ 'clutches']['drops'] dStats['bigaxe']['clutches']['hits'] += d['bigaxe'][ 'clutches']['hits'] dStats['bigaxe']['clutches']['points'] += d['bigaxe'][ 'clutches']['points'] dStats['bigaxe']['regular']['attempts'] += d['bigaxe'][ 'regular']['attempts'] dStats['bigaxe']['regular']['drops'] += d['bigaxe'][ 'regular']['drops'] dStats['bigaxe']['regular']['fives'] += d['bigaxe'][ 'regular']['fives'] dStats['bigaxe']['regular']['threes'] += d['bigaxe'][ 'regular']['threes'] dStats['bigaxe']['regular']['ones'] += d['bigaxe'][ 'regular']['ones'] dStats['bigaxe']['regular']['zeros'] += d['bigaxe'][ 'regular']['zeros'] dStats['bigaxe']['regular']['points'] += d['bigaxe'][ 'regular']['points'] dStats['bigaxe']['regular']['hits'] += d['bigaxe'][ 'regular']['hits'] dStats['standard']['clutches']['attempts'] += d[ 'standard']['clutches']['attempts'] dStats['standard']['clutches']['drops'] += d['standard'][ 'clutches']['drops'] dStats['standard']['clutches']['hits'] += d['standard'][ 'clutches']['hits'] dStats['standard']['clutches']['points'] += d['standard'][ 'clutches']['points'] dStats['standard']['regular']['attempts'] += d['standard'][ 'regular']['attempts'] dStats['standard']['regular']['drops'] += d['standard'][ 'regular']['drops'] dStats['standard']['regular']['fives'] += d['standard'][ 'regular']['fives'] dStats['standard']['regular']['threes'] += d['standard'][ 'regular']['threes'] dStats['standard']['regular']['ones'] += d['standard'][ 'regular']['ones'] dStats['standard']['regular']['zeros'] += d['standard'][ 'regular']['zeros'] dStats['standard']['regular']['points'] += d['standard'][ 'regular']['points'] dStats['standard']['regular']['hits'] += d['standard'][ 'regular']['hits'] # Add the thrower ID and version dStats['_thrower'] = sThrower dStats['_version'] = 2 # Replace the stats dRes = Record_ReDB.r \ .db(dStatsStruct['db']) \ .table(dStatsStruct['table']) \ .get(sThrower) \ .replace(dStats) \ .run(oCon) print(dRes['replaced'] and '.' or 'failed', end='') # Clear newline print('') # Return OK return True
def run(): # Notify print("Updating NATF practices to version 2", end='') # Get the structure for the practices dStruct = Practice.struct() # Get a connection to the DB with Record_ReDB._with(dStruct['host']) as oCon: # Go through all the practices that haven't been converted while True: # Get a single unconverted practice itRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .filter(lambda row: row.has_fields({"_version": True}).not_()) \ .limit(1) \ .run(oCon) dOld = [d for d in itRes] # If there's none, we're done with the loop if not dOld: break # Flatten dOld = dOld[0] # Init the new document dNew = { "_id": dOld['_id'], "_created": dOld['_created'], "_version": 2, "thrower": dOld['thrower'], "data": [], "stats": { "bigaxe": { "clutches": { "attempts": 0, "drops": 0, "hits": 0, "points": 0 }, "regular": { "attempts": 0, "fives": 0, "threes": 0, "ones": 0, "zeros": 0, "drops": 0, "hits": 0, "points": 0 } }, "standard": { "clutches": { "attempts": 0, "drops": 0, "hits": 0, "points": 0 }, "regular": { "attempts": 0, "fives": 0, "threes": 0, "ones": 0, "zeros": 0, "drops": 0, "hits": 0, "points": 0 } }, } } # Go through each throw for d in dOld['data']: # Add the bigaxe field to the throw d['bigaxe'] = False # Add it to the data dNew['data'].append(d) # If it's a clutch if d['clutch']: # Increase the attempts dNew['stats']['standard']['clutches']['attempts'] += 1 # If it's a drop if d['value'] == 'd': dNew['stats']['standard']['clutches']['drops'] += 1 # If it's a hit elif d['value'] == 7: dNew['stats']['standard']['clutches']['hits'] += 1 dNew['stats']['standard']['clutches']['points'] += 7 # Else it's a standard throw else: # Increase the attempts dNew['stats']['standard']['regular']['attempts'] += 1 # Increase the appropriate value if d['value'] == 'd': dNew['stats']['standard']['regular']['drops'] += 1 elif d['value'] == 5: dNew['stats']['standard']['regular']['fives'] += 1 dNew['stats']['standard']['regular']['hits'] += 1 dNew['stats']['standard']['regular']['points'] += 5 elif d['value'] == 3: dNew['stats']['standard']['regular']['threes'] += 1 dNew['stats']['standard']['regular']['hits'] += 1 dNew['stats']['standard']['regular']['points'] += 3 elif d['value'] == 1: dNew['stats']['standard']['regular']['ones'] += 1 dNew['stats']['standard']['regular']['hits'] += 1 dNew['stats']['standard']['regular']['points'] += 1 elif d['value'] == 0: dNew['stats']['standard']['regular']['zeros'] += 1 # Replace the entire record dRes = Record_ReDB.r \ .db(dStruct['db']) \ .table(dStruct['table']) \ .get(dOld['_id']) \ .replace(dNew) \ .run(oCon) # Notify of work (or failure) print(dRes['replaced'] and '.' or 'failed', end='') # Clear line print('') # Return OK return True