def runmatches(confrontations=confrontations_by_strategy(), min_matches=config.min_matches,
        matches_missing=0, matchduration=config.duration):
    """confrontations with the matches that have to be run

    confrontations - confrontations to be runned
    min_matches - minimum number of matches per confrontation
    matches_missing - if the total number of matches missing is supplied prints
    the progress
    """
    if matches_missing:
        matches_played=0

    for fcpD_vs_opp in confrontations:
        # play the required number of matches
        n_played_matches=len(fcpD_vs_opp)
        dbgmsg="{2} - {0} of {1} matches played."
        logging.debug(dbgmsg.format(n_played_matches,min_matches,fcpD_vs_opp))

        while n_played_matches < min_matches:
            dbgmsg="{2} - {0} of {1} matches played, playing new match."
            logging.debug(dbgmsg.format(n_played_matches, min_matches,
                fcpD_vs_opp))

            statusclient.live(delta=config.duration.seconds,
                    criticaldelta=config.duration.seconds*2)
            fcpD_vs_opp.playnewmatch()
            statusclient.live()
            if matches_missing:
                # update the counter...
                matches_played+=1
                print_progress(matches_played, matches_missing,
                        matchduration=matchduration)
            n_played_matches=len(fcpD_vs_opp)
def main():
    statusclient.live()
    for generation_type in config.generation_types:
        if generation_type is GenerationType.DecisionTree:
            cfs=list(confrontations_by_decisiontree())
            naive_prediction=naive_prediction_for_decisiontree
        elif generation_type is GenerationType.Strategy:
            cfs=list(confrontations_by_strategy())
            naive_prediction=naive_prediction_for_strategy
        elif generation_type is GenerationType.ControlGroup:
            cfs=list(confrontations_controlgroup())
            naive_prediction=naive_prediction_for_controlgroup
        else:
            print generation_type, "is", GenerationType.DecisionTree, (generation_type is
                    GenerationType.DecisionTree)
            print generation_type, "is", GenerationType.Strategy, (generation_type is
                    GenerationType.Strategy)
            print generation_type, "is", GenerationType.ControlGroup, (generation_type is
                    GenerationType.ControlGroup)
            assert False, "this is not meant to happen..."


        outmsg="GENERATING MATCHES OF TYPE {0}".format(generation_type)
        print outmsg
        logging.info(outmsg)
        (nmatches, naive_duration, naive_size)=naive_prediction()
        naive_prediction_msg="naive prediction: {1} runs, {0} duration".format(naive_duration, nmatches)
        print naive_prediction_msg
        logging.info(naive_prediction_msg)

        (nmatches_missing, duration, size)=smart_prediction(cfs)
        print "{0} expected duration time.".format(duration)
        print "{0} expected size.".format(human_size(size))
        finish=datetime.datetime.now()+duration

        expected_finish_str="expected to finish @ {0}".format(finish)
        print expected_finish_str
        logging.info(expected_finish_str)

        report.report("upload",passwd=passwd)

        runmatches(cfs,matches_missing=nmatches_missing)

    statusclient.finish()