|
|
|
@ -1,16 +1,24 @@ |
|
|
|
use std::{borrow::BorrowMut, fs::File, io::Write, path::Path}; |
|
|
|
|
|
|
|
use super::{ |
|
|
|
protos::game::{game_status::Piles, CardKind, GameStatus, Image}, |
|
|
|
protos::game::{game_status::Piles, CardKind, Cards, GameStatus, Image}, |
|
|
|
socket_manager::SocketManager, |
|
|
|
ServiceData, |
|
|
|
}; |
|
|
|
use crate::{ |
|
|
|
games::{CardId, CardIdx, PileKind, RunningPile}, |
|
|
|
server::protos::{ |
|
|
|
game::game_status::CustomInfoMessage, |
|
|
|
protocol::server_client_packet::{self, Data}, |
|
|
|
server::{ |
|
|
|
protos::{ |
|
|
|
game::{cards::Card, game_status::CustomInfoMessage}, |
|
|
|
protocol::server_client_packet::{self, Data}, |
|
|
|
}, |
|
|
|
time::EpochTime, |
|
|
|
}, |
|
|
|
}; |
|
|
|
use anyhow::{anyhow, Result}; |
|
|
|
use bytes::BytesMut; |
|
|
|
use prost_types::Timestamp; |
|
|
|
use tar::{Archive, Builder}; |
|
|
|
|
|
|
|
pub(super) async fn get_status( |
|
|
|
data: &mut ServiceData, |
|
|
|
@ -172,6 +180,79 @@ pub(super) async fn get_card_image( |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
pub(super) async fn get_cards_images( |
|
|
|
data: &mut ServiceData, |
|
|
|
socket_mgr: &SocketManager, |
|
|
|
cards: Cards, |
|
|
|
) -> Result<()> { |
|
|
|
log::info!("Getting images location"); |
|
|
|
let time = std::time::Instant::now(); |
|
|
|
let uuid = data.user_id.get()?; |
|
|
|
let lobby: u32 = match data.db.get_lobby_for_user(uuid).await { |
|
|
|
Some(l) => l, |
|
|
|
None => return Err(anyhow!("User isn't in a lobby")), |
|
|
|
}; |
|
|
|
let game_id = match data.running_games.read().await.get(&lobby) { |
|
|
|
Some(x) => x.read().await.0, |
|
|
|
None => return Err(anyhow!("User isn't in a lobby with a running game",)), |
|
|
|
}; |
|
|
|
let game = &data.games[game_id as usize]; |
|
|
|
let mut b = Vec::new(); |
|
|
|
let mut ar = Builder::new(b); |
|
|
|
for card_kind in cards.cards { |
|
|
|
let (face, back) = game.get_card_paths(&card_kind.kind); |
|
|
|
// log::info!("Loading face image [{:?}]", time.elapsed());
|
|
|
|
|
|
|
|
while !face.exists() || !back.exists() { |
|
|
|
tokio::task::yield_now().await; |
|
|
|
} |
|
|
|
fn is_newer<P: AsRef<Path>>(c: &Card, p: &P) -> bool { |
|
|
|
c.time |
|
|
|
.clone() |
|
|
|
.and_then(|x| { |
|
|
|
p.as_ref() |
|
|
|
.metadata() |
|
|
|
.and_then(|x| x.modified()) |
|
|
|
.ok() |
|
|
|
.map(|y| (y.into(), x.into())) |
|
|
|
}) |
|
|
|
.map_or(false, |(x, y): (EpochTime, EpochTime)| x > y) |
|
|
|
} |
|
|
|
|
|
|
|
if is_newer(&card_kind, &face) { |
|
|
|
ar.append_file( |
|
|
|
format!("{}/face.png", card_kind.kind), |
|
|
|
&mut File::open(face).unwrap(), |
|
|
|
) |
|
|
|
.unwrap(); |
|
|
|
} |
|
|
|
|
|
|
|
if is_newer(&card_kind, &back) { |
|
|
|
ar.append_file( |
|
|
|
format!("{}/back.png", card_kind.kind), |
|
|
|
&mut File::open(back).unwrap(), |
|
|
|
) |
|
|
|
.unwrap(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
File::create("a.tar").unwrap().write_all(&ar.into_inner().unwrap()).unwrap(); |
|
|
|
// TODO Apply lz4 compression
|
|
|
|
// log::info!("Loaded images {} [{:?}]", card_kind.kind, time.elapsed());
|
|
|
|
// socket_mgr
|
|
|
|
// .write(
|
|
|
|
// data.user_id.get_ref()?,
|
|
|
|
// Data::ReturnCardImage(Image {
|
|
|
|
// face: face_buf,
|
|
|
|
// back: back_buf,
|
|
|
|
// kind: card_kind.kind,
|
|
|
|
// }),
|
|
|
|
// )
|
|
|
|
// .await?;
|
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
pub(super) async fn on_click( |
|
|
|
data: &mut ServiceData, |
|
|
|
socket_mgr: &SocketManager, |
|
|
|
|