|
| 1 | +use super::tombstone::Tombstone; |
| 2 | +use crate::character::Character; |
| 3 | +use crate::item::Item; |
| 4 | +use crate::location::Location; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use std::collections::HashMap; |
| 7 | + |
| 8 | +/// v0.4.0 of the game struct, kept for backwards compatibility when upgrading |
| 9 | +/// the game data file to the new version |
| 10 | +/// FIXME this will be removed on a subsequent version |
| 11 | +#[derive(Serialize, Deserialize)] |
| 12 | +struct Game040 { |
| 13 | + pub player: Character, |
| 14 | + pub location: Location, |
| 15 | + pub gold: i32, |
| 16 | + inventory: HashMap<String, Vec<Box<dyn Item>>>, |
| 17 | + tombstones: HashMap<Location, Tombstone>, |
| 18 | +} |
| 19 | + |
| 20 | +/// Get a new Game instance out of a v0.4.0 one |
| 21 | +pub fn deserialize(data: &[u8]) -> Result<super::Game, bincode::Error> { |
| 22 | + let mut v4game: Game040 = bincode::deserialize(&data)?; |
| 23 | + let mut new_game = super::Game::new(); |
| 24 | + std::mem::swap(&mut new_game.player, &mut v4game.player); |
| 25 | + std::mem::swap(&mut new_game.location, &mut v4game.location); |
| 26 | + std::mem::swap(&mut new_game.inventory, &mut v4game.inventory); |
| 27 | + new_game.tombstones = v4game |
| 28 | + .tombstones |
| 29 | + .drain() |
| 30 | + .map(|(l, t)| (l.to_string(), t)) |
| 31 | + .collect(); |
| 32 | + new_game.gold = v4game.gold; |
| 33 | + Ok(new_game) |
| 34 | +} |
| 35 | + |
| 36 | +#[cfg(test)] |
| 37 | +mod tests { |
| 38 | + use super::*; |
| 39 | + use crate::item; |
| 40 | + use item::equipment::Equipment; |
| 41 | + |
| 42 | + #[test] |
| 43 | + fn test_backwards_compatibility() { |
| 44 | + // build a game040 with everything |
| 45 | + let mut game_v4 = Game040 { |
| 46 | + location: Location::home(), |
| 47 | + player: Character::player(), |
| 48 | + gold: 10, |
| 49 | + inventory: HashMap::new(), |
| 50 | + tombstones: HashMap::new(), |
| 51 | + }; |
| 52 | + game_v4.player.sword = Some(item::equipment::Sword::new(1)); |
| 53 | + game_v4.player.shield = Some(item::equipment::Shield::new(1)); |
| 54 | + game_v4 |
| 55 | + .inventory |
| 56 | + .insert("potion".to_string(), vec![Box::new(item::Potion::new(1))]); |
| 57 | + |
| 58 | + let mut tombstone_game = super::super::Game::new(); |
| 59 | + tombstone_game.add_item("potion", Box::new(item::Potion::new(1))); |
| 60 | + game_v4 |
| 61 | + .tombstones |
| 62 | + .insert(Location::home(), Tombstone::drop(&mut tombstone_game)); |
| 63 | + |
| 64 | + let data = bincode::serialize(&game_v4).unwrap(); |
| 65 | + let mut new_game = deserialize(&data).unwrap(); |
| 66 | + |
| 67 | + assert_eq!(10, new_game.gold); |
| 68 | + assert!(new_game.location.is_home()); |
| 69 | + assert!(new_game.player.sword.is_some()); |
| 70 | + assert!(new_game.player.shield.is_some()); |
| 71 | + assert_eq!(1 as usize, *new_game.inventory().get("potion").unwrap()); |
| 72 | + // pick up tombstone @ home |
| 73 | + new_game.visit_home(); |
| 74 | + assert_eq!(2 as usize, *new_game.inventory().get("potion").unwrap()); |
| 75 | + } |
| 76 | +} |
0 commit comments