Skip to content

Commit

Permalink
modify js to support new API output
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashank Atreya committed Aug 14, 2024
1 parent 25d35f3 commit 3ea1c14
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 174 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/node_modules/
composer.lock
package-lock.json
.idea/
236 changes: 62 additions & 174 deletions resources/scripts/ext.dota2WebApi.toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,12 @@ $( function() {
return { winningTeam: winningTeam, matchID: matchID, text: text };
}

function getHeroesData() {
const data = JSON.parse( mw.message( 'dota2webapi-heroes.json' ).plain() ).heroes,
heroes = { };
for ( let i = 0; i < data.length; ++i ) {
heroes[ data[ i ].id ] = data[ i ].localized_name.replace( '\'', '' );
}
return heroes;
}

function processMatchForBracketDetails( vars, i ) {
// jQuery variables
const $status = $( '#insert-bracket-match-details-dialog .dota2webapi-result td.status:eq(' + i + ')' ),
$radiantTeam = $( '#insert-bracket-match-details-dialog .dota2webapi-result td.radiant-team:eq(' + i + ')' ),
$direTeam = $( '#insert-bracket-match-details-dialog .dota2webapi-result td.dire-team:eq(' + i + ')' ),
$matchData = $( '#insert-bracket-match-details-dialog .dota2webapi-result td.match-data:eq(' + i + ')' ),
heroes = getHeroesData();
$matchData = $( '#insert-bracket-match-details-dialog .dota2webapi-result td.match-data:eq(' + i + ')' );
$matchData.data( 'matchid', vars.matchIDs[ i ] );

$status.text( 'In progress...' )
Expand All @@ -162,7 +152,6 @@ $( function() {
data: {
action: 'dota2dbapi',
matchid: vars.matchIDs[ i ],
data: 'picks_bans|duration|radiant_win|teams|start_time',
pagename: mw.config.get( 'wgPageName' ),
format: 'json'
}
Expand All @@ -172,52 +161,36 @@ $( function() {
radiantBans, direBans,
end = '';

if ( data.dota2dbapi.isresult ) {
const result = data.dota2dbapi.result;
if ( data.dota2dbapi.error === undefined ) {
const result = data.dota2dbapi;

if ( result.picks_bans.radiant.pick_1 !== undefined ) {
if ( Object.keys( result.heroVeto ).length !== 0 ) {
radiantPicks = '';
let heroId;
for ( let j = 1; j <= 5; ++j ) {
radiantPicks += '|t{r}h' + j + '=';
if ( ( heroId = result.picks_bans.radiant[ 'pick_' + j ] ) !== null ) {
if ( heroes[ heroId ] !== undefined ) {
radiantPicks += heroes[ heroId ].toLowerCase();
}
}
for ( let j = 0; j < 5; ++j ) {
radiantPicks += `|t{r}h${j + 1}=`;
radiantPicks += result.heroVeto.radiant.picks[ j ].hero.toLowerCase();
}
radiantPicks += '\n';

radiantBans = '';
for ( let j = 1; j <= 7; ++j ) {
radiantBans += '|t{r}b' + j + '=';
if ( ( heroId = result.picks_bans.radiant[ 'ban_' + j ] ) !== null ) {
if ( heroes[ heroId ] !== undefined ) {
radiantBans += heroes[ heroId ].toLowerCase();
}
}
for ( let j = 0; j < 7; ++j ) {
radiantBans += `|t{r}b${j + 1}=`;
radiantBans += result.heroVeto.radiant.bans[ j ].hero.toLowerCase();

}
radiantBans += '\n';

direPicks = '';
for ( let j = 1; j <= 5; ++j ) {
direPicks += '|t{d}h' + j + '=';
if ( ( heroId = result.picks_bans.dire[ 'pick_' + j ] ) !== null ) {
if ( heroes[ heroId ] !== undefined ) {
direPicks += heroes[ heroId ].toLowerCase();
}
}
for ( let j = 0; j < 5; ++j ) {
direPicks += `|t{d}h${j + 1}=`;
direPicks += result.heroVeto.dire.picks[ j ].hero.toLowerCase();
}
direPicks += '\n';

direBans = '';
for ( let j = 1; j <= 7; ++j ) {
direBans += '|t{d}b' + j + '=';
if ( ( heroId = result.picks_bans.dire[ 'ban_' + j ] ) !== null ) {
if ( heroes[ heroId ] !== undefined ) {
direBans += heroes[ heroId ].toLowerCase();
}
}
for ( let j = 0; j < 7; ++j ) {
direBans += `|t{d}b${j + 1}=`;
direBans += result.heroVeto.dire.bans[ j ].hero.toLowerCase();
}
direBans += '\n';
} else {
Expand All @@ -228,23 +201,22 @@ $( function() {
}

end += '|length=';
end += result.duration;
end += result.length;
end += ' ';
end += '|win={w}';
end += '\n';
end += '}}\n';

vars.ok = true;
vars.teams.push( {
radiant: result.teams.radiant,
dire: result.teams.dire
radiant: result.teamRadiant,
dire: result.teamDire
} );

$radiantTeam.text( result.teams.radiant );
$direTeam.text( result.teams.dire );
$radiantTeam.text( result.teamRadiant );
$direTeam.text( result.teamDire );
$radiantTeam.addClass( 'radiant-side' );
$direTeam.addClass( 'dire-side' );
if ( result.radiant_win !== undefined ) {
if ( result.winner === 'radiant' ) {
$radiantTeam.addClass( 'winning-faction' );
$matchData.data( 'winningFaction', 'radiant' );
} else {
Expand All @@ -255,11 +227,11 @@ $( function() {
$matchData.data( 'direPicks', direPicks );
$matchData.data( 'radiantBans', radiantBans );
$matchData.data( 'direBans', direBans );
$matchData.data( 'startTime', result.start_time );
$matchData.data( 'startTime', result.startTime );
$matchData.data( 'wikitextEnd', end );
$status.text( 'Success' );
} else {
$status.text( data.dota2dbapi.result.error );
$status.text( data.dota2dbapi.error );
}

$status.removeClass( 'loading' );
Expand Down Expand Up @@ -475,10 +447,10 @@ $( function() {
text += '|winner=' + winningTeam + '}}\n';

text += $( '#insert-full-match-details-dialog .dota2webapi-result .match-' + params.row + ' .match-data' ).data( 'wikitextStart' );
text += '|team1Picks=' + team1Picks.join( ',' ) + '\n';
text += '|team2Picks=' + team2Picks.join( ',' ) + '\n';
text += '|team1Bans=' + team1Bans.join( ',' ) + '\n';
text += '|team2Bans=' + team2Bans.join( ',' ) + '\n';
text += '|team1Picks=' + team1Picks.map( ( hero ) => hero.hero ).join( ',' ) + '\n';
text += '|team2Picks=' + team2Picks.map( ( hero ) => hero.hero ).join( ',' ) + '\n';
text += '|team1Bans=' + team1Bans.map( ( hero ) => hero.hero ).join( ',' ) + '\n';
text += '|team2Bans=' + team2Bans.map( ( hero ) => hero.hero ).join( ',' ) + '\n';
text += '}}\n';
text += $( '#insert-full-match-details-dialog .dota2webapi-result .match-' + params.row + ' .match-data' ).data( 'wikitextMiddle' );
text += team1Roster;
Expand All @@ -488,39 +460,12 @@ $( function() {
return { winningTeam: winningTeam, matchID: matchID, text: text };
}

function getItemsData() {
const data = JSON.parse( mw.message( 'dota2webapi-items.json' ).plain() ).items, items = { };
let itemName;
for ( let i = 0; i < data.length; ++i ) {
itemName = data[ i ].name.replace( '_', ' ' );
if ( itemName.indexOf( 'recipe' ) === 0 ) {
itemName = 'recipe';
}
items[ data[ i ].id ] = itemName;
}

items[ 18 ] = 'band of elvenskin';
items[ 26 ] = 'morbid mask';
items[ 106 ] = 'necro1';
items[ 141 ] = 'daedalus';
items[ 149 ] = 'crystalys';
items[ 152 ] = 'shadow blade';
items[ 185 ] = 'drums';
items[ 193 ] = 'necro2';
items[ 194 ] = 'necro3';
items[ 196 ] = 'diffusal2';

return items;
}

function processMatchForFullDetails( vars, i ) {
// jQuery variables
const $status = $( '#insert-full-match-details-dialog .dota2webapi-result td.status:eq(' + i + ')' ),
$radiantTeam = $( '#insert-full-match-details-dialog .dota2webapi-result td.radiant-team:eq(' + i + ')' ),
$direTeam = $( '#insert-full-match-details-dialog .dota2webapi-result td.dire-team:eq(' + i + ')' ),
$matchData = $( '#insert-full-match-details-dialog .dota2webapi-result td.match-data:eq(' + i + ')' ),
heroes = getHeroesData(),
items = getItemsData();
$matchData = $( '#insert-full-match-details-dialog .dota2webapi-result td.match-data:eq(' + i + ')' );
$matchData.data( 'matchid', vars.matchIDs[ i ] );

$status.text( 'In progress...' )
Expand All @@ -532,7 +477,6 @@ $( function() {
data: {
action: 'dota2dbapi',
matchid: vars.matchIDs[ i ],
data: 'picks_bans|kills_deaths|players|radiant_win|teams|start_time',
pagename: mw.config.get( 'wgPageName' ),
format: 'json'
}
Expand All @@ -543,143 +487,87 @@ $( function() {
const middle = '{{Match series scoreboard header}}\n';

start += '|matchID=' + vars.matchIDs[ i ] + ' ';
if ( data.dota2dbapi.isresult ) {
const result = data.dota2dbapi.result;
if ( data.dota2dbapi.error === undefined ) {
const result = data.dota2dbapi;

start += '|VOD=';
start += '\n';
// radiantPicks = "{{MatchSeries/Picks";
radiantPicks = result.heroVeto.radiant.picks;
// radiantPicks += "}}\n";

if ( result.picks_bans.radiant.pick_1 !== undefined ) {
// radiantPicks = "{{MatchSeries/Picks";
radiantPicks = [ ];
let heroId;
for ( let j = 1; j <= 5; ++j ) {
if ( ( heroId = result.picks_bans.radiant[ 'pick_' + j ] ) !== null ) {
radiantPicks.push( heroes[ heroId ] !== undefined ? heroes[ heroId ].toLowerCase() : '' );
} else {
radiantPicks.push( '' );
}
}
// radiantPicks += "}}\n";

// direPicks = "{{MatchSeries/Picks";
direPicks = [ ];
for ( let j = 1; j <= 5; ++j ) {
if ( ( heroId = result.picks_bans.dire[ 'pick_' + j ] ) !== null ) {
direPicks.push( heroes[ heroId ] !== undefined ? heroes[ heroId ].toLowerCase() : '' );
} else {
direPicks.push( '' );
}
}
// direPicks += "}}\n";
// direPicks = "{{MatchSeries/Picks";
direPicks = result.heroVeto.dire.picks;
// for ( let j = 1; j <= 5; ++j ) {
// direPicks += "}}\n";

// radiantBans = "{{MatchSeries/Bans";
radiantBans = [ ];
for ( let j = 1; j <= 7; ++j ) {
if ( ( heroId = result.picks_bans.radiant[ 'ban_' + j ] ) !== null ) {
radiantBans.push( heroes[ heroId ] !== undefined ? heroes[ heroId ].toLowerCase() : '' );
} else {
radiantBans.push( '' );
}
}
// radiantBans += "}}\n";
// radiantBans = "{{MatchSeries/Bans";
radiantBans = result.heroVeto.radiant.bans;
// radiantBans += "}}\n";

// direBans = "{{MatchSeries/Bans";
direBans = [ ];
for ( let j = 1; j <= 7; ++j ) {
if ( ( heroId = result.picks_bans.dire[ 'ban_' + j ] ) !== null ) {
direBans.push( heroes[ heroId ] !== undefined ? heroes[ heroId ].toLowerCase() : '' );
} else {
direBans.push( '' );
}
}
// direBans += "}}\n";
} else {
radiantPicks = [ '', '', '', '', '' ];
direPicks = [ '', '', '', '', '' ];
radiantBans = [ '', '', '', '', '', '' ];
direBans = [ '', '', '', '', '', '' ];
}
// direBans = "{{MatchSeries/Bans";
direBans = result.heroVeto.dire.bans;

// start += '|radiantKills=' + result.kills.radiant + ' ';
// start += '|direKills=' + result.kills.dire + '\n';

const factions = { R: 'radiant', D: 'dire' };
const factionRosters = { radiant: '', dire: '' };
for ( const t in factions ) {
let factionRoster = '{{Match series faction|faction=' + factions[ t ] + '|kills=' + result.kills[ factions[ t ] ] + '}}\n';
for ( let j = 1; j <= 5; ++j ) {
const player = result.players[ factions[ t ] ][ 'player_' + j ];
let factionRoster = '{{Match series faction|faction=' + factions[ t ] + '|kills=' + result[ factions[ t ] + 'Score' ] + '}}\n';
for ( let j = 0; j < 5; ++j ) {
const player = result[ factions[ t ] ].players[ j ];
factionRoster += '{{Match series player|player=';
factionRoster += player.name + ' ';
factionRoster += '|hero=' + ( heroes[ player.hero ] !== undefined ? heroes[ player.hero ].toLowerCase() : '' ) + ' ';
factionRoster += '|hero=' + player.heroName + ' ';
factionRoster += '|lvl=' + player.level;
factionRoster += '|k=' + player.kills;
factionRoster += '|d=' + player.deaths;
factionRoster += '|a=' + player.assists;
factionRoster += '|lh=' + player.last_hits;
factionRoster += '|lh=' + player.lastHits;
factionRoster += '|den=' + player.denies;
factionRoster += '|gpm=' + player.gold_per_min;
factionRoster += '|xpm=' + player.xp_per_min;
factionRoster += '|items=';
for ( let k = 1; k <= 6; ++k ) {
if ( items[ player[ 'item_' + k ] ] !== undefined ) {
factionRoster += items[ player[ 'item_' + k ] ];
}
if ( k < 6 ) {
factionRoster += ',';
}
}
if ( player.hero === 'Lone Druid' ) {
factionRoster += '|bearitems=';
for ( let k = 1; k <= 6; ++k ) {
if ( items[ player[ 'bearitem_' + k ] ] !== undefined ) {
factionRoster += items[ player[ 'bearitem_' + k ] ];
}
if ( k < 6 ) {
factionRoster += ',';
}
}
}
factionRoster += '|gpm=' + player.goldPerMinute;
factionRoster += '|xpm=' + player.xpPerMinute;
factionRoster += '|items=' + player.items;
// @TODO LONE DRUID BEAR ITEMS
factionRoster += '}}\n';
}
factionRosters[ factions[ t ] ] = factionRoster;
}

end += '{{Match series stats end}}\n';

vars.ok = true;
vars.teams.push( {
radiant: result.teams.radiant,
dire: result.teams.dire
radiant: result.teamRadiant,
dire: result.teamDire
} );

$radiantTeam.text( result.teams.radiant );
$direTeam.text( result.teams.dire );
$radiantTeam.text( result.teamRadiant );
$direTeam.text( result.teamDire );
$radiantTeam.addClass( 'radiant-side' );
$direTeam.addClass( 'dire-side' );
if ( result.radiant_win !== undefined ) {
if ( result.winner === 'radiant' ) {
$radiantTeam.addClass( 'winning-faction' );
$matchData.data( 'winningFaction', 'radiant' );
} else {
$direTeam.addClass( 'winning-faction' );
$matchData.data( 'winningFaction', 'dire' );
}
$matchData.data( 'radiantScore', result.deaths.dire );
$matchData.data( 'direScore', result.deaths.radiant );
$matchData.data( 'radiantScore', result.radiantScore );
$matchData.data( 'direScore', result.direScore );
$matchData.data( 'radiantPicks', radiantPicks );
$matchData.data( 'direPicks', direPicks );
$matchData.data( 'radiantBans', radiantBans );
$matchData.data( 'direBans', direBans );
$matchData.data( 'radiantRoster', factionRosters.radiant );
$matchData.data( 'direRoster', factionRosters.dire );
$matchData.data( 'startTime', result.start_time );
$matchData.data( 'startTime', result.startTime );
$matchData.data( 'wikitextStart', start );
$matchData.data( 'wikitextMiddle', middle );
$matchData.data( 'wikitextEnd', end );
$status.text( 'Success' );
} else {
$status.text( data.dota2dbapi.result.error );
$status.text( data.dota2dbapi.error );
}

$status.removeClass( 'loading' );
Expand Down

0 comments on commit 3ea1c14

Please sign in to comment.