From 9090c86b4f391b724fbb83e1ff1a4bb807a9dff3 Mon Sep 17 00:00:00 2001 From: mytja Date: Mon, 30 Oct 2023 01:35:44 +0100 Subject: [PATCH] =?UTF-8?q?preklopi=20na=20lobby=20namesto=20http=20pollin?= =?UTF-8?q?ga=20-=20linked=20to=20#44=20rejoin=20igralcev=20-=20resolves?= =?UTF-8?q?=20#47=20kon=C4=8Danje=20igre=20v=20primeru=20disconnectanih=20?= =?UTF-8?q?igralcev=20-=20resolves=20#48?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tarok/lib/game.dart | 122 +++++++++++++++++++------------- tarok/lib/game_controller.dart | 7 ++ tarok/lib/lobby.dart | 28 +++++--- tarok/lib/lobby_controller.dart | 77 ++++++++++++-------- 4 files changed, 145 insertions(+), 89 deletions(-) diff --git a/tarok/lib/game.dart b/tarok/lib/game.dart index 59389bd..5156698 100644 --- a/tarok/lib/game.dart +++ b/tarok/lib/game.dart @@ -56,7 +56,9 @@ class Game extends StatelessWidget { ), ), - // REZULTATI, KLEPET, CHAT + // REZULTATI + // KLEPET + // CHAT Container( alignment: Alignment.topRight, child: Card( @@ -72,15 +74,31 @@ class Game extends StatelessWidget { appBar: AppBar( automaticallyImplyLeading: false, elevation: 0, - flexibleSpace: TabBar(tabs: [ - const Tab(icon: Icon(Icons.timeline)), - const Tab(icon: Icon(Icons.chat)), - if (controller.replay) - const Tab(icon: Icon(Icons.fast_rewind)), - if (DEVELOPER_MODE) - const Tab(icon: Icon(Icons.bug_report)), - const Tab(icon: Icon(Icons.info)), - ]), + flexibleSpace: TabBar( + onTap: (index) { + if (index == 1) { + controller.unreadMessages.value = 0; + } + }, + tabs: [ + const Tab(icon: Icon(Icons.timeline)), + Tab( + icon: controller.unreadMessages.value == 0 + ? const Icon(Icons.chat) + : Badge( + label: Text( + controller.unreadMessages.value + .toString(), + ), + child: const Icon(Icons.chat), + ), + ), + if (controller.replay) + const Tab(icon: Icon(Icons.fast_rewind)), + if (DEVELOPER_MODE) + const Tab(icon: Icon(Icons.bug_report)), + const Tab(icon: Icon(Icons.info)), + ]), ), body: TabBarView(children: [ Column( @@ -320,44 +338,49 @@ class Game extends StatelessWidget { ), ], ), - Column(children: [ - TextField( - controller: controller.controller.value, - onSubmitted: (String value) async { - await controller.sendMessage(); - }, - ), - ListView( - shrinkWrap: true, - children: controller.chat - .map((e) => Row(children: [ - Initicon( - text: controller - .getUserFromPosition(e.userId) - .name, - elevation: 4, - size: 40, - backgroundColor: HSLColor.fromAHSL( - 1, - hashCode(controller - .getUserFromPosition( - e.userId) - .name) % - 360, - 1, - 0.6) - .toColor(), - borderRadius: BorderRadius.zero, - ), - const SizedBox(width: 10), - Flexible( - child: Text( - "${controller.getUserFromPosition(e.userId).name}: ${e.message}"), - ), - ])) - .toList(), - ), - ]), + SingleChildScrollView( + child: Column(children: [ + TextField( + controller: controller.controller.value, + onSubmitted: (String value) async { + await controller.sendMessage(); + }, + ), + ListView( + physics: + const AlwaysScrollableScrollPhysics(), + shrinkWrap: true, + children: controller.chat + .map((e) => Row(children: [ + Initicon( + text: controller + .getUserFromPosition( + e.userId) + .name, + elevation: 4, + size: 40, + backgroundColor: HSLColor.fromAHSL( + 1, + hashCode(controller + .getUserFromPosition( + e.userId) + .name) % + 360, + 1, + 0.6) + .toColor(), + borderRadius: BorderRadius.zero, + ), + const SizedBox(width: 10), + Flexible( + child: Text( + "${controller.getUserFromPosition(e.userId).name}: ${e.message}"), + ), + ])) + .toList(), + ), + ]), + ), if (controller.replay) Column(children: [ Center( @@ -1640,7 +1663,8 @@ class Game extends StatelessWidget { if (controller.gamesPlayed.value != controller.gamesRequired.value && !controller.bots && - controller.gamesRequired.value != -1) + controller.gamesRequired.value != -1 && + !controller.requestedGameEnd.value) ElevatedButton( onPressed: () async => await controller.gameStartEarly(), diff --git a/tarok/lib/game_controller.dart b/tarok/lib/game_controller.dart index 3f7e6d1..3757a39 100644 --- a/tarok/lib/game_controller.dart +++ b/tarok/lib/game_controller.dart @@ -84,6 +84,8 @@ class GameController extends GetxController { var gamesRequired = (-1).obs; var canExtendGame = true.obs; + var unreadMessages = 0.obs; + var playerId = "player".obs; Timer? currentTimer; @@ -417,6 +419,7 @@ class GameController extends GetxController { Future sendMessageString(String m) async { if (bots) return; + unreadMessages.value = 0; final Uint8List message = Messages.Message( chatMessage: Messages.ChatMessage( userId: playerId.value, @@ -446,6 +449,7 @@ class GameController extends GetxController { Future sendMessage() async { if (bots) return; + unreadMessages.value = 0; final Uint8List message = Messages.Message( chatMessage: Messages.ChatMessage( userId: playerId.value, @@ -1064,6 +1068,7 @@ class GameController extends GetxController { kingSelect.value = false; kingSelection.value = false; zaruf.value = false; + requestedGameEnd.value = false; currentPredictions.value = Messages.Predictions(); copyGames(); @@ -1351,6 +1356,7 @@ class GameController extends GetxController { if (start) { currentTimer = Timer.periodic(const Duration(milliseconds: 50), (timer) { + //print(i); userWidgets[i].timer -= 0.05; userWidgets.refresh(); }); @@ -1360,6 +1366,7 @@ class GameController extends GetxController { userWidgets.refresh(); } else if (msg.hasChatMessage()) { final chatMessage = msg.chatMessage; + if (msg.playerId != playerId.value) unreadMessages.value++; chat.insert(0, chatMessage); } else if (msg.hasStashedTarock()) { final stashedTarock = msg.stashedTarock; diff --git a/tarok/lib/lobby.dart b/tarok/lib/lobby.dart index 3e395c0..ef95b52 100644 --- a/tarok/lib/lobby.dart +++ b/tarok/lib/lobby.dart @@ -532,6 +532,7 @@ class Lobby extends StatelessWidget { // PRIORITY QUEUE GridView.count( + childAspectRatio: 0.75, crossAxisCount: 4, physics: const NeverScrollableScrollPhysics(), // to disable GridView's scrolling @@ -540,6 +541,7 @@ class Lobby extends StatelessWidget { ...controller.priorityQueue.map( (e) => GestureDetector( onTap: () { + debugPrint("Priority"); Get.toNamed("/game", parameters: { "playing": e.requiredPlayers.toString(), "gameId": e.id, @@ -548,6 +550,7 @@ class Lobby extends StatelessWidget { }, child: Card( child: Column( + mainAxisSize: MainAxisSize.min, children: [ Center( child: Text( @@ -579,17 +582,22 @@ class Lobby extends StatelessWidget { ), ), ), - ...List.generate( - e.requiredPlayers - e.user.length, - (index) => const SizedBox( - height: 40, - child: Text( - "Pridružite se igri", - style: TextStyle( - fontSize: 25, + ListView.builder( + shrinkWrap: true, + itemCount: e.requiredPlayers - e.user.length, + itemBuilder: (BuildContext context, int index) { + return const Center( + child: SizedBox( + height: 40, + child: Text( + "Pridružite se igri", + style: TextStyle( + fontSize: 25, + ), + ), ), - ), - ), + ); + }, ), ], ), diff --git a/tarok/lib/lobby_controller.dart b/tarok/lib/lobby_controller.dart index ac53240..0f2101c 100644 --- a/tarok/lib/lobby_controller.dart +++ b/tarok/lib/lobby_controller.dart @@ -86,14 +86,13 @@ class LobbyController extends GetxController { } void dialog() { - Get.defaultDialog( - title: 'Prilagodite si igro', - content: ConstrainedBox( - constraints: const BoxConstraints( - minWidth: 1000, - ), - child: Obx( - () => SingleChildScrollView( + Get.dialog( + AlertDialog( + scrollable: true, + title: const Text('Prilagodite si igro'), + content: Obx( + () => SizedBox( + width: double.maxFinite, child: Column( children: guest.value ? [] @@ -162,29 +161,29 @@ class LobbyController extends GetxController { ), ), ), + actions: [ + TextButton( + onPressed: () async { + if (guest.value) { + botGame(3); + return; + } + await newGame(3); + }, + child: const Text('V tri'), + ), + TextButton( + onPressed: () async { + if (guest.value) { + botGame(4); + return; + } + await newGame(4); + }, + child: const Text('V štiri'), + ), + ], ), - actions: [ - TextButton( - onPressed: () async { - if (guest.value) { - botGame(3); - return; - } - await newGame(3); - }, - child: const Text('V tri'), - ), - TextButton( - onPressed: () async { - if (guest.value) { - botGame(4); - return; - } - await newGame(4); - }, - child: const Text('V štiri'), - ), - ], ); } @@ -428,6 +427,15 @@ class LobbyController extends GetxController { } else if (msg.hasGameJoin()) { for (int i = 0; i < priorityQueue.length; i++) { if (priorityQueue[i].id != msg.gameJoin.gameId) continue; + bool found = false; + for (int n = 0; n < priorityQueue[i].user.length; n++) { + if (priorityQueue[i].user[n].id != msg.gameJoin.player.id) { + continue; + } + found = true; + break; + } + if (found) break; priorityQueue[i].user.add( User( id: msg.gameJoin.player.id, @@ -440,6 +448,15 @@ class LobbyController extends GetxController { } for (int i = 0; i < queue.length; i++) { if (queue[i].id != msg.gameJoin.gameId) continue; + bool found = false; + for (int n = 0; n < queue[i].user.length; n++) { + if (queue[i].user[n].id != msg.gameJoin.player.id) { + continue; + } + found = true; + break; + } + if (found) break; queue[i].user.add( User( id: msg.gameJoin.player.id,