|
|
|
@ -1,4 +1,4 @@ |
|
|
|
use std::{borrow::BorrowMut, fs::File, io::Write, path::Path}; |
|
|
|
use std::{fs::File, path::Path}; |
|
|
|
|
|
|
|
use super::{ |
|
|
|
protos::game::{game_status::Piles, CardKind, Cards, GameStatus, Image}, |
|
|
|
@ -9,16 +9,21 @@ use crate::{ |
|
|
|
games::{CardId, CardIdx, PileKind, RunningPile}, |
|
|
|
server::{ |
|
|
|
protos::{ |
|
|
|
game::{cards::Card, game_status::CustomInfoMessage}, |
|
|
|
game::{ |
|
|
|
cards::Card, |
|
|
|
game_status::CustomInfoMessage, |
|
|
|
images::{self, DataPacket, SetUp}, |
|
|
|
Images, |
|
|
|
}, |
|
|
|
protocol::server_client_packet::{self, Data}, |
|
|
|
}, |
|
|
|
time::EpochTime, |
|
|
|
}, |
|
|
|
}; |
|
|
|
use anyhow::{anyhow, Result}; |
|
|
|
use bytes::BytesMut; |
|
|
|
use prost_types::Timestamp; |
|
|
|
use tar::{Archive, Builder}; |
|
|
|
use bytes::{Buf, Bytes}; |
|
|
|
use libflate::deflate::Encoder; |
|
|
|
use tar::Builder; |
|
|
|
|
|
|
|
pub(super) async fn get_status( |
|
|
|
data: &mut ServiceData, |
|
|
|
@ -180,13 +185,15 @@ pub(super) async fn get_card_image( |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
const MESSAGE_SIZE: usize = 10_000_000; |
|
|
|
|
|
|
|
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 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, |
|
|
|
@ -197,8 +204,7 @@ pub(super) async fn get_cards_images( |
|
|
|
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); |
|
|
|
let mut ar = Builder::new(Encoder::new(Vec::new())); |
|
|
|
for card_kind in cards.cards { |
|
|
|
let (face, back) = game.get_card_paths(&card_kind.kind); |
|
|
|
// log::info!("Loading face image [{:?}]", time.elapsed());
|
|
|
|
@ -234,22 +240,31 @@ pub(super) async fn get_cards_images( |
|
|
|
) |
|
|
|
.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?;
|
|
|
|
let mut b = Bytes::from(ar.into_inner().unwrap().finish().into_result().unwrap()); |
|
|
|
let n = (b.len() / MESSAGE_SIZE) + if b.len() % MESSAGE_SIZE == 0 { 0 } else { 1 }; |
|
|
|
socket_mgr |
|
|
|
.write( |
|
|
|
data.user_id.get_ref()?, |
|
|
|
Data::ReturnCardsImages(Images { |
|
|
|
data: Some(images::Data::Setup(SetUp { number: n as u32 })), |
|
|
|
}), |
|
|
|
) |
|
|
|
.await?; |
|
|
|
for i in 0..(n as u32) { |
|
|
|
let mut t = b.take(MESSAGE_SIZE); |
|
|
|
let mut v = vec![0; t.remaining()]; |
|
|
|
t.copy_to_slice(&mut v); |
|
|
|
b = t.into_inner(); |
|
|
|
socket_mgr |
|
|
|
.write( |
|
|
|
data.user_id.get_ref()?, |
|
|
|
Data::ReturnCardsImages(Images { |
|
|
|
data: Some(images::Data::DataPacket(DataPacket { id: i, data: v })), |
|
|
|
}), |
|
|
|
) |
|
|
|
.await?; |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
|