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
+66
View File
@@ -0,0 +1,66 @@
import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
part 'database.g.dart';
class Users extends Table {
TextColumn get uuid => text().unique()();
TextColumn get gameRoomUuid => text().references(GameRooms, #uuid)();
TextColumn get name => text().withLength(min: 2, max: 32)();
DateTimeColumn get createdAt => dateTime().nullable()();
@override
bool get isStrict => true;
}
enum GameStatus {
open,
running,
closed,
cancelled,
}
class GameRooms extends Table {
TextColumn get uuid => text().unique()();
TextColumn get status => textEnum<GameStatus>()();
TextColumn get code => text().withLength(min: 6, max: 6)();
DateTimeColumn get createdAt => dateTime().nullable()();
@override
bool get isStrict => true;
@override
List<Set<Column>> get uniqueKeys => [
{code, status},
];
}
@DriftDatabase(tables: [Users, GameRooms])
class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 1;
static QueryExecutor _openConnection() {
return NativeDatabase.createInBackground(File('./db.sqlite'));
}
@override
MigrationStrategy get migration {
return MigrationStrategy(
beforeOpen: (details) async {
// Statements to run to make sqlite performant, running in WAL mode, etc
await customStatement('PRAGMA foreign_keys = ON');
await customStatement('PRAGMA journal_mode=WAL');
await customStatement('PRAGMA busy_timeout=5000');
await customStatement('PRAGMA synchronous=NORMAL');
await customStatement('PRAGMA cache_size=10000');
await customStatement('PRAGMA temp_store=MEMORY');
await customStatement('PRAGMA mmap_size=268435456');
},
);
}
}
+62
View File
@@ -0,0 +1,62 @@
import 'package:backend/db/database.dart';
import 'package:drift/drift.dart';
import 'package:logging/logging.dart';
import 'package:uuid/uuid.dart';
final log = Logger('Db');
class Db {
static final _db = AppDatabase();
static Future<User?> getUserById(String uuid) {
log.finest('Getting user $uuid');
return _db.managers.users.filter((f) => f.uuid.equals(uuid)).getSingleOrNull();
}
static Future<User?> createUser({required String username, required String roomCode}) async {
log.finest('Creating user $username in room $roomCode');
final room = await _db.managers.gameRooms
.filter((f) => f.code.equals(roomCode) & f.status.isIn([GameStatus.open, GameStatus.running]))
.getSingleOrNull()
.catchError((Object err) {
log.info('Failed to find available room:$roomCode', err, StackTrace.current);
return null;
});
if (room == null) return null;
return _db.managers.users
.createReturningOrNull(
(o) => o(createdAt: Value(DateTime.now()), uuid: const Uuid().v4(), name: username, gameRoomUuid: room.uuid),
)
.catchError((Object err) {
log.severe('Failed to create user', err, StackTrace.current);
throw Exception(err.toString());
});
}
static Future<GameRoom?> createRoom({required String roomCode}) {
log.finest('Creating room with code $roomCode');
return _db.managers.gameRooms
.createReturningOrNull(
(o) => o(createdAt: Value(DateTime.now()), status: GameStatus.open, uuid: const Uuid().v4(), code: roomCode),
)
.catchError(
(Object err) {
log.severe('Failed to create room', err, StackTrace.current);
return null;
},
);
}
static Future<GameRoom?> getRoomByCode(String? roomCode) async {
log.finest('Getting room by code $roomCode');
final room = await _db.managers.gameRooms
.filter((f) => f.code.equals(roomCode) & f.status.isIn([GameStatus.open, GameStatus.running]))
.getSingleOrNull();
return room;
}
static Future<GameRoom?> getRoomById(String roomUuid) async {
log.finest('Getting room $roomUuid');
throw UnimplementedError();
}
}