2025-11-05 20:24:51 +00:00
|
|
|
// Copyright (C) 2025 Emilis Bliūdžius
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2025-10-09 22:27:21 +01:00
|
|
|
use core::{num::NonZeroU8, time::Duration};
|
2025-10-04 09:26:37 +01:00
|
|
|
use std::sync::Arc;
|
2025-06-23 09:48:28 +01:00
|
|
|
|
|
|
|
|
use werewolves_proto::{
|
|
|
|
|
message::{ClientMessage, Identification, host::HostMessage},
|
|
|
|
|
player::PlayerId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use crate::{
|
2025-09-26 21:15:52 +01:00
|
|
|
LogError,
|
2025-06-23 09:48:28 +01:00
|
|
|
communication::lobby::LobbyComms,
|
|
|
|
|
connection::JoinedPlayers,
|
|
|
|
|
game::{GameEnd, GameRunner},
|
2025-10-09 22:27:21 +01:00
|
|
|
lobby::{Lobby, LobbyPlayers},
|
2025-06-23 09:48:28 +01:00
|
|
|
saver::Saver,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub struct IdentifiedClientMessage {
|
|
|
|
|
pub identity: Identification,
|
|
|
|
|
pub message: ClientMessage,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn run_game(joined_players: JoinedPlayers, comms: LobbyComms, mut saver: impl Saver) {
|
2025-10-09 22:27:21 +01:00
|
|
|
let mut lobby = Lobby::new(joined_players, comms);
|
|
|
|
|
if let Some(dummies) = option_env!("DUMMY_PLAYERS").and_then(|p| p.parse::<NonZeroU8>().ok()) {
|
|
|
|
|
log::info!("creating {dummies} dummy players");
|
|
|
|
|
lobby.set_players_in_lobby(LobbyPlayers::with_dummies(dummies));
|
|
|
|
|
lobby.send_lobby_info_to_host().await.log_debug();
|
|
|
|
|
}
|
|
|
|
|
let mut state = RunningState::Lobby(lobby);
|
2025-06-23 09:48:28 +01:00
|
|
|
loop {
|
|
|
|
|
match &mut state {
|
|
|
|
|
RunningState::Lobby(lobby) => {
|
|
|
|
|
if let Some(game) = lobby.next().await {
|
|
|
|
|
state = RunningState::Game(game)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
RunningState::Game(game) => {
|
|
|
|
|
if let Some(result) = game.next().await {
|
|
|
|
|
match saver.save(game.proto_game()) {
|
|
|
|
|
Ok(path) => {
|
|
|
|
|
log::info!("saved game to {path}");
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("saving game: {err}");
|
|
|
|
|
let game_clone = game.proto_game().clone();
|
|
|
|
|
let mut saver_clone = saver.clone();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let started = chrono::Utc::now();
|
|
|
|
|
loop {
|
|
|
|
|
tokio::time::sleep(Duration::from_secs(30)).await;
|
|
|
|
|
match saver_clone.save(&game_clone) {
|
|
|
|
|
Ok(path) => {
|
|
|
|
|
log::info!("saved game from {started} to {path}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("saving game from {started}: {err}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
state = match state {
|
|
|
|
|
RunningState::Game(game) => {
|
|
|
|
|
RunningState::GameOver(GameEnd::new(game, result))
|
|
|
|
|
}
|
|
|
|
|
_ => unsafe { core::hint::unreachable_unchecked() },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
RunningState::GameOver(end) => {
|
|
|
|
|
if let Some(mut new_lobby) = end.next().await {
|
|
|
|
|
new_lobby.send_lobby_info_to_clients().await;
|
2025-09-26 21:15:52 +01:00
|
|
|
new_lobby.send_lobby_info_to_host().await.log_debug();
|
2025-06-23 09:48:28 +01:00
|
|
|
state = RunningState::Lobby(new_lobby)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub enum Message {
|
|
|
|
|
Host(HostMessage),
|
|
|
|
|
Client(IdentifiedClientMessage),
|
2025-10-04 09:26:37 +01:00
|
|
|
ConnectedList(Arc<[PlayerId]>),
|
2025-06-23 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum RunningState {
|
|
|
|
|
Lobby(Lobby),
|
|
|
|
|
Game(GameRunner),
|
|
|
|
|
GameOver(GameEnd),
|
|
|
|
|
}
|