Skip to content

Commit

Permalink
SA-#132 Added isActive to check if player has left
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Eskov committed Mar 30, 2021
1 parent 2ddc9f9 commit a52380e
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 15 deletions.
11 changes: 7 additions & 4 deletions lib/entities/participant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ const UID = 'uid';
const LAST_ACTIVE = 'lastActive';
const ROLE = 'role';
const IS_PLAYING_ALONG = 'isPlayingAlong';
const IS_ACTIVE = 'isActive';

class Participant {
String name;
String uid;
String lastActive;
Role role;
bool isPlayingAlong;
bool isActive;
DocumentReference reference;

Participant(this.name, this.uid, this.lastActive, this.role, this.isPlayingAlong);
Participant(this.name, this.uid, this.lastActive, this.role, this.isPlayingAlong, this.isActive);

factory Participant.fromSnapshot(DocumentSnapshot snapshot) {
var newParticipant = Participant.fromJson(snapshot.data());
Expand All @@ -24,7 +26,7 @@ class Participant {
}

factory Participant.fromJson(Map<String, dynamic> json) {
return Participant(json[NAME], json[UID], json[LAST_ACTIVE], toRoleEnum(json[ROLE]), json[IS_PLAYING_ALONG]);
return Participant(json[NAME], json[UID], json[LAST_ACTIVE], toRoleEnum(json[ROLE]), json[IS_PLAYING_ALONG], json[IS_ACTIVE]);
}

Map<String, dynamic> toJson() {
Expand All @@ -33,12 +35,13 @@ class Participant {
UID: uid,
LAST_ACTIVE: lastActive,
ROLE: role.toExactString,
IS_PLAYING_ALONG: isPlayingAlong
IS_PLAYING_ALONG: isPlayingAlong,
IS_ACTIVE: isActive
};
}

@override
String toString() {
return '${runtimeType}{$NAME: $name, $UID: $uid, $LAST_ACTIVE: $lastActive, $ROLE: $role, $IS_PLAYING_ALONG: $isPlayingAlong}';
return '${runtimeType}{$NAME: $name, $UID: $uid, $LAST_ACTIVE: $lastActive, $ROLE: $role, $IS_PLAYING_ALONG: $isPlayingAlong, $IS_ACTIVE: $isActive}';
}
}
14 changes: 11 additions & 3 deletions lib/services/participant_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ class ParticipantService {
return participantsSnap.docs.map((snap) => Participant.fromSnapshot(snap)).toList();
}

void setParticipantInactive(DocumentReference participantRef) async {
participantRef.update({IS_ACTIVE: false});
}

void setParticipantActive(DocumentReference participantRef) async {
participantRef.update({IS_ACTIVE: true});
}

Future<List<Participant>> findActiveGameParticipants(DocumentReference gameRef) async {
List<Participant> participants = await findParticipants(gameRef);
participants.sort((a, b) => (a.name).compareTo(b.name));
participants.sort((a, b) => (a.name).compareTo(b.name));
return participants
.where((participant) => DateTime.parse(participant.lastActive)
.isAfter(DateTime.now().subtract(Duration(seconds: ACTIVITY_INTERVAL))))
.where((participant) => participant.isActive && DateTime.parse(participant.lastActive)
.isAfter(DateTime.now().subtract(Duration(seconds: ACTIVITY_INTERVAL))))
.toList();
}

Expand Down
20 changes: 19 additions & 1 deletion lib/ui/components/back_alert_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:superagile_app/services/participant_service.dart';
import 'package:superagile_app/services/score_service.dart';
import 'package:superagile_app/services/timer_service.dart';
import 'package:superagile_app/ui/views/start_page.dart';
import 'package:superagile_app/utils/global_theme.dart';
Expand All @@ -11,6 +14,17 @@ import 'agile_button.dart';
final _log = Logger((BackDialogAlert).toString());

class BackDialogAlert extends StatelessWidget {

final _participantService = ParticipantService();
final ScoreService scoreService = ScoreService();
DocumentReference participantRef;
int questionNr;
BackDialogAlert([DocumentReference participantRef, int questionNr]) {
this.participantRef = participantRef;
this.questionNr = questionNr;
}


final TimerService timerService = TimerService();
@override
Widget build(BuildContext context) {
Expand All @@ -21,9 +35,13 @@ class BackDialogAlert extends StatelessWidget {
padding: EdgeInsets.symmetric(horizontal: 25, vertical: 5),
child: Center(
child: AgileButton(
onPressed: () {
onPressed: () async {
timerService.cancelActivityTimer();
_log.info('Participant clicked exit and answered YES, timer cancelled');
if (participantRef != null) {
scoreService.deleteOldScore(participantRef, questionNr);
_participantService.setParticipantInactive(participantRef);
}
Navigator.pop(context);
Navigator.pushReplacement(
context,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/views/game_question_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class _GameQuestionPage extends State<GameQuestionPage> {
Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => BackDialogAlert(),
builder: (context) => BackDialogAlert(participantRef, questionNr),
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/views/host_start_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class _HostStartPageState extends State<HostStartPage> {
var hostRef = await _participantService.addParticipant(
gameRef,
Participant(_nameController.text, loggedInUserUid,
DateTime.now().toString(), Role.HOST, isPlayingAlong));
DateTime.now().toString(), Role.HOST, isPlayingAlong, true));
await _gameService.changeGameState(gameRef, GameState.WAITING_ROOM);
_log.info(
'${hostRef} HOST isPlayingAlong:${isPlayingAlong} and navigates to WaitingRoomPage');
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/views/player_start_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class _PlayerStartPageState extends State<PlayerStartPage> {
.findActiveGameByRef(gameRef);
_log.info(
'${playerRef} rejoins existing game:${gameRef.id} as Player');
_participantService.setParticipantActive(playerRef);
return _gameStateRouter
.joinCreatedGameAsExistingParticipant(
game.gameState,
Expand Down Expand Up @@ -158,7 +159,7 @@ class _PlayerStartPageState extends State<PlayerStartPage> {
DocumentReference playerRef = await _participantService.addParticipant(
gameRef,
Participant(_nameController.text, loggedInUserUid,
DateTime.now().toString(), Role.PLAYER, true));
DateTime.now().toString(), Role.PLAYER, true, true));
Game game = await _gameService.findActiveGameByRef(gameRef);
return _gameStateRouter.joinCreatedGameAsExistingParticipant(
game.gameState, playerRef, gameRef, context);
Expand Down
4 changes: 1 addition & 3 deletions lib/ui/views/question_results_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
Expand All @@ -20,7 +19,6 @@ import 'package:superagile_app/ui/views/congratulations_page.dart';
import 'package:superagile_app/utils/game_state_router.dart';
import 'package:superagile_app/utils/global_theme.dart';
import 'package:superagile_app/utils/labels.dart';

import 'final_page.dart';
import 'game_question_page.dart';

Expand Down Expand Up @@ -140,7 +138,7 @@ class _QuestionResultsPageState extends State<QuestionResultsPage> {
Future<bool> _onBackPressed() {
return showDialog(
context: context,
builder: (context) => BackDialogAlert(),
builder: (context) => BackDialogAlert(participantRef, questionNr),
);
}

Expand Down
2 changes: 1 addition & 1 deletion test/waiting_room_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main() {
var pin = await gameService.generateAvailable4DigitPin();
var gameRef = await gameService.addGame(Game(pin, loggedInUserUid, true, null));
var hostRef = await participantService.addParticipant(
gameRef, Participant('hosting_participant', loggedInUserUid, DateTime.now().toString(), Role.HOST, false));
gameRef, Participant('hosting_participant', loggedInUserUid, DateTime.now().toString(), Role.HOST, false, true));
await tester.pumpWidget(makeTestableWidget(WaitingRoomPage(gameRef, hostRef)));

final nameFinder = find.text('hosting_participant');
Expand Down

0 comments on commit a52380e

Please sign in to comment.