Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
significantly reduce replay file size
Browse files Browse the repository at this point in the history
  • Loading branch information
MinusKelvin committed Nov 16, 2019
1 parent 8b95eda commit 2d0d71f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
playout*
Cargo.lock
options.yaml
replay.json
replay.dat
3 changes: 2 additions & 1 deletion gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ libtetris = { path = "../libtetris" }
battle = { path = "../battle" }
bot = { path = "../bot" }
serde = "1"
serde_json = "1"
bincode = "1"
libflate = "0.1"
serde_yaml = "0.8"
rand = "0.7.0"
rand_pcg = "0.2.0"
Expand Down
11 changes: 7 additions & 4 deletions gui/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rand::prelude::*;
use crate::input::InputSource;
use crate::replay::InfoReplay;
use std::collections::VecDeque;
use libflate::deflate;

type InputFactory = dyn Fn(Board) -> (Box<dyn InputSource>, String);

Expand Down Expand Up @@ -74,16 +75,18 @@ impl EventHandler for LocalGame<'_> {
while timer::check_update_time(ctx, 60) {
let do_update = match self.state {
State::GameOver(0) => {
serde_json::to_writer(
std::io::BufWriter::new(
std::fs::File::create("replay.json"
).unwrap()),
let mut encoder = deflate::Encoder::new(
std::fs::File::create("replay.dat"
).unwrap());
bincode::serialize_into(
&mut encoder,
&InfoReplay {
replay: self.battle.replay.clone(),
p1_info_updates: self.p1_info_updates.clone(),
p2_info_updates: self.p2_info_updates.clone()
}
).unwrap();
encoder.finish().unwrap();

// Don't catch up after pause due to replay saving
while timer::check_update_time(ctx, 60) {}
Expand Down
7 changes: 4 additions & 3 deletions gui/src/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::Resources;
use battle::{ Battle, Controller, Replay };
use std::collections::VecDeque;
use serde::{ Serialize, Deserialize };
use libflate::deflate;

pub struct ReplayGame<'a, P> {
gui: Gui,
Expand All @@ -23,8 +24,8 @@ impl<'a, P: AsRef<std::path::Path> + Clone> ReplayGame<'a, P> {
pub fn new(resources: &'a mut Resources, file: P) -> Self {
let InfoReplay {
replay, p1_info_updates, p2_info_updates
} = serde_json::from_reader(
std::io::BufReader::new(std::fs::File::open(file.clone()).unwrap())
} = bincode::deserialize_from(
deflate::Decoder::new(std::fs::File::open(file.clone()).unwrap())
).unwrap();
let battle = Battle::new(
replay.config, replay.p1_seed, replay.p2_seed, replay.garbage_seed
Expand Down Expand Up @@ -61,7 +62,7 @@ impl<P: AsRef<std::path::Path> + Clone> EventHandler for ReplayGame<'_, P> {
loop {
match std::fs::File::open(self.file.clone()) {
Ok(f) => {
match serde_json::from_reader(std::io::BufReader::new(f)) {
match bincode::deserialize_from(deflate::Decoder::new(f)) {
Ok(r) => {
replay = r;
break
Expand Down

0 comments on commit 2d0d71f

Please sign in to comment.