Files
chocomae/src/game/result/ranking_board.js
T
2018-12-03 00:25:16 +09:00

266 lines
7.3 KiB
JavaScript

function RankingBoard(type) {
if(type === RecordBoard.TYPE_SAMPLE) {
this.printSample();
return;
}
this.makeMedalSprites();
this.dbConnectManager = new DBConnectManager();
setTimeout(
(function() {
this.requestRanking(DBConnectManager.TYPE_MY_RANKING_HOUR);
this.requestRanking(DBConnectManager.TYPE_MY_RANKING_DAY);
this.requestRanking(DBConnectManager.TYPE_MY_RANKING_MONTH);
}).bind(this),
RecordBoard.DELAY_UPDATING_RESULT_RECORD_MS
);
}
RankingBoard.prototype.requestRanking = function(type) {
var today = new Date();
var date = DateUtil.getYYYYMMDD(today);
var time = DateUtil.getHHMMSS(today);
this.dbConnectManager.requestRanking(
type, sessionStorageManager.getMaestroID(), date, time,
(function(type, rankingRecordManager) {
if(rankingRecordManager.getCount() == 0) {
console.log("ranking board - no data");
return;
}
var beginIndex = this.getBeginIndex(rankingRecordManager);
var endIndex = this.getEndIndex(rankingRecordManager);
for(var i = 0; i < endIndex - beginIndex; i++) {
var index = beginIndex + i;
var data = rankingRecordManager.getAt(index);
this.printRecord(type, i, data);
}
switch(type) {
case DBConnectManager.TYPE_MY_RANKING_HOUR:
this.showMedal(this.medalHour, this.getMyRank(rankingRecordManager));
break;
case DBConnectManager.TYPE_MY_RANKING_DAY:
this.showMedal(this.medalDay, this.getMyRank(rankingRecordManager));
break;
case DBConnectManager.TYPE_MY_RANKING_MONTH:
this.showMedal(this.medalMonth, this.getMyRank(rankingRecordManager));
break;
}
}).bind(this)
);
}
RankingBoard.prototype.getMyRank = function(rankingRecordManager) {
var playerID = Number(sessionStorageManager.getPlayerID());
for(var i = 0; i < rankingRecordManager.getCount(); i++) {
var data = rankingRecordManager.getAt(i);
var playerIDNo = Number(data["playerID"]);
if(playerIDNo === playerID)
return i + 1;
}
return -1;
}
RankingBoard.prototype.getBeginIndex = function(rankingRecordManager) {
var rankingRecordCount = rankingRecordManager.getCount();
var myRank = this.getMyRank(rankingRecordManager);
var myRankIndex = myRank - 1;
var startIndex = 0;
var endIndex = rankingRecordCount;
if(rankingRecordCount > 7) {
if(myRank < rankingRecordCount - 3) { // my rank is better than the worst 3
if(myRank < 5) {
startIndex = 0;
endIndex = rankingRecordCount > 7 ? 7 : rankingRecordCount;
} else {
startIndex = myRankIndex - 3;
endIndex = myRankIndex + 4;
}
} else { // my rank is in the worst 3
startIndex = rankingRecordCount > 7 ? rankingRecordCount - 7 : 0;
endIndex = rankingRecordCount;
}
}
return startIndex;
}
RankingBoard.prototype.getEndIndex = function(rankingRecordManager) {
var rankingRecordCount = rankingRecordManager.getCount();
var myRank = this.getMyRank(rankingRecordManager);
var myRankIndex = myRank - 1;
var startIndex = 0;
var endIndex = rankingRecordCount;
if(rankingRecordCount > 7) {
if(myRank < rankingRecordCount - 3) { // my rank is better than the worst 3
if(myRank < 5) {
startIndex = 0;
endIndex = rankingRecordCount > 7 ? 7 : rankingRecordCount;
} else {
startIndex = myRankIndex - 3;
endIndex = myRankIndex + 4;
}
} else { // my rank is in the worst 3
startIndex = rankingRecordCount > 7 ? rankingRecordCount - 7 : 0;
endIndex = rankingRecordCount;
}
}
return endIndex;
}
RankingBoard.prototype.printRecord = function(type, index, data) {
var style = { font: "20px Arial", fill: "#fff", align: "right", boundsAlignH: "right", boundsAlignV: "top" };
// rank
// style.boundsAlignH = "right";
var rankText = game.add.text(this.getPosX(type), this.getPosY(index), data.rank, style);
rankText.anchor.set(1, 0);
rankText.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
// player name
var playerNameText = game.add.text(this.getPosX(type) + 20, this.getPosY(index), data.playerName, style);
playerNameText.anchor.set(0, 0);
playerNameText.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
// bestRecord
var recordText = game.add.text(
this.getPosX(type) + 180, this.getPosY(index),
RecordUtil.getRecordValueWithUnit(data.bestRecord, sessionStorageManager.getPlayingAppID()),
style
);
recordText.anchor.set(1, 0);
recordText.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
}
RankingBoard.prototype.getPosX = function(type) {
var posX = 0;
switch(type) {
case DBConnectManager.TYPE_MY_RANKING_HOUR:
case DBConnectManager.TYPE_ALL_RANKING_HOUR:
posX = 330;
break;
case DBConnectManager.TYPE_MY_RANKING_DAY:
case DBConnectManager.TYPE_ALL_RANKING_DAY:
posX = 560;
break;
case DBConnectManager.TYPE_MY_RANKING_MONTH:
case DBConnectManager.TYPE_ALL_RANKING_MONTH:
posX = 790;
}
return posX;
}
RankingBoard.prototype.getPosY = function(index) {
return game.world.height / 2 + 90 + 30 * index;
}
RankingBoard.prototype.printSample = function() {
// TYPE_MY_RANKING_HOUR
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_HOUR, 0,
new RankingRecord(1, 0, "홍길동", 1080)
);
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_HOUR, 1,
new RankingRecord(2, 0, "3-1김영희", 860)
);
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_HOUR, 2,
new RankingRecord(3, 0, "6-3이철수", 480)
);
// TYPE_MY_RANKING_DAY
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_DAY, 0,
new RankingRecord(1, 0, "050215", 1580)
);
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_DAY, 1,
new RankingRecord(2, 0, "홍길동", 1080)
);
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_DAY, 2,
new RankingRecord(3, 0, "3-1김영희", 860)
);
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_DAY, 3,
new RankingRecord(4, 0, "6-3이철수", 480)
);
// TYPE_MY_RANKING_MONTH
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_MONTH, 0,
new RankingRecord(1, 0, "050215", 1580)
);
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_MONTH, 1,
new RankingRecord(2, 0, "홍길동", 1080)
);
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_MONTH, 2,
new RankingRecord(3, 0, "3-1김영희", 860)
);
this.printRecord(
DBConnectManager.TYPE_MY_RANKING_MONTH, 3,
new RankingRecord(4, 0, "6-3이철수", 480)
);
}
RankingBoard.loadResources = function() {
game.load.image('medal_gold', '../../../resources/image/ui/medal_gold.png');
game.load.image('medal_silver', '../../../resources/image/ui/medal_silver.png');
game.load.image('medal_bronze', '../../../resources/image/ui/medal_bronze.png');
}
RankingBoard.prototype.makeMedalSprites = function() {
var rankAreaPositionY = 630;
this.medalHour = game.add.image(425, rankAreaPositionY, 'medal_gold');
this.medalHour.anchor.set(0.5);
this.medalHour.alpha = 0;
this.medalDay = game.add.image(655, rankAreaPositionY, 'medal_gold');
this.medalDay.anchor.set(0.5);
this.medalDay.alpha = 0;
this.medalMonth = game.add.image(885, rankAreaPositionY, 'medal_gold');
this.medalMonth.anchor.set(0.5);
this.medalMonth.alpha = 0;
}
RankingBoard.prototype.showMedal = function(medal, myRank) {
if(myRank > 3) {
medal.alpha = 0;
return;
}
if(myRank == 1)
medal.loadTexture("medal_gold");
else if(myRank == 2)
medal.loadTexture("medal_silver");
else if(myRank == 3)
medal.loadTexture("medal_bronze");
medal.alpha = 1;
medal.scale.setTo(0.5);
game.add.tween(medal.scale).to( {x: 0.7, y: 0.7}, 1000, Phaser.Easing.Bounce.Out, true);
}