feat: create statistics view
This commit is contained in:
@@ -0,0 +1,635 @@
|
||||
import 'package:animated_custom_dropdown/custom_dropdown.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:tallee/core/common.dart';
|
||||
import 'package:tallee/core/constants.dart';
|
||||
import 'package:tallee/core/custom_theme.dart';
|
||||
import 'package:tallee/core/enums.dart';
|
||||
import 'package:tallee/data/db/database.dart';
|
||||
import 'package:tallee/data/models/game.dart';
|
||||
import 'package:tallee/data/models/group.dart';
|
||||
import 'package:tallee/data/models/player.dart';
|
||||
import 'package:tallee/data/models/statistic.dart';
|
||||
import 'package:tallee/l10n/generated/app_localizations.dart';
|
||||
import 'package:tallee/presentation/widgets/buttons/animated_dialog_button.dart';
|
||||
|
||||
class CreateStatisticView extends StatefulWidget {
|
||||
const CreateStatisticView({super.key, required this.onStatisticCreated});
|
||||
|
||||
final void Function() onStatisticCreated;
|
||||
|
||||
@override
|
||||
State<CreateStatisticView> createState() => _CreateStatisticViewState();
|
||||
}
|
||||
|
||||
class _CreateStatisticViewState extends State<CreateStatisticView> {
|
||||
bool isLoading = false;
|
||||
|
||||
/* Data loaded from the database */
|
||||
List<Player> players = [];
|
||||
List<Game> games = [];
|
||||
List<Group> groups = [];
|
||||
|
||||
/* User selections */
|
||||
StatisticType? selectedType;
|
||||
List<StatisticScope> selectedScope = [];
|
||||
List<Game> selectedGames = [];
|
||||
List<Player> selectedPlayers = [];
|
||||
List<Group> selectedGroups = [];
|
||||
Timeframe? selectedTimeframe;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
loadAllData();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var loc = AppLocalizations.of(context);
|
||||
|
||||
return ScaffoldMessenger(
|
||||
child: Scaffold(
|
||||
appBar: AppBar(title: Text(loc.create_statistic)),
|
||||
body: Stack(
|
||||
alignment: AlignmentDirectional.center,
|
||||
children: [
|
||||
SingleChildScrollView(
|
||||
padding: EdgeInsets.only(
|
||||
bottom: MediaQuery.of(context).padding.bottom + 80,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Classifier title
|
||||
Padding(
|
||||
padding: const EdgeInsetsGeometry.only(left: 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
loc.create_statistic_classifier_title,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
loc.create_statistic_classifier_subtitle,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 12,
|
||||
),
|
||||
softWrap: true,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Classifier selection
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 8,
|
||||
horizontal: 16,
|
||||
),
|
||||
child: CustomDropdown<StatisticType>(
|
||||
closedHeaderPadding: const EdgeInsets.symmetric(
|
||||
vertical: 16,
|
||||
horizontal: 16,
|
||||
),
|
||||
listItemBuilder:
|
||||
(context, item, isSelected, onItemSelect) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
translateStatisticTypeToString(item, context),
|
||||
style: itemStyle,
|
||||
),
|
||||
if (isSelected)
|
||||
const Icon(
|
||||
Icons.check,
|
||||
color: CustomTheme.textColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
headerBuilder: (context, selectedType, enabled) => Text(
|
||||
translateStatisticTypeToString(selectedType, context),
|
||||
style: headerStyle,
|
||||
),
|
||||
hintText: loc.select_a_classifier,
|
||||
items: StatisticType.values,
|
||||
decoration: decoration,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
selectedType = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// Scope title
|
||||
Padding(
|
||||
padding: const EdgeInsetsGeometry.only(left: 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
loc.create_statistic_scope_title,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
loc.create_statistic_scope_subtitle,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Scope selection
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 8,
|
||||
horizontal: 16,
|
||||
),
|
||||
child: CustomDropdown<StatisticScope>.multiSelect(
|
||||
closedHeaderPadding: const EdgeInsets.symmetric(
|
||||
vertical: 16,
|
||||
horizontal: 16,
|
||||
),
|
||||
hintText: loc.select_a_scope,
|
||||
items: StatisticScope.values,
|
||||
decoration: decoration,
|
||||
listItemBuilder:
|
||||
(context, scope, isSelected, onItemSelect) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
translateScopeToString(scope, context),
|
||||
style: itemStyle,
|
||||
),
|
||||
if (isSelected)
|
||||
const Icon(
|
||||
Icons.check,
|
||||
color: CustomTheme.textColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
headerListBuilder: (context, selectedItems, enabled) =>
|
||||
Text(
|
||||
selectedItems
|
||||
.map((s) => translateScopeToString(s, context))
|
||||
.join(', '),
|
||||
style: headerStyle,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onListChanged: (List<StatisticScope> values) {
|
||||
setState(() {
|
||||
selectedScope = values;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
if (selectedScope.contains(StatisticScope.selectedGames)) ...[
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// games title
|
||||
Padding(
|
||||
padding: const EdgeInsetsGeometry.only(left: 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
loc.create_statistic_games_title,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
loc.create_statistic_games_subtitle,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// game selection
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 8,
|
||||
horizontal: 16,
|
||||
),
|
||||
child: CustomDropdown<Game>.multiSelect(
|
||||
enabled: !isLoading,
|
||||
disabledDecoration: disabledDecoration,
|
||||
closedHeaderPadding: const EdgeInsets.symmetric(
|
||||
vertical: 16,
|
||||
horizontal: 16,
|
||||
),
|
||||
hintText: isLoading ? loc.loading : loc.select_a_game,
|
||||
items: games,
|
||||
decoration: decoration,
|
||||
listItemBuilder:
|
||||
(context, item, isSelected, onItemSelect) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
// Name
|
||||
Text(item.name, style: itemStyle),
|
||||
const SizedBox(width: 12),
|
||||
|
||||
// Ruleset
|
||||
Text(
|
||||
translateRulesetToString(
|
||||
item.ruleset,
|
||||
context,
|
||||
),
|
||||
style: hintStyle.copyWith(fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// Check icon
|
||||
if (isSelected)
|
||||
const Icon(
|
||||
Icons.check,
|
||||
color: CustomTheme.textColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
headerListBuilder: (context, selectedItems, enabled) =>
|
||||
Text(
|
||||
selectedItems.map((g) => g.name).join(', '),
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onListChanged: (List<Game> values) {
|
||||
setState(() {
|
||||
selectedGames = values;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
if (selectedScope.contains(
|
||||
StatisticScope.selectedGroups,
|
||||
)) ...[
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// groups title
|
||||
Padding(
|
||||
padding: const EdgeInsetsGeometry.only(left: 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
loc.create_statistic_groups_title,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
loc.create_statistic_groups_subtitle,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// groups selection
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 8,
|
||||
horizontal: 16,
|
||||
),
|
||||
child: CustomDropdown<Group>.multiSelect(
|
||||
enabled: !isLoading,
|
||||
disabledDecoration: disabledDecoration,
|
||||
closedHeaderPadding: const EdgeInsets.symmetric(
|
||||
vertical: 16,
|
||||
horizontal: 16,
|
||||
),
|
||||
hintText: isLoading ? loc.loading : loc.select_a_group,
|
||||
items: groups,
|
||||
decoration: decoration,
|
||||
listItemBuilder:
|
||||
(context, item, isSelected, onItemSelect) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
// Name
|
||||
Text(item.name, style: itemStyle),
|
||||
const SizedBox(width: 12),
|
||||
|
||||
// Ruleset
|
||||
Text(
|
||||
' ${item.members.length.toString()} ${loc.members}',
|
||||
style: hintStyle.copyWith(fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (isSelected)
|
||||
const Icon(
|
||||
Icons.check,
|
||||
color: CustomTheme.textColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
headerListBuilder: (context, selectedItems, enabled) =>
|
||||
Text(
|
||||
selectedItems.map((g) => g.name).join(', '),
|
||||
style: headerStyle,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onListChanged: (List<Group> groups) {
|
||||
setState(() {
|
||||
selectedGroups = groups;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
if (selectedScope.contains(StatisticScope.timeframe)) ...[
|
||||
const SizedBox(height: 10),
|
||||
|
||||
// timeframe title
|
||||
Padding(
|
||||
padding: const EdgeInsetsGeometry.only(left: 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
loc.create_statistic_timeframe_title,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
loc.create_statistic_timeframe_subtitle,
|
||||
textAlign: TextAlign.start,
|
||||
style: const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// groups selection
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 8,
|
||||
horizontal: 16,
|
||||
),
|
||||
child: CustomDropdown<Timeframe>(
|
||||
enabled: !isLoading,
|
||||
excludeSelected: false,
|
||||
disabledDecoration: disabledDecoration,
|
||||
closedHeaderPadding: const EdgeInsets.symmetric(
|
||||
vertical: 16,
|
||||
horizontal: 16,
|
||||
),
|
||||
hintText: isLoading
|
||||
? loc.loading
|
||||
: loc.select_a_timeframe,
|
||||
items: Timeframe.values,
|
||||
decoration: decoration,
|
||||
listItemBuilder:
|
||||
(context, timeframe, isSelected, onItemSelect) =>
|
||||
Row(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
translateTimeframeToString(
|
||||
timeframe,
|
||||
context,
|
||||
),
|
||||
style: itemStyle,
|
||||
),
|
||||
if (isSelected)
|
||||
const Icon(
|
||||
Icons.check,
|
||||
color: CustomTheme.textColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
headerBuilder: (context, selectedTimeframe, enabled) =>
|
||||
Text(
|
||||
translateTimeframeToString(
|
||||
selectedTimeframe,
|
||||
context,
|
||||
),
|
||||
style: headerStyle,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onChanged: (Timeframe? timeframe) {
|
||||
setState(() {
|
||||
selectedTimeframe = timeframe;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// Create statistic button
|
||||
Positioned(
|
||||
bottom: MediaQuery.of(context).padding.bottom,
|
||||
child: AnimatedDialogButton(
|
||||
buttonConstraints: const BoxConstraints(minWidth: 350),
|
||||
buttonText: loc.create_statistic,
|
||||
onPressed: selectedType != null && selectedScope.isNotEmpty
|
||||
? () => submitStatistic()
|
||||
: null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
CustomDropdownDecoration get decoration => CustomDropdownDecoration(
|
||||
listItemDecoration: const ListItemDecoration(
|
||||
selectedIconBorder: BorderSide(color: CustomTheme.primaryColor, width: 1),
|
||||
selectedIconColor: CustomTheme.primaryColor,
|
||||
highlightColor: CustomTheme.secondaryColor,
|
||||
splashColor: Colors.transparent,
|
||||
selectedColor: CustomTheme.onBoxColor,
|
||||
),
|
||||
listItemStyle: itemStyle,
|
||||
headerStyle: headerStyle,
|
||||
hintStyle: hintStyle,
|
||||
closedFillColor: CustomTheme.boxColor,
|
||||
closedBorder: Border.all(color: CustomTheme.boxBorderColor, width: 1),
|
||||
expandedFillColor: CustomTheme.boxColor,
|
||||
expandedBorder: Border.all(color: CustomTheme.boxBorderColor, width: 1),
|
||||
);
|
||||
|
||||
CustomDropdownDisabledDecoration get disabledDecoration =>
|
||||
CustomDropdownDisabledDecoration(
|
||||
fillColor: CustomTheme.boxColor.withAlpha(125),
|
||||
border: Border.all(
|
||||
color: CustomTheme.boxBorderColor.withAlpha(125),
|
||||
width: 1,
|
||||
),
|
||||
headerStyle: disabledHeaderStyle,
|
||||
hintStyle: disabledHintStyle,
|
||||
);
|
||||
|
||||
TextStyle get headerStyle => const TextStyle(
|
||||
color: CustomTheme.textColor,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
|
||||
TextStyle get itemStyle =>
|
||||
const TextStyle(color: CustomTheme.textColor, fontSize: 14);
|
||||
|
||||
TextStyle get hintStyle =>
|
||||
const TextStyle(color: CustomTheme.hintColor, fontSize: 14);
|
||||
|
||||
TextStyle get disabledHeaderStyle => const TextStyle(
|
||||
color: CustomTheme.hintColor,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
|
||||
TextStyle get disabledHintStyle =>
|
||||
const TextStyle(color: CustomTheme.hintColor, fontSize: 14);
|
||||
|
||||
Future<void> loadAllData() async {
|
||||
isLoading = true;
|
||||
final db = Provider.of<AppDatabase>(context, listen: false);
|
||||
|
||||
Future.wait([
|
||||
db.playerDao.getAllPlayers(),
|
||||
db.groupDao.getAllGroups(),
|
||||
db.gameDao.getAllGames(),
|
||||
Future.delayed(Constants.MINIMUM_SKELETON_DURATION),
|
||||
])
|
||||
.then((results) async {
|
||||
players = results[0];
|
||||
groups = results[1];
|
||||
games = results[2];
|
||||
isLoading = false;
|
||||
})
|
||||
.catchError((error) {
|
||||
print('Error loading data: $error');
|
||||
});
|
||||
}
|
||||
|
||||
void submitStatistic() {
|
||||
final newStatistic = Statistic(
|
||||
type: selectedType!,
|
||||
scopes: selectedScope,
|
||||
timeframe: selectedTimeframe,
|
||||
selectedGroups: selectedGroups,
|
||||
selectedGames: selectedGames,
|
||||
);
|
||||
// final db = Provider.of<AppDatabase>(context, listen: false);
|
||||
// db.statisticDao.addStatistic(newStatistic);
|
||||
Navigator.of(context).pop(newStatistic);
|
||||
}
|
||||
}
|
||||
|
||||
String translateTimeframeToString(Timeframe timeframe, BuildContext context) {
|
||||
final loc = AppLocalizations.of(context);
|
||||
switch (timeframe) {
|
||||
case Timeframe.last7Days:
|
||||
return loc.timeframe_last_7_days;
|
||||
case Timeframe.last30Days:
|
||||
return loc.timeframe_last_30_days;
|
||||
case Timeframe.last90Days:
|
||||
return loc.timeframe_last_90_days;
|
||||
case Timeframe.last180Days:
|
||||
return loc.timeframe_last_180_days;
|
||||
case Timeframe.lastYear:
|
||||
return loc.timeframe_last_year;
|
||||
case Timeframe.allTime:
|
||||
return loc.timeframe_all_time;
|
||||
}
|
||||
}
|
||||
|
||||
String translateScopeToString(StatisticScope scope, BuildContext context) {
|
||||
final loc = AppLocalizations.of(context);
|
||||
switch (scope) {
|
||||
case StatisticScope.allPlayers:
|
||||
return loc.statistic_scope_all_players;
|
||||
case StatisticScope.selectedGroups:
|
||||
return loc.statistic_scope_selected_groups;
|
||||
case StatisticScope.selectedGames:
|
||||
return loc.statistic_scope_selected_games;
|
||||
case StatisticScope.timeframe:
|
||||
return loc.statistic_scope_timeframe;
|
||||
}
|
||||
}
|
||||
|
||||
String translateStatisticTypeToString(
|
||||
StatisticType type,
|
||||
BuildContext context,
|
||||
) {
|
||||
final loc = AppLocalizations.of(context);
|
||||
switch (type) {
|
||||
case StatisticType.totalMatches:
|
||||
return loc.statistic_type_total_matches;
|
||||
case StatisticType.totalWins:
|
||||
return loc.statistic_type_total_wins;
|
||||
case StatisticType.totalScore:
|
||||
return loc.statistic_type_total_score;
|
||||
case StatisticType.totalLosses:
|
||||
return loc.statistic_type_total_losses;
|
||||
case StatisticType.averageScore:
|
||||
return loc.statistic_type_average_score;
|
||||
case StatisticType.bestScore:
|
||||
return loc.statistic_type_best_score;
|
||||
case StatisticType.worstScore:
|
||||
return loc.statistic_type_worst_score;
|
||||
case StatisticType.winrate:
|
||||
return loc.statistic_type_winrate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:tallee/core/adaptive_page_route.dart';
|
||||
import 'package:tallee/core/constants.dart';
|
||||
import 'package:tallee/data/db/database.dart';
|
||||
import 'package:tallee/data/models/match.dart';
|
||||
import 'package:tallee/data/models/player.dart';
|
||||
import 'package:tallee/l10n/generated/app_localizations.dart';
|
||||
import 'package:tallee/presentation/views/main_menu/statistics_view/create_statistic_view.dart';
|
||||
import 'package:tallee/presentation/widgets/app_skeleton.dart';
|
||||
import 'package:tallee/presentation/widgets/buttons/main_menu_button.dart';
|
||||
import 'package:tallee/presentation/widgets/tiles/quick_info_tile.dart';
|
||||
import 'package:tallee/presentation/widgets/tiles/statistics_tile.dart';
|
||||
import 'package:tallee/presentation/widgets/top_centered_message.dart';
|
||||
|
||||
class StatisticsView extends StatefulWidget {
|
||||
/// A view that displays player statistics
|
||||
const StatisticsView({super.key});
|
||||
|
||||
@override
|
||||
State<StatisticsView> createState() => _StatisticsViewState();
|
||||
}
|
||||
|
||||
class _StatisticsViewState extends State<StatisticsView> {
|
||||
int matchCount = 0;
|
||||
int groupCount = 0;
|
||||
|
||||
List<(Player, int)> winCounts = List.filled(6, (
|
||||
Player(name: 'Skeleton Player'),
|
||||
1,
|
||||
));
|
||||
List<(Player, int)> matchCounts = List.filled(6, (
|
||||
Player(name: 'Skeleton Player'),
|
||||
1,
|
||||
));
|
||||
List<(Player, double)> winRates = List.filled(6, (
|
||||
Player(name: 'Skeleton Player'),
|
||||
1,
|
||||
));
|
||||
bool isLoading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
loadStatisticData();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final loc = AppLocalizations.of(context);
|
||||
return LayoutBuilder(
|
||||
builder: (BuildContext context, BoxConstraints constraints) {
|
||||
return Stack(
|
||||
alignment: AlignmentDirectional.center,
|
||||
children: [
|
||||
SingleChildScrollView(
|
||||
child: AppSkeleton(
|
||||
enabled: isLoading,
|
||||
fixLayoutBuilder: true,
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(minWidth: constraints.maxWidth),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
QuickInfoTile(
|
||||
width: constraints.maxWidth * 0.45,
|
||||
height: constraints.maxHeight * 0.13,
|
||||
title: loc.matches,
|
||||
icon: Icons.groups_rounded,
|
||||
value: matchCount,
|
||||
),
|
||||
SizedBox(width: constraints.maxWidth * 0.05),
|
||||
QuickInfoTile(
|
||||
width: constraints.maxWidth * 0.45,
|
||||
height: constraints.maxHeight * 0.13,
|
||||
title: loc.groups,
|
||||
icon: Icons.groups_rounded,
|
||||
value: groupCount,
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(height: constraints.maxHeight * 0.02),
|
||||
Visibility(
|
||||
visible:
|
||||
winCounts.isEmpty &&
|
||||
matchCounts.isEmpty &&
|
||||
winRates.isEmpty,
|
||||
replacement: Column(
|
||||
children: [
|
||||
StatisticsTile(
|
||||
icon: Icons.sports_score,
|
||||
title: loc.wins,
|
||||
width: constraints.maxWidth * 0.95,
|
||||
values: winCounts,
|
||||
itemCount: 3,
|
||||
barColor: Colors.green,
|
||||
),
|
||||
SizedBox(height: constraints.maxHeight * 0.02),
|
||||
StatisticsTile(
|
||||
icon: Icons.percent,
|
||||
title: loc.winrate,
|
||||
width: constraints.maxWidth * 0.95,
|
||||
values: winRates,
|
||||
itemCount: 5,
|
||||
barColor: Colors.orange[700]!,
|
||||
),
|
||||
SizedBox(height: constraints.maxHeight * 0.02),
|
||||
StatisticsTile(
|
||||
icon: Icons.casino,
|
||||
title: loc.amount_of_matches,
|
||||
width: constraints.maxWidth * 0.95,
|
||||
values: matchCounts,
|
||||
itemCount: 10,
|
||||
barColor: Colors.blue,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: TopCenteredMessage(
|
||||
icon: Icons.info,
|
||||
title: loc.info,
|
||||
message: AppLocalizations.of(
|
||||
context,
|
||||
).no_statistics_available,
|
||||
),
|
||||
),
|
||||
SizedBox(height: MediaQuery.paddingOf(context).bottom),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
bottom: MediaQuery.paddingOf(context).bottom + 20,
|
||||
child: MainMenuButton(
|
||||
text: loc.create_statistic,
|
||||
icon: Icons.bar_chart,
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
adaptivePageRoute(
|
||||
builder: (context) => CreateStatisticView(
|
||||
onStatisticCreated: loadStatisticData,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Loads matches and players from the database
|
||||
/// and calculates statistics for each player
|
||||
void loadStatisticData() {
|
||||
final db = Provider.of<AppDatabase>(context, listen: false);
|
||||
|
||||
Future.wait([
|
||||
db.matchDao.getAllMatches(),
|
||||
db.playerDao.getAllPlayers(),
|
||||
db.matchDao.getMatchCount(),
|
||||
db.groupDao.getGroupCount(),
|
||||
Future.delayed(Constants.MINIMUM_SKELETON_DURATION),
|
||||
]).then((results) async {
|
||||
if (!mounted) return;
|
||||
|
||||
final matches = results[0] as List<Match>;
|
||||
final players = results[1] as List<Player>;
|
||||
matchCount = results[2] as int;
|
||||
groupCount = results[3] as int;
|
||||
|
||||
winCounts = _calculateWinsForAllPlayers(
|
||||
matches: matches,
|
||||
players: players,
|
||||
context: context,
|
||||
);
|
||||
matchCounts = _calculateMatchAmountsForAllPlayers(
|
||||
matches: matches,
|
||||
players: players,
|
||||
context: context,
|
||||
);
|
||||
winRates = computeWinRatePercent(
|
||||
winCounts: winCounts,
|
||||
matchCounts: matchCounts,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
isLoading = false;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Calculates the number of wins for each player
|
||||
/// and returns a sorted list of tuples (playerName, winCount)
|
||||
List<(Player, int)> _calculateWinsForAllPlayers({
|
||||
required List<Match> matches,
|
||||
required List<Player> players,
|
||||
required BuildContext context,
|
||||
}) {
|
||||
List<(Player, int)> winCounts = [];
|
||||
final loc = AppLocalizations.of(context);
|
||||
|
||||
// Getting the winners
|
||||
for (var match in matches) {
|
||||
final mvps = match.mvp;
|
||||
for (var winner in mvps) {
|
||||
final index = winCounts.indexWhere((entry) => entry.$1.id == winner.id);
|
||||
// -1 means winner not found in winCounts
|
||||
if (index != -1) {
|
||||
final current = winCounts[index].$2;
|
||||
winCounts[index] = (winner, current + 1);
|
||||
} else {
|
||||
winCounts.add((winner, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Adding all players with zero wins
|
||||
for (var player in players) {
|
||||
final index = winCounts.indexWhere((entry) => entry.$1.id == player.id);
|
||||
// -1 means player not found in winCounts
|
||||
if (index == -1) {
|
||||
winCounts.add((player, 0));
|
||||
}
|
||||
}
|
||||
|
||||
// Replace player IDs with names
|
||||
for (int i = 0; i < winCounts.length; i++) {
|
||||
final playerId = winCounts[i].$1.id;
|
||||
final player = players.firstWhere(
|
||||
(p) => p.id == playerId,
|
||||
orElse: () => Player(id: playerId, name: loc.not_available),
|
||||
);
|
||||
winCounts[i] = (player, winCounts[i].$2);
|
||||
}
|
||||
|
||||
winCounts.sort((a, b) => b.$2.compareTo(a.$2));
|
||||
|
||||
return winCounts;
|
||||
}
|
||||
|
||||
/// Calculates the number of matches played for each player
|
||||
/// and returns a sorted list of tuples (playerName, matchCount)
|
||||
List<(Player, int)> _calculateMatchAmountsForAllPlayers({
|
||||
required List<Match> matches,
|
||||
required List<Player> players,
|
||||
required BuildContext context,
|
||||
}) {
|
||||
List<(Player, int)> matchCounts = [];
|
||||
final loc = AppLocalizations.of(context);
|
||||
|
||||
// Counting matches for each player
|
||||
for (var match in matches) {
|
||||
for (Player player in match.players) {
|
||||
// Check if the player is already in matchCounts
|
||||
final index = matchCounts.indexWhere(
|
||||
(entry) => entry.$1.id == player.id,
|
||||
);
|
||||
|
||||
// -1 -> not found
|
||||
if (index == -1) {
|
||||
// Add new entry
|
||||
matchCounts.add((player, 1));
|
||||
} else {
|
||||
// Update existing entry
|
||||
final currentMatchAmount = matchCounts[index].$2;
|
||||
matchCounts[index] = (player, currentMatchAmount + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Adding all players with zero matches
|
||||
for (var player in players) {
|
||||
final index = matchCounts.indexWhere((entry) => entry.$1.id == player.id);
|
||||
// -1 means player not found in matchCounts
|
||||
if (index == -1) {
|
||||
matchCounts.add((player, 0));
|
||||
}
|
||||
}
|
||||
|
||||
// Replace player IDs with names
|
||||
for (int i = 0; i < matchCounts.length; i++) {
|
||||
final playerId = matchCounts[i].$1.id;
|
||||
final player = players.firstWhere(
|
||||
(p) => p.id == playerId,
|
||||
orElse: () => Player(id: playerId, name: loc.not_available),
|
||||
);
|
||||
matchCounts[i] = (player, matchCounts[i].$2);
|
||||
}
|
||||
|
||||
matchCounts.sort((a, b) => b.$2.compareTo(a.$2));
|
||||
|
||||
return matchCounts;
|
||||
}
|
||||
|
||||
List<(Player, double)> computeWinRatePercent({
|
||||
required List<(Player, int)> winCounts,
|
||||
required List<(Player, int)> matchCounts,
|
||||
}) {
|
||||
final Map<Player, int> winsMap = {for (var e in winCounts) e.$1: e.$2};
|
||||
final Map<Player, int> matchesMap = {for (var e in matchCounts) e.$1: e.$2};
|
||||
|
||||
// Get all unique player names
|
||||
final player = {...matchesMap.keys};
|
||||
|
||||
// Calculate win rates
|
||||
final result = player.map((name) {
|
||||
final int w = winsMap[name] ?? 0;
|
||||
final int m = matchesMap[name] ?? 0;
|
||||
// Calculate percentage and round to 2 decimal places
|
||||
// Avoid division by zero
|
||||
final double percent = (m > 0)
|
||||
? double.parse(((w / m)).toStringAsFixed(2))
|
||||
: 0;
|
||||
return (name, percent);
|
||||
}).toList();
|
||||
|
||||
// Sort the result: first by winrate descending,
|
||||
// then by wins descending in case of a tie
|
||||
result.sort((a, b) {
|
||||
final cmp = b.$2.compareTo(a.$2);
|
||||
if (cmp != 0) return cmp;
|
||||
final wa = winsMap[a.$1] ?? 0;
|
||||
final wb = winsMap[b.$1] ?? 0;
|
||||
return wb.compareTo(wa);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user