summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 2e6ca10f701025cb1d8090ffc82921eb35f761b5 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

use std::thread::sleep;
use std::time::Duration;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;

mod server;
mod gameserver;
mod room;
mod util;
mod controls;
mod components;
mod resources;
mod systems;
mod worldmessages;
mod pos;
mod componentwrapper;
mod parameter;
mod assemblage;
mod componentparameter;
mod encyclopedia;
mod template;
mod roomtemplate;
mod savestate;
mod playerid;
mod defaultencyclopedia;
mod playerstate;
mod roomid;
mod persistence;
mod worldloader;
mod world;
mod sprite;
mod attack;

pub use self::{
	pos::Pos,
	playerid::PlayerId,
	roomid::RoomId,
	util::Result,
	sprite::Sprite,
	template::Template,
	encyclopedia::Encyclopedia
};

use self::{
	gameserver::GameServer,
	server::unixserver::UnixServer,
	server::tcpserver::TcpServer,
	server::Server,
	defaultencyclopedia::default_encyclopedia,
	persistence::FileStorage,
	controls::Action,
	worldloader::WorldLoader,
	world::World,
	worldmessages::MessageCache
};



fn main() -> Result<()>{
	
	let mut servers: Vec<Box<dyn Server>> = Vec::new();

	let addr = Path::new("\0rustifarm");
	let unixserver = UnixServer::new(&addr)?;
	servers.push(Box::new(unixserver));
	
	let addr = "127.0.0.1:1234".parse()?;
	let inetserver = TcpServer::new(&addr)?;
	servers.push(Box::new(inetserver));
	
	let mut gameserver = GameServer::new(servers);
	
	
	let loader = WorldLoader::new(PathBuf::from_str(&(std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".to_string()).to_owned() + "/content/maps/"))?);
	
	let storage = FileStorage::new(FileStorage::savedir().expect("couldn't find any save directory"));

	let mut world = World::new(default_encyclopedia(), loader, Box::new(storage), RoomId::from_str("room"));
	
	println!("asciifarm started");
	
	let mut message_cache = MessageCache::default();
	
	let mut count = 0;
	loop {
		let actions = gameserver.update();
		for action in actions {
			match action {
				Action::Input(player, control) => {
					let _ = world.control_player(player, control);
				}
				Action::Join(player) => {
					world.add_player(&player)?;
				}
				Action::Leave(player) => {
					world.remove_player(&player)?;
				}
			}
		}
		world.update();
		if count % 50 == 0 {
			world.save();
		}
		let messages = world.view();
		for (player, mut message) in messages {
			message_cache.trim(&player, &mut message);
			if message.is_empty(){
				continue;
			}
			let _ = gameserver.send(&player, message.to_json());
		}
		
		count += 1;
		sleep(Duration::from_millis(100));
	}
}