Skip to content

Commit

Permalink
Board_Create
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Oct 13, 2023
1 parent a4fe475 commit 5db24f4
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 18 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ name: Rust

on:
push:
branches: [ async-rewrite ]
branches: [master]
pull_request:
branches: [ async-rewrite ]
branches: [master]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
11 changes: 10 additions & 1 deletion src/handlers/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ use eo::{

use crate::{map::MapHandle, player::PlayerHandle};

fn create(reader: StreamReader, player_id: EOShort, map: MapHandle) {
let _board_id = reader.get_short();
reader.seek(1);
let subject = reader.get_break_string();
let body = reader.get_break_string();
map.post_board_message(player_id, subject, body);
}

fn open(reader: StreamReader, player_id: EOShort, map: MapHandle) {
let board_id = reader.get_short();
map.open_board(player_id, board_id);
map.open_board(player_id, board_id + 1);
}

fn take(reader: StreamReader, player_id: EOShort, map: MapHandle) {
Expand All @@ -34,6 +42,7 @@ pub async fn board(action: PacketAction, reader: StreamReader, player: PlayerHan
};

match action {
PacketAction::Create => create(reader, player_id, map),
PacketAction::Open => open(reader, player_id, map),
PacketAction::Take => take(reader, player_id, map),
_ => error!("Unhandled packet Board_{:?}", action),
Expand Down
5 changes: 5 additions & 0 deletions src/map/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ pub enum Command {
player_id: EOShort,
npc_index: EOChar,
},
PostBoardMessage {
player_id: EOShort,
subject: String,
body: String,
},
RecoverNpcs,
RecoverPlayers,
RequestPaperdoll {
Expand Down
7 changes: 7 additions & 0 deletions src/map/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mod open_shop;
mod open_skill_master;
mod play_effect;
mod player_in_range_of_tile;
mod post_board_message;
mod recover_npcs;
mod recover_players;
mod request_paperdoll;
Expand Down Expand Up @@ -259,6 +260,12 @@ impl Map {
npc_index,
} => self.open_skill_master(player_id, npc_index).await,

Command::PostBoardMessage {
player_id,
subject,
body,
} => self.post_board_message(player_id, subject, body).await,

Command::RecoverNpcs => self.recover_npcs().await,

Command::RecoverPlayers => self.recover_players().await,
Expand Down
1 change: 1 addition & 0 deletions src/map/map/get_board_tile_spec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn get_board_tile_spec(board_id: EOShort) -> EmfTileSpec {}
174 changes: 174 additions & 0 deletions src/map/map/post_board_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
use eo::data::{EOInt, EOShort};
use mysql_async::{params, prelude::Queryable, Conn, Row};

use crate::{errors::DataNotFoundError, utils::get_board_tile_spec, SETTINGS};

use super::Map;

impl Map {
pub async fn post_board_message(&mut self, player_id: EOShort, subject: String, body: String) {
let character = match self.characters.get(&player_id) {
Some(character) => character,
None => return,
};

let board_id = match character.player.as_ref().unwrap().get_board_id().await {
Some(board_id) => board_id,
None => return,
};

let board_tile_spec = match get_board_tile_spec(board_id) {
Some(spec) => spec,
None => return self.open_board(player_id, board_id),
};

if !self.player_in_range_of_tile(player_id, board_tile_spec) {
return self.open_board(player_id, board_id);
}

let subject = if subject.len() > SETTINGS.board.max_subject_length as usize {
String::from(&subject[..SETTINGS.board.max_subject_length as usize])
} else {
subject
};

let body = if body.len() > SETTINGS.board.max_post_length as usize {
String::from(&body[..SETTINGS.board.max_post_length as usize])
} else {
body
};

let character_id = character.id;

let player = match &character.player {
Some(player) => player.clone(),
None => return,
};

let pool = self.pool.clone();
tokio::spawn(async move {
let mut conn = pool.get_conn().await.unwrap();
let (recent_posts, total_posts) =
match get_board_post_counts(&mut conn, board_id, character_id).await {
Ok((recent_posts, total_posts)) => (recent_posts, total_posts),
Err(e) => {
error!("Failed to get board post counts: {}", e);
return;
}
};

let map = match player.get_map().await {
Ok(map) => map,
Err(e) => {
error!("Failed to get map: {}", e);
return;
}
};

if recent_posts >= SETTINGS.board.max_recent_posts
|| total_posts >= SETTINGS.board.max_user_posts
{
return map.open_board(player_id, board_id);
}

match insert_post(&mut conn, board_id, character_id, subject, body).await {
Ok(_) => {}
Err(e) => {
error!("Failed to insert post: {}", e);
}
}

map.open_board(player_id, board_id);
});
}
}

async fn get_board_post_counts(
conn: &mut Conn,
board_id: EOShort,
character_id: EOInt,
) -> Result<(EOInt, EOInt), Box<dyn std::error::Error>> {
let limit = if board_id == SETTINGS.board.admin_board as EOShort {
SETTINGS.board.admin_max_posts
} else {
SETTINGS.board.max_posts
};

let mut row: Row = match conn
.exec_first(
include_str!("../../sql/get_recent_post_count.sql"),
params! {
"board_id" => board_id,
"character_id" => character_id,
"post_time" => SETTINGS.board.recent_post_time,
},
)
.await
{
Ok(Some(row)) => row,
Ok(None) => {
return Err(Box::new(DataNotFoundError {
kind: "BoardPost".to_string(),
id: character_id as EOShort,
}))
}
Err(e) => {
error!("Failed to get recent post count: {}", e);
return Err(Box::new(e));
}
};

let recent_posts: EOInt = row.take("recent_posts").unwrap_or(0);

let mut row: Row = match conn
.exec_first(
include_str!("../../sql/get_total_post_count.sql"),
params! {
"board_id" => board_id,
"character_id" => character_id,
"limit" => limit,
},
)
.await
{
Ok(Some(row)) => row,
Ok(None) => {
return Err(Box::new(DataNotFoundError {
kind: "BoardPost".to_string(),
id: character_id as EOShort,
}))
}
Err(e) => {
error!("Failed to get recent post count: {}", e);
return Err(Box::new(e));
}
};

let total_posts: EOInt = row.take("total_posts").unwrap_or(0);

Ok((recent_posts, total_posts))
}

async fn insert_post(
conn: &mut Conn,
board_id: EOShort,
character_id: EOInt,
subject: String,
body: String,
) -> Result<(), Box<dyn std::error::Error>> {
match conn
.exec_drop(
include_str!("../../sql/create_board_post.sql"),
params! {
"board_id" => board_id,
"character_id" => character_id,
"subject" => subject,
"body" => body,
},
)
.await
{
Ok(_) => Ok(()),
Err(e) => Err(Box::new(e)),
}
}
2 changes: 1 addition & 1 deletion src/map/map/view_board_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::utils::get_board_tile_spec;
use super::Map;

impl Map {
pub async fn view_board_post(&mut self, player_id: EOShort, post_id: EOShort) {
pub async fn view_board_post(&self, player_id: EOShort, post_id: EOShort) {
let character = match self.characters.get(&player_id) {
Some(character) => character,
None => return,
Expand Down
8 changes: 8 additions & 0 deletions src/map/map_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ impl MapHandle {
});
}

pub fn post_board_message(&self, player_id: EOShort, subject: String, body: String) {
let _ = self.tx.send(Command::PostBoardMessage {
player_id,
subject,
body,
});
}

pub fn recover_npcs(&self) {
let _ = self.tx.send(Command::RecoverNpcs);
}
Expand Down
2 changes: 2 additions & 0 deletions src/sql/create_board_post.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSERT INTO BoardPost (board_id, character_id, subject, body)
VALUES (:board_id, :character_id, :subject, :body);
5 changes: 5 additions & 0 deletions src/sql/get_recent_post_count.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT COUNT(1) 'recent_posts'
FROM `BoardPost`
WHERE `board_id` = :board_id
AND `character_id` = :character_id
AND `created_at` > DATE_SUB(NOW(), INTERVAL :post_time MINUTE);
8 changes: 8 additions & 0 deletions src/sql/get_total_post_count.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT COUNT(1) 'total_posts'
FROM (
SELECT `id`, `character_id`
FROM `BoardPost`
WHERE `board_id` = :board_id
ORDER BY `id` DESC LIMIT :limit
) `a`
WHERE `a`.`character_id` = :character_id;
16 changes: 8 additions & 8 deletions src/utils/get_board_tile_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use eo::{data::EOShort, pubs::EmfTileSpec};

pub fn get_board_tile_spec(board_id: EOShort) -> Option<EmfTileSpec> {
match board_id {
0 => Some(EmfTileSpec::Board1),
1 => Some(EmfTileSpec::Board2),
2 => Some(EmfTileSpec::Board3),
3 => Some(EmfTileSpec::Board4),
4 => Some(EmfTileSpec::Board5),
5 => Some(EmfTileSpec::Board6),
6 => Some(EmfTileSpec::Board7),
7 => Some(EmfTileSpec::Board8),
1 => Some(EmfTileSpec::Board1),
2 => Some(EmfTileSpec::Board2),
3 => Some(EmfTileSpec::Board3),
4 => Some(EmfTileSpec::Board4),
5 => Some(EmfTileSpec::Board5),
6 => Some(EmfTileSpec::Board6),
7 => Some(EmfTileSpec::Board7),
8 => Some(EmfTileSpec::Board8),
_ => {
warn!("{} is not a valid board id", board_id);
None
Expand Down

0 comments on commit 5db24f4

Please sign in to comment.