Skip to content

Commit

Permalink
Add separate Firebase model
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanogunleye committed May 19, 2024
1 parent 2790c27 commit ec56e13
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 34 deletions.
22 changes: 22 additions & 0 deletions lib/model/firebase/player.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:json_annotation/json_annotation.dart';

part 'player.g.dart';

@JsonSerializable(explicitToJson: true)
class Player {
/// Name of player
String name;

/// Flag for if this is the current user's turn
bool isPlaying;

Player({
required this.name,
required this.isPlaying,
});

factory Player.fromJson(Map<String, dynamic> json) =>
_$PlayerFromJson(json);

Map<String, dynamic> toJson() => _$PlayerToJson(this);
}
27 changes: 27 additions & 0 deletions lib/model/firebase/room.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:karma_palace/model/firebase/player.dart';

part 'room.g.dart';

@JsonSerializable(explicitToJson: true)
class Room {
/// Room ID. This is the same ID used for the deck API
String id;

/// List of players in the room
List<Player> players;

/// Player in play
String currentPlayer;

Room({
required this.id,
required this.players,
required this.currentPlayer,
});

factory Room.fromJson(Map<String, dynamic> json) =>
_$RoomFromJson(json);

Map<String, dynamic> toJson() => _$RoomToJson(this);
}
2 changes: 2 additions & 0 deletions lib/model/internal/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import 'package:karma_palace/model/internal/card.dart';
import 'package:karma_palace/model/internal/player.dart';

class Room {
String id;
List<Player> players;
List<Card> deck;
List<Card> playingPile;
List<Card> discardPile;

Room({
required this.id,
required this.players,
required this.deck,
required this.playingPile,
Expand Down
81 changes: 47 additions & 34 deletions lib/service/messaging_service.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
// import 'package:firebase_database/firebase_database.dart';
// import 'package:karma_palace/model/internal/player.dart';
// import 'package:karma_palace/model/internal/room.dart';
import 'package:firebase_database/firebase_database.dart';

import '../model/firebase/player.dart' as firebase_player;
import '../model/firebase/room.dart';
import '../model/internal/player.dart';

class MessagingService {
// static final MessagingService _messagingService =
// MessagingService._internal();
//
// factory MessagingService() {
// return _messagingService;
// }
//
// MessagingService._internal();
//
// void saveNewRoom(String roomId, Player player) async {
// Room room = Room(players: [
// player,
// ]);
//
// FirebaseDatabase database = FirebaseDatabase.instance;
// DatabaseReference ref = database.ref('room/$roomId');
// await ref.set(room.toJson());
// }
//
// Future<void> joinExistingRoom(String roomId, Player player) async {
// FirebaseDatabase database = FirebaseDatabase.instance;
// DatabaseReference roomRef = database.ref('room/$roomId');
// DatabaseEvent event = await roomRef.once();
// if (event.snapshot.exists) {
// DatabaseReference roomPlayerRef = roomRef.child('players');
// DatabaseReference newPlayerRef = roomPlayerRef.push();
// newPlayerRef.set(player.toJson());
// }
//
// throw 'Room does not exist!';
// }
static final MessagingService _messagingService =
MessagingService._internal();

factory MessagingService() {
return _messagingService;
}

MessagingService._internal();

void createRoom(String id, Player player) async {
Room room = Room(
id: id,
players: [
firebase_player.Player(
name: player.name,
isPlaying: true,
),
],
currentPlayer: player.name,
);

FirebaseDatabase database = FirebaseDatabase.instance;
DatabaseReference ref = database.ref('room/${room.id}');
await ref.set(room.toJson());
}

Future<void> joinRoom(String id, Player player) async {
FirebaseDatabase database = FirebaseDatabase.instance;
DatabaseReference roomRef = database.ref('room/$id');

DatabaseEvent event = await roomRef.once();
if (event.snapshot.exists) {
DatabaseReference roomPlayerRef = roomRef.child('players');
DatabaseReference newPlayerRef = roomPlayerRef.push();
newPlayerRef.set(firebase_player.Player(
name: player.name,
isPlaying: false,
).toJson());
}

throw 'Room does not exist!';
}
}

0 comments on commit ec56e13

Please sign in to comment.