Skip to content

Commit

Permalink
preklopi na lobby namesto http pollinga - linked to #44
Browse files Browse the repository at this point in the history
rejoin igralcev - resolves #47
končanje igre v primeru disconnectanih igralcev - resolves #48
  • Loading branch information
mytja committed Oct 30, 2023
1 parent b63b7c5 commit 9090c86
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 89 deletions.
122 changes: 73 additions & 49 deletions tarok/lib/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class Game extends StatelessWidget {
),
),

// REZULTATI, KLEPET, CHAT
// REZULTATI
// KLEPET
// CHAT
Container(
alignment: Alignment.topRight,
child: Card(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
Expand Down
7 changes: 7 additions & 0 deletions tarok/lib/game_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -417,6 +419,7 @@ class GameController extends GetxController {

Future<void> sendMessageString(String m) async {
if (bots) return;
unreadMessages.value = 0;
final Uint8List message = Messages.Message(
chatMessage: Messages.ChatMessage(
userId: playerId.value,
Expand Down Expand Up @@ -446,6 +449,7 @@ class GameController extends GetxController {

Future<void> sendMessage() async {
if (bots) return;
unreadMessages.value = 0;
final Uint8List message = Messages.Message(
chatMessage: Messages.ChatMessage(
userId: playerId.value,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
Expand All @@ -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;
Expand Down
28 changes: 18 additions & 10 deletions tarok/lib/lobby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -548,6 +550,7 @@ class Lobby extends StatelessWidget {
},
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Center(
child: Text(
Expand Down Expand Up @@ -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,
),
),
),
),
),
);
},
),
],
),
Expand Down
77 changes: 47 additions & 30 deletions tarok/lib/lobby_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
? []
Expand Down Expand Up @@ -162,29 +161,29 @@ class LobbyController extends GetxController {
),
),
),
actions: <Widget>[
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: <Widget>[
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'),
),
],
);
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 9090c86

Please sign in to comment.