aboutsummaryrefslogtreecommitdiff
path: root/server/examples/client.rs
blob: a3c31359492036ba76008515b7efa93b4d19384c (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
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
use std::{
    io::{stdin, BufRead, BufReader, Write},
    net::TcpStream,
    thread,
};

use glam::Vec2;
use undercooked::protocol::{PacketC, PacketS};

fn main() {
    let mut sock = TcpStream::connect("127.0.0.1:27031").unwrap();

    let sock2 = BufReader::new(sock.try_clone().unwrap());
    thread::spawn(move || {
        for line in sock2.lines() {
            let line = line.unwrap();
            let packet: PacketC = serde_json::from_str(&line).unwrap();
            eprintln!("{packet:?}")
        }
    });

    for line in stdin().lines() {
        let line = line.unwrap();
        let mut toks = line.split(" ");
        let packet = match toks.next().unwrap() {
            "j" => PacketS::Join {
                character: 0,
                name: "test".to_string(),
            },
            "p" => PacketS::Position {
                pos: Vec2::new(
                    toks.next().unwrap().parse().unwrap(),
                    toks.next().unwrap().parse().unwrap(),
                ),
                boosting: false,
                rot: 0.,
            },
            "i" => PacketS::Position {
                pos: Vec2::new(
                    toks.next().unwrap().parse().unwrap(),
                    toks.next().unwrap().parse().unwrap(),
                ),
                boosting: false,
                rot: toks.next().unwrap_or("0").parse().unwrap(),
            },
            _ => {
                println!("unknown");
                continue;
            }
        };
        sock.write_all(serde_json::to_string(&packet).unwrap().as_bytes())
            .unwrap();
        sock.write_all(b"\n").unwrap();
    }
}