Removing unrelated developement

master
Cristiano Pires 1 year ago
parent 136cbe0760
commit 7bde1c3a0c

@ -1,8 +1,6 @@
const GuildM = require('../models/guilds');
const roleRulesM = require('../models/autoRoleRule');
const feedsM = require('../models/feeds');
const leagueGameM = require('../models/leagueGame');
const leagueM = require('../models/league');
const xmlparser = require('xml-js')
const {isValidObjectId} = require('mongoose')
@ -449,320 +447,3 @@ exports.deleteFeed = (req,res) =>
{
feedsM.findOneAndDelete({_id:req.headers.feedid}).then(()=>{res.json({success:true})}).catch(error=>{res.json({error})})
}
exports.getTournamentPage = (req,res)=>
{
if(!req.query.league) return res.render('leagues');
if(!isValidObjectId(req.query.league)) return res.redirect('/leagues')
leagueM.findById(req.query.league)
.then(league=>
{
if(league) res.render('leagues')
})
.catch(
res.render('leagues')
)
}
exports.getAllLeagues = (req, res)=>
{
leagueM.find()
.then((leagues, err)=>
{
if(err) return res.json({Error:'Yea there was a problem.'});
if(!leagues) return res.json({Error:'Yea there was a problem.'});
if(leagues.length==0) return res.json({Error:'No leagues.'})
return res.json({data:leagues})
})
.catch(err=>console.log('Error finding leagues.'))
}
exports.getLeague = async (req, res)=>
{
if(!isValidObjectId(req.headers.leagueid)) return res.json({Error:'Yea this aint it chief... stop trying to mess with the system.'})
leagueM.findById(req.headers.leagueid)
.then((league, err)=>
{
if(err) return res.json({Error:'Yea there was a problem.'});
if(!league) return res.json({Error:'Yea there was a problem.'});
if(league.length==0) return res.json({Error:'No leagues.'})
return res.json({data:league})
})
.catch(err=>console.log())
}
exports.getLeaguePage = (req,res)=>
{
if(req.originalUrl == '/leagues/') return res.redirect('/leagues')
return res.render('league')
}
exports.registerGame = async (req, res)=>
{
const GameData =
{
result:req.body.result,
info:req.body.gameData,
id: req.headers.id,
leagueid: req.headers.leagueid
}
const data = req.body.data;
var league = await leagueM.findById(GameData.leagueid).then(league=>{return league})
if(!league) return res.json({Error:'The league id is not valid'})
var Game = league.Games.filter(x=>(x.gId == GameData.id && x.Played == false));
if(Game.length == 0) return res.json({Error: 'The game id is not valid.'})
const gameStartTime = new Date(Date.now()-(GameData.info.gameTime*1000)).getTime(); //Epoch - gametime;
const result = GameData.result;
var update = await leagueGameM.findById(GameData.id).then(game=>{return game});
var PA = fillPlayer(update.PA, update.PA.Name == GameData.info.PlayerA.summonerName? GameData.info.PlayerA: GameData.info.PlayerB, update.PA.Name == GameData.result.winner);
var PB = fillPlayer(update.PB, update.PB.Name == GameData.info.PlayerA.summonerName? GameData.info.PlayerA: GameData.info.PlayerB, update.PB.Name == GameData.result.winner);
update.PA = PA;
update.PB = PB;
update.Winner = result.winner;
update.WinCondition = result.winCondition;
update.Played = true;
update.PlayedTimeStamp = gameStartTime;
const updated = await leagueGameM.findByIdAndUpdate(GameData.id,update).then(()=>
{
return true
})
.catch(()=>
{
return false
})
if(!updated) return res.json({Error:'The game failed to register.'})
var _PA = update.PA;
_PA.extraData= update.PA.Name == GameData.info.PlayerA.summonerName? GameData.info.PlayerA: GameData.info.PlayerB;
var _PB = update.PB;
_PB.extraData = update.PA.Name == GameData.info.PlayerA.summonerName? GameData.info.PlayerA: GameData.info.PlayerB;
Game[0] = {
PA:_PA,
PB:_PB,
Winner:update.Winner,
WinCondition:update.WinCondition,
Played:true,
PlayedTimeStamp:gameStartTime,
gId:GameData.id
}
const index = league.Games.findIndex(x=>x.gId=GameData.id);
league.Games[index] = Game[0];
const PAIndex = league.Players.findIndex(x=>x.Name == GameData.info.PlayerA.summonerName)
const PBIndex = league.Players.findIndex(x=>x.Name == GameData.info.PlayerB.summonerName)
const originalA = league.Players[PAIndex];
league.Players[PAIndex]={
Name: originalA.Name,
Points: originalA.Points + (originalA.Name == result.winner?1:0),
PlayedPoints: originalA.PlayedPoints + 1,
Kills: originalA.Kills + GameData.info.PlayerA.Kills,
Deaths: originalA.Deaths + GameData.info.PlayerA.Deaths,
CreepScore: originalA.CreepScore + GameData.info.PlayerA.CS
}
const originalB = league.Players[PBIndex];
league.Players[PBIndex]={
Name: originalB.Name,
Points: originalB.Points + (originalB.Name == result.winner?1:0),
PlayedPoints: originalA.PlayedPoints + 1,
Kills: originalB.Kills + GameData.info.PlayerB.Kills,
Deaths: originalB.Deaths + GameData.info.PlayerB.Deaths,
CreepScore: originalB.CreepScore + GameData.info.PlayerB.CS
}
const leagueUpdated = await leagueM.findByIdAndUpdate(GameData.leagueid,league).then(()=>
{
return true;
})
.catch(()=>{return false})
if(!leagueUpdated) return res.json({Error:'Failed to update league.'})
res.json({success:true})
}
function fillPlayer(P, data,winner)
{
P.winner = winner;
P.Kills = data.Kills;
P.Deaths = data.Deaths;
P.CS = data.CS
return P;
}
exports.getTournamentListenerPage = (req,res)=>
{
return res.render('leaguelistener')
}
exports.createLeague = async (req, res)=>
{
const verification = await verifyBody(req.body);
if(verification.Error) return res.json(verification)
var LeagueReference =
{
Name:verification.name,
Players:[],
Games:[],
Owner:verification.owner
}
for(player of verification.players)
{
(async (player)=>
{
LeagueReference.Players.push(
{
Name:player,
Points:0,
PlayedPoints:0,
Kills:0,
Deaths:0,
CreepScore:0
}
);
})(player)
}
var games = await scheduler(verification.players)
LeagueReference.Games=games;
var league = new leagueM();
league.Name = LeagueReference.Name;
league.Players = LeagueReference.Players;
league.Games = LeagueReference.Games;
league.Owner = LeagueReference.Owner
league.save()
.then(leagueS=>
{
return res.json({Success:true, data:leagueS});
})
.catch(err=>
{
return res.json({Error:'There was an error trying to save your league, try again later.'});
})
}
async function verifyBody(body)
{
if(!body.owner) return {Error:'Stop trying to mess with the system.'};
if(!body.name) return {Error:'The league needs to be named.'};
if(!body.players) return {Error:'The league needs to have players playing it.'}
var players = [...body.players.split(',')];
var playerPayload = [];
var promises = [];
for (var player of players)
{
(async (player)=>
{
promises.push(new Promise ((resolve)=>
{
fetch(`https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/${player}`,
{
method: "GET",
headers: {
"X-Riot-Token": "RGAPI-fc17a511-f587-495b-ac01-80716870d46b"
}
})
.then(response=>{return response.json()})
.then(res=>
{
if(res.status) playerPayload = {Error:'Player '+player+' does not exist.'};
try
{
playerPayload.push(res.name)
}
catch{}
resolve();
})
.catch(err=>{console.log(err)})
}))
})(player)
}
var payload = await Promise.all(promises).then(() =>
{
if(playerPayload.Error) return playerPayload;
return {name:body.name, owner:body.owner,players:playerPayload}
})
return payload;
}
async function scheduler(players)
{
var games = [];
var promises = [];
for(var player of players)
{
(async (player)=>
{
promises.push(new Promise ((resolve)=>
{
for(var i = players.length-1; i>=0;i--)
{
(async (player2)=>
{
if(player == player2) resolve();
else
{
games.push({
PA:{
Name:player,
winner:false,
Kills:0,
Deaths:0,
CS:0
},
PB:{
Name:player2,
winner:false,
Kills:0,
Deaths:0,
CS:0
},
Winner:'',
WinCondition:'',
Played:false,
PlayedTimeStamp:'',
gId:''
})
resolve()
}
})(players[i])
}
}))
})(player)
}
var aux = [];
for await(var game of games)
{
(async (game)=>
{
promises.push(new Promise ((resolve)=>
{
var gameM = new leagueGameM();
gameM.PA = game.PA;
gameM.PB = game.PB;
gameM.Winner = ' ';
gameM.WinCondition = ' ';
gameM.Played = false;
gameM.PlayedTimeStamp = ' ';
gameM.save().then((g)=>
{
game.gId = g.id;
aux.push(game)
resolve();
})
}));
})(game);
}
var payload = await Promise.all(promises).then(() =>
{
return aux;
})
return payload;
}
exports.createLeaguePage = (req, res)=>
{
return res.render('createLeague');
}
exports.createLeaguePage = (req, res)=>
{
res.render('createLeague');
}
exports.getGame = (req, res)=>
{
return res.render('game')
}
exports.getGameData = (req, res)=>
{
//TODO: getGameData
//TODO: send as json
}
Loading…
Cancel
Save