Skip to content

Commit

Permalink
Merge branch 'beta' into charles
Browse files Browse the repository at this point in the history
  • Loading branch information
mmpotulo28 authored Jun 26, 2024
2 parents 1f4e5dc + c17edcc commit e44d49a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
8 changes: 7 additions & 1 deletion assets/js/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const startBtn = document.getElementById('host-start-btn');
const cancelBtn = document.getElementById('host-cancel-btn');
const rewardAmount = document.querySelector('.reward-amount');
const nftImage = document.querySelector('.nft-image');
const statusText = document.getElementById('status-text');
const fundForm = document.getElementById('fund-form');
const closeBtn = document.querySelector('.close-btn');
const hostDeposit = document.getElementById('host-deposit');
Expand Down Expand Up @@ -184,6 +183,13 @@ async function setQuizDetails(details) {
details.playerCount.innerHTML = 'Players: ' + details.playerNames.length;
details.redirect = false;

// set campaign
const login = JSON.parse(sessionStorage.getItem('login')) || {};
const dummyObject = dummyObject || {};
login.campaign = dummyObject.campaign || '';
details.campaign = dummyObject.campaign || '';
sessionStorage.setItem('login', JSON.stringify(login));

await checkGameStatus(details);
}

Expand Down
30 changes: 25 additions & 5 deletions assets/js/postquiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ playBtn?.addEventListener('click', () => {
let tokenTransferred = false;
let reload = true;
let time = 5;
let campaign = 'defaultCampaign';

async function setScoreBoard() {
// get username from login
const login = JSON.parse(sessionStorage.getItem('login'));
const username = login.username;
const myPin = gamePin;
campaign = login.campaign;

// get score object from session storage
const sessionUser = JSON.parse(sessionStorage.getItem('sessionUser'));
Expand Down Expand Up @@ -109,21 +111,38 @@ setScoreBoard();
async function checkWin(scoreData, username, gamePin, score) {
// check if score equals the number of questions
// if (score < questions[`Q${topicID}`].length / 2) {
// alert('You should get 50% of the questions correctly to win, try again!');
// // restart the game move the play.html
// !alert('You should get 50% of the questions correctly to win, try again!');
// restart the game move the play.html
// window.location.href = `../play/quiz.html?topic=${topicID}&gamePin=${gamePin}&retry=${score}`;
// return;
// }

// get the campaign from login object
const login = JSON.parse(sessionStorage.getItem('login'));
campaign = login.campaign;

const retry = new URLSearchParams(window.location.search).get('retry');

// set overall ranking
if (retry) {
await setOverallRanking({ username, score, retry: '2', gamePin });
await setOverallRanking({ username, score, retry: '2', gamePin, campaign });
} else {
await setOverallRanking({ username, score, gamePin });
await setOverallRanking({ username, score, gamePin, campaign });
}

// format username
function shortenUsername(username) {
const usernameArr = username.split(' ');
if (usernameArr.length > 1) {
return `${usernameArr[0][0]} ${usernameArr[1]}`;
} else if (username.length > 13) {
return username.slice(0, 10);
}
return username;
}

username = shortenUsername(username);

if (scoreData[0].username === username && !tokenTransferred) {
alert('Congratulations! You are the won, click OK to accept your NFT');

Expand Down Expand Up @@ -152,7 +171,7 @@ async function checkWin(scoreData, username, gamePin, score) {
}

// set overall ranking
async function setOverallRanking({ username, score, retry, gamePin }) {
async function setOverallRanking({ username, score, retry, gamePin, campaign }) {
console.log('setting overall ranking for', username, score, retry, gamePin);
const currentTime = new Date().getTime();

Expand All @@ -163,5 +182,6 @@ async function setOverallRanking({ username, score, retry, gamePin }) {
time: currentTime,
retry: retry,
gamePin: gamePin,
campaign: campaign,
});
}
4 changes: 1 addition & 3 deletions assets/js/ranking.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ async function getRanking() {
// group the rankings by gamePin
const rankingByGamePin = {};
sortedRanking.forEach((player) => {
// if player does not have a gamePin, add NA
if (player.campaign) {
player.gamePin = player.campaign;
console.log(player.campaign);
}
if (!rankingByGamePin[player.gamePin]) {
rankingByGamePin[player.gamePin] = [];
Expand All @@ -29,7 +27,7 @@ async function getRanking() {
// create the ranking table for each gamePin
Object.keys(rankingByGamePin).forEach(async (gamePin) => {
const players = rankingByGamePin[gamePin];
if (players.length < 2) return;
// Code removed
const gamePinRow = document.createElement('tr');
gamePinRow.innerHTML = `
<td colspan="6" class="game-pin-col">${gamePin}</td>
Expand Down
26 changes: 14 additions & 12 deletions pages/auth/fb.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ async function createScoreBoard({ gamePin, username, score, topicID, wallet }) {
let scoreData = Object.values(scoreSnapshot.val() || {});

// if username is 2 names, take initial and surname
function shortenUsername(username) {
const usernameArr = username.split(' ');
if (usernameArr.length > 1) {
return `${usernameArr[0][0]} ${usernameArr[1]}`;
} else if (username.length > 13) {
return username.slice(0, 10);
}
return username;
}

username = shortenUsername(username);
function shortenUsername(username) {
const usernameArr = username.split(' ');
if (usernameArr.length > 1) {
return `${usernameArr[0][0]} ${usernameArr[1]}`;
} else if (username.length > 13) {
return username.slice(0, 10);
}
return username;
}

username = shortenUsername(username);

// Initialize scoreData if it's null
if (!scoreData) {
Expand Down Expand Up @@ -345,7 +345,7 @@ async function setPlayers({ gamePin, topicID, playerNames }) {
}

// set overall ranking
async function overallRanking({ username, points, time, retry, gamePin }) {
async function overallRanking({ username, points, time, retry, gamePin, campaign }) {
let overallRankingSnapshot = null;
let overallRankingRef = null;
try {
Expand Down Expand Up @@ -375,6 +375,7 @@ async function overallRanking({ username, points, time, retry, gamePin }) {
startTime: Date.now(),
duration: time,
gamePin: gamePin,
campaign: campaign,
});
} else {
// update the score if the username already exists
Expand All @@ -391,6 +392,7 @@ async function overallRanking({ username, points, time, retry, gamePin }) {
obj.time = time;
obj.duration = time - obj.startTime;
obj.gamePin = gamePin;
obj.campaign = campaign;
}
return obj;
});
Expand Down
13 changes: 4 additions & 9 deletions pages/play/host/transfer/indexMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,10 @@ async function sendTransaction() {
try {
// Send the transaction
transfertext.textContent = 'processing...';
try {
const txHash = await ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters],
});
} catch (error) {
console.error('Transaction failed', error);
transfertext.textContent = 'Transaction failed';
}
const txHash = await ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters],
});

// delay for 10s
transfertext.textContent = 'Waiting confirmation...';
Expand Down

0 comments on commit e44d49a

Please sign in to comment.