1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use pns::SimulatedNet;

use std::{
    collections::HashMap,
    fs::File,
    io::{self, BufRead, BufReader, Error},
    str::FromStr,
};

fn main() -> Result<(), Error> {
    let mut save = false;
    let mut load = false;

    let mut args = std::env::args();
    args.next();
    for arg in args {
        match arg.as_ref() {
            "save" => save = true,
            "load" => load = true,
            _ => eprintln!("Ignore invalid argument '{arg}'"),
        }
    }

    let mut net = SimulatedNet::load("pns/examples/example.pn".as_ref())?;

    let mut names = HashMap::new();
    let file = File::open("pns/examples/example.pnk")?;
    for (tid, line) in net.transition_ids().zip(BufReader::new(file).lines()) {
        names.insert(tid, line?);
    }

    if save {
        net.save("examples/example_copy.pn".as_ref())?;
    }

    if load {
        net.load_state("examples/example.pns".as_ref())
            .expect("State initialization failed");
    } else {
        net.add_state();
    }
    let mut forward = true;
    for mut state in &mut net {
        loop {
            let fire = if forward {
                state.fire()
            } else {
                state.unfire()
            };

            if fire.transitions.is_empty() {
                println!("Reverse play direction!");
                forward = !forward;
                continue;
            }

            for (i, tid) in fire.transitions.iter().enumerate() {
                println!("{}: {}", i + 1, names[tid]);
            }
            let stdin = io::stdin();
            let mut string = String::new();
            let size = stdin.read_line(&mut string)?;
            if size == 0 {
                continue;
            }
            match string.chars().next().unwrap() {
                'r' => {
                    println!("Reverse play direction!");
                    forward = !forward;
                    continue;
                }
                'q' => break,
                's' => {
                    println!(
                        "{}",
                        if state.save("examples/example.pns".as_ref()).is_ok() {
                            "Saving successful"
                        } else {
                            "Saving failed"
                        }
                    );
                    continue;
                }
                _ => (),
            }
            match usize::from_str(&string[..(string.len() - 1)]) {
                Ok(num) if num != 0 && num <= fire.transitions.len() => {
                    fire.finish(num - 1);
                }
                _ => {
                    println!(
                        "You have to input a valid number from 1 to {}",
                        fire.transitions.len()
                    );
                    println!("You can also quit by writing \"q\", save the current state by writing \"s\" or reverse by writing \"r\"");
                    continue;
                }
            }
        }
    }

    Ok(())
}