Skip to content

Commit

Permalink
Add player results screen
Browse files Browse the repository at this point in the history
  • Loading branch information
julien4215 committed Dec 12, 2024
1 parent 8bdda2f commit 1e8253b
Show file tree
Hide file tree
Showing 13 changed files with 778 additions and 103 deletions.
31 changes: 31 additions & 0 deletions lib/src/model/broadcast/broadcast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class BroadcastRound with _$BroadcastRound {
required DateTime? startsAt,
required DateTime? finishedAt,
required bool startsAfterPrevious,
required String? url,
}) = _BroadcastRound;
}

Expand Down Expand Up @@ -136,9 +137,39 @@ class BroadcastPlayerExtended with _$BroadcastPlayerExtended {
required int played,
required double? score,
required int? ratingDiff,
required int? performance,
}) = _BroadcastPlayerExtended;
}

typedef BroadcastFideData = ({
({
int? standard,
int? rapid,
int? blitz,
}) ratings,
int? birthYear,
});

typedef BroadcastPlayerResults = ({
BroadcastPlayerExtended player,
BroadcastFideData fideData,
IList<BroadcastPlayerResultData> games,
});

enum BroadcastPoints { one, half, zero }

@freezed
class BroadcastPlayerResultData with _$BroadcastPlayerResultData {
const factory BroadcastPlayerResultData({
required BroadcastRoundId roundId,
required BroadcastGameId gameId,
required Side color,
required BroadcastPoints? points,
required int? ratingDiff,
required BroadcastPlayer opponent,
}) = _BroadcastPlayerResult;
}

enum RoundStatus {
live,
finished,
Expand Down
205 changes: 205 additions & 0 deletions lib/src/model/broadcast/broadcast_federation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
const fedIdToName = {
'FID': 'FIDE',
'USA': 'United States of America',
'IND': 'India',
'CHN': 'China',
'RUS': 'Russia',
'AZE': 'Azerbaijan',
'FRA': 'France',
'UKR': 'Ukraine',
'ARM': 'Armenia',
'GER': 'Germany',
'ESP': 'Spain',
'NED': 'Netherlands',
'HUN': 'Hungary',
'POL': 'Poland',
'ENG': 'England',
'ROU': 'Romania',
'NOR': 'Norway',
'UZB': 'Uzbekistan',
'ISR': 'Israel',
'CZE': 'Czech Republic',
'SRB': 'Serbia',
'CRO': 'Croatia',
'GRE': 'Greece',
'IRI': 'Iran',
'TUR': 'Turkiye',
'SLO': 'Slovenia',
'ARG': 'Argentina',
'SWE': 'Sweden',
'GEO': 'Georgia',
'ITA': 'Italy',
'CUB': 'Cuba',
'AUT': 'Austria',
'PER': 'Peru',
'BUL': 'Bulgaria',
'BRA': 'Brazil',
'DEN': 'Denmark',
'SUI': 'Switzerland',
'CAN': 'Canada',
'SVK': 'Slovakia',
'LTU': 'Lithuania',
'VIE': 'Vietnam',
'AUS': 'Australia',
'BEL': 'Belgium',
'MNE': 'Montenegro',
'MDA': 'Moldova',
'KAZ': 'Kazakhstan',
'ISL': 'Iceland',
'COL': 'Colombia',
'BIH': 'Bosnia & Herzegovina',
'EGY': 'Egypt',
'FIN': 'Finland',
'MGL': 'Mongolia',
'PHI': 'Philippines',
'BLR': 'Belarus',
'LAT': 'Latvia',
'POR': 'Portugal',
'CHI': 'Chile',
'MEX': 'Mexico',
'MKD': 'North Macedonia',
'INA': 'Indonesia',
'PAR': 'Paraguay',
'EST': 'Estonia',
'SGP': 'Singapore',
'SCO': 'Scotland',
'VEN': 'Venezuela',
'IRL': 'Ireland',
'URU': 'Uruguay',
'TKM': 'Turkmenistan',
'MAR': 'Morocco',
'MAS': 'Malaysia',
'BAN': 'Bangladesh',
'ALG': 'Algeria',
'RSA': 'South Africa',
'AND': 'Andorra',
'ALB': 'Albania',
'KGZ': 'Kyrgyzstan',
'KOS': 'Kosovo *',
'FAI': 'Faroe Islands',
'ZAM': 'Zambia',
'MYA': 'Myanmar',
'NZL': 'New Zealand',
'ECU': 'Ecuador',
'CRC': 'Costa Rica',
'NGR': 'Nigeria',
'JPN': 'Japan',
'SYR': 'Syria',
'DOM': 'Dominican Republic',
'LUX': 'Luxembourg',
'WLS': 'Wales',
'BOL': 'Bolivia',
'TUN': 'Tunisia',
'UAE': 'United Arab Emirates',
'MNC': 'Monaco',
'TJK': 'Tajikistan',
'PAN': 'Panama',
'LBN': 'Lebanon',
'NCA': 'Nicaragua',
'ESA': 'El Salvador',
'ANG': 'Angola',
'TTO': 'Trinidad & Tobago',
'SRI': 'Sri Lanka',
'IRQ': 'Iraq',
'JOR': 'Jordan',
'UGA': 'Uganda',
'MAD': 'Madagascar',
'ZIM': 'Zimbabwe',
'MLT': 'Malta',
'SUD': 'Sudan',
'KOR': 'South Korea',
'PUR': 'Puerto Rico',
'HON': 'Honduras',
'GUA': 'Guatemala',
'PAK': 'Pakistan',
'JAM': 'Jamaica',
'THA': 'Thailand',
'YEM': 'Yemen',
'LBA': 'Libya',
'CYP': 'Cyprus',
'NEP': 'Nepal',
'HKG': 'Hong Kong, China',
'SSD': 'South Sudan',
'BOT': 'Botswana',
'PLE': 'Palestine',
'KEN': 'Kenya',
'AHO': 'Netherlands Antilles',
'MAW': 'Malawi',
'LIE': 'Liechtenstein',
'TPE': 'Chinese Taipei',
'AFG': 'Afghanistan',
'MOZ': 'Mozambique',
'KSA': 'Saudi Arabia',
'BAR': 'Barbados',
'NAM': 'Namibia',
'HAI': 'Haiti',
'ARU': 'Aruba',
'CIV': 'Cote d’Ivoire',
'CPV': 'Cape Verde',
'SUR': 'Suriname',
'LBR': 'Liberia',
'IOM': 'Isle of Man',
'MTN': 'Mauritania',
'BRN': 'Bahrain',
'GHA': 'Ghana',
'OMA': 'Oman',
'BRU': 'Brunei Darussalam',
'GCI': 'Guernsey',
'GUM': 'Guam',
'KUW': 'Kuwait',
'JCI': 'Jersey',
'MRI': 'Mauritius',
'SEN': 'Senegal',
'BAH': 'Bahamas',
'MDV': 'Maldives',
'NRU': 'Nauru',
'TOG': 'Togo',
'FIJ': 'Fiji',
'PLW': 'Palau',
'GUY': 'Guyana',
'LES': 'Lesotho',
'CAY': 'Cayman Islands',
'SOM': 'Somalia',
'SWZ': 'Eswatini',
'TAN': 'Tanzania',
'LCA': 'Saint Lucia',
'ISV': 'US Virgin Islands',
'SLE': 'Sierra Leone',
'BER': 'Bermuda',
'SMR': 'San Marino',
'BDI': 'Burundi',
'QAT': 'Qatar',
'ETH': 'Ethiopia',
'DJI': 'Djibouti',
'SEY': 'Seychelles',
'PNG': 'Papua New Guinea',
'DMA': 'Dominica',
'STP': 'Sao Tome and Principe',
'MAC': 'Macau',
'CAM': 'Cambodia',
'VIN': 'Saint Vincent and the Grenadines',
'BUR': 'Burkina Faso',
'COM': 'Comoros Islands',
'GAB': 'Gabon',
'RWA': 'Rwanda',
'CMR': 'Cameroon',
'MLI': 'Mali',
'ANT': 'Antigua and Barbuda',
'CHA': 'Chad',
'GAM': 'Gambia',
'COD': 'Democratic Republic of the Congo',
'SKN': 'Saint Kitts and Nevis',
'BHU': 'Bhutan',
'NIG': 'Niger',
'GRN': 'Grenada',
'BIZ': 'Belize',
'CAF': 'Central African Republic',
'ERI': 'Eritrea',
'GEQ': 'Equatorial Guinea',
'IVB': 'British Virgin Islands',
'LAO': 'Laos',
'SOL': 'Solomon Islands',
'TGA': 'Tonga',
'TLS': 'Timor-Leste',
'VAN': 'Vanuatu',
};
12 changes: 12 additions & 0 deletions lib/src/model/broadcast/broadcast_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ Future<IList<BroadcastPlayerExtended>> broadcastPlayers(
);
}

@riverpod
Future<BroadcastPlayerResults> broadcastPlayerResult(
Ref ref,
BroadcastTournamentId broadcastTournamentId,
String playerId,
) {
return ref.withClient(
(client) => BroadcastRepository(client)
.getPlayerResults(broadcastTournamentId, playerId),
);
}

@Riverpod(keepAlive: true)
BroadcastImageWorkerFactory broadcastImageWorkerFactory(Ref ref) {
return const BroadcastImageWorkerFactory();
Expand Down
57 changes: 56 additions & 1 deletion lib/src/model/broadcast/broadcast_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ class BroadcastRepository {
mapper: _makePlayerFromJson,
);
}

Future<BroadcastPlayerResults> getPlayerResults(
BroadcastTournamentId tournamentId,
String playerId,
) {
return client.readJson(
Uri(path: 'broadcast/$tournamentId/players/$playerId'),
mapper: _makePlayerResultsFromJson,
);
}
}

BroadcastList _makeBroadcastResponseFromJson(
Expand All @@ -79,7 +89,7 @@ Broadcast _broadcastFromPick(RequiredPick pick) {
round: _roundFromPick(pick('round').required()),
group: pick('group').asStringOrNull(),
roundToLinkId:
pick('roundToLink', 'id').asBroadcastRoundIddOrNull() ?? roundId,
pick('roundToLink', 'id').asBroadcastRoundIdOrNull() ?? roundId,
);
}

Expand Down Expand Up @@ -143,6 +153,7 @@ BroadcastRound _roundFromPick(RequiredPick pick) {
startsAt: pick('startsAt').asDateTimeFromMillisecondsOrNull(),
finishedAt: pick('finishedAt').asDateTimeFromMillisecondsOrNull(),
startsAfterPrevious: pick('startsAfterPrevious').asBoolOrFalse(),
url: pick('url').asStringOrNull(),
);
}

Expand Down Expand Up @@ -219,5 +230,49 @@ BroadcastPlayerExtended _playerExtendedFromPick(RequiredPick pick) {
played: pick('played').asIntOrThrow(),
score: pick('score').asDoubleOrNull(),
ratingDiff: pick('ratingDiff').asIntOrNull(),
performance: pick('performance').asIntOrNull(),
);
}

BroadcastPlayerResults _makePlayerResultsFromJson(
Map<String, dynamic> json,
) {
return (
player: _playerExtendedFromPick(pick(json).required()),
fideData: _fideDataFromPick(pick(json, 'fide')),
games:
pick(json, 'games').asListOrThrow(_makePlayerResultFromPick).toIList()
);
}

BroadcastFideData _fideDataFromPick(Pick pick) {
return (
ratings: (
standard: pick('ratings', 'standard').asIntOrNull(),
rapid: pick('ratings', 'rapid').asIntOrNull(),
blitz: pick('ratings', 'blitz').asIntOrNull()
),
birthYear: pick('year').asIntOrNull(),
);
}

BroadcastPlayerResultData _makePlayerResultFromPick(RequiredPick pick) {
final pointsString = pick('points').asStringOrNull();
BroadcastPoints? points;
if (pointsString == '1') {
points = BroadcastPoints.one;
} else if (pointsString == '1/2') {
points = BroadcastPoints.half;
} else if (pointsString == '0') {
points = BroadcastPoints.zero;
}

return BroadcastPlayerResultData(
roundId: pick('round').asBroadcastRoundIdOrThrow(),
gameId: pick('id').asBroadcastGameIdOrThrow(),
color: pick('color').asSideOrThrow(),
ratingDiff: pick('ratingDiff').asIntOrNull(),
points: points,
opponent: _playerFromPick(pick('opponent').required()),
);
}
Loading

0 comments on commit 1e8253b

Please sign in to comment.