/**
* DEPENDENCIES:
* /assets/common/js/elementUtil.js
* /assets/common/js/tournamentUtil.js
*
*
* USER OPTIONS:
* onStartClick : function
* points : number
* position : number
* playerCount : number
* started : boolean
* timeRemaining : number
*
*
* PUBLIC PROPERTIES:
* element
*
*
* PUBLIC FUNCTIONS:
* setProgress(_data)
*
*/
TournamentBar = function(_element, _userOptions)
{
let tournamentBarComponent;
let divPlayerPosition;
let divTimeRemaining;
let divPlayerPoints;
let btnStartTournament;
let countdownTimer = null;
let tournament;
let ended = false;
const CLASS_NOT_STARTED = "notStarted";
const CLASS_WARNING = "warning";
let options =
{
"onStartClick":null,
"points":0,
"position":0,
"playerCount":0,
"started":false,
"timeRemaining":0
};
(function initialize()
{
overwriteOptions(_userOptions);
addComponentElements();
addComponentHandlers();
updateProgress();
startTimer();
}());
function overwriteOptions(_userOptions)
{
if(typeof _userOptions != "undefined")
{
for(let property in options)
{
if(typeof _userOptions[property] != "undefined")
{
options[property] = _userOptions[property];
}
}
}
}
function addComponentElements()
{
tournamentBarComponent = ElementUtil.createElement("div", _element, "tournamentBarComponent");
divPlayerPosition = ElementUtil.createElement("div", tournamentBarComponent, "divPlayerPosition");
divTimeRemaining = ElementUtil.createElement("div", tournamentBarComponent, "divTimeRemaining");
divPlayerPoints = ElementUtil.createElement("div", tournamentBarComponent, "divPlayerPoints");
btnStartTournament = ElementUtil.createElement("button", tournamentBarComponent, "btnStartTournament", "START TOURNAMENT");
}
function addComponentHandlers()
{
tournamentBarComponent.onclick = tournamentBarComponent_onClick;
btnStartTournament.onclick = btnStartTournament_onClick;
}
function updateProgress()
{
tournamentBarComponent.classList.toggle(CLASS_NOT_STARTED, !options.started);
if(options.started)
{
divPlayerPosition.innerHTML = (options.position + "/" + options.playerCount + " ");
divPlayerPoints.innerHTML = parseFloat(options.points).toFixed(2);
if(countdownTimer == null)
{
startTimer();
}
else
{
updateTimer();
}
}
}
function startTimer()
{
stopTimer();
countdownTimer = setInterval(updateTimer, 1000);
}
function stopTimer()
{
if(countdownTimer != null)
{
clearInterval(countdownTimer);
countdownTimer = null;
}
}
function updateTimer()
{
if(options.timeRemaining > 0)
{
let showWarning = (options.timeRemaining < 60) ? true : false;
divTimeRemaining.classList.toggle(CLASS_WARNING, showWarning);
divTimeRemaining.innerHTML = FormatUtil.secondsToCountdownTime(options.timeRemaining);
options.timeRemaining -= 1;
if(options.timeRemaining <= 0)
{
ended = true;
}
}
else
{
divTimeRemaining.classList.add(CLASS_WARNING);
divTimeRemaining.innerHTML = "0:00";
options.timeRemaining = 0;
stopTimer();
if(ended)
{
ended = false;
alertTournamentEnd();
}
}
}
function alertTournamentEnd()
{
if(tournament && tournament.buy_in_duration)
{
let message = "";
let buttons = [{"label":"CLOSE"}];
let stageDetails = TournamentUtil.getStageDetails(tournament);
if(stageDetails.type == TournamentUtil.TYPE_LATE_REG)
{
message = ("Your " + tournament.buy_in_duration + "mins on " + tournament.name + " is now completed. You are currently placed in " + TournamentUtil.getPositionString(options.position) + " of " + options.playerCount + " players.");
buttons.unshift({"label":"REBUY", "handler":btnRebuy_onClick});
}
else
{
message = ("" + tournament.name + " completed, you are currently placed in " + TournamentUtil.getPositionString(options.position) + " of " + options.playerCount + " players. Payouts will be allocated as soon as the tournament is completed.");
buttons.unshift({"label":"PAYOUT", "handler":btnPayout_onClick});
}
Alert.show(message, buttons);
}
}
function btnRebuy_onClick()
{
if(tournament)
{
if(tournament.buy_in)
{
let buttons = [{"label":"Yes", "handler":joinTournament}, {"label":"No"}];
Alert.show(("Are you sure you would like to buy in to this tournament for " + FormatUtil.toZar(tournament.buy_in) + "?"), buttons);
}
else
{
joinTournament();
}
}
}
function joinTournament()
{
Services.joinTournament(joinTournament_onComplete, tournament.id);
}
function joinTournament_onComplete(_response)
{
setProgress(_response.tournament);
if(tournament.buy_in)
{
Wallet.refresh();
}
}
function btnPayout_onClick()
{
showTournamentDetails();
}
function tournamentBarComponent_onClick()
{
showTournamentDetails();
}
function showTournamentDetails()
{
if(tournament)
{
if(!TournamentDetails.isOpen())
{
TournamentDetails.show(tournament.id);
}
TournamentDetails.moveToTop();
}
}
function btnStartTournament_onClick(event)
{
event.stopPropagation();
if(typeof options.onStartClick == "function")
{
options.onStartClick();
}
}
function setProgress(_tournament)
{
tournament = _tournament;
let tournamentProgress = TournamentUtil.toTournamentProgress(_tournament);
options.points = tournamentProgress.points;
options.position = tournamentProgress.position;
options.playerCount = tournamentProgress.playerCount;
options.started = tournamentProgress.started;
options.timeRemaining = tournamentProgress.timeRemaining;
updateProgress();
}
return(
{
"element":_element,
"stopTimer":stopTimer,
"setProgress":setProgress
});
}