Skip to content

Commit

Permalink
replay sistem - resolves #13
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Oct 7, 2023
1 parent ff4f7c7 commit 710da62
Show file tree
Hide file tree
Showing 7 changed files with 1,079 additions and 857 deletions.
61 changes: 31 additions & 30 deletions messages/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -209,37 +209,38 @@ message Message {
string username = 1;
string player_id = 2;
string game_id = 3;
bool silent = 4;

oneof data {
Connection connection = 4;
Licitiranje licitiranje = 5;
Card card = 6;
LicitiranjeStart licitiranje_start = 7;
GameStart game_start = 8;
LoginRequest login_request = 9;
LoginInfo login_info = 10;
LoginResponse login_response = 11;
ClearDesk clear_desk = 12;
Results results = 13;
UserList user_list = 14;
KingSelection king_selection = 15;
StartPredictions start_predictions = 16;
Predictions predictions = 17;
TalonReveal talon_reveal = 18;
PlayingReveal playing_reveal = 19;
TalonSelection talon_selection = 20;
Stash stash = 21;
GameEnd game_end = 22;
GameStartCountdown game_start_countdown = 23;
Predictions predictions_resend = 24;
Radelci radelci = 25;
Time time = 26;
ChatMessage chat_message = 27;
InvitePlayer invite_player = 28;
StashedTarock stashed_tarock = 29;
ClearHand clear_hand = 30;
ReplayLink replay_link = 31;
ReplayMove replay_move = 32;
ReplaySelectGame replay_select_game = 33;
Connection connection = 10;
Licitiranje licitiranje = 11;
Card card = 12;
LicitiranjeStart licitiranje_start = 13;
GameStart game_start = 14;
LoginRequest login_request = 15;
LoginInfo login_info = 16;
LoginResponse login_response = 17;
ClearDesk clear_desk = 18;
Results results = 19;
UserList user_list = 20;
KingSelection king_selection = 21;
StartPredictions start_predictions = 22;
Predictions predictions = 23;
TalonReveal talon_reveal = 24;
PlayingReveal playing_reveal = 25;
TalonSelection talon_selection = 26;
Stash stash = 27;
GameEnd game_end = 28;
GameStartCountdown game_start_countdown = 29;
Predictions predictions_resend = 30;
Radelci radelci = 31;
Time time = 32;
ChatMessage chat_message = 33;
InvitePlayer invite_player = 34;
StashedTarock stashed_tarock = 35;
ClearHand clear_hand = 36;
ReplayLink replay_link = 37;
ReplayMove replay_move = 38;
ReplaySelectGame replay_select_game = 39;
}
}
132 changes: 92 additions & 40 deletions tarok/lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ class Controller extends GetxController {
var barvic = false.obs;
var mondfang = false.obs;

final bool bbots = Get.parameters["bots"] == "true";
final String gameId = Get.parameters["gameId"]!;
final int playing = int.parse(Get.parameters["playing"]!);
final bool replay = Get.parameters["replay"] == "true";

stockskis.StockSkis? stockskisContext;
var controller = TextEditingController().obs;
var gameLinkController = TextEditingController().obs;
List prijatelji = [].obs;

late final WebSocket socket;
Expand Down Expand Up @@ -391,6 +397,24 @@ class Controller extends GetxController {
socket.send(message);
}

Future<void> sendReplayNext() async {
if (bots) return;
if (!replay) return;
final Uint8List message = Messages.Message(
replayMove: Messages.ReplayMove(),
).writeToBuffer();
socket.send(message);
}

Future<void> sendReplayGame(int game) async {
if (bots) return;
if (!replay) return;
final Uint8List message = Messages.Message(
replaySelectGame: Messages.ReplaySelectGame(game: game),
).writeToBuffer();
socket.send(message);
}

Future<void> sendMessage() async {
if (bots) return;
final Uint8List message = Messages.Message(
Expand Down Expand Up @@ -562,7 +586,6 @@ class Controller extends GetxController {
}
cards.remove(card);

turn.value = false;
bool early = await addToStih("player", "player", card.asset);

if (early) return;
Expand All @@ -584,7 +607,6 @@ class Controller extends GetxController {
.writeToBuffer();
socket.send(message);
turn.value = false;
cards.remove(card);
resetPremoves();
premovedCard.value = null;
}
Expand Down Expand Up @@ -904,17 +926,29 @@ class Controller extends GetxController {
startPredicting.value = false;
final userId = msg.playerId;

if (userId != playerId) {
for (int i = 0; i < userWidgets.length; i++) {
if (userWidgets[i].id != userId) continue;
for (int n = 0; n < userWidgets[i].cards.length; n++) {
stockskis.Card c = userWidgets[i].cards[n];
if (card.id != c.card.asset) continue;
userWidgets[i].cards.removeAt(n);
break;
}
for (int i = 0; i < userWidgets.length; i++) {
if (userWidgets[i].id != userId) continue;
for (int n = 0; n < userWidgets[i].cards.length; n++) {
stockskis.Card c = userWidgets[i].cards[n];
if (card.id != c.card.asset) continue;
userWidgets[i].cards.removeAt(n);
break;
}

if (userId != playerId) {
break;
}

turn.value = false;

for (int n = 0; n < cards.length; n++) {
stockskis.LocalCard c = cards[n];
if (card.id != c.asset) continue;
cards.removeAt(n);
break;
}

break;
}

if (firstCard.value == null) {
Expand Down Expand Up @@ -1045,7 +1079,7 @@ class Controller extends GetxController {

if (users.isEmpty) return;

debugPrint("urejam vrstni red v `users`");
debugPrint("urejam vrstni red v `users` ${users}");

// uredimo vrstni red naših igralcev
//users = [];
Expand Down Expand Up @@ -1078,6 +1112,9 @@ class Controller extends GetxController {
}

debugPrint("anotacije so bile dodane");

users.refresh();
userWidgets.refresh();
} else if (msg.hasGameStartCountdown()) {
countdown.value = msg.gameStartCountdown.countdown;
} else if (msg.hasLicitiranje()) {
Expand Down Expand Up @@ -1114,38 +1151,51 @@ class Controller extends GetxController {
validCards();
} else if (msg.hasResults()) {
final r = msg.results;
results.value = r;
bool radelc = false;
for (int i = 0; i < results.value!.user.length; i++) {
if (results.value!.user[i].radelc) {
radelc = true;
break;
}
}
for (int n = 0; n < users.length; n++) {
users[n].points.add(
stockskis.ResultsPoints(
playing: false,
points: 0,
results: ResultsCompLayer.messagesToStockSkis(r),
radelc: radelc,
),
);

if (!msg.silent) {
results.value = r;
}
for (int i = 0; i < r.user.length; i++) {
final user = r.user[i];
for (int k = 0; k < user.user.length; k++) {
Messages.User u = user.user[k];
for (int n = 0; n < users.length; n++) {
if (users[n].id != u.id) continue;
debugPrint(
"found ${u.id} with points ${user.points} and total ${users[n].total}");
users[n].points.last.points += user.points;
users[n].total += user.points;
users[n].points.last.playing = user.playing;

if (msg.silent || !replay) {
results.value = r;
bool radelc = false;
for (int i = 0; i < results.value!.user.length; i++) {
if (results.value!.user[i].radelc) {
radelc = true;
break;
}
}
for (int n = 0; n < users.length; n++) {
debugPrint("Dodajam točke igralcu ${users[n].id}");
users[n].points.add(
stockskis.ResultsPoints(
playing: false,
points: 0,
results: ResultsCompLayer.messagesToStockSkis(r),
radelc: radelc,
),
);
}
for (int i = 0; i < r.user.length; i++) {
final user = r.user[i];
for (int k = 0; k < user.user.length; k++) {
Messages.User u = user.user[k];
for (int n = 0; n < users.length; n++) {
if (users[n].id != u.id) continue;
debugPrint(
"found ${u.id} with points ${user.points} and total ${users[n].total}");
users[n].points.last.points += user.points;
users[n].total += user.points;
users[n].points.last.playing = user.playing;
break;
}
}
}
users.refresh();
}

if (msg.silent) {
results.value = null;
}
} else if (msg.hasTalonSelection()) {
final talonSelection = msg.talonSelection;
Expand Down Expand Up @@ -1268,6 +1318,8 @@ class Controller extends GetxController {
}
} else if (msg.hasClearHand()) {
cards.value = [];
} else if (msg.hasReplayLink()) {
gameLinkController.value.text = msg.replayLink.replay;
}
},
onDone: () {
Expand Down
Loading

0 comments on commit 710da62

Please sign in to comment.