Skip to content

Commit

Permalink
feat: win message
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjercan committed Apr 5, 2024
1 parent d2bee77 commit 45a93d6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
16 changes: 16 additions & 0 deletions examples/multi/client.cl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class PlayerLobby inherits Thread {

fight_lose: Bool <- false;
fight_win: Bool <- false;
game_winner: Int <- ~1;
game_win: Bool <- false;

coin: Coin <- new Coin.init(0, 0, new Float.from_int(10));
players: List (* Player *) <- new List.single(new Object);
Expand All @@ -98,6 +100,7 @@ class PlayerLobby inherits Thread {
message: PlayerDisconnected => remove_player(message);
message: PlayerFightLose => fight_lose <- true;
message: PlayerFightWin => fight_win <- true;
message: PlayerWin => { game_winner <- message.player_id(); game_win <- true; };
message: DisconnectedMessage => { keep_running <- false; client.close(); };
message: Message => abort();
esac
Expand Down Expand Up @@ -198,6 +201,19 @@ class PlayerLobby inherits Thread {
if fight_lose then { raylib.clearBackground(raylib.red()); fight_lose <- false; } else 0 fi;
if fight_win then { new IO.out_string("*really nice sounds and particles*\n"); fight_win <- false; } else 0 fi;

if 0 <= game_winner then
{
if game_win then {
if (game_winner = player_id) then { new IO.out_string("You win!\n"); }
else { new IO.out_string("You lost!\n"); } fi;

game_win <- false;
} else 0 fi;

raylib.drawText("Last winner: ".concat(game_winner.to_string()), 10, 10, 20, raylib.black());
}
else 0 fi;

raylib;
}
};
Expand Down
31 changes: 30 additions & 1 deletion examples/multi/message.cl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class MessageHelper inherits Serde {
else if kind = "6" then new PlayerDisconnected.deserialize(rest)
else if kind = "7" then new PlayerFightWin.deserialize(rest)
else if kind = "8" then new PlayerFightLose.deserialize(rest)
else { abort(); new Tuple; } fi fi fi fi fi fi fi fi fi
else if kind = "9" then new PlayerWin.deserialize(rest)
else { abort(); new Tuple; } fi fi fi fi fi fi fi fi fi fi
};
};

Expand Down Expand Up @@ -314,3 +315,31 @@ class PlayerFightLose inherits Message {
in new Tuple.init(msg, rest)
};
};

class PlayerWin inherits Message {
-- 1 byte for the kind of message: 9
-- 8 bytes for the player id

player_id: Int;

init(id: Int): SELF_TYPE {
{
player_id <- id;
self;
}
};

player_id(): Int { player_id };

serialize(): String {
let id: String <- new MessageHelper.serialize_int(player_id)
in "9".concat(id)
};

deserialize(input: String): Tuple (* Message, String *) {
let id: Int <- new MessageHelper.deserialize_int(input.substr(0, 8)),
msg: Message <- new PlayerWin.init(id),
rest: String <- input.substr(8, input.length() - 8)
in new Tuple.init(msg, rest)
};
};
35 changes: 35 additions & 0 deletions examples/multi/server.cl
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,18 @@ class Player inherits Thread {
}
};

score_reset(): Object {
{
score <- 0;
lobby.broadcast(new PlayerScore.init(player_id, score));
}
};

score_increase(): Object {
{
score <- score + 1;
lobby.broadcast(new PlayerScore.init(player_id, score));
lobby.check_win(self);
}
};

Expand Down Expand Up @@ -302,6 +310,33 @@ class PlayerLobby inherits Thread {
}
pool
};

check_win(player: Player): Object {
if 25 <= player.score() then
{
broadcast(new PlayerWin.init(player.player_id()));
reset();
}
else 0 fi
};

reset(): Object {
let iter: List (* Player *) <- players
in
while not isvoid iter loop
{
case iter.value() of
player: Player => {
player.random();
player.score_reset();
player.update(new PlayerInput.init(player.player_id(), false, false, false, false));
};
player: Object => 0;
esac;
iter <- iter.next();
}
pool
};
};

class Main {
Expand Down

0 comments on commit 45a93d6

Please sign in to comment.