improved listening for bot messages
This commit is contained in:
parent
d2bcdaa7db
commit
fa36eb605c
4 changed files with 62 additions and 24 deletions
|
|
@ -115,10 +115,17 @@ class ChatController extends State<ChatPageWithRoom>
|
|||
// #Pangea
|
||||
final PangeaController pangeaController = MatrixState.pangeaController;
|
||||
late Choreographer choreographer = Choreographer(pangeaController, this);
|
||||
final List<GameRoundModel> gameRounds = [];
|
||||
final GlobalKey<RoundTimerState> roundTimerStateKey =
|
||||
GlobalKey<RoundTimerState>();
|
||||
RoundTimer? timer;
|
||||
|
||||
final List<GameRoundModel> gameRounds = [];
|
||||
|
||||
List<String> get completedRoundEventIds => gameRounds
|
||||
.where((round) => round.isCompleted)
|
||||
.map((round) => round.messageIDs)
|
||||
.expand((x) => x)
|
||||
.toList();
|
||||
// Pangea#
|
||||
|
||||
Room get room => sendingClient.getRoomById(roomId) ?? widget.room;
|
||||
|
|
@ -414,7 +421,8 @@ class ChatController extends State<ChatPageWithRoom>
|
|||
List<Event> get visibleEvents =>
|
||||
timeline?.events
|
||||
.where(
|
||||
(x) => x.isVisibleInGui,
|
||||
(x) =>
|
||||
x.isVisibleInGui && !completedRoundEventIds.contains(x.eventId),
|
||||
)
|
||||
.toList() ??
|
||||
<Event>[];
|
||||
|
|
@ -552,7 +560,6 @@ class ChatController extends State<ChatPageWithRoom>
|
|||
//#Pangea
|
||||
choreographer.stateListener.close();
|
||||
choreographer.dispose();
|
||||
roundTimerStateKey.currentState?.stopTimeout();
|
||||
//Pangea#
|
||||
super.dispose();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,15 @@ class ChatEventList extends StatelessWidget {
|
|||
final horizontalPadding = FluffyThemes.isColumnMode(context) ? 8.0 : 0.0;
|
||||
|
||||
final events = controller.timeline!.events
|
||||
.where((event) => event.isVisibleInGui)
|
||||
.where(
|
||||
(event) =>
|
||||
event.isVisibleInGui
|
||||
// #Pangea
|
||||
&&
|
||||
!controller.completedRoundEventIds.contains(event.eventId)
|
||||
// Pangea#
|
||||
,
|
||||
)
|
||||
.toList();
|
||||
final animateInEventIndex = controller.animateInEventIndex;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,26 +1,18 @@
|
|||
import 'package:fluffychat/pangea/utils/bot_name.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
extension MembershipUpdate on SyncUpdate {
|
||||
List<String> botMessages(String roomID) {
|
||||
List<Event> messages(Room chat) {
|
||||
if (rooms?.join == null ||
|
||||
!rooms!.join!.containsKey(roomID) ||
|
||||
rooms!.join![roomID]!.timeline?.events == null) {
|
||||
!rooms!.join!.containsKey(chat.id) ||
|
||||
rooms!.join![chat.id]!.timeline?.events == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final messageEvents = rooms!.join![roomID]!.timeline!.events!
|
||||
return rooms!.join![chat.id]!.timeline!.events!
|
||||
.where(
|
||||
(event) => event.type == EventTypes.Message,
|
||||
)
|
||||
.toList();
|
||||
if (messageEvents.isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return messageEvents
|
||||
.where((event) => event.senderId == BotName.byEnvironment)
|
||||
.map((event) => event.eventId)
|
||||
.map((event) => Event.fromMatrixEvent(event, chat))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import 'dart:async';
|
|||
|
||||
import 'package:fluffychat/pages/chat/chat.dart';
|
||||
import 'package:fluffychat/pangea/extensions/sync_update_extension.dart';
|
||||
import 'package:fluffychat/pangea/utils/bot_name.dart';
|
||||
import 'package:fluffychat/pangea/widgets/chat/round_timer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
|
@ -13,25 +14,52 @@ class GameRoundModel {
|
|||
|
||||
final ChatController controller;
|
||||
final Completer<void> roundCompleter = Completer<void>();
|
||||
late DateTime createdAt;
|
||||
RoundTimer timer;
|
||||
DateTime? startTime;
|
||||
DateTime? endTime;
|
||||
RoundState state = RoundState.notStarted;
|
||||
|
||||
StreamSubscription? syncSubscription;
|
||||
final Set<String> messageIDs = {};
|
||||
|
||||
GameRoundModel({
|
||||
required this.controller,
|
||||
required this.timer,
|
||||
}) {
|
||||
client.onSync.stream.firstWhere((update) {
|
||||
final botEventIDs = update.botMessages(controller.roomId);
|
||||
return botEventIDs.isNotEmpty;
|
||||
}).then((_) => startRound());
|
||||
createdAt = DateTime.now();
|
||||
syncSubscription ??= client.onSync.stream.listen((update) {
|
||||
final newMessages = update.messages(controller.room);
|
||||
final botMessages = newMessages
|
||||
.where((msg) => msg.senderId == BotName.byEnvironment)
|
||||
.toList();
|
||||
|
||||
if (botMessages.isNotEmpty &&
|
||||
botMessages.any(
|
||||
(msg) =>
|
||||
msg.originServerTs.isAfter(createdAt) &&
|
||||
!messageIDs.contains(msg.eventId),
|
||||
)) {
|
||||
if (state == RoundState.notStarted) {
|
||||
startRound();
|
||||
} else if (state == RoundState.inProgress) {
|
||||
endRound();
|
||||
}
|
||||
}
|
||||
|
||||
for (final message in newMessages) {
|
||||
if (message.originServerTs.isAfter(createdAt) &&
|
||||
!messageIDs.contains(message.eventId) &&
|
||||
!message.eventId.startsWith("Pangea Chat")) {
|
||||
messageIDs.add(message.eventId);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Client get client => controller.pangeaController.matrixState.client;
|
||||
|
||||
bool get isCompleted => roundCompleter.isCompleted;
|
||||
|
||||
void startRound() {
|
||||
debugPrint("starting round");
|
||||
state = RoundState.inProgress;
|
||||
|
|
@ -42,12 +70,15 @@ class GameRoundModel {
|
|||
}
|
||||
|
||||
void endRound() {
|
||||
debugPrint("ending round");
|
||||
debugPrint("ending round, message IDs: $messageIDs");
|
||||
endTime = DateTime.now();
|
||||
state = RoundState.completed;
|
||||
controller.roundTimerStateKey.currentState?.stopTimeout();
|
||||
syncSubscription?.cancel();
|
||||
roundCompleter.complete();
|
||||
}
|
||||
|
||||
void dispose() {}
|
||||
void dispose() {
|
||||
syncSubscription?.cancel();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue