function advanceToNextRound(db, match) {
	engine.defineWinner(match);
	const mid = match.id;

	const matches = db.collection('matches');
	matches.updateOne({ 'team1_source': 'W' + mid }, { $set: { 'team1_id': match.winner } });
	matches.updateOne({ 'team2_source': 'W' + mid }, { $set: { 'team2_id': match.winner } });

	if (match.phase == 'S') { // Loser of semi-final goes forward to third place play-off
		matches.updateOne({ 'team1_source': 'L' + mid }, { $set: { 'team1_id': match.loser } });
		matches.updateOne({ 'team2_source': 'L' + mid }, { $set: { 'team2_id': match.loser } });
	}
}
/**
 * In the final phase, once a match is played, advance winner to next round
 * @param {object} db
 * @param {object} match - Match that has been played
 */
In the final phase, once a match is played, advance winner to next round
@param {object} db
@param {object} match - Match that has been played
In the final phase once a match is played advance winner to next round @param { object } db @param { object } match - Match that has been played
function advanceToNextRound(db, match) {
	engine.defineWinner(match);
	const mid = match.id;
	const matches = db.collection('matches');
	matches.updateOne({ 'team1_source': 'W' + mid }, { $set: { 'team1_id': match.winner } });
	matches.updateOne({ 'team2_source': 'W' + mid }, { $set: { 'team2_id': match.winner } });
	if (match.phase == 'S') { 
		matches.updateOne({ 'team1_source': 'L' + mid }, { $set: { 'team1_id': match.loser } });
		matches.updateOne({ 'team2_source': 'L' + mid }, { $set: { 'team2_id': match.loser } });
	}
}
function advanceToNextRound ( db , match ) { engine . defineWinner ( match ) ; const mid = match . id ; const matches = db . collection ( 'matches' ) ; matches . updateOne ( { 'team1_source' : 'W' + mid } , { $set : { 'team1_id' : match . winner } } ) ; matches . updateOne ( { 'team2_source' : 'W' + mid } , { $set : { 'team2_id' : match . winner } } ) ; if ( match . phase == 'S' ) { matches . updateOne ( { 'team1_source' : 'L' + mid } , { $set : { 'team1_id' : match . loser } } ) ; matches . updateOne ( { 'team2_source' : 'L' + mid } , { $set : { 'team2_id' : match . loser } } ) ; } }
In the final phase, once a match is played, advance winner to next round
 @param {object} db
 @param {object} match - Match that has been played
In the final phase once a match is played advance winner to next round @param { object } db @param { object } match - Match that has been played
// Loser of semi-final goes forward to third place play-off