Mostly working websocket stuff, some message weirdness at the moment...

This commit is contained in:
Nate Anderson
2025-02-10 18:55:15 -07:00
parent 623474e0c6
commit b37862a321
19 changed files with 543 additions and 87 deletions
+4 -1
View File
@@ -6,10 +6,13 @@ part 'jwt.g.dart';
class JWTBody {
String uuid;
String roomUuid;
String roomCode;
// Issued at in epoch seconds
int iat;
// Expires at in epoch seconds
int exp;
JWTBody({required this.uuid, required this.roomUuid, required this.iat, required this.exp});
JWTBody({required this.uuid, required this.roomUuid, required this.roomCode, required this.iat, required this.exp});
factory JWTBody.fromJson(Map<String, dynamic> json) => _$JWTBodyFromJson(json);
+47 -19
View File
@@ -25,11 +25,14 @@ class CreateRoomResponse {
}
sealed class GameRoomMessage {
const GameRoomMessage();
GameRoomMessage(this.roomUuid);
final String roomUuid;
abstract final String type;
factory GameRoomMessage.fromJson(Map<String, dynamic> json) {
return switch (json['type']) {
'ping' => PingMessage.fromJson(json),
'roomPing' => RoomPingMessage.fromJson(json),
'playerVote' => PlayerVoteMessage.fromJson(json),
_ => throw Exception('Unknown message type'),
};
@@ -38,36 +41,61 @@ sealed class GameRoomMessage {
Map<String, dynamic> toJson();
}
enum PingDestination { client, server }
@JsonSerializable()
class PingMessage extends GameRoomMessage {
final DateTime timestamp;
DateTime timestamp;
final PingDestination dest;
final String userUuid;
const PingMessage({required this.timestamp});
PingMessage(super.roomUuid, {required this.dest, required this.userUuid}) {
timestamp = DateTime.now();
type = 'ping';
}
factory PingMessage.fromJson(Map<String, dynamic> json) =>
PingMessage(timestamp: DateTime.parse(json['timestamp'] as String));
factory PingMessage.fromJson(Map<String, dynamic> json) => _$PingMessageFromJson(json);
@override
Map<String, dynamic> toJson() => {
'type': 'ping',
'timestamp': timestamp.toIso8601String(),
};
Map<String, dynamic> toJson() => _$PingMessageToJson(this);
@override
late final String type;
}
@JsonSerializable()
class RoomPingMessage extends GameRoomMessage {
late final DateTime timestamp;
final PingDestination dest;
RoomPingMessage(super.roomUuid, {required this.dest}) {
timestamp = DateTime.now();
type = 'roomPing';
}
factory RoomPingMessage.fromJson(Map<String, dynamic> json) => _$RoomPingMessageFromJson(json);
@override
Map<String, dynamic> toJson() => _$RoomPingMessageToJson(this);
@override
late final String type;
}
@JsonSerializable()
class PlayerVoteMessage extends GameRoomMessage {
final String playerUuid;
final int vote;
const PlayerVoteMessage({required this.playerUuid, required this.vote});
PlayerVoteMessage(super.roomUuid, {required this.playerUuid, required this.vote}) {
type = 'playerVote';
}
factory PlayerVoteMessage.fromJson(Map<String, dynamic> json) => PlayerVoteMessage(
playerUuid: json['playerUuid'] as String,
vote: json['vote'] as int,
);
factory PlayerVoteMessage.fromJson(Map<String, dynamic> json) => _$PlayerVoteMessageFromJson(json);
@override
Map<String, dynamic> toJson() => {
'type': 'playerVote',
'playerUuid': playerUuid,
'vote': vote,
};
Map<String, dynamic> toJson() => _$PlayerVoteMessageToJson(this);
@override
late final String type;
}