Skip to content
Snippets Groups Projects
Commit 635e0a18 authored by vjrj's avatar vjrj
Browse files

Suffle with penalty of nodes

parent 32de7afb
No related branches found
No related tags found
No related merge requests found
...@@ -7,21 +7,55 @@ import 'node_type.dart'; ...@@ -7,21 +7,55 @@ import 'node_type.dart';
class NodeListCubit extends HydratedCubit<NodeListState> { class NodeListCubit extends HydratedCubit<NodeListState> {
NodeListCubit() : super(NodeListState()); NodeListCubit() : super(NodeListState());
void shuffle(NodeType type) { void shuffle(NodeType type, bool withPenalty) {
switch (type) { switch (type) {
case NodeType.duniter: case NodeType.duniter:
emit(state.copyWith(duniterNodes: shuffleFirstN(state.duniterNodes))); if (withPenalty) {
emit(state.copyWith(
duniterNodes: shuffleFirstNWithPenalty(state.duniterNodes)));
} else {
emit(state.copyWith(duniterNodes: shuffleFirstN(state.duniterNodes)));
}
break; break;
case NodeType.cesiumPlus: case NodeType.cesiumPlus:
emit(state.copyWith( if (withPenalty) {
cesiumPlusNodes: shuffleFirstN(state.cesiumPlusNodes))); emit(state.copyWith(
cesiumPlusNodes: shuffleFirstNWithPenalty(
state.cesiumPlusNodes)));
} else {
emit(state.copyWith(
cesiumPlusNodes: shuffleFirstN(state.cesiumPlusNodes)));
}
break; break;
case NodeType.gva: case NodeType.gva:
emit(state.copyWith(gvaNodes: shuffleFirstN(state.gvaNodes))); if (withPenalty) {
emit(state.copyWith(
gvaNodes: shuffleFirstNWithPenalty(state.gvaNodes)));
} else {
emit(state.copyWith(gvaNodes: shuffleFirstN(state.gvaNodes)));
}
break; break;
} }
} }
// shuffle first n nodes, keeping the first node last
List<Node> shuffleFirstNWithPenalty(List<Node> list, [int n = 5]) {
if (list.length <= n) {
final Node firstElement = list.removeAt(0);
list.shuffle();
list.add(firstElement);
} else {
final List<Node> subList = list.sublist(0, n);
final Node firstElement = subList.removeAt(0);
subList.shuffle();
subList.add(firstElement);
for (int i = 0; i < n; i++) {
list[i] = subList[i];
}
}
return list;
}
// shuffle fist n nodes // shuffle fist n nodes
List<Node> shuffleFirstN(List<Node> list, [int n = 5]) { List<Node> shuffleFirstN(List<Node> list, [int n = 5]) {
if (list.length <= n) { if (list.length <= n) {
......
...@@ -26,7 +26,7 @@ class NodeInfoCard extends StatelessWidget { ...@@ -26,7 +26,7 @@ class NodeInfoCard extends StatelessWidget {
: state.gvaNodes; : state.gvaNodes;
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
context.read<NodeListCubit>().shuffle(type); context.read<NodeListCubit>().shuffle(type, true);
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text(tr('long_press_to_refresh')), content: Text(tr('long_press_to_refresh')),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment