-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rs
108 lines (90 loc) · 3.36 KB
/
server.rs
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
use std::net::TcpStream;
use std::collections::HashMap;
use flate2::Compression;
use std::io::Cursor;
use std::fs::File;
use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use std::io::{Read, Write, Seek, SeekFrom};
use std::fs::OpenOptions;
use crate::client::Client;
pub struct Server {
pub server_name: String,
pub server_motd: String,
pub software_name: String,
pub clients: HashMap<i8, Client>,
pub name_to_id: HashMap<String, Client>,
pub world_data: Vec<u8>,
pub world_x: u16,
pub world_z: u16,
pub world_y: u16,
pub spawn_x: u16,
pub spawn_y: u16,
pub spawn_z: u16
}
impl Server{
pub fn new(server_name: &str, server_motd: &str) -> Server
{
return Server {server_name: String::from(server_name),
server_motd: String::from(server_motd),
software_name: String::from("RSCube 0.0.2a"),
clients: HashMap::new(),
name_to_id: HashMap::new(),
world_data: Vec::new(), world_x: 0, world_y: 0, world_z: 0, spawn_x: 0, spawn_y: 0, spawn_z: 0};
}
pub fn add_client(self: &mut Server, stream: TcpStream) -> i8
{
let mut key: i8 = -1;
for i in 0..127
{
if !self.clients.contains_key(&i)
{
key = i;
break;
}
}
if (key < 0)
{
println!("[WARN] Player ID pool exhausted");
}
else{
println!("New player ID assigned: {}", key);
self.clients.insert(key, Client::new(key,stream));
}
return key;
}
pub fn world_load(self: &mut Server)
{
let file = File::open("maps/world.lvl").unwrap();
let mut header: [u8; 18] = [0; 18];
let mut d = GzDecoder::new(file);
d.read_exact(&mut header).unwrap();
let mut cur = Cursor::new(&header);
let magicNumber = cur.read_u16::<LittleEndian>().unwrap();
println!("Magic Number: {}", magicNumber);
self.world_x = cur.read_u16::<LittleEndian>().unwrap();
self.world_z = cur.read_u16::<LittleEndian>().unwrap();
self.world_y = cur.read_u16::<LittleEndian>().unwrap();
self.spawn_x = cur.read_u16::<LittleEndian>().unwrap();
self.spawn_z = cur.read_u16::<LittleEndian>().unwrap();
self.spawn_y = cur.read_u16::<LittleEndian>().unwrap();
println!("World Size: {} x {} x {}", self.world_x, self.world_y, self.world_z);
d.read_to_end(&mut self.world_data).unwrap();
}
pub fn world_save(self: &mut Server)
{
let mut file = OpenOptions::new()
.write(true).open("maps/world.lvl").unwrap();
let mut enc = GzEncoder::new(&mut file,Compression::default());
enc.write_u16::<LittleEndian>(1874).unwrap();
enc.write_u16::<LittleEndian>(self.world_x).unwrap();
enc.write_u16::<LittleEndian>(self.world_z).unwrap();
enc.write_u16::<LittleEndian>(self.world_y).unwrap();
enc.write_u16::<LittleEndian>(self.spawn_x).unwrap();
enc.write_u16::<LittleEndian>(self.spawn_z).unwrap();
enc.write_u16::<LittleEndian>(self.spawn_y).unwrap();
enc.write_u32::<LittleEndian>(0);
enc.write(& self.world_data);
}
}