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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use treeflection::{NodeRunner, Node};
use bincode;
use rand::Rng;
use rand;
use crate::json_upgrade;

use std::net::{TcpListener, UdpSocket, IpAddr, SocketAddr};
use std::io::Read;
use std::io::Write;
use std::str;
use std::time::{Instant, Duration};

use crate::input::ControllerInput;

pub struct NetCommandLine {
    listener: TcpListener
}

impl NetCommandLine {
    pub fn new() -> NetCommandLine {
        let listener = TcpListener::bind("127.0.0.1:1613").unwrap();
        listener.set_nonblocking(true).unwrap();

        NetCommandLine {
            listener: listener,
        }
    }

    pub fn step<T>(&mut self, root_node: &mut T) where T: Node {
        let mut buf = [0; 1024];
        if let Ok((mut stream, _)) = self.listener.accept() {
            match stream.read(&mut buf) {
                Ok(amt) => {
                    if amt > 1 {
                        if let Ok(string) = str::from_utf8(&buf[1..amt]) {
                            if buf[0] == 0x43 { // 'C'
                                let out = NetCommandLine::run_inner(&string, root_node);
                                if let Err(e) = stream.write(out.as_bytes()) {
                                    println!("command send failed {}", e);
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    println!("command receive failed {}", e);
                }
            }
        }
    }

    fn run_inner<T>(command: &str, package: &mut T) -> String where T: Node {
        match NodeRunner::new(command) {
            Ok(runner) => {
                let result = package.node_step(runner);
                result
            },
            Err(msg) => msg
        }
    }
}

/*  Message Formats:
    Matchmaking Request:
        1 byte - 0x00
        n bytes - bincode serialized MatchMakingRequest

    Initiate Connection:
        1 byte   - 0x01
        n bytes - bincode serialized InitConnection

    Ping Request:
        1 byte - 0x02
        1 byte - ping id

    Ping Response:
        1 byte - 0x03
        1 byte - ping id

    Controller Input Message
        1 byte  - 0x04
        n bytes - bincode serialized controller input data

    Disconnect notification:
        1 byte - 0xAA
*/

pub struct Netplay {
    // structure: peers Vec<frames Vec<controllers Vec<ControllerInput>>>
    // frame 0 has index 2
    pub confirmed_inputs:  Vec<Vec<Vec<ControllerInput>>>,
    match_making_response: Option<MatchMakingResponse>,
    peers:                 Vec<SocketAddr>,
    seed:                  u64,
    socket:                UdpSocket,
    state:                 NetplayState,
    state_frame:           usize,
    last_received_frame:   usize,
    index:                 usize,
    init_msgs:             Vec<InitConnection>,
    ping_msgs:             Vec<u8>,
    start_request_msgs:    Vec<usize>,
    start_confirm_msgs:    Vec<usize>,
    running_msgs:          Vec<InputConfirm>,
}

impl Netplay {
    pub fn new() -> Netplay {
        let socket = UdpSocket::bind("0.0.0.0:8413").unwrap();
        socket.set_nonblocking(true).unwrap();
        Netplay {
            state:                 NetplayState::Offline,
            state_frame:           0,
            last_received_frame:   0,
            confirmed_inputs:      vec!(),
            match_making_response: None,
            peers:                 vec!(),
            seed:                  0,
            index:                 0,
            init_msgs:             vec!(),
            ping_msgs:             vec!(),
            start_request_msgs:    vec!(),
            start_confirm_msgs:    vec!(),
            running_msgs:          vec!(),
            socket,
        }
    }

    /// Call this once every frame
    pub fn step(&mut self) {
        if !self.skip_frame() {
            self.state_frame += 1;
        }

        // receive messages
        loop {
            let mut buf = [0; 1024];
            if let Ok((_, addr)) = self.socket.recv_from(&mut buf) { // returns Err if there is no packet waiting
                match buf[0] {
                    0x00 => {
                        if let Ok(data) = bincode::deserialize(&buf[1..]) {
                            self.match_making_response = Some(data);
                        }
                    }
                    0x01 => {
                        if self.peers.contains(&addr) {
                            if let Ok(data) = bincode::deserialize(&buf[1..]) {
                                self.init_msgs.push(data);
                            }
                        }
                    }
                    0x02 => {
                        if self.peers.contains(&addr) {
                            self.socket.send_to(&[3, buf[1]], addr).unwrap();
                        }
                    }
                    0x03 => {
                        if self.peers.contains(&addr) {
                            self.ping_msgs.push(buf[1]);
                        }
                    }
                    0x04 => {
                        if self.peers.contains(&addr) {
                            if let Ok(data) = bincode::deserialize(&buf[1..]) {
                                self.running_msgs.push(data);
                            }
                        }
                    }
                    0xAA => {
                        self.disconnect_with_reason("Peer disconnected");
                    }
                    _ => {
                        println!("Couldn't process netplay message starting with: {:?}", &buf[0..32]);
                    }
                }
                self.last_received_frame = self.state_frame;
            }
            else {
                break;
            }
        }

        if self.peers.len() > 0 && self.state_frame - self.last_received_frame > 600 {
            self.disconnect_with_reason("Connection timed out: no packets received in the last 10 seconds");
        }

        // process messages
        match self.state.clone() {
            NetplayState::Offline => { }
            NetplayState::Disconnected { .. } => { }
            NetplayState::MatchMaking { request, } => {
                if self.state_frame % 600 == 1 { // Send a request every 10 seconds
                    let mut data = bincode::serialize(&request).unwrap();
                    data.insert(0, 0x00);
                    if let Err(_) = self.socket.send_to(&data, "matchmaking.pfsandbox.net:8413") {
                        self.disconnect_with_reason("matchmaking.pfsandbox.net:8413 is inaccessible");
                    }
                }
                if let &Some(ref response) = &self.match_making_response {
                    for peer in response.addresses.iter() {
                        if !self.peers.contains(peer) {
                            self.peers.push(peer.clone());
                            self.confirmed_inputs.push(vec!());
                        }
                    }
                }
                if self.peers.len() as u8 + 1 == request.num_players {
                    self.set_state(NetplayState::InitConnection (InitConnection {
                        random:        rand::thread_rng().gen::<u64>(),
                        build_version: request.build_version.clone(),
                        hash:          request.package_hash.clone()
                    }));
                }
            }
            NetplayState::InitConnection (local) => {
                // send init
                let mut data = bincode::serialize(&local).unwrap();
                data.insert(0, 0x01);
                self.broadcast(&data, "init");

                // receive init
                if let Some(init) = self.init_msgs.pop() {
                    if init.hash != local.hash {
                        self.disconnect_with_reason("Package hashes did not match, ensure everyone is using the same package.");
                    }
                    else if init.build_version != local.build_version {
                        self.disconnect_with_reason("Build versions did not match, ensure everyone is using the same PF Sandbox build.");
                    }
                    else {
                        self.set_state(NetplayState::PingTest { local_init: local.clone(), pings: [Ping::default(); 255] });
                    }

                    // Use peer 0's random value to generate the game seed for all games in the current session.
                    // Repeating seeds like this shouldnt be noticeable
                    // TODO: handle multiple peers
                    if local.random < init.random {
                        self.index = 0;
                        self.seed = local.random;
                    }
                    else {
                        self.index = 1;
                        self.seed = init.random;
                    }
                }
            }
            NetplayState::PingTest { local_init, mut pings } => {
                // if we havnt received a ping yet then resend init message
                if pings.iter().all(|x| x.time_received.is_none()) {
                    let mut data = bincode::serialize(&local_init).unwrap();
                    data.insert(0, 0x01);
                    self.broadcast(&data, "init2");
                }

                // request a ping from peer and record the time_sent
                if let Some(next_ping) = pings.iter().enumerate().find(|x| x.1.time_sent.is_none()).map(|x| x.0) {
                    self.broadcast(&[2, next_ping as u8], "ping");
                    pings[next_ping].time_sent = Some(Instant::now());

                    // record the time_received of received pings
                    for ping_msg in self.ping_msgs.iter() {
                        pings[*ping_msg as usize].time_received = Some(Instant::now());
                    }
                    self.state = NetplayState::PingTest { local_init, pings };
                }
                else {
                    let mut ping_total = Duration::from_secs(0);
                    for ping in pings.iter().take(225) { // skip the last 30 as we dont want the most recent packets showing up as dropped.
                        if let (Some(time_sent), Some(time_received)) = (ping.time_sent, ping.time_received) {
                            ping_total += time_received.duration_since(time_sent);
                        } else {
                            ping_total += Duration::from_millis(200); // punish for dropping packet
                        }
                    }

                    let ping_total = ping_total.as_secs() as f64 + ping_total.subsec_nanos() as f64 / 1_000_000_000.0;
                    let ping_avg = ping_total / 255.0;
                    let ping_max = 100.0; // TODO: Grab from config
                    if ping_avg > ping_max {
                        self.disconnect_with_reason(format!("The ping was '{}' which was above the limit of '{}'", ping_avg, ping_max).as_ref());
                    } else {
                        self.set_state(NetplayState::Running);
                        // TODO: Need to force input reset all history at this point
                    }
                }
            }
            NetplayState::Running => {
                let peer = 0; // TODO: handle multiple peers
                let mut found_msg = true;
                let mut to_delete = vec!();
                while found_msg {
                    found_msg = false;
                    for (i, msg) in self.running_msgs.iter().enumerate() {
                        let inputs_len = self.confirmed_inputs[peer].len();
                        // msg.frame starts at 1 because its taken from the peers state_frame which is incremented before any logic is run
                        if msg.frame == inputs_len + 1 {
                            self.confirmed_inputs[peer].push(msg.inputs.clone());
                            found_msg = true;
                            to_delete.push(i)
                        }
                    }

                    to_delete.reverse();
                    for i in to_delete.iter() {
                        self.running_msgs.remove(*i);
                    }
                    to_delete.clear();
                }
            }
        }
        debug!("state: {}", self.state.to_string());
        debug!("number_of_peers: {}", self.number_of_peers());
        debug!("local_index: {}",  self.local_index());
        debug!("frame: {}", self.frame());
        debug!("frames_to_step: {}", self.frames_to_step());
        debug!("skip_frame: {}", self.skip_frame());
    }

    pub fn state(&self) -> NetplayState {
        self.state.clone()
    }

    /// Returns the index of the local machine
    pub fn local_index(&self) -> usize {
        match &self.state {
            &NetplayState::Running { .. } => self.index,
            _ => 0
        }
    }

    /// Returns the total number of peers including the local machine
    pub fn number_of_peers(&self) -> usize {
        self.peers.len() + 1
    }

    // TODO: Optimize by only starting from a frame where the inputs differ
    /// Returns the number of frames that need to be stepped/restepped including the current frame
    pub fn frames_to_step(&self) -> usize {
        let input_frames = self.confirmed_inputs.iter().map(|x| x.len()).min().unwrap_or(1);
        match &self.state {
            &NetplayState::Running => self.state_frame.saturating_sub(input_frames).max(1),
            _ => 1
        }
    }

    pub fn frame(&self) -> usize {
        match &self.state {
            &NetplayState::Running => self.state_frame,
            _ => 0
        }
    }

    // TODO: take ping into account
    /// Returns true if the local machine should do nothing for a frame so that peers can catch up.
    pub fn skip_frame(&self) -> bool {
        let input_frames = self.confirmed_inputs.iter().map(|x| x.len()).min().unwrap_or(1);
        match &self.state {
            &NetplayState::Running => self.state_frame > input_frames + 1,
            _ => false
        }
    }

    /// Return the seed used for this netplay session
    pub fn get_seed(&self) -> Option<u64> {
        match &self.state {
            &NetplayState::Running { .. } => {
                Some(self.seed)
            }
            _ => None
        }
    }

    fn broadcast(&mut self, message: &[u8], message_name: &str) {
        let mut fail = false;
        for peer in self.peers.iter() {
            if let Err(_) = self.socket.send_to(message, peer) {
                fail = true;
                break;
            }
        }
        if fail {
            self.disconnect_with_reason(format!("Peer is inaccessible: failed to send {}", message_name).as_ref());
        }
    }

    fn clear(&mut self) {
        self.confirmed_inputs.clear();
        self.index = 0;
        self.init_msgs.clear();
        self.last_received_frame = 0;
        self.match_making_response = None;
        self.peers.clear();
        self.ping_msgs.clear();
        self.running_msgs.clear();
        self.seed = 0;
        self.start_confirm_msgs.clear();
        self.start_request_msgs.clear();
        self.state_frame = 0;
    }

    pub fn direct_connect(&mut self, address: IpAddr, hash: String) {
        self.clear();
        self.peers.push(SocketAddr::new(address, 8413));
        self.confirmed_inputs.push(vec!());
        self.set_state(NetplayState::InitConnection (InitConnection {
            random:        rand::thread_rng().gen::<u64>(),
            build_version: json_upgrade::build_version(),
            hash
        }));
    }

    pub fn connect_match_making(&mut self, region: String, num_players: u8, package_hash: String) {
        self.clear();
        let request = MatchMakingRequest {
            build_version: json_upgrade::build_version(),
            region,
            num_players,
            package_hash,
        };
        self.set_state(NetplayState::MatchMaking { request });
    }

    fn set_state(&mut self, state: NetplayState) {
        self.state = state;
        self.state_frame = 0;
        self.last_received_frame = 0;
    }

    fn disconnect_with_reason(&mut self, reason: &str) {
        match &self.state {
            &NetplayState::Offline |
            &NetplayState::Disconnected { .. } => { }
            _ => {
                for peer in self.peers.iter() {
                    self.socket.send_to(&[0xAA], peer).ok();
                }
                self.set_state(NetplayState::Disconnected { reason: String::from(reason) });
                self.clear();
            }
        }
    }

    pub fn set_offline(&mut self) {
        match &self.state {
            &NetplayState::Offline => { }
            _ => {
                for peer in self.peers.iter() {
                    self.socket.send_to(&[0xAA], peer).ok();
                }
                self.set_state(NetplayState::Offline);
                self.clear();
            }
        }
    }

    pub fn send_controller_inputs(&mut self, inputs: Vec<ControllerInput>) {
        if let &NetplayState::Running = &self.state {
            let input_confirm = InputConfirm {
                frame: self.state_frame,
                inputs
            };
            let mut data = bincode::serialize(&input_confirm).unwrap();
            data.insert(0, 0x04);
            self.broadcast(&data, "controller input");
        }
        // TODO: Store InputConfirm so we can resend it later (maybe repeat it every step() for n steps, no idea how to best handle this sort of thing)
    }
}

/// State flow sequence:
///     Offline -> MatchMaking -> InitConnection -> Ping Test -> Running -> Disconnected -> Offline
#[derive(Clone)]
pub enum NetplayState {
    Offline,
    Running,
    InitConnection (InitConnection),
    MatchMaking    { request: MatchMakingRequest },
    Disconnected   { reason: String },
    PingTest       { local_init: InitConnection, pings:  [Ping; 255] },
}

impl NetplayState {
    pub fn to_string(&self) -> String {
        match self {
            &NetplayState::Offline               => String::from("Offline"),
            &NetplayState::Running               => String::from("Running"),
            &NetplayState::InitConnection (_)    => String::from("InitConnection"),
            &NetplayState::MatchMaking    { .. } => String::from("MatchMaking"),
            &NetplayState::Disconnected   { .. } => String::from("Disconnected"),
            &NetplayState::PingTest       { .. } => String::from("PingTest"),
        }
    }
}

#[derive(Clone, Serialize)]
pub struct MatchMakingRequest {
    pub region:    String,
    package_hash:  String,
    build_version: String,
    num_players:   u8
}

#[derive(Clone, Deserialize)]
struct MatchMakingResponse {
    addresses: Vec<SocketAddr>
}

#[derive(Clone, Serialize, Deserialize)]
pub struct InitConnection {
    build_version:  String,
    hash:           String,
    random:         u64
}

#[derive(Clone, Default, Copy)]
pub struct Ping {
    time_sent:     Option<Instant>,
    time_received: Option<Instant>,
}

#[derive(Clone, Serialize, Deserialize)]
struct InputConfirm {
    inputs: Vec<ControllerInput>,
    frame:  usize,
}