Fix logic in sending custom and user selected icons

- fix sending empty icons
- fix custom icons overriding user selected icons
- code style
This commit is contained in:
Yaki Khadafi 2017-03-07 16:25:36 +03:00
parent 50c481dacb
commit 577ccf75ab
5 changed files with 88 additions and 84 deletions

View file

@ -2075,9 +2075,8 @@ namespace pvpgn
clienttag_uint = tag_case_str_to_uint(clienttag);
// custom stats
if (prefs_get_custom_icons() == 1)
if (prefs_get_custom_icons() == 1 && customicons_allowed_by_client(clienttag_uint))
{
const char *text;

View file

@ -2535,19 +2535,6 @@ namespace pvpgn
// allow set icon to a user directly from the database (override default tag always if not null)
if (usericon = account_get_user_icon(account, clienttag))
std::sprintf(revtag, "%s", usericon);
// if custom_icons is enabled then set a custom client tag by player rating
if (prefs_get_custom_icons() == 1)
{
t_icon_info * icon;
// do not override userselectedicon if it's not null
if (!usericon && (icon = customicons_get_icon_by_account(account, clienttag)))
std::snprintf(revtag, sizeof revtag, "%s", icon->icon_code);
// FIXME: it replaces tag with icon on a client side for all clients (HarpyWar)
std::strcpy(playerinfo, revtag);
}
else if (clienttag == CLIENTTAG_BNCHATBOT_UINT)
{
std::strcpy(playerinfo, revtag); /* FIXME: what to return here? */
@ -2646,6 +2633,14 @@ namespace pvpgn
else
std::strcpy(playerinfo, revtag); /* open char */
// if custom_icons is enabled then set a custom client tag by player rating
// do not override user selected icon if it's not null
if (!usericon && prefs_get_custom_icons() == 1 && customicons_allowed_by_client(clienttag))
{
if (t_icon_info * icon = customicons_get_icon_by_account(account, clienttag))
std::snprintf(playerinfo, sizeof playerinfo, "%s", icon->icon_code);
}
#ifdef WITH_LUA
// change icon info from Lua
if (const char * iconinfo = lua_handle_user_icon((t_connection*)c, playerinfo))
@ -3735,35 +3730,19 @@ namespace pvpgn
while ((*clantag_str) == 0) clantag_str++;
}
// allow set icon to a user directly from the database (override default icon always if not null)
const char* usericon = account_get_user_icon(account, clienttag);
// if custom stats is enabled then set a custom client icon by player rating
if (prefs_get_custom_icons() == 1)
// allow set icon to a user directly from the database (override default icon always if not null)
if (usericon)
{
t_icon_info * icon;
bool to_free = false;
// do not override userselectedicon if it's not null
if (!usericon && (icon = customicons_get_icon_by_account(account, clienttag)))
{
usericon = xstrdup(icon->icon_code);
to_free = true;
}
acctlevel = 0;
if (clantag)
std::sprintf(tempplayerinfo, "%s %s %u %s", revtag, usericon, acctlevel, clantag_str);
else
std::sprintf(tempplayerinfo, "%s %s %u", revtag, usericon, acctlevel);
if (to_free == true)
{
xfree((void*)usericon);
}
eventlog(eventlog_level_info, __FUNCTION__, "[{}] {} using user-selected icon [{}]", conn_get_socket(c), revtag, usericon);
}
// default icon "WAR3" or "W3XP"
else if (acctlevel == 0 && !usericon) {
else if (acctlevel == 0)
{
if (clantag)
std::sprintf(tempplayerinfo, "%s %s 0 %s", revtag, revtag, clantag_str);
else
@ -3773,21 +3752,26 @@ namespace pvpgn
// display race icon with a level number
else
{
if (!usericon) {
if (clantag)
std::sprintf(tempplayerinfo, "%s %1u%c3W %u %s", revtag, raceiconnumber, raceicon, acctlevel, clantag_str);
else
std::sprintf(tempplayerinfo, "%s %1u%c3W %u", revtag, raceiconnumber, raceicon, acctlevel);
eventlog(eventlog_level_info, __FUNCTION__, "[{}] {} using generated icon [{}{}3W]", conn_get_socket(c), revtag, raceiconnumber, raceicon);
}
// if custom stats is enabled then set a custom client icon by player rating
// do not override user selected icon if any
if (!usericon && prefs_get_custom_icons() == 1 && customicons_allowed_by_client(clienttag))
{
if (t_icon_info* icon = customicons_get_icon_by_account(account, clienttag))
{
if (clantag)
std::sprintf(tempplayerinfo, "%s %1u%c3W %u %s", revtag, raceiconnumber, raceicon, acctlevel, clantag_str);
std::sprintf(tempplayerinfo, "%s %s %u %s", revtag, icon->icon_code, 0, clantag_str);
else
std::sprintf(tempplayerinfo, "%s %1u%c3W %u", revtag, raceiconnumber, raceicon, acctlevel);
eventlog(eventlog_level_info, __FUNCTION__, "[{}] {} using generated icon [{}{}3W]", conn_get_socket(c), revtag, raceiconnumber, raceicon);
}
else {
if (clantag)
std::sprintf(tempplayerinfo, "%s %s %u %s", revtag, usericon, acctlevel, clantag_str);
else
std::sprintf(tempplayerinfo, "%s %s %u", revtag, usericon, acctlevel);
eventlog(eventlog_level_info, __FUNCTION__, "[{}] {} using user-selected icon [{}]", conn_get_socket(c), revtag, usericon);
std::sprintf(tempplayerinfo, "%s %s %u", revtag, icon->icon_code, 0);
}
}
#ifdef WITH_LUA
// change icon info from Lua
if (const char * iconinfo = lua_handle_user_icon(c, tempplayerinfo))

View file

@ -496,16 +496,8 @@ namespace pvpgn
packet_set_type(rpacket, SERVER_FINDANONGAME_ICONREPLY);
bn_int_set(&rpacket->u.server_findanongame_iconreply.count, bn_int_get(packet->u.client_findanongame_inforeq.count));
bn_byte_set(&rpacket->u.server_findanongame_iconreply.option, CLIENT_FINDANONGAME_GET_ICON);
if (prefs_get_custom_icons() == 1)
{
// get current custom icon
t_icon_info * icon;
if (icon = customicons_get_icon_by_account(acc, clienttag))
std::memcpy(&rpacket->u.server_findanongame_iconreply.curricon, icon->icon_code, 4);
}
else if ((uicon = account_get_user_icon(acc, clienttag)))
if (uicon = account_get_user_icon(acc, clienttag))
{
std::memcpy(&rpacket->u.server_findanongame_iconreply.curricon, uicon, 4);
}
@ -516,6 +508,18 @@ namespace pvpgn
std::memcpy(&rpacket->u.server_findanongame_iconreply.curricon, user_icon, 4);
}
// if custom stats is enabled then set a custom client icon by player rating
// do not override user selected icon if any
bool assignedCustomIcon = false;
if (!uicon && prefs_get_custom_icons() == 1 && customicons_allowed_by_client(clienttag))
{
if (t_icon_info * icon = customicons_get_icon_by_account(acc, clienttag))
{
assignedCustomIcon = true;
std::memcpy(&rpacket->u.server_findanongame_iconreply.curricon, icon->icon_code, 4);
}
}
bn_byte_set(&rpacket->u.server_findanongame_iconreply.table_width, table_width);
bn_byte_set(&rpacket->u.server_findanongame_iconreply.table_size, table_width*table_height);
for (j = 0; j < table_height; j++){
@ -527,32 +531,24 @@ namespace pvpgn
tempicon.icon_code[2] = '3';
tempicon.icon_code[3] = 'W';
tempicon.portrait_code = (account_icon_to_profile_icon(tempicon.icon_code, acc, clienttag));
if (i <= 4){
if (i <= 4)
{
//Building the icon for the races
bn_short_set(&tempicon.required_wins, icon_req_race_wins);
if (account_get_racewins(acc, race[i], clienttag) >= icon_req_race_wins) {
if (prefs_get_custom_icons() == 1)
tempicon.client_enabled = 0;
else
tempicon.client_enabled = 1;
}
else{
if (assignedCustomIcon || account_get_racewins(acc, race[i], clienttag) < icon_req_race_wins)
tempicon.client_enabled = 0;
}
else
tempicon.client_enabled = 1;
}
else{
else
{
//Building the icon for the tourney
icon_req_tourney_wins = anongame_infos_get_ICON_REQ_TOURNEY(j + 1);
bn_short_set(&tempicon.required_wins, icon_req_tourney_wins);
if (account_get_racewins(acc, race[i], clienttag) >= icon_req_tourney_wins) {
if (prefs_get_custom_icons() == 1)
tempicon.client_enabled = 0;
else
tempicon.client_enabled = 1;
}
else{
if (assignedCustomIcon || account_get_racewins(acc, race[i], clienttag) < icon_req_tourney_wins)
tempicon.client_enabled = 0;
}
else
tempicon.client_enabled = 1;
}
packet_append_data(rpacket, &tempicon, sizeof(tempicon));
}
@ -571,13 +567,13 @@ namespace pvpgn
// Modified by aancw 16/12/2014
unsigned int desired_icon;
char user_icon[5];
t_account * account;
// disable with custom icons
if (prefs_get_custom_icons() == 1)
{
t_account * account = conn_get_account(c);
t_clienttag clienttag = conn_get_clienttag(c);
// do nothing when custom icon enabled and exists
if (prefs_get_custom_icons() == 1 && customicons_allowed_by_client(clienttag) && customicons_get_icon_by_account(account, clienttag))
return 0;
}
/*FIXME: In this case we do not get a 'count' but insted of it we get the icon
that the client wants to set.'W3H2' for an example. For now it is ok, since they share
@ -593,8 +589,6 @@ namespace pvpgn
eventlog(eventlog_level_info, __FUNCTION__, "[{}] Set icon packet to ICON [{}]", conn_get_socket(c), user_icon);
}
account = conn_get_account(c);
// ICON SWITCH HACK PROTECTION
if (check_user_icon(account, user_icon) == 0)
{
@ -603,7 +597,7 @@ namespace pvpgn
//conn_set_state(c,conn_state_destroy); // dont kill user session
}
account_set_user_icon(conn_get_account(c), conn_get_clienttag(c), user_icon);
account_set_user_icon(account, clienttag, user_icon);
//FIXME: Still need a way to 'refresh the user/channel'
//_handle_rejoin_command(conn_get_account(c),"");
/* ??? channel_update_userflags() */

View file

@ -555,6 +555,32 @@ namespace pvpgn
}
extern bool customicons_allowed_by_client(t_clienttag clienttag)
{
switch (clienttag)
{
// Starcraft variations
case CLIENTTAG_STARCRAFT_UINT:
case CLIENTTAG_BROODWARS_UINT:
case CLIENTTAG_SHAREWARE_UINT:
case CLIENTTAG_STARJAPAN_UINT:
// Diablo
case CLIENTTAG_DIABLORTL_UINT:
case CLIENTTAG_DIABLOSHR_UINT:
// Warcraft 2
case CLIENTTAG_WARCIIBNE_UINT:
// Warcraft 3
case CLIENTTAG_WARCRAFT3_UINT:
case CLIENTTAG_WAR3XP_UINT:
return true;
default:
break;
}
return false;
}
/* Format stats text, with attributes from a storage, and output text to a user */
extern const char * customicons_get_stats_text(t_account * account, t_clienttag clienttag)
{
@ -632,7 +658,7 @@ namespace pvpgn
// get attribute field name from a storage
if (!(attr_key = _find_attr_key((char*)clienttag_str)))
{
eventlog(eventlog_level_trace, __FUNCTION__, "could not find attr_key in iconset for tag {}", clienttag_str);
eventlog(eventlog_level_trace, __FUNCTION__, "no custom iconset defined for client tag {}", clienttag_str);
return NULL;
}

View file

@ -71,6 +71,7 @@ namespace pvpgn
extern std::string customicons_stash_get_list(t_clienttag clienttag, bool return_alias = false);
extern int prefs_get_custom_icons();
extern bool customicons_allowed_by_client(t_clienttag clienttag);
extern t_icon_info * customicons_get_icon_by_account(t_account * account, t_clienttag clienttag);
extern const char * customicons_get_stats_text(t_account * account, t_clienttag clienttag);
extern t_icon_info * customicons_get_icon_by_rating(int rating, char * clienttag);