summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 9b313be1db340817c911b51576ca6affc120182a (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
93
94
95
96


use std::thread::sleep;
use std::time::Duration;
use std::path::Path;

pub mod server;
pub mod gameserver;
pub mod room;
pub mod util;
pub mod controls;
pub mod assemblages;
pub mod components;
pub mod resources;
pub mod systems;

use self::gameserver::{GameServer, Action};
use self::server::unixserver::UnixServer;
use self::server::tcpserver::TcpServer;
use self::server::Server;
use self::assemblages::{Player, Wall, Grass};

use serde_json;


fn main() {
	

	let addr = Path::new("\0rustifarm");
	let unixserver = UnixServer::new(&addr).expect("binding unix server failed");
	
	let addr = "127.0.0.1:1234".parse().unwrap();
	let inetserver = TcpServer::new(&addr).expect("binding inet server failed");
	
	
	let servers: Vec<Box<dyn Server>> = vec![Box::new(unixserver), Box::new(inetserver)];
	let mut gameserver = GameServer::new(servers);
	
	
	let mut room = room::Room::new((32, 32));
	
	gen_room(&mut room);
	
	loop {
		let actions = gameserver.update();
		for action in actions {
			match action {
				Action::Join(name) => {room.add_player(&name, &Player::new(&name));}
				Action::Leave(name) => {room.remove_player(&name);}
				Action::Input(name, control) => {room.control(name, control);}
			}
		}
		room.update();
		let (field, mapping) = room.view();
		let updatemsg = create_update_message(room.get_size(), field, mapping);
		gameserver.broadcast(updatemsg.as_str());
		sleep(Duration::from_millis(100));
	}
}

fn gen_room(room: &mut room::Room){
	
	let (width, height) = room.get_size();
	for x in 0..width {
		room.add_obj(&Wall, (x, 0));
		room.add_obj(&Wall, (x, height - 1));
	}
	for y in 1..height-1 {
		room.add_obj(&Wall, (0, y));
		room.add_obj(&Wall, (width - 1, y));
	}
	for x in 1..width-1 {
		for y in 1..height-1 {
			room.add_obj(&Grass::new(), (x, y));
		}
	}
}


fn create_update_message((width, height): (i32, i32), field: Vec<usize>, mapping: Vec<Vec<String>>) -> String {
	let updatemsg= serde_json::json!([
		"world",
		[
			[
				"field",
				{
					"width": width,
					"height": height,
					"field": field,
					"mapping": mapping
				}
			]
		]
	]);
	updatemsg.to_string()
}