Skip to content

Commit

Permalink
Support custom AP
Browse files Browse the repository at this point in the history
  • Loading branch information
narumi147 committed Sep 30, 2024
1 parent 00cbd7d commit df12b43
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 22 deletions.
38 changes: 38 additions & 0 deletions lib/app/modules/faker/faker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:chaldea/app/api/atlas.dart';
import 'package:chaldea/app/app.dart';
import 'package:chaldea/app/modules/battle/formation/formation_card.dart';
import 'package:chaldea/app/modules/common/builders.dart';
import 'package:chaldea/app/modules/common/filter_group.dart';
import 'package:chaldea/app/modules/craft_essence/craft_list.dart';
import 'package:chaldea/app/modules/servant/servant_list.dart';
import 'package:chaldea/generated/l10n.dart';
Expand Down Expand Up @@ -1142,6 +1143,42 @@ class _FakeGrandOrderState extends State<FakeGrandOrder> {
}
},
),
ListTile(
dense: true,
title: const Text('Notify when AP recovered at'),
subtitle: FilterGroup<int>(
padding: const EdgeInsets.only(top: 4),
options: [...fakerSettings.recoveredAps.toList()..sort(), 0],
values: FilterGroupData(),
optionBuilder: (v) => v == 0 ? Text(agent.user.userGame?.actMax.toString() ?? 'Full') : Text(v.toString()),
onFilterChanged: (_, lastChanged) {
setState(() {
if (lastChanged != null) {
fakerSettings.recoveredAps.remove(lastChanged);
agent.network.setLocalNotification();
}
});
},
),
trailing: IconButton(
onPressed: () {
InputCancelOkDialog(
title: 'AP',
keyboardType: TextInputType.number,
validate: (s) {
int v = int.tryParse(s) ?? -1;
return v > 0 && v < 200 && v < Maths.max(ConstData.userLevel.values.map((e) => e.maxAp));
},
onSubmit: (s) {
fakerSettings.recoveredAps.add(int.parse(s));
agent.network.setLocalNotification();
if (mounted) setState(() {});
},
).showDialog(context);
},
icon: const Icon(Icons.add),
),
),
ListTile(
dense: true,
title: const Text('Max refresh count of Support list'),
Expand All @@ -1160,6 +1197,7 @@ class _FakeGrandOrderState extends State<FakeGrandOrder> {
},
child: Text(fakerSettings.maxFollowerListRetryCount.toString())),
),
const Divider(),
SwitchListTile(
dense: true,
value: fakerSettings.dumpResponse,
Expand Down
2 changes: 1 addition & 1 deletion lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class MessageLookup extends MessageLookupByLibrary {
"ap_campaign_time_mismatch_hint":
MessageLookupByLibrary.simpleMessage("Quest Campaigns\' start time of non-JP regions may be incorrect"),
"ap_efficiency": MessageLookupByLibrary.simpleMessage("AP rate"),
"ap_fully_recovered": MessageLookupByLibrary.simpleMessage("All AP Recovered"),
"ap_fully_recovered": MessageLookupByLibrary.simpleMessage("All AP Restored"),
"app_data_folder": MessageLookupByLibrary.simpleMessage("Data Folder"),
"app_data_use_external_storage": MessageLookupByLibrary.simpleMessage("Use External Storage (SD card)"),
"appearance": MessageLookupByLibrary.simpleMessage("Appearance"),
Expand Down
4 changes: 2 additions & 2 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/generated/models/userdata/autologin.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ap": "AP",
"ap_campaign_time_mismatch_hint": "Quest Campaigns' start time of non-JP regions may be incorrect",
"ap_efficiency": "AP rate",
"ap_fully_recovered": "All AP Recovered",
"ap_fully_recovered": "All AP Restored",
"app_data_folder": "Data Folder",
"app_data_use_external_storage": "Use External Storage (SD card)",
"appearance": "Appearance",
Expand Down
44 changes: 32 additions & 12 deletions lib/models/faker/shared/network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,38 @@ abstract class NetworkManagerBase<TRequest extends FRequestBase, TUser extends A
if (!db.settings.fakerSettings.apRecoveredNotification) return;
final userGame = mstData.user;
if (userGame == null) return;
final int id = LocalNotificationUtil.generateUserApFullId(user.region.index, userGame.userId);
if (userGame.actRecoverAt < DateTime.now().timestamp + 2) return;
final recoverAt = DateTime.fromMillisecondsSinceEpoch(userGame.actRecoverAt * 1000);
await LocalNotificationUtil.scheduleNotification(
id: id,
dateTime: recoverAt.subtract(const Duration(minutes: 5)),
title: S.current.ap_fully_recovered,
body: [
'[${user.serverName}] ${userGame.name}',
recoverAt.toCustomString(year: false, millisecond: false),
].join('\n'),
);
for (final targetAp in {0, ...db.settings.fakerSettings.recoveredAps}) {
final isFull = targetAp == 0;
int recoverAt;
if (isFull) {
recoverAt = userGame.actRecoverAt;
} else if (userGame.calCurAp() < targetAp && targetAp < userGame.actMax) {
recoverAt = userGame.actRecoverAt - (userGame.actMax - targetAp) * 60 * 5;
} else {
continue;
}
final now = DateTime.now().timestamp;
final int id = LocalNotificationUtil.generateUserApRecoverId(user.region.index, userGame.userId, targetAp);
if (!isFull && recoverAt < now + 2) {
// await LocalNotificationUtil.plugin.cancel(id);
continue;
}
final recoverTime = recoverAt.sec2date();
DateTime notifyTime = recoverTime.subtract(Duration(minutes: isFull ? 5 : 1));
if (notifyTime.timestamp <= now + 1) {
notifyTime = (now + 2).sec2date();
}

await LocalNotificationUtil.scheduleNotification(
id: id,
dateTime: notifyTime,
title: isFull ? S.current.ap_fully_recovered : 'AP $targetAp!',
body: [
'[${user.serverName}] ${userGame.name}',
recoverTime.toCustomString(year: false, millisecond: false),
].join('\n'),
);
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion lib/models/userdata/autologin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ part '../../generated/models/userdata/autologin.g.dart';
class FakerSettings {
bool dumpResponse;
bool apRecoveredNotification;
Set<int> recoveredAps;
int maxFollowerListRetryCount;
List<AutoLoginDataJP> jpAutoLogins;
List<AutoLoginDataCN> cnAutoLogins;

FakerSettings({
this.dumpResponse = false,
this.apRecoveredNotification = false,
Set<int>? recoveredAps,
this.maxFollowerListRetryCount = 20,
List<AutoLoginDataJP>? jpAutoLogins,
List<AutoLoginDataCN>? cnAutoLogins,
}) : jpAutoLogins = jpAutoLogins ?? [],
}) : recoveredAps = recoveredAps ?? {},
jpAutoLogins = jpAutoLogins ?? [],
cnAutoLogins = cnAutoLogins ?? [];

factory FakerSettings.fromJson(Map<String, dynamic> json) => _$FakerSettingsFromJson(json);
Expand Down
14 changes: 9 additions & 5 deletions lib/utils/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ abstract class LocalNotificationUtil {
}
}

//
static int generateUserApFullId(int region, int userId) {
// 8 digits
return (10 + region + 1) * 1000000 + userId % 1000000;
static const int _kUserIdMod = 10 ^ 5;
static int generateUserApRecoverId(int region, int userId, int ap) {
// 1+3+17+8=29 bits
int v = 1;
v = (v << 3) + region;
v = (v << 17) + userId % _kUserIdMod;
v = (v << 8) + ap;
return v;
}

static bool isUserApFullId(int id) {
return id ~/ 10000000 == 1;
return (id >> (3 + 17 + 8)) == 1;
}
}

0 comments on commit df12b43

Please sign in to comment.