Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Sequelize = require('sequelize');
- const express = require('express');
- const app = express();
- const axios = require('axios');
- const Op = Sequelize.Op;
- const config = {
- "api_key":"RGAPI-9055f9b3-4743-4936-a306-bf501ba5cfd2"
- }
- var sequelize = new Sequelize('lol', 'root', '', {
- host: 'localhost',
- dialect: 'mysql',
- pool: {
- max: 5,
- min: 0,
- idle: 10000
- },
- }
- )
- var Match = sequelize.define('match',{
- gameId: {
- type: Sequelize.BIGINT,
- field: 'gameId',
- primaryKey: true
- },
- region:{
- type: Sequelize.STRING,
- field: 'region'
- },
- mapId: {
- type: Sequelize.BIGINT,
- field: 'mapId',
- },
- gameMode: {
- type: Sequelize.STRING,
- field: 'gameMode',
- },
- gameType: {
- type: Sequelize.STRING,
- field: 'gameType',
- },
- createdAt:{
- type: Sequelize.DATE,
- field: 'createdAt'
- },
- updatedAt:{
- type: Sequelize.DATE,
- field: 'updatedAt'
- }
- })
- var PlayedIn = sequelize.define('playedin', {
- playerId: {
- type: Sequelize.STRING,
- field: 'playerId',
- par: true
- },
- lane: {
- type: Sequelize.STRING,
- field: 'lane',
- },
- gameId: {
- type: Sequelize.BIGINT,
- field: 'gameId',
- primaryKey: true
- },
- region:{
- type: Sequelize.STRING,
- field: 'region'
- },
- champion: {
- type: Sequelize.STRING,
- field: 'champion'
- },
- role: {
- type: Sequelize.STRING,
- field: 'role'
- },
- createdAt:{
- type: Sequelize.DATE,
- field: 'createdAt'
- },
- updatedAt:{
- type: Sequelize.DATE,
- field: 'updatedAt'
- }
- })
- var Player = sequelize.define('player', {
- id: {
- type: Sequelize.INTEGER,
- autoIncrement: true,
- primaryKey: true
- },
- playerId: {
- type: Sequelize.STRING,
- field: 'playerId',
- },
- profileIconId: {
- type: Sequelize.INTEGER,
- field:'profileIconId'
- },
- accountId: {
- type: Sequelize.STRING,
- field: 'accountId'
- },
- puuid: {
- type: Sequelize.STRING,
- field: 'puuid'
- },
- region: {
- type: Sequelize.STRING,
- field: 'region'
- },
- name: {
- type: Sequelize.STRING,
- field: 'name'
- },
- summonerLevel:{
- type: Sequelize.INTEGER,
- field: 'summonerLevel'
- },
- createdAt:{
- type: Sequelize.DATE,
- field: 'createdAt'
- },
- updatedAt:{
- type: Sequelize.DATE,
- field: 'updatedAt'
- }
- });
- sequelize
- .authenticate()
- .then(() => {
- console.log('Connection has been established successfully.');
- })
- .catch(err => {
- console.error('Unable to connect to the database:', err);
- });
- app.listen(3200, function() {
- console.log("App listening on 3200");
- });
- app.get("/match", function (req,res){
- let param = req.query.matchId;
- let paramReg = req.query.reg;
- axios.get(`https://${paramReg}.api.riotgames.com/lol/match/v4/matches/${param}?api_key=${config.api_key}`)
- .then(response => {
- res.json(response.data);
- })
- })
- app.get("/", function(req,res){
- let parm = req.query.name;
- let paramReg = req.query.reg;
- Player.findOne({ where: {name: parm } })
- .then(player =>{
- if(player === null){
- console.log("playerNotFound");
- insertPlayer(parm,paramReg);
- }
- else {
- console.log("playerFound");
- fetchPlayedIn(player.accountId,paramReg);
- }
- })
- .catch(err => console.log(`problem: ${err} u samom pocetku`));
- setTimeout(()=>fillMatchData(parm,res,paramReg),5000);
- });
- function fillMatchData(username,res,paramReg){
- var returnArray = [];
- var player = Player.findOne({where: {name: username}});
- var matches = sequelize.query(`select plin.gameId, gameMode,gameType, name, lane, role, champion, profileIconId from playedins plin left join matches mc on plin.gameId = mc.gameId left JOIN players pl on plin.playerId = pl.accountId where plin.gameId in (select gameId from playedins plins left join players plys on plins.playerId = plys.accountId where plys.name ="${username}") order by plin.gameId`);
- Promise.all([player,matches])
- .then(()=>{
- returnArray.push(player);
- returnArray.push(matches);
- res.json(returnArray);
- })
- }
- function insertPlayer(param,paramReg){
- console.log("insertPlayer "+ paramReg);
- let url = `https://${paramReg}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${param}?api_key=${config.api_key}`;
- axios.get(encodeURI(url))
- .then(response =>{
- makePlayer(response.data,paramReg);
- fetchPlayedIn(response.data.accountId,paramReg);
- },
- err => console.log(`API PROBLEM: ${err} u insertPlayer`))
- .catch(err => console.log(`problem: ${err} u insertPlayer`));;
- }
- function makePlayer(player,paramReg){
- console.log("makePlayer");
- Player.findOne({where: {accountId: player.accountId}})
- .then(res => {
- if(res===null){
- Player.create({
- playerId: player.id,
- profileIconId: player.profileIconId,
- accountId: player.accountId,
- puuid: player.puuid,
- region: paramReg,
- name: player.name,
- summonerLevel: player.summonerLevel,
- createdAt: new Date(),
- updatedAt: new Date()
- })
- .catch(err => console.log(`Create player greska: ${err}`))
- }
- })
- }
- function getMatchById(accId){
- console.log("getMatchById");
- PlayedIn.findOne({ where: {playerId: accId} })
- .then(response =>{
- if(response === null)
- fetchPlayedIn(accId);
- else{
- PlayedIn.findAll({where: {playerId: accId}})
- .then(response =>{
- response.map(row =>{
- lookForMatch(row);
- })
- })
- fetchPlayedIn(accId);
- }
- })
- }
- function fetchPlayedIn(accId,paramReg){
- console.log("fetchPlayedIn");
- console.log(accId);
- axios.get(`https://${paramReg}.api.riotgames.com/lol/match/v4/matchlists/by-account/${accId}?endIndex=5&api_key=${config.api_key}`)
- .then(response => {
- let matches = response.data.matches;
- let dataQueue = [];
- matches.map(match => dataQueue.push(match));
- console.log(`--------------------------------${dataQueue.length}-------------------------`);
- proccessQueue(dataQueue,accId,paramReg);
- },
- err => console.log(`API PROBLEM: ${err} u insertPlayer`))
- .catch(err => console.log(`${err} u fetchPlayedIn`))
- }
- function proccessQueue(queue,accId,paramReg){
- for (var i = 0; i < queue.length; i++) {
- (function (i) {
- setTimeout(function () {
- console.log(`//////////////////////////iteracija br ${i}/////////////////////////`)
- checkPlayedIn(queue.pop(),accId,paramReg);
- }, 1000*i);
- })(i);
- };
- }
- function checkPlayedIn(match,accId,paramReg){
- if(match){
- PlayedIn.findOne({where: {gameId : match.gameId, playerId :accId}})
- .then(found =>{
- if(found === null){
- PlayedIn.create({
- playerId: accId,
- lane: match.lane,
- gameId: match.gameId,
- region: paramReg,
- champion: match.champion,
- role: match.role,
- createdAt: new Date(),
- updatedAt: new Date()
- });
- }
- lookForMatch(match,paramReg);
- }
- )
- .catch(err => console.log("greska u PlayedIn.findOne " + err))
- }
- }
- function lookForMatch(game,paramReg){
- console.log("lookForMatch");
- Match.findOne({ where: {gameId: game.gameId} })
- .then(response =>{
- if(response === null)
- fetchMatch(game,paramReg);
- })
- .catch(err=> console.log(" greska u lookForMatch " +err))
- }
- function fetchMatch(game,paramReg){
- console.log("fetchMatch");
- axios.get(`https://${paramReg}.api.riotgames.com/lol/match/v4/matches/${game.gameId}?api_key=${config.api_key}`)
- .then(response => {
- let responseData = response.data;
- Match.create({
- gameId: responseData.gameId,
- region: paramReg,
- mapId: responseData.mapId,
- gameMode: responseData.gameMode,
- gameType: responseData.gameType,
- createdAt: new Date(),
- updatedAt: new Date()
- });
- responseData.participants.map((player) =>{
- checkPlayer(responseData,player,paramReg);
- })
- },err => console.log(`API PROBLEM: ${err} u insertPlayer`))
- .catch(err => console.log(`${err} u fetchMatch`))
- }
- function checkPlayer(game,player,paramReg){
- let accId = game.participantIdentities[player.participantId-1].player.accountId;
- console.log(`-----------------------------------------------------checkPlayer ${accId}`);
- PlayedIn.create({
- playerId: accId,
- lane: player.timeline.lane,
- gameId: game.gameId,
- region: paramReg,
- champion: player.championId,
- role: player.timeline.role,
- createdAt: new Date(),
- updatedAt: new Date()
- })
- .catch(err => `problem ${err} u else checkPlayer`);
- let summName = game.participantIdentities[player.participantId-1].player.summonerName;
- console.log(summName);
- Player.findOne({ where: {accountId: accId} })
- .then(pl =>{
- if(pl === null){
- let url = `https://${paramReg}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${summName}?api_key=${config.api_key}`;
- axios.get(encodeURI(url))
- .then(response =>{
- makePlayer(response.data,paramReg);
- })
- .catch(err=> console.log(`greska u ceckPlayer ${err}`));
- }
- })
- .catch(err => console.log(`problem: ${err} sa upitom player check one u checkPlayer`));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement