Skip to content

Commit

Permalink
Merge pull request #2322 from MishimaHaruna/fix-allychat-leaveguild
Browse files Browse the repository at this point in the history
Fix allychat leaveguild
  • Loading branch information
MishimaHaruna authored Nov 16, 2018
2 parents 1db2ef5 + 6101698 commit 283d34b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 73 deletions.
48 changes: 19 additions & 29 deletions src/map/atcommand.c
Original file line number Diff line number Diff line change
Expand Up @@ -8996,9 +8996,7 @@ static void atcommand_channel_help(int fd, const char *command, bool can_create)
clif->message(fd, msg_fd(fd,1428));// - binds global chat to <channel name>, making anything you type in global be sent to the channel
safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1429),command);// -- %s unbind
clif->message(fd, atcmd_output);
clif->message(fd, msg_fd(fd,1430));// - unbinds your global chat from its attached channel (if binded)
safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1429),command);// -- %s unbind
clif->message(fd, atcmd_output);
clif->message(fd, msg_fd(fd,1430));// - unbinds your global chat from its attached channel (if bound)
if( can_create ) {
safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1456),command);// -- %s ban <channel name> <character name>
clif->message(fd, atcmd_output);
Expand All @@ -9023,7 +9021,6 @@ ACMD(channel)
{
struct channel_data *chan;
char subcmd[HCS_NAME_LENGTH], sub1[HCS_NAME_LENGTH], sub2[HCS_NAME_LENGTH], sub3[HCS_NAME_LENGTH];
unsigned char k = 0;
sub1[0] = sub2[0] = sub3[0] = '\0';

if (!*message || sscanf(message, "%19s %19s %19s %19s", subcmd, sub1, sub2, sub3) < 1) {
Expand Down Expand Up @@ -9060,7 +9057,7 @@ ACMD(channel)
} else if (strcmpi(subcmd,"list") == 0) {
// sub1 = list type; sub2 = unused; sub3 = unused
if (sub1[0] != '\0' && strcmpi(sub1,"colors") == 0) {
for (k = 0; k < channel->config->colors_count; k++) {
for (int k = 0; k < channel->config->colors_count; k++) {
safesnprintf(atcmd_output, sizeof(atcmd_output), "[ %s list colors ] : %s", command, channel->config->colors_name[k]);

clif->messagecolor_self(fd, channel->config->colors[k], atcmd_output);
Expand Down Expand Up @@ -9089,6 +9086,7 @@ ACMD(channel)
}
} else if (strcmpi(subcmd,"setcolor") == 0) {
// sub1 = channel name; sub2 = color; sub3 = unused
int k;
if (sub1[0] != '#') {
clif->message(fd, msg_fd(fd,1405));// Channel name must start with a '#'
return false;
Expand All @@ -9106,10 +9104,7 @@ ACMD(channel)
return false;
}

for (k = 0; k < channel->config->colors_count; k++) {
if (strcmpi(sub2, channel->config->colors_name[k]) == 0)
break;
}
ARR_FIND(0, channel->config->colors_count, k, strcmpi(sub2, channel->config->colors_name[k]) == 0);
if (k == channel->config->colors_count) {
safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1411), sub2);// Unknown color '%s'
clif->message(fd, atcmd_output);
Expand All @@ -9120,51 +9115,45 @@ ACMD(channel)
clif->message(fd, atcmd_output);
} else if (strcmpi(subcmd,"leave") == 0) {
// sub1 = channel name; sub2 = unused; sub3 = unused
int k;
if (sub1[0] != '#') {
clif->message(fd, msg_fd(fd,1405));// Channel name must start with a '#'
return false;
}
for (k = 0; k < sd->channel_count; k++) {
if (strcmpi(sub1+1,sd->channels[k]->name) == 0)
break;
}
if (k == sd->channel_count) {
ARR_FIND(0, VECTOR_LENGTH(sd->channels), k, strcmpi(sub1 + 1, VECTOR_INDEX(sd->channels, k)->name) == 0);
if (k == VECTOR_LENGTH(sd->channels)) {
safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1425),sub1);// You're not part of the '%s' channel
clif->message(fd, atcmd_output);
return false;
}
if (sd->channels[k]->type == HCS_TYPE_ALLY) {
do {
for (k = 0; k < sd->channel_count; k++) {
if (sd->channels[k]->type == HCS_TYPE_ALLY) {
channel->leave(sd->channels[k],sd);
break;
}
if (VECTOR_INDEX(sd->channels, k)->type == HCS_TYPE_ALLY) {
for (k = VECTOR_LENGTH(sd->channels) - 1; k >= 0; k--) {
// Loop downward to avoid issues when channel->leave() compacts the array
if (VECTOR_INDEX(sd->channels, k)->type == HCS_TYPE_ALLY) {
channel->leave(VECTOR_INDEX(sd->channels, k), sd);
}
} while (k != sd->channel_count);
}
} else {
channel->leave(sd->channels[k],sd);
channel->leave(VECTOR_INDEX(sd->channels, k), sd);
}
safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1426),sub1); // You've left the '%s' channel
clif->message(fd, atcmd_output);
} else if (strcmpi(subcmd,"bindto") == 0) {
// sub1 = channel name; sub2 = unused; sub3 = unused
int k;
if (sub1[0] != '#') {
clif->message(fd, msg_fd(fd,1405));// Channel name must start with a '#'
return false;
}

for (k = 0; k < sd->channel_count; k++) {
if (strcmpi(sub1+1,sd->channels[k]->name) == 0)
break;
}
if (k == sd->channel_count) {
ARR_FIND(0, VECTOR_LENGTH(sd->channels), k, strcmpi(sub1 + 1, VECTOR_INDEX(sd->channels, k)->name) == 0);
if (k == VECTOR_LENGTH(sd->channels)) {
safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1425),sub1);// You're not part of the '%s' channel
clif->message(fd, atcmd_output);
return false;
}

sd->gcbind = sd->channels[k];
sd->gcbind = VECTOR_INDEX(sd->channels, k);
safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1431),sub1); // Your global chat is now bound to the '%s' channel
clif->message(fd, atcmd_output);
} else if (strcmpi(subcmd,"unbind") == 0) {
Expand Down Expand Up @@ -9336,6 +9325,7 @@ ACMD(channel)
dbi_destroy(iter);
} else if (strcmpi(subcmd,"setopt") == 0) {
// sub1 = channel name; sub2 = option name; sub3 = value
int k;
const char* opt_str[3] = {
"None",
"JoinAnnounce",
Expand Down
53 changes: 15 additions & 38 deletions src/map/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ static void channel_join_sub(struct channel_data *chan, struct map_session_data
if (idb_put(chan->users, sd->status.char_id, sd))
return;

RECREATE(sd->channels, struct channel_data *, ++sd->channel_count);
sd->channels[sd->channel_count - 1] = chan;
VECTOR_ENSURE(sd->channels, 1, 1);
VECTOR_PUSH(sd->channels, chan);

if (!stealth && (chan->options&HCS_OPT_ANNOUNCE_JOIN)) {
char message[60];
Expand All @@ -329,7 +329,7 @@ static void channel_join_sub(struct channel_data *chan, struct map_session_data
}

/* someone is cheating, we kindly disconnect the bastard */
if (sd->channel_count > 200) {
if (VECTOR_LENGTH(sd->channels) > 200) {
sockt->eof(sd->fd);
}

Expand Down Expand Up @@ -411,32 +411,16 @@ static enum channel_operation_status channel_join(struct channel_data *chan, str
*/
static void channel_leave_sub(struct channel_data *chan, struct map_session_data *sd)
{
unsigned char i;
int i;

nullpo_retv(chan);
nullpo_retv(sd);
for (i = 0; i < sd->channel_count; i++) {
if (sd->channels[i] == chan) {
sd->channels[i] = NULL;
break;
}
}
if (i < sd->channel_count) {
unsigned char cursor = 0;
for (i = 0; i < sd->channel_count; i++) {
if (sd->channels[i] == NULL)
continue;
if (cursor != i) {
sd->channels[cursor] = sd->channels[i];
}
cursor++;
}
if (!(sd->channel_count = cursor)) {
aFree(sd->channels);
sd->channels = NULL;
}
ARR_FIND(0, VECTOR_LENGTH(sd->channels), i, VECTOR_INDEX(sd->channels, i) == chan);
if (i < VECTOR_LENGTH(sd->channels)) {
VECTOR_ERASE(sd->channels, i);
}
}

/**
* Leaves a channel.
*
Expand Down Expand Up @@ -475,14 +459,9 @@ static void channel_leave(struct channel_data *chan, struct map_session_data *sd
static void channel_quit(struct map_session_data *sd)
{
nullpo_retv(sd);
while (sd->channel_count > 0) {
while (VECTOR_LENGTH(sd->channels) > 0) {
// Loop downward to avoid unnecessary array compactions by channel_leave
struct channel_data *chan = sd->channels[sd->channel_count-1];

if (chan == NULL) {
sd->channel_count--;
continue;
}
struct channel_data *chan = VECTOR_LAST(sd->channels);

channel->leave(chan, sd);
}
Expand Down Expand Up @@ -585,13 +564,12 @@ static void channel_guild_leave_alliance(const struct guild *g_source, const str
*/
static void channel_quit_guild(struct map_session_data *sd)
{
unsigned char i;

nullpo_retv(sd);
for (i = 0; i < sd->channel_count; i++) {
struct channel_data *chan = sd->channels[i];
for (int i = VECTOR_LENGTH(sd->channels) - 1; i >= 0; i--) {
// Loop downward to avoid issues when channel->leave() compacts the array
struct channel_data *chan = VECTOR_INDEX(sd->channels, i);

if (chan == NULL || chan->type != HCS_TYPE_ALLY)
if (chan->type != HCS_TYPE_ALLY)
continue;

channel->leave(chan, sd);
Expand Down Expand Up @@ -855,15 +833,14 @@ static void do_final_channel(void)
{
struct DBIterator *iter = db_iterator(channel->db);
struct channel_data *chan;
unsigned char i;

for( chan = dbi_first(iter); dbi_exists(iter); chan = dbi_next(iter) ) {
channel->delete(chan);
}

dbi_destroy(iter);

for(i = 0; i < channel->config->colors_count; i++) {
for (int i = 0; i < channel->config->colors_count; i++) {
aFree(channel->config->colors_name[i]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/map/clif.c
Original file line number Diff line number Diff line change
Expand Up @@ -11089,8 +11089,8 @@ static void clif_parse_WisMessage(int fd, struct map_session_data *sd)

if (chan) {
int k;
ARR_FIND(0, sd->channel_count, k, sd->channels[k] == chan);
if (k < sd->channel_count || channel->join(chan, sd, "", true) == HCS_STATUS_OK) {
ARR_FIND(0, VECTOR_LENGTH(sd->channels), k, VECTOR_INDEX(sd->channels, k) == chan);
if (k < VECTOR_LENGTH(sd->channels) || channel->join(chan, sd, "", true) == HCS_STATUS_OK) {
channel->send(chan,sd,message);
} else {
clif->message(fd, msg_fd(fd,1402)); //You're not in that channel, type '@join <#channel_name>'
Expand Down
5 changes: 5 additions & 0 deletions src/map/guild.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@ static int guild_member_added(int guild_id, int account_id, int char_id, int fla
//Next line commented because it do nothing, look at guild_recv_info [LuzZza]
//clif->charnameupdate(sd); //Update display name [Skotlex]

// Makes the character join their respective guild's channel for #ally chat
if (channel->config->ally && channel->config->ally_autojoin) {
channel->join(g->channel, sd, "", true);
}

return 0;
}

Expand Down
8 changes: 6 additions & 2 deletions src/map/pc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,7 @@ static bool pc_authok(struct map_session_data *sd, int login_id2, time_t expirat
sd->bg_queue.client_has_bg_data = 0;
sd->bg_queue.type = 0;

VECTOR_INIT(sd->channels);
VECTOR_INIT(sd->script_queues);
VECTOR_INIT(sd->achievement); // Achievements [Smokexyz/Hercules]
VECTOR_INIT(sd->storage.item); // initialize storage item vector.
Expand Down Expand Up @@ -5837,8 +5838,11 @@ static int pc_setpos(struct map_session_data *sd, unsigned short map_index, int
vending->close(sd);
}

if (map->list[sd->bl.m].channel) {
channel->leave(map->list[sd->bl.m].channel,sd);
if (sd->mapindex != 0) {
// Only if the character is already on a map
if (map->list[sd->bl.m].channel) {
channel->leave(map->list[sd->bl.m].channel,sd);
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/map/pc.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,7 @@ END_ZEROED_BLOCK;
int shadowform_id;

/* [Ind/Hercules] */
struct channel_data **channels;
unsigned char channel_count;
VECTOR_DECL(struct channel_data *) channels;
struct channel_data *gcbind;
unsigned char fontcolor;
int fontcolor_tid;
Expand Down
1 change: 1 addition & 0 deletions src/map/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,7 @@ static int unit_free(struct block_list *bl, clr_type clrtype)
aFree(sd->instance);
sd->instance = NULL;
}
VECTOR_CLEAR(sd->channels);
VECTOR_CLEAR(sd->script_queues);
VECTOR_CLEAR(sd->achievement); // Achievement [Smokexyz/Hercules]
VECTOR_CLEAR(sd->storage.item);
Expand Down

0 comments on commit 283d34b

Please sign in to comment.