With just half a week left in the regular season, the Mets, Giants, and Cardinals are all within 2.5 games of each other. It’s been an extremely close race pretty much all season, especially after the Mets made up ground in late August after Yoenis Cespedes returned from the disabled list. Here are the standings after Wednesday’s games:

W L GB Magic/Tragic Number Mets 85 74 +1.5 2 Giants 83 75 - 4 Cardinals 82 76 1 4

And here are the teams' remaining schedules:

Mets — Three games vs. Phillies

Giants — One game vs. Rockies, three games vs. Dodgers

Cardinals — One game vs. Reds, three games vs. Pirates

Tiebreakers become much easier to solve if there are only two teams involved. Let’s get the easy one out of the way—if two teams tie for the second Wild Card spot then they would play a Game 163—i.e., an extra regular season game—with the winner advancing to the Wild Card game itself. But if two teams end up with the same record for the top spot, then the team with the better head-to-head record during the regular season would host the Wild Card game. The Mets and Cardinals have a 4-3 record against the Giants this season, so if either New York or St. Louis ties with San Francisco they would host the Wild Card game on October 5. Pretty straightforward so far, right?

If the Mets tie with the Cardinals for the top spot a second tiebreaker would be needed. Because the two teams split their season series at 3-3, the factor determining home field advantage would move to whichever team had a better record within their own division—in other words, if the Mets were better against the NL East than the Cardinals were against the NL Central then the Mets host the Wild Card game. Heading into Thursday, the Mets have a 38-35 mark against the East while St. Louis owns a 38-34 record in the Central. Every remaining game is within each team’s own division, making the last week of the season doubly important in case of a tie.

Here’s where it gets tricky. Say a tiebreaker is needed where all three teams finish the regular season with identical records, something that has never happened in MLB history. In order to determine how three teams will divide two playoff spots, combined head-to-head record among the teams are used first. The Mets and Cardinals both went 7-6 against the other two teams, while the Giants went 6-8. Therefore, the Mets and Cardinals will get two chances to make the playoffs and the Giants just one chance.

This is how it would work: The Mets and Cardinals would meet the Monday after the regular season (with the host being determined by better division record, more on this in a minute) for the top Wild Card spot. The loser would play the Giants in San Francisco on Tuesday for the second spot. The winners of the two games will face off in the Wild Card game on Wednesday.

The last detail would be who hosts the first play-in game if all three teams tie. If the Mets and Cardinals both finish with the same regular season records, and the same record against their own division, the host is determined by whichever team has the better record in the final 81 non-interleague regular season games. With three games left, the Mets are 41-37 in such games, while the Cardinals are 41-36 with four games left. It is extremely unlikely this method ends up being used, but good to know nonetheless.

So to recap. If the Mets...

Tie with either team for the bottom WC spot, with the third clinching the top:

The Mets play a Game 163 in order to earn a Wild Card berth.

Tie with the Giants for the top WC spot, and the Cardinals miss:

The Mets host the Giants in the Wild Card game on October 5, due to New York’s 4-3 season series record.

Tie with the Cardinals for the top WC spot, and the Giants miss:

The Mets and Cardinals will play in the Wild Card game on October 5, with the host being determined by whoever has a better in-division record. If that is the same, whoever had a better record against NL teams hosts the game.

Finish with the same record as both the Giants and Cardinals:

The Mets and Cardinals play on October 3, with the winner getting the top Wild Card spot. The loser plays the Giants on October 4 for the bottom Wild Card spot. The winners of the two games play in the Wild Card game on October 5.

Got it? And, after all this madness, the winner of the Wild Card game gets to face the Cubs.