Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing guild replies #186

Merged
merged 6 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/map/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ pub enum Command {
GetRidAndSize {
respond_to: oneshot::Sender<([i32; 2], i32)>,
},
GetPlayerCount {
respond_to: oneshot::Sender<usize>,
filter: fn(&Character) -> bool,
},
AwardExperience {
player_id: i32,
amount: i32,
Expand Down
4 changes: 3 additions & 1 deletion src/map/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ impl Map {
Command::GetRidAndSize { respond_to } => {
self.get_rid_and_size(respond_to);
}

Command::GetPlayerCount { respond_to, filter } => {
let _ = respond_to.send(self.characters.values().filter(|c| filter(c)).count());
}
Command::AwardExperience { player_id, amount } => {
self.award_experience(player_id, amount)
}
Expand Down
7 changes: 6 additions & 1 deletion src/map/map/auto_pickup/list_auto_pickup_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ impl Map {
let items = character
.auto_pickup_items
.iter()
.filter_map(|item_id| ITEM_DB.items.get(*item_id as usize - 1).map(|item| item.name.as_str()))
.filter_map(|item_id| {
ITEM_DB
.items
.get(*item_id as usize - 1)
.map(|item| item.name.as_str())
})
.collect::<Vec<_>>();

self.show_info_box(
Expand Down
17 changes: 16 additions & 1 deletion src/map/map/guild/join_guild.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use eolib::protocol::net::{server::GuildAgreeServerPacket, PacketAction, PacketFamily};
use eolib::protocol::net::{
server::{GuildAgreeServerPacket, GuildReply},
PacketAction, PacketFamily,
};

use super::super::Map;

Expand Down Expand Up @@ -35,5 +38,17 @@ impl Map {
},
);
}

let character = match self.characters.get(&recruiter_id) {
Some(character) => character,
None => return,
};

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

recruiter.send_guild_reply(GuildReply::Accepted);
}
}
94 changes: 57 additions & 37 deletions src/map/map/guild/request_to_join_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,75 @@ impl Map {
None => return,
};

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

if character.guild_tag.is_some() {
send_reply!(player, GuildReply::AlreadyMember);
return;
}

let player = match character.player.as_ref() {
Some(player) => player,
None => return,
};
let recruiter = self
.characters
.values()
.find(|c| c.name == recruiter_name)
.map(|character| character.to_owned());

let recruiter = match self.characters.values().find(|c| c.name == recruiter_name) {
Some(character) => character,
None => {
send_reply!(player, GuildReply::RecruiterNotHere);
return;
let world = self.world.to_owned();
let character_name = character.name.to_owned();

tokio::spawn(async move {
match world.get_character_by_name(recruiter_name.to_owned()).await {
Ok(_) => {}
_ => {
send_reply!(player, GuildReply::RecruiterOffline);
return;
}
}
};

if recruiter.guild_tag.is_none() {
send_reply!(player, GuildReply::RecruiterWrongGuild);
return;
}
let recruiter = match recruiter {
Some(character) => character,
None => {
send_reply!(player, GuildReply::RecruiterNotHere);
return;
}
};

if let Some(tag) = &recruiter.guild_tag {
if *tag != guild_tag {
if recruiter.guild_tag.is_none() {
send_reply!(player, GuildReply::RecruiterWrongGuild);
return;
}
}

if recruiter.guild_rank.unwrap_or(9) > 1 {
send_reply!(player, GuildReply::NotRecruiter);
return;
}
if let Some(tag) = &recruiter.guild_tag {
if *tag != guild_tag {
send_reply!(player, GuildReply::RecruiterWrongGuild);
return;
}
}

if let Some(player) = recruiter.player.as_ref() {
player.set_interact_player_id(Some(player_id));
player.send(
PacketAction::Reply,
PacketFamily::Guild,
&GuildReplyServerPacket {
reply_code: GuildReply::JoinRequest,
reply_code_data: Some(GuildReplyServerPacketReplyCodeData::JoinRequest(
GuildReplyServerPacketReplyCodeDataJoinRequest {
player_id,
name: capitalize(&character.name),
},
)),
},
);
}
if recruiter.guild_rank.unwrap_or(9) > 1 {
send_reply!(player, GuildReply::NotRecruiter);
return;
}

if let Some(player) = recruiter.player.as_ref() {
player.set_interact_player_id(Some(player_id));
player.send(
PacketAction::Reply,
PacketFamily::Guild,
&GuildReplyServerPacket {
reply_code: GuildReply::JoinRequest,
reply_code_data: Some(GuildReplyServerPacketReplyCodeData::JoinRequest(
GuildReplyServerPacketReplyCodeDataJoinRequest {
player_id,
name: capitalize(&character_name),
},
)),
},
);
}
});
}
}
5 changes: 4 additions & 1 deletion src/map/map/utils/attack_npc_replies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ impl Map {
npc_index,
damage: damage_dealt,
hp_percentage: npc.get_hp_percentage(),
caster_tp: self.characters.get(&player_id).map(|character| character.tp),
caster_tp: self
.characters
.get(&player_id)
.map(|character| character.tp),
kill_steal_protection: Some(if protected {
NpcKillStealProtectionState::Protected
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/map/map_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@ impl MapHandle {
rx.await.unwrap()
}

pub async fn get_player_count(&self, filter: fn(&Character) -> bool) -> usize {
let (tx, rx) = oneshot::channel();
let _ = self.tx.send(Command::GetPlayerCount {
respond_to: tx,
filter,
});
rx.await.unwrap()
}

pub fn award_experience(&self, player_id: i32, amount: i32) {
let _ = self.tx.send(Command::AwardExperience { player_id, amount });
}
Expand Down
8 changes: 7 additions & 1 deletion src/player/player/handlers/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ impl Player {
if SETTINGS.guild.min_players == 1 {
player.send_guild_reply(GuildReply::CreateAddConfirm);
} else {
let player_count = map.get_player_count(|c| c.guild_tag.is_none()).await;
if player_count < SETTINGS.guild.min_players {
player.send_guild_reply(GuildReply::NoCandidates);
return;
}

player.send_guild_reply(GuildReply::CreateBegin);
map.send_guild_create_requests(
player_id,
Expand Down Expand Up @@ -1400,7 +1406,7 @@ async fn update_guild_ranks(player: &PlayerHandle, tag: &str, ranks: [String; 9]
}
}

player.send_guild_reply(GuildReply::Updated);
player.send_guild_reply(GuildReply::RanksUpdated);
}

async fn get_guild_description(conn: &mut Conn, tag: &str) -> String {
Expand Down