-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.dart
More file actions
42 lines (36 loc) · 1.32 KB
/
user.dart
File metadata and controls
42 lines (36 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import 'package:flutter/material.dart';
import 'package:studypoints/avatar/data/avatar.dart';
import 'package:studypoints/avatar/data/repository.dart';
import 'package:studypoints/avatar/data/shop_item.dart';
import 'package:studypoints/tasks/data/task.dart';
class UserService {
int hcCount = 0;
Avatar avatar;
List<Task> tasks = [];
List<String> boughtItems = [
ShopItemRepository().firstOfType('face').id,
ShopItemRepository().firstOfType('hair').id,
ShopItemRepository().firstOfType('body').id,
ShopItemRepository().firstOfType('hairEffect').id,
...ShopItemRepository().fetchType('skin').map((item) => item.id),
];
UserService({Avatar avatar}) : this.avatar = avatar ?? Avatar();
void clearTask(Task task, BuildContext context) {
tasks.removeWhere((t) => t.id == task.id);
var reward =
task.subtasks.values.fold(10, (sum, val) => val ? sum + 10 : sum);
hcCount += reward;
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('You earned $reward HC for completing a task!'),
));
}
bool ownsItem(ShopItem item) => boughtItems.contains(item.id);
bool canBuy(ShopItem item) =>
hcCount >= item.cost && !boughtItems.contains(item.id);
void buy(ShopItem item) {
if (canBuy(item)) {
hcCount -= item.cost;
boughtItems.add(item.id);
}
}
}