finished object json writes

This commit is contained in:
ntr 2019-09-10 12:38:20 +10:00
parent 759ac868dd
commit c853299c46
2 changed files with 29 additions and 3 deletions

View File

@ -1,3 +1,5 @@
use std::fs::File;
use rand::prelude::*;
use uuid::Uuid;
@ -864,7 +866,12 @@ pub fn game_update(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
result.iter().next().ok_or(format_err!("game {:?} could not be written", game))?;
if game.finished() {
info!("game finished state={:?}", game);
info!("finished id={:?}", game.id);
match game_json_file_write(&game) {
Ok(dest) => info!("wrote dest={:?}", dest),
Err(e) => error!("json write error={:?}", e),
};
if let Some(i) = game.instance {
instance_game_finished(tx, &game, i)?;
@ -874,6 +881,12 @@ pub fn game_update(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
return Ok(());
}
fn game_json_file_write(g: &Game) -> Result<String, Error> {
let dest = format!("/var/lib/mnml/data/games/{}.mnml.game.json", g.id);
serde_json::to_writer(File::create(&dest)?, g)?;
Ok(dest)
}
pub fn game_skill(tx: &mut Transaction, account: &Account, game_id: Uuid, construct_id: Uuid, target_construct_id: Option<Uuid>, skill: Skill) -> Result<Game, Error> {
let mut game = game_get(tx, game_id)?;

View File

@ -1,3 +1,5 @@
use std::fs::File;
use uuid::Uuid;
use serde_cbor::{from_slice, to_vec};
@ -547,15 +549,26 @@ pub fn instance_update(tx: &mut Transaction, instance: Instance) -> Result<Insta
result.iter().next().ok_or(err_msg("no instance row returned"))?;
// info!("{:?} wrote instance", instance.id);
trace!("{:?} wrote instance", instance.id);
if instance.finished() {
info!("instance finished state={:?}", instance);
info!("finished id={:?}", instance.id);
match instance_json_file_write(&instance) {
Ok(dest) => info!("wrote dest={:?}", dest),
Err(e) => error!("json write error={:?}", e),
};
}
return Ok(instance);
}
fn instance_json_file_write(g: &Instance) -> Result<String, Error> {
let dest = format!("/var/lib/mnml/data/instances/{}.mnml.instance.json", g.id);
serde_json::to_writer(File::create(&dest)?, g)?;
Ok(dest)
}
pub fn instance_get(tx: &mut Transaction, instance_id: Uuid) -> Result<Instance, Error> {
let query = "
SELECT *