2022-07-16 21:00:02 +00:00
// TODO: Refactor this ugly shit who wrote this lmao
2022-08-24 14:30:30 +00:00
function vote ( type , id , dir ) {
2022-07-16 21:00:02 +00:00
const upvotes = document . getElementsByClassName ( type + '-' + id + '-up' ) ;
const downvotes = document . getElementsByClassName ( type + '-' + id + '-down' ) ;
const scoretexts = document . getElementsByClassName ( type + '-score-' + id ) ;
for ( let i = 0 ; i < upvotes . length ; i ++ ) {
2022-09-04 23:15:37 +00:00
2022-07-16 21:00:02 +00:00
const upvote = upvotes [ i ]
const downvote = downvotes [ i ]
const scoretext = scoretexts [ i ]
const score = Number ( scoretext . textContent ) ;
if ( dir == "1" ) {
if ( upvote . classList . contains ( 'active' ) ) {
upvote . classList . remove ( 'active' )
upvote . classList . remove ( 'active-anim' )
scoretext . textContent = score - 1
votedirection = "0"
} else if ( downvote . classList . contains ( 'active' ) ) {
upvote . classList . add ( 'active' )
upvote . classList . add ( 'active-anim' )
downvote . classList . remove ( 'active' )
downvote . classList . remove ( 'active-anim' )
scoretext . textContent = score + 2
votedirection = "1"
} else {
upvote . classList . add ( 'active' )
upvote . classList . add ( 'active-anim' )
scoretext . textContent = score + 1
votedirection = "1"
}
if ( upvote . classList . contains ( 'active' ) ) {
scoretext . classList . add ( 'score-up' )
scoretext . classList . add ( 'score-up-anim' )
scoretext . classList . remove ( 'score-down' )
scoretext . classList . remove ( 'score' )
} else if ( downvote . classList . contains ( 'active' ) ) {
scoretext . classList . add ( 'score-down' )
scoretext . classList . remove ( 'score-up' )
scoretext . classList . remove ( 'score-up-anim' ) ;
scoretext . classList . remove ( 'score' )
} else {
scoretext . classList . add ( 'score' )
scoretext . classList . remove ( 'score-up' )
scoretext . classList . remove ( 'score-up-anim' ) ;
scoretext . classList . remove ( 'score-down' )
}
}
else {
if ( downvote . classList . contains ( 'active' ) ) {
downvote . classList . remove ( 'active' )
downvote . classList . remove ( 'active-anim' )
scoretext . textContent = score + 1
votedirection = "0"
} else if ( upvote . classList . contains ( 'active' ) ) {
downvote . classList . add ( 'active' )
downvote . classList . add ( 'active-anim' )
upvote . classList . remove ( 'active' )
upvote . classList . remove ( 'active-anim' )
scoretext . textContent = score - 2
votedirection = "-1"
} else {
downvote . classList . add ( 'active' )
downvote . classList . add ( 'active-anim' )
scoretext . textContent = score - 1
votedirection = "-1"
}
if ( upvote . classList . contains ( 'active' ) ) {
scoretext . classList . add ( 'score-up' )
scoretext . classList . add ( 'score-up-anim' )
scoretext . classList . remove ( 'score-down' )
scoretext . classList . remove ( 'score' )
} else if ( downvote . classList . contains ( 'active' ) ) {
scoretext . classList . add ( 'score-down' )
scoretext . classList . remove ( 'score-up-anim' )
scoretext . classList . remove ( 'score-up' )
scoretext . classList . remove ( 'score' )
} else {
scoretext . classList . add ( 'score' )
scoretext . classList . remove ( 'score-up' )
scoretext . classList . remove ( 'score-down' )
scoretext . classList . remove ( 'score-up-anim' )
}
}
}
2022-10-14 11:31:02 +00:00
const xhr = createXhrWithFormKey ( "/vote/" + type . replace ( '-mobile' , '' ) + "/" + id + "/" + votedirection ) ;
2022-10-14 12:01:11 +00:00
xhr [ 0 ] . send ( xhr [ 1 ] ) ;
2022-07-16 21:00:02 +00:00
}
function pick ( kind , canbuy1 , canbuy2 ) {
2022-08-06 02:03:10 +00:00
const buy1 = document . getElementById ( 'buy1' )
if ( canbuy1 && kind != "grass" )
buy1 . disabled = false ;
else
buy1 . disabled = true ;
const buy2 = document . getElementById ( 'buy2' )
if ( canbuy2 && kind != "benefactor" )
buy2 . disabled = false ;
else
buy2 . disabled = true ;
2022-07-16 21:00:02 +00:00
let ownednum = Number ( document . getElementById ( ` ${ kind } -owned ` ) . textContent ) ;
document . getElementById ( 'giveaward' ) . disabled = ( ownednum == 0 ) ;
document . getElementById ( 'kind' ) . value = kind ;
2022-10-16 22:14:50 +00:00
if ( document . getElementsByClassName ( 'picked' ) . length > 0 ) {
document . getElementsByClassName ( 'picked' ) [ 0 ] . classList . toggle ( 'picked' ) ;
}
2022-07-16 21:00:02 +00:00
document . getElementById ( kind ) . classList . toggle ( 'picked' )
if ( kind == "flairlock" ) {
document . getElementById ( 'notelabel' ) . innerHTML = "New flair:" ;
document . getElementById ( 'note' ) . placeholder = "Insert new flair here, or leave empty to add 1 day to the duration of the current flair. 100 characters max." ;
document . getElementById ( 'note' ) . maxLength = 100 ;
}
else {
document . getElementById ( 'notelabel' ) . innerHTML = "Note (optional):" ;
document . getElementById ( 'note' ) . placeholder = "Note to include in award notification" ;
document . getElementById ( 'note' ) . maxLength = 200 ;
}
}
function buy ( mb ) {
const kind = document . getElementById ( 'kind' ) . value ;
url = ` /buy/ ${ kind } `
if ( mb ) url += "?mb=true"
2022-10-14 11:31:02 +00:00
const xhr = createXhrWithFormKey ( url ) ;
2022-10-14 12:01:11 +00:00
xhr [ 0 ] . onload = function ( ) {
2022-07-16 21:00:02 +00:00
let data
2022-10-14 12:01:11 +00:00
try { data = JSON . parse ( xhr [ 0 ] . response ) }
2022-07-16 21:00:02 +00:00
catch ( e ) { console . log ( e ) }
2022-10-16 22:22:00 +00:00
success = xhr [ 0 ] . status >= 200 && xhr [ 0 ] . status < 300 ;
2022-10-14 10:47:08 +00:00
showToast ( success , getMessageFromJsonData ( success , data ) , true ) ;
if ( success ) {
2022-12-19 16:32:13 +00:00
if ( kind != "lootbox" )
{
document . getElementById ( 'giveaward' ) . disabled = false ;
let owned = document . getElementById ( ` ${ kind } -owned ` )
let ownednum = Number ( owned . textContent ) + 1 ;
owned . textContent = ownednum
}
2022-07-16 21:00:02 +00:00
}
} ;
2022-10-14 12:01:11 +00:00
xhr [ 0 ] . send ( xhr [ 1 ] ) ;
2022-07-16 21:00:02 +00:00
}
2022-10-07 03:25:34 +00:00
function giveaward ( t ) {
2022-10-19 17:54:18 +00:00
const kind = document . getElementById ( 'kind' ) . value ;
2022-11-03 19:08:22 +00:00
postToast ( t , t . dataset . action ,
2022-10-07 03:25:34 +00:00
{
2022-10-19 17:54:18 +00:00
"kind" : kind ,
2022-10-07 03:25:34 +00:00
"note" : document . getElementById ( 'note' ) . value
2022-10-19 17:54:18 +00:00
} ,
2022-11-03 19:07:08 +00:00
( ) => {
let owned = document . getElementById ( ` ${ kind } -owned ` )
let ownednum = Number ( owned . textContent ) - 1 ;
owned . textContent = ownednum
if ( ownednum == 0 )
document . getElementById ( 'giveaward' ) . disabled = true ;
2022-10-19 17:54:18 +00:00
}
2022-10-07 03:25:34 +00:00
) ;
}
2022-12-30 12:14:18 +00:00
const data _url = document . querySelectorAll ( '[data-url]' ) ;
for ( const element of data _url ) {
if ( element . dataset . nonce != nonce ) continue
element . onclick = ( ) => {
document . getElementById ( 'giveaward' ) . dataset . action = element . dataset . url
} ;
}