optimization of the detection algorithem.
BOOK update.
This commit is contained in:
@@ -7,6 +7,7 @@ import 'dart:convert';
|
||||
import 'package:image/image.dart' as img;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../utils/translations.dart';
|
||||
import '../utils/target_scoring.dart';
|
||||
import '../utils/target_colors.dart';
|
||||
import '../widgets/result_share.dart';
|
||||
import 'session_screen.dart' show targetCount, shotCount;
|
||||
@@ -30,11 +31,11 @@ class TargetViewScreen extends StatefulWidget {
|
||||
class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
final int _currentIndex = 0;
|
||||
List<Offset> _bullets = [];
|
||||
List<int> _scores = []; // score of each bullet, same order as _bullets
|
||||
List<Offset> _scaledBullets = []; // Bullets scaled to display target size
|
||||
int _totalScore = 0;
|
||||
int _shotCount = 0; // shots fired, misses included (bulletCount)
|
||||
Map<int, int> _folderChartScores = {};
|
||||
Offset? _targetCenter;
|
||||
Uint8List? _targetImageBytes;
|
||||
img.Image? _targetImageDecoded;
|
||||
String _date = '';
|
||||
@@ -139,6 +140,25 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
// Bullets, their scores and the total of one saved target. Newer saves store
|
||||
// each shot's score (hand-entered shots keep the score that was entered);
|
||||
// older ones are scored from the positions, by the same rule as saving.
|
||||
static ({List<Offset> bullets, List<int> scores, int total}) _readTarget(
|
||||
Map<String, dynamic> data) {
|
||||
final bullets = [
|
||||
for (final b in (data['bullets'] as List?) ?? const [])
|
||||
Offset((b['x'] as num).toDouble(), (b['y'] as num).toDouble())
|
||||
];
|
||||
final saved = data['scores'];
|
||||
final scores = saved is List && saved.length == bullets.length
|
||||
? [for (final x in saved) (x as num).toInt()]
|
||||
: [for (final b in bullets) TargetScoring.scoreAt(b)];
|
||||
final total = data['totalScore'] is int
|
||||
? data['totalScore'] as int
|
||||
: scores.fold(0, (a, b) => a + b);
|
||||
return (bullets: bullets, scores: scores, total: total);
|
||||
}
|
||||
|
||||
Future<void> _loadJsonAndImage() async {
|
||||
try {
|
||||
// Load default target for bullet positioning reference
|
||||
@@ -151,12 +171,6 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate target center from default target (for bullet positioning)
|
||||
_targetCenter = Offset(
|
||||
defaultTargetDecoded.width / 2,
|
||||
defaultTargetDecoded.height / 2 - 1,
|
||||
);
|
||||
|
||||
// Load selected target for display
|
||||
final displayByteData = await rootBundle.load(_selectedTargetPath);
|
||||
final displayBytes = displayByteData.buffer.asUint8List();
|
||||
@@ -173,20 +187,22 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
if (widget.isCombinedView) {
|
||||
// In combined view, collect all bullets from all files
|
||||
final allBullets = <Offset>[];
|
||||
final allScores = <int>[];
|
||||
final chartScores = <int, int>{};
|
||||
var total = 0;
|
||||
String latestDate = 'Unknown Date';
|
||||
|
||||
var shots = 0;
|
||||
for (var file in widget.jsonFiles) {
|
||||
final jsonString = await file.readAsString();
|
||||
final data = jsonDecode(jsonString);
|
||||
final data = jsonDecode(jsonString) as Map<String, dynamic>;
|
||||
final target = _readTarget(data);
|
||||
|
||||
final bulletsJson = data['bullets'] as List?;
|
||||
shots += (data['bulletCount'] as int?) ?? bulletsJson?.length ?? 0;
|
||||
if (bulletsJson != null) {
|
||||
allBullets.addAll(
|
||||
bulletsJson.map((b) => Offset(b['x'].toDouble(), b['y'].toDouble())).toList(),
|
||||
);
|
||||
}
|
||||
shots += (data['bulletCount'] as int?) ?? target.bullets.length;
|
||||
allBullets.addAll(target.bullets);
|
||||
allScores.addAll(target.scores);
|
||||
total += target.total;
|
||||
chartScores[chartScores.length + 1] = target.total;
|
||||
|
||||
if (data['date'] != null) {
|
||||
latestDate = data['date'];
|
||||
@@ -214,6 +230,9 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
}
|
||||
|
||||
_bullets = allBullets;
|
||||
_scores = allScores;
|
||||
_totalScore = total;
|
||||
_folderChartScores = chartScores;
|
||||
_shotCount = shots;
|
||||
} else {
|
||||
// Single file view - load from current file
|
||||
@@ -240,8 +259,10 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
} else {
|
||||
_date = rawDate;
|
||||
}
|
||||
final bulletsJson = data['bullets'] as List;
|
||||
_bullets = bulletsJson.map((b) => Offset(b['x'].toDouble(), b['y'].toDouble())).toList();
|
||||
final target = _readTarget(data as Map<String, dynamic>);
|
||||
_bullets = target.bullets;
|
||||
_scores = target.scores;
|
||||
_totalScore = target.total;
|
||||
_shotCount = (data['bulletCount'] as int?) ?? _bullets.length;
|
||||
}
|
||||
|
||||
@@ -261,41 +282,12 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
);
|
||||
}).toList();
|
||||
|
||||
_totalScore = _bullets
|
||||
.map((b) => _scoreForBullet(b, _targetCenter!))
|
||||
.fold(0, (sum, score) => sum + score);
|
||||
|
||||
// Pre-compute folder chart scores to avoid sync file reads in build
|
||||
if (widget.isCombinedView) {
|
||||
final chartScores = <int, int>{};
|
||||
for (int i = 0; i < widget.jsonFiles.length; i++) {
|
||||
final data = jsonDecode(await widget.jsonFiles[i].readAsString());
|
||||
final bullets = (data['bullets'] as List?) ?? [];
|
||||
int s = 0;
|
||||
for (final b in bullets) {
|
||||
s += _scoreForBullet(Offset(b['x'].toDouble(), b['y'].toDouble()), _targetCenter!);
|
||||
}
|
||||
chartScores[i + 1] = s;
|
||||
}
|
||||
_folderChartScores = chartScores;
|
||||
}
|
||||
|
||||
setState(() {});
|
||||
} catch (e) {
|
||||
debugPrint("Error loading JSON or image: $e");
|
||||
}
|
||||
}
|
||||
|
||||
int _scoreForBullet(Offset bullet, Offset center) {
|
||||
const bulletRadius = 25;
|
||||
final distance = (bullet - center).distance - bulletRadius;
|
||||
final rings = [14, 70, 125, 180, 235, 290, 345, 400, 455, 510];
|
||||
for (int i = 0; i < rings.length; i++) {
|
||||
if (distance <= rings[i]) return 10 - i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Future<void> _deleteTarget() async {
|
||||
try {
|
||||
final file = File(_targetFilePath);
|
||||
@@ -317,8 +309,7 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
|
||||
// What the screen shows, as one image: target and tiles left, chart right
|
||||
void _share() {
|
||||
final center = _targetCenter ?? Offset.zero;
|
||||
final scores = [for (final b in _bullets) _scoreForBullet(b, center)];
|
||||
final scores = _scores;
|
||||
|
||||
final List<(String, String)> tiles;
|
||||
final List<MapEntry<int, int>> chart;
|
||||
@@ -543,10 +534,7 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
// Scaled bullets are already scaled to display target dimensions
|
||||
final dx = scaledBullet.dx * scaleX + offsetX;
|
||||
final dy = scaledBullet.dy * scaleY + offsetY;
|
||||
// Score is calculated using original bullet position and default target center
|
||||
final originalBullet = _bullets[i];
|
||||
final score = _scoreForBullet(
|
||||
originalBullet, _targetCenter ?? Offset.zero);
|
||||
final score = _scores[i];
|
||||
final isHighlighted = widget.isCombinedView
|
||||
? (_pressedScoreGroup == score)
|
||||
: (i == _highlightedBulletIndex);
|
||||
@@ -695,8 +683,7 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
// Single file view: each shot as a bar
|
||||
final Map<int, int> shotScores = {};
|
||||
for (int i = 0; i < _bullets.length; i++) {
|
||||
final score = _scoreForBullet(_bullets[i], _targetCenter ?? Offset.zero);
|
||||
shotScores[i + 1] = score; // Shot number starts at 1
|
||||
shotScores[i + 1] = _scores[i]; // Shot number starts at 1
|
||||
}
|
||||
|
||||
if (shotScores.isEmpty) {
|
||||
@@ -741,8 +728,7 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
|
||||
List<Widget> _buildGroupedScoreButtons() {
|
||||
final Map<int, int> scoreGroups = {};
|
||||
for (final bullet in _bullets) {
|
||||
final score = _scoreForBullet(bullet, _targetCenter!);
|
||||
for (final score in _scores) {
|
||||
scoreGroups[score] = (scoreGroups[score] ?? 0) + 1;
|
||||
}
|
||||
|
||||
@@ -835,8 +821,7 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
|
||||
final scoredBullets = _bullets.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final bullet = entry.value;
|
||||
final score = _scoreForBullet(bullet, _targetCenter ?? Offset.zero);
|
||||
return {'index': index, 'score': score, 'bullet': bullet};
|
||||
return {'index': index, 'score': _scores[index], 'bullet': bullet};
|
||||
}).toList();
|
||||
|
||||
// Sort descending by score
|
||||
|
||||
Reference in New Issue
Block a user