# `sol.models.tourney` -- Tournaments

class `sol.models.tourney.``Rank`(points, bucholz, netscore, totscore, rate)
  property `bucholz`

    Alias for field number 1

  property `netscore`

    Alias for field number 2

  property `points`

    Alias for field number 0

  property `rate`

    Alias for field number 4

  property `totscore`

    Alias for field number 3

class `sol.models.tourney.``RankingStats`

  An interim object used keep the ongoing values needed to compute the ranking of a single competitor.

  `bucholz`

    Bucholz.

  `drawn`()

    Update stats after a tie.

  `lost`(netscore)

    Update stats after a lost match.

  `netscore`

    Net score.

  `played_matches`

    Number of played matches.

  `points`

    Overall points.

  `rank`(rate)

    Return the final `Rank`.

  `real_points`

    Points made against real competitors.

  `totscore`

    Total score.

  `virtual_points`

    Estimated further points, after retirement.

  `won`(netscore, against_phantom)

    Update stats after a winned match.

class `sol.models.tourney.``Tourney`(**kwargs)

  A single tournament.

  class `AbstractVisitor`(tourney, pivot, competitors, done)

    Abstract visitor.

    Parameters

      - tourney -- a `Tourney` instance

      - pivot -- a `Competitor` instance

      - competitors -- a list of possible opponents

      - done -- the set of already played pairings

    This is an iterator class, used by the method `Tourney._combine()`: it yields all possible competitors to pivot in some order, without repeating already played matches present in done, a set containing previous matches (both `(a, b)` and `(b, a)`).

    The iteration honors the tourney's delaycompatriotpairing: when `True`, players with the same nationality of the pivot and with the same points will be considered later.

    Concrete subclasses must reimplement the method `computeVisitOrder()`, that determines the actual order.

    `computeVisitOrder`()

      Return a sequence of positions, the indexes into the list self.competitors.

  class `DazedVisitor`(tourney, pivot, competitors, done)

    Visit the competitors, giving precedence to the competitors with the same points.

    This starts looking at the competitors with the same points as the pivot, and then goes on with the others: this is to postpone as much as possible the match between the strongest competitors.

    `computeVisitOrder`()

      First count how many competitors have the same points as the pivot, then if possible iterate over the second half of them, then over the first half, and finally over the remaining ones.

    `countSamePointsAsPivot`()

      Return the count of competitors with the same points as the pivot.

  class `SerialVisitor`(tourney, pivot, competitors, done)

    Visit the competitors in order.

    Given that the list of competitors is sorted by their rank, this effectively tries to combine players with the same strength.

    `computeVisitOrder`()

      Simply return `range(len(self.competitors))`.

  class `StaggeredVisitor`(tourney, pivot, competitors, done)

    Visit the competitors, giving precedence to the competitors with the same points.

    This is similar to `DazedVisitor` except that when there are 50 or more competitors with the same points, instead of splitting them in two halves of the same size it uses an arbitrary offset of 25 (i.e. the 1st competitor is paired with the 26th, the 2nd with the 27th, and so on): this should placate the gripes about unfair pairings between strongest and weakest competitors at the first turn.

    `computeVisitOrder`()

      First count how many competitors have the same points as the pivot, then if possible iterate over the second half of them, then over the first half, and finally over the remaining ones.

  `_areFinalTurnsEnoughForPrizing`()

    Determine whether final rounds are enough to complete the tourney with prize-giving.

  `_assignAsisPrizes`()

    Assign decreasing integer numbers as final prizes, down to 1 to the last competitor.

  `_assignBoards`(matches, comp_boards, available_boards=None)

    Assign a table to each match, possibly one not already used.

  `_assignCentesimalPrizes`()

    Assigns 100 to the winner, 1 to the last, linear interpolation to the others.

  `_assignFixed40Prizes`()

    Assign fixed prizes to the first 40 competitors.

  `_assignFixedPrizes`(prizes=None)

    Assign fixed prizes to the first 16 competitors.

  `_assignMillesimalPrizes`()

    Assign 1000 points to the winner stepping down in fixed amount.

  `_combine`(competitors, done, _level=0)

    Build the next round, based on current ranking.

    This recursively tries to build the next round, pairing together competitors that did not already played against each other.

  `_computeFinalWins`()

    Compute the number of matches won by each competitor in the finals

  `_makeFirstTurn`()

    Create the first round of a tourney, pairing competitors in a random way.

  `_makeNextAAATurn`(competitors, done)

    Mechanically generate next turn out of all possible matches.

  `_makeNextTurn`()

    Build the next round of the game.

  `allPlayers`()

    Generator that return all involved players.

  `assignPrizes`()

    Consolidate final points.

  `caption`(html=None, localized=True)

    Return a possibly HTML-decorated caption of the entity.

    Parameters

      - html -- either `None` (the default) or a boolean value

      - localized -- a boolean value, `True` by default

    Return type

      str

    If html is `None` or `True` then the result may be an HTML representation of the entity, otherwise it is plain text.

    If localized is `False` then the localization is turned off.

  classmethod `check_insert`(session, fields, user_id)

    Check description validity

  `check_update`(fields)

    Check description validity

  `competitors`

    List of `competitors`.

  `computeRanking`(turn=None)

    Recompute competitors ranking.

    Parameters

      turn -- if given, compute the ranking up to that turn

    Returns

      a list of tuples, each containing one of the competitors and a `Rank` instance, sorted on the second item in descending order

    Compute each competitor rank by examining the matches of this tourney, summing up each other's current ranking position as the bucholz.

  `countdownstarted`

    Timestamp of the start of the clock countdown, milliseconds since Unix epoch.

  `couplings`

    Kind of pairing method used to build next round. It may be all, serial, dazed or staggered.

  `currentturn`

    The current round.

  `date`

    Event date.

  `delaycompatriotpairing`

    Whether the pairing of players belonging to the same country shall be postponed or not.

  `delaytoppairing`

    Number of rounds for which pairing of top players should be postponed, if possible.

  `description`

    Event description.

  `duration`

    Duration in minutes of each round, used by the clock.

  `finalkind`

    Kind of finals. It may be simple or bestof3.

  `finals`

    The number of finals that will be played.

  `finalturns`

    Whether the tourney is in final rounds state.

  `idchampionship`

    Related `championship`'s ID.

  `idhostingclub`

    Hosting `club`'s ID.

  `idowner`

    ID of the `user` that is responsible for this record.

  `idrating`

    Possible `rating` ID this tourney uses and updates.

  `idtourney`

    Primary key.

  `location`

    Event location.

  `makeFinalTurn`()

    Generate the final matches.

  `makeNextTurn`()

    Setup the next round.

    If there are no matches, build up the first round using a random coupler. Otherwise, using current ranking, create the next round pairing any given competitor with a not-yet-met other one that follows him in the ranking.

  `matches`

    List of `matches`, sorted by round and board.

  `owner`

    The `owner` of this record, admin when `None`.

  `phantomscore`

    The score assigned to a player in matches against the Phantom.

  `prealarm`

    Prealarm before the end of the round.

  `prized`

    Whether the tourney is closed, and final prizes updated.

  `rankedturn`

    The highest round considered in the ranking.

  property `ranking`

    Competitors sorted by their rank.

    Return type

      sequence

    Returns

      sorted list of `competitors`

  `replay`(date, newidowner=None)

    Clone this tourney, creating new one at given date.

    Of the original, only the competitors are copied. This is particularly useful for doubles (or team), so that the players get copied in the same order.

  `resetPrizes`()

    Reset assigned final points.

  `retirements`

    Policy used to adjust the bucholz of competitors who played against withdrawn players.

  `serialize`(serializer)

    Reduce a single tourney to a simple dictionary.

    Parameters

      serializer -- a `Serializer` instance

    Return type

      dict

    Returns

      a plain dictionary containing a flatified view of this tourney

  `updateRanking`()

    Recompute and update competitors ranking.