-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2790c27
commit ec56e13
Showing
4 changed files
with
98 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'; | ||
} | ||
} |