text
stringlengths 1
372
|
|---|
in case you are not working with the card template,
|
you must either install the provider package
|
or use your own method of accessing the FirebaseFirestore
|
instance from various parts of your codebase.
|
<topic_end>
|
<topic_start>
|
4. create a firestore controller class
|
though you can talk to firestore directly,
|
you should write a dedicated controller class
|
to make the code more readable and maintainable.
|
how you implement the controller depends on your game
|
and on the exact design of your multiplayer experience.
|
for the case of the card template,
|
you could synchronize the contents of the two circular playing areas.
|
it’s not enough for a full multiplayer experience,
|
but it’s a good start.
|
to create a controller, copy,
|
then paste the following code into a new file called
|
lib/multiplayer/firestore_controller.dart.
|
<code_start>
|
import 'dart:async';
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
import 'package:flutter/foundation.dart';
|
import 'package:logging/logging.dart';
|
import '../game_internals/board_state.dart';
|
import '../game_internals/playing_area.dart';
|
import '../game_internals/playing_card.dart';
|
class FirestoreController {
|
static final _log = Logger('FirestoreController');
|
final FirebaseFirestore instance;
|
final BoardState boardState;
|
/// for now, there is only one match. but in order to be ready
|
/// for match-making, put it in a firestore collection called matches.
|
late final _matchRef = instance.collection('matches').doc('match_1');
|
late final _areaOneRef = _matchRef
|
.collection('areas')
|
.doc('area_one')
|
.withconverter<list<playingcard>>(
|
fromFirestore: _cardsFromFirestore, toFirestore: _cardsToFirestore);
|
late final _areaTwoRef = _matchRef
|
.collection('areas')
|
.doc('area_two')
|
.withconverter<list<playingcard>>(
|
fromFirestore: _cardsFromFirestore, toFirestore: _cardsToFirestore);
|
StreamSubscription? _areaOneFirestoreSubscription;
|
StreamSubscription? _areaTwoFirestoreSubscription;
|
StreamSubscription? _areaOneLocalSubscription;
|
StreamSubscription? _areaTwoLocalSubscription;
|
FirestoreController({required this.instance, required this.boardState}) {
|
// subscribe to the remote changes (from firestore).
|
_areaOneFirestoreSubscription = _areaOneRef.snapshots().listen((snapshot) {
|
_updateLocalFromFirestore(boardState.areaOne, snapshot);
|
});
|
_areaTwoFirestoreSubscription = _areaTwoRef.snapshots().listen((snapshot) {
|
_updateLocalFromFirestore(boardState.areaTwo, snapshot);
|
});
|
// subscribe to the local changes in game state.
|
_areaOneLocalSubscription = boardState.areaOne.playerChanges.listen((_) {
|
_updateFirestoreFromLocalAreaOne();
|
});
|
_areaTwoLocalSubscription = boardState.areaTwo.playerChanges.listen((_) {
|
_updateFirestoreFromLocalAreaTwo();
|
});
|
_log.fine('Initialized');
|
}
|
void dispose() {
|
_areaOneFirestoreSubscription?.cancel();
|
_areaTwoFirestoreSubscription?.cancel();
|
_areaOneLocalSubscription?.cancel();
|
_areaTwoLocalSubscription?.cancel();
|
_log.fine('Disposed');
|
}
|
/// takes the raw JSON snapshot coming from firestore and attempts to
|
/// convert it into a list of [playingcard]s.
|
List<PlayingCard> _cardsFromFirestore(
|
DocumentSnapshot<Map<String, dynamic>> snapshot,
|
SnapshotOptions? options,
|
) {
|
final data = snapshot.data()?['cards'] as list?;
|
if (data == null) {
|
_log.info('No data found on firestore, returning empty list');
|
return [];
|
}
|
final list = List.castFrom<Object?, Map<String, object?>>(data);
|
try {
|
return list.map((raw) => PlayingCard.fromJson(raw)).toList();
|
} catch (e) {
|
throw FirebaseControllerException(
|
'failed to parse data from firestore: $e');
|
}
|
}
|
/// takes a list of [playingcard]s and converts it into a JSON object
|
/// that can be saved into firestore.
|
Map<String, object?> _cardsToFirestore(
|
List<PlayingCard> cards,
|
SetOptions? options,
|
) {
|
return {'cards': cards.map((c) => c.toJson()).toList()};
|
}
|
/// updates firestore with the local state of [area].
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.