-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
384 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use eo::{ | ||
data::{EOShort, StreamReader}, | ||
protocol::PacketAction, | ||
}; | ||
|
||
use crate::{map::MapHandle, player::PlayerHandle}; | ||
|
||
fn open(reader: StreamReader, player_id: EOShort, map: MapHandle) { | ||
let board_id = reader.get_short(); | ||
map.open_board(player_id, board_id); | ||
} | ||
|
||
fn take(reader: StreamReader, player_id: EOShort, map: MapHandle) { | ||
let _board_id = reader.get_short(); | ||
let post_id = reader.get_short(); | ||
map.view_board_post(player_id, post_id); | ||
} | ||
|
||
pub async fn board(action: PacketAction, reader: StreamReader, player: PlayerHandle) { | ||
let player_id = match player.get_player_id().await { | ||
Ok(player_id) => player_id, | ||
Err(e) => { | ||
error!("Failed to get player id: {}", e); | ||
return; | ||
} | ||
}; | ||
|
||
let map = match player.get_map().await { | ||
Ok(map) => map, | ||
Err(e) => { | ||
error!("Failed to get map: {}", e); | ||
return; | ||
} | ||
}; | ||
|
||
match action { | ||
PacketAction::Open => open(reader, player_id, map), | ||
PacketAction::Take => take(reader, player_id, map), | ||
_ => error!("Unhandled packet Board_{:?}", action), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use eo::{ | ||
data::{EOChar, EOShort, StreamBuilder, EO_BREAK_CHAR}, | ||
protocol::{PacketAction, PacketFamily}, | ||
}; | ||
use mysql_async::{params, prelude::Queryable, Row}; | ||
|
||
use crate::{utils::get_board_tile_spec, SETTINGS}; | ||
|
||
use super::Map; | ||
|
||
struct BoardPost { | ||
id: EOShort, | ||
author: String, | ||
subject: String, | ||
} | ||
|
||
impl Map { | ||
pub fn open_board(&mut self, player_id: EOShort, board_id: EOShort) { | ||
let character = match self.characters.get(&player_id) { | ||
Some(character) => character, | ||
None => return, | ||
}; | ||
|
||
let board_tile_spec = match get_board_tile_spec(board_id) { | ||
Some(spec) => spec, | ||
None => return, | ||
}; | ||
|
||
if !self.player_in_range_of_tile(player_id, board_tile_spec) { | ||
return; | ||
} | ||
|
||
let player = match &character.player { | ||
Some(player) => player.clone(), | ||
None => return, | ||
}; | ||
|
||
player.set_board_id(board_id); | ||
|
||
let pool = self.pool.clone(); | ||
tokio::spawn(async move { | ||
let mut builder = StreamBuilder::new(); | ||
|
||
let mut conn = pool.get_conn().await.unwrap(); | ||
let limit = if board_id == SETTINGS.board.admin_board as EOShort { | ||
SETTINGS.board.admin_max_posts | ||
} else { | ||
SETTINGS.board.max_posts | ||
}; | ||
|
||
let posts = conn | ||
.exec_map( | ||
include_str!("../../sql/get_board_posts.sql"), | ||
params! { | ||
"board_id" => board_id, | ||
"limit" => limit, | ||
}, | ||
|mut row: Row| BoardPost { | ||
id: row.take("id").unwrap(), | ||
author: row.take("author").unwrap(), | ||
subject: row.take("subject").unwrap(), | ||
}, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
builder.add_char(board_id as EOChar); | ||
builder.add_char(posts.len() as EOChar); | ||
|
||
for post in posts { | ||
builder.add_short(post.id); | ||
builder.add_byte(EO_BREAK_CHAR); | ||
builder.add_break_string(&post.author); | ||
builder.add_break_string(&post.subject); | ||
} | ||
|
||
player.send(PacketAction::Open, PacketFamily::Board, builder.get()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use eo::{data::EOShort, protocol::Coords, pubs::EmfTileSpec}; | ||
|
||
use crate::utils::in_client_range; | ||
|
||
use super::Map; | ||
|
||
impl Map { | ||
pub fn player_in_range_of_tile(&self, player_id: EOShort, spec_id: EmfTileSpec) -> bool { | ||
let character = match self.characters.get(&player_id) { | ||
Some(character) => character, | ||
None => return false, | ||
}; | ||
|
||
for row in &self.file.spec_rows { | ||
for tile in row.tiles.iter().filter(|tile| tile.spec == spec_id) { | ||
if in_client_range( | ||
&character.coords, | ||
&Coords { | ||
x: tile.x, | ||
y: row.y, | ||
}, | ||
) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
false | ||
} | ||
} |
Oops, something went wrong.