const express = require('express') const app = express() const port = 9700 const web3operatorAddress = "http://vps.playpoolstudios.com:2015/" const apiAddress = "https://vps.playpoolstudios.com/metahunt/api/" app.get('/', (req, res) => { res.send('Validator is validating') }) app.listen(port, () => { console.log(`Mhunt Validator is listening on port ${port}`) }) app.get('/validateSession', async (req,res)=>{ const {tournamentId, address} = req.query; if(!tournamentId || !address){res.send("invalid params"); return;} let tournament = GetTournamentById(tournamentId); if(tournament==null){ await CheckForStartedTourneys(); } tournament = GetTournamentById(tournamentId); if(tournament == null){ res.send("This tournament is not either started or valid"); return; } }) /* ------------------------------------------------------------------------------- */ let tournamentsList; let startedTournaments; CheckForStartedTourneys(); setInterval(async()=>{ CheckForStartedTourneys(); },60000) async function CheckForStartedTourneys() { const tournamentsResponse = await fetch(apiAddress + "get_tournaments_raw.php"); const tournaments = await tournamentsResponse.json(); tournamentsList = []; startedTournaments = []; const now = new Date(); const tenMinutesAgo = new Date(now.getTime() - 10 * 60000); // 10 minutes ago from now tournaments.forEach(async tournament => { const tournamentDate = new Date(tournament.start_date); // Converts the string date to a Date object // Create a new TournamentData instance using the tournament JSON data const newTournament = new TournamentData( tournament.id, tournament.name, tournament.start_date, tournament.game_mode, tournament.reward, tournament.php_reward, tournament.is_test, tournament.ticket_count, tournament.owner_id ); if (tournamentDate >= tenMinutesAgo && tournamentDate <= now) { console.log(`Tournament "${tournament.name}" started within the last 10 minutes.`); const participentsResponse = await fetch(web3operatorAddress+"getTournamentParticipants?id="+newTournament.id); const participents = await participentsResponse.json(); participents["wallets"].forEach(participent => { newTournament.participents.push(participent); }) startedTournaments.push(newTournament); }else if(tournamentDate > now){ console.log(`Tournament "${tournament.name}" is yet to come`) }else{ // console.log(`Tournament "${tournament.name}" is expired`) } // Push the new tournament instance into tournamentsList tournamentsList.push(newTournament); }); } function GetTournamentById(tournamentId){ startedTournaments.forEach(tournament => { if(tournament.id == tournamentId){ return tournament; } }) return null; } class TournamentData { constructor(id, name, start_date, game_mode, reward, php_reward, is_test, ticket_count, owner_id) { this.id = id; this.name = name; this.start_date = start_date; this.game_mode = game_mode; this.reward = reward; this.php_reward = php_reward; this.is_test = is_test; this.ticket_count = ticket_count; this.owner_id = owner_id; this.participents = []; } // Method to display tournament details displayDetails() { console.log(`Tournament ID: ${this.id}`); console.log(`Name: ${this.name}`); console.log(`Date: ${this.start_date}`); console.log(`Game Mode: ${this.game_mode}`); console.log(`Reward: ${this.reward}`); console.log(`PHP Reward: ${this.php_reward}`); console.log(`Test Tournament: ${this.is_test ? "Yes" : "No"}`); console.log(`Ticket Count: ${this.ticket_count}`); console.log(`Owner ID: ${this.owner_id}`); } }