aboutsummaryrefslogtreecommitdiff
path: root/server/replaytool/src/main.rs
blob: 8dde133d6316ec89272117c922a24880a9f5b926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2025 Hurry Curry! Contributors

    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, version 3 of the License only.

    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/>.

*/

pub mod record;
pub mod render;
pub mod replay;

use crate::{
    record::record,
    render::{RenderArgs, render},
    replay::replay,
};
use anyhow::Context;
use clap::Parser;
use futures_util::{SinkExt, StreamExt};
use hurrycurry_protocol::{Character, Message, PacketC, PacketS, PlayerClass};
use log::{LevelFilter, debug, error, info, warn};
use serde::{Deserialize, Serialize};
use std::{
    path::PathBuf,
    time::{Duration, SystemTime},
};
use tokio::{net::TcpListener, time::sleep};
use tokio_tungstenite::tungstenite::{self};

#[derive(Parser)]
enum Args {
    /// Connects as a spectator and saves the protocol packets for replay
    Record {
        /// Dont stop after the first game but restart instead
        #[arg(short, long)]
        r#loop: bool,
        url: String,
        output: PathBuf,
    },
    /// Starts a local server that replays previously recorded sessions
    Replay {
        input: PathBuf,
    },
    /// Runs a replay server and the client in movie mode to record that replay
    Render(#[command(flatten)] RenderArgs),
    DownloadMap {
        url: String,
        mapname: String,
    },
}

#[derive(Serialize, Deserialize)]
struct Event {
    ts: f64,
    packet: PacketC,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    env_logger::builder()
        .filter_level(LevelFilter::Info)
        .parse_env("LOG")
        .init();

    rustls::crypto::ring::default_provider()
        .install_default()
        .unwrap();

    let args = Args::parse();

    match args {
        Args::Record {
            url,
            output,
            r#loop,
        } => loop {
            let out = if r#loop {
                output.join(format!(
                    "replay-{}",
                    SystemTime::now()
                        .duration_since(SystemTime::UNIX_EPOCH)
                        .unwrap()
                        .as_secs()
                ))
            } else {
                output.clone()
            };
            if let Err(e) = record(&out, &url).await {
                warn!("recording failed: {e:#}");
                sleep(Duration::from_secs(1)).await;
            }
            if r#loop {
                info!("restarting...");
            } else {
                break;
            }
        },
        Args::Replay { input } => {
            let ws_listener = TcpListener::bind("127.0.0.1:27032").await?;
            info!("listening for websockets on {}", ws_listener.local_addr()?);

            loop {
                replay(&ws_listener, &input).await?;
            }
        }
        Args::Render(a) => {
            render(a).await?;
        }
        Args::DownloadMap { url, mapname } => {
            download_map(&url, &mapname).await?;
        }
    }
    Ok(())
}

pub async fn download_map(url: &str, mapname: &str) -> anyhow::Result<()> {
    let (mut sock, _) = tokio_tungstenite::connect_async(url).await?;

    sock.send(tungstenite::Message::Text(
        serde_json::to_string(&PacketS::Join {
            name: "map downloader".to_string(),
            character: Character::default(),
            class: PlayerClass::Chef,
            id: None,
            position: None,
        })
        .unwrap()
        .into(),
    ))
    .await?;

    while let Some(Ok(message)) = sock.next().await {
        match message {
            tungstenite::Message::Text(line) => {
                let packet: PacketC = serde_json::from_str(&line).context("invalid packet")?;
                debug!("<- {packet:?}");

                match packet {
                    PacketC::Joined { id } => {
                        sock.send(tungstenite::Message::Text(
                            serde_json::to_string(&PacketC::Communicate {
                                player: id,
                                message: Some(Message::Text(format!("/download map {mapname}"))),
                                timeout: None,
                            })
                            .unwrap()
                            .into(),
                        ))
                        .await?;
                    }
                    PacketC::ServerMessage {
                        message: Message::Text(t),
                        ..
                    } => {
                        println!("{t}");
                        return Ok(());
                    }
                    _ => (),
                }
            }
            tungstenite::Message::Close(_) => {
                error!("connection closed by server");
                break;
            }
            _ => (),
        }
    }

    Ok(())
}