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
use std::collections::HashMap;
use std::thread;
use std::thread::JoinHandle;

use crate::error::ServerError;
use crate::group::{Message, SendGroup};
use log::{error, info};
use rask_engine::error::EngineError;
use rask_engine::network::packet::ReadResource;
use rask_engine::resources::registry;

pub trait Game {
    fn run(self) -> Result<JoinHandle<()>, ServerError>;
}

#[allow(dead_code)]
pub struct RaskGame {
    game: (),
    group: SendGroup,
    users: Vec<User>,
    will_to_live: bool,
    res_cache: HashMap<u32, Vec<u8>>,
}

#[derive(Debug, Clone)]
pub struct User {
    name: String,
    sender: ws::Sender,
}

impl User {
    pub fn new(name: String, sender: ws::Sender) -> Self {
        User { name, sender }
    }
}

impl Game for RaskGame {
    fn run(self) -> Result<JoinHandle<()>, ServerError> {
        thread::Builder::new()
            .name(format!("group{}", self.group.id))
            .spawn(move || self.game_loop())
            .map_err(ServerError::GameCreation)
    }
}

const RES_PATH: &str = "res";

impl RaskGame {
    pub fn new(group: SendGroup) -> Self {
        Self {
            game: (),
            group,
            users: Vec::new(),
            will_to_live: true,
            res_cache: HashMap::new(),
        }
    }

    fn push_buffer(&mut self, buf_id: u32, user_id: usize) -> Result<(), ServerError> {
        self.users
            .get_mut(user_id)
            .ok_or(ServerError::InvalidUser(user_id))?
            .sender
            .send(ws::Message::from(
                self.res_cache
                    .get(&buf_id)
                    .ok_or_else(|| {
                        EngineError::ResourceMissing(format!(
                            "Resource {} is not loaded yet",
                            buf_id
                        ))
                    })?
                    .as_slice(),
            ))?;
        Ok(())
    }

    fn load_char(&mut self, chr: registry::CharacterInfo) -> Result<(), ServerError> {
        if self.res_cache.contains_key(&chr.id) {
            return Ok(());
        }
        self.res_cache.insert(
            chr.id,
            chr.read_from_file(RES_PATH).ok_or_else(|| {
                EngineError::ResourceMissing(format!("Failed to serialize {:?}", chr))
            })?,
        );
        Ok(())
    }

    fn load_resource(&mut self, res: registry::ResourceInfo) -> Result<(), ServerError> {
        if self.res_cache.contains_key(&res.id) {
            return Ok(());
        }
        self.res_cache.insert(
            res.id,
            res.read_from_file(RES_PATH).ok_or_else(|| {
                EngineError::ResourceMissing(format!("Failed to serialize {:?}", res))
            })?,
        );
        Ok(())
    }

    fn level_one(&mut self, uid: usize) -> Result<(), ServerError> {
        self.load_resource(registry::EMPTY)?;
        self.load_resource(registry::THIEF)?;
        self.load_char(registry::CHAR)?;
        self.push_buffer(registry::EMPTY.id, uid)?;
        self.push_buffer(registry::THIEF.id, uid)?;
        self.push_buffer(registry::CHAR.id, uid)
    }

    fn game_loop(mut self) {
        let _messages = self.get_messages();
        while self.will_to_live {
            //game.handle_events(messages);
            //game.tick();
            //let b = game.get_broadcast()
            //self.users.iter().foreach(|u| u.sender.send(b));
            let _messages = self.get_messages();
            thread::sleep(std::time::Duration::from_secs(5));
        }
        info!("thread killed itself");
    }

    fn add_user(&mut self, user: &User) {
        self.users.push(user.clone());
        if let Err(e) = self.level_one(self.users.len() - 1) {
            error!("Error during resoure distribution: {}", e);
        }
    }

    fn get_messages(&mut self) -> Vec<Message> {
        //  info!("receiver {:#?} is still alive", self.group.receiver);
        let (mut data, control): (Vec<Message>, Vec<Message>) =
            self.group.receiver.try_iter().partition(Message::is_data);
        control.iter().for_each(|x| match x {
            Message::Park => {
                data = Vec::new();
                thread::park();
            }
            Message::Kill => self.will_to_live = false,
            Message::Add(user) => self.add_user(&user),
            Message::Remove(sender) => {
                if let Some(pos) = self.users.iter().position(|x| x.sender == *sender) {
                    self.users.swap_remove(pos);
                }
            }
            _ => (),
        });
        data
    }
}