aboutsummaryrefslogtreecommitdiff
path: root/src/gens.rs
blob: 7374731c826d9be5438a3bce7befc1aa6cafd548 (plain)
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
//! Helpers to determine if a node can send a particular packet.

/// These functions are unfortunately all a bit stringly typed atm
pub mod can_send {
    use crate::data::{Level, PacketType, Upgrade};
    use Level::*;

    #[inline]
    pub fn base() -> Vec<&'static str> {
        vec!["ping", "capture"]
    }

    /// Guard nodes are the most versatile
    #[inline]
    pub fn guard(lvl: Level) -> Vec<&'static str> {
        match lvl {
            // This is just kinda gross
            One => {
                let mut p = base();
                p.append(&mut vec!["payload", "cns", "reset"]);
                p
            }
            Two => {
                let mut p = guard(One);
                p.append(&mut vec!["nitm", "virus"]);
                p
            }
            Three => {
                let mut p = guard(Two);
                p.append(&mut vec!["takeover"]);
                p
            }
        }
    }

    /// Compute nodes can ping and compute but not capture
    pub fn compute() -> Vec<&'static str> {
        vec!["ping", "compute"]
    }

    /// Relays only relay!
    pub fn relay() -> Vec<&'static str> {
        vec![]
    }
}

pub mod send {
    use crate::{
        data::{
            Level::*,
            PacketType as Pkt,
            Upgrade::{self, *},
        },
        stats::strengths,
    };

    /// Turn the string type identifier into a packet
    ///
    /// This function makes heavy use of the stats module, which is
    /// responsible for balancing all of these values.
    pub fn build_packet(node: Upgrade, type_: &'static str, prev: Option<Pkt>) -> Pkt {
        match (prev, node, type_) {
            // All pings are the same
            (None, _, "ping") => Pkt::Ping,

            // TODO: captures should improve
            (None, Base, "capture") | (None, Guard(_), "capture") => Pkt::Capture,

            (None, Compute(lvl), "compute") => Pkt::Compute {
                curr: Default::default(),
                max: strengths::compute_max(lvl),
                step: strengths::compute_step(lvl),
            },

            (Some(prev), Guard(lvl), "payload") => Pkt::Payload {
                inner: Box::new(prev),
                reward: strengths::payload_reward(lvl),
            },

            (None, Guard(_), "cns") => Pkt::CNS,
            (None, Guard(_), "reset") => Pkt::Reset,
            (None, Guard(_), "nitm") => Pkt::Nitm,
            (None, Guard(_), "virus") => Pkt::Virus,

            // Only level 3 guards can send takeovers
            (None, Guard(Three), "takeover") => Pkt::TakeOver,
            
            // Can't touch this
            (_, _, _) => unreachable!(),
        }
    }
}