Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. const Sequelize = require('sequelize');
  2. const express = require('express');
  3. const app = express();
  4. const axios = require('axios');
  5. const Op = Sequelize.Op;
  6.  
  7. const config = {
  8. "api_key":"RGAPI-9055f9b3-4743-4936-a306-bf501ba5cfd2"
  9.  
  10. }
  11.  
  12. var sequelize = new Sequelize('lol', 'root', '', {
  13. host: 'localhost',
  14. dialect: 'mysql',
  15.  
  16. pool: {
  17. max: 5,
  18. min: 0,
  19. idle: 10000
  20. },
  21. }
  22.  
  23. )
  24.  
  25. var Match = sequelize.define('match',{
  26. gameId: {
  27. type: Sequelize.BIGINT,
  28. field: 'gameId',
  29. primaryKey: true
  30. },
  31. region:{
  32. type: Sequelize.STRING,
  33. field: 'region'
  34. },
  35. mapId: {
  36. type: Sequelize.BIGINT,
  37. field: 'mapId',
  38.  
  39. },
  40. gameMode: {
  41. type: Sequelize.STRING,
  42. field: 'gameMode',
  43.  
  44. },
  45. gameType: {
  46. type: Sequelize.STRING,
  47. field: 'gameType',
  48.  
  49. },
  50. createdAt:{
  51. type: Sequelize.DATE,
  52. field: 'createdAt'
  53. },
  54. updatedAt:{
  55. type: Sequelize.DATE,
  56. field: 'updatedAt'
  57. }
  58.  
  59. })
  60.  
  61. var PlayedIn = sequelize.define('playedin', {
  62. playerId: {
  63. type: Sequelize.STRING,
  64. field: 'playerId',
  65. par: true
  66. },
  67. lane: {
  68. type: Sequelize.STRING,
  69. field: 'lane',
  70.  
  71. },
  72. gameId: {
  73. type: Sequelize.BIGINT,
  74. field: 'gameId',
  75. primaryKey: true
  76. },
  77. region:{
  78. type: Sequelize.STRING,
  79. field: 'region'
  80. },
  81. champion: {
  82. type: Sequelize.STRING,
  83. field: 'champion'
  84. },
  85. role: {
  86. type: Sequelize.STRING,
  87. field: 'role'
  88. },
  89. createdAt:{
  90. type: Sequelize.DATE,
  91. field: 'createdAt'
  92. },
  93. updatedAt:{
  94. type: Sequelize.DATE,
  95. field: 'updatedAt'
  96. }
  97. })
  98.  
  99. var Player = sequelize.define('player', {
  100. id: {
  101. type: Sequelize.INTEGER,
  102. autoIncrement: true,
  103. primaryKey: true
  104. },
  105. playerId: {
  106. type: Sequelize.STRING,
  107. field: 'playerId',
  108.  
  109. },
  110. profileIconId: {
  111. type: Sequelize.INTEGER,
  112. field:'profileIconId'
  113. },
  114. accountId: {
  115. type: Sequelize.STRING,
  116. field: 'accountId'
  117. },
  118. puuid: {
  119. type: Sequelize.STRING,
  120. field: 'puuid'
  121. },
  122. region: {
  123. type: Sequelize.STRING,
  124. field: 'region'
  125. },
  126. name: {
  127. type: Sequelize.STRING,
  128. field: 'name'
  129. },
  130. summonerLevel:{
  131. type: Sequelize.INTEGER,
  132. field: 'summonerLevel'
  133. },
  134.  
  135. createdAt:{
  136. type: Sequelize.DATE,
  137. field: 'createdAt'
  138. },
  139. updatedAt:{
  140. type: Sequelize.DATE,
  141. field: 'updatedAt'
  142. }
  143. });
  144.  
  145. sequelize
  146. .authenticate()
  147. .then(() => {
  148. console.log('Connection has been established successfully.');
  149. })
  150. .catch(err => {
  151. console.error('Unable to connect to the database:', err);
  152. });
  153.  
  154. app.listen(3200, function() {
  155. console.log("App listening on 3200");
  156. });
  157.  
  158. app.get("/match", function (req,res){
  159. let param = req.query.matchId;
  160. let paramReg = req.query.reg;
  161. axios.get(`https://${paramReg}.api.riotgames.com/lol/match/v4/matches/${param}?api_key=${config.api_key}`)
  162. .then(response => {
  163. res.json(response.data);
  164. })
  165. })
  166.  
  167. app.get("/", function(req,res){
  168. let parm = req.query.name;
  169. let paramReg = req.query.reg;
  170.  
  171. Player.findOne({ where: {name: parm } })
  172. .then(player =>{
  173. if(player === null){
  174. console.log("playerNotFound");
  175. insertPlayer(parm,paramReg);
  176. }
  177. else {
  178. console.log("playerFound");
  179. fetchPlayedIn(player.accountId,paramReg);
  180. }
  181. })
  182. .catch(err => console.log(`problem: ${err} u samom pocetku`));
  183. setTimeout(()=>fillMatchData(parm,res,paramReg),5000);
  184. });
  185.  
  186. function fillMatchData(username,res,paramReg){
  187. var returnArray = [];
  188. var player = Player.findOne({where: {name: username}});
  189.  
  190. 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`);
  191.  
  192. Promise.all([player,matches])
  193. .then(()=>{
  194. returnArray.push(player);
  195. returnArray.push(matches);
  196. res.json(returnArray);
  197. })
  198.  
  199. }
  200.  
  201.  
  202. function insertPlayer(param,paramReg){
  203. console.log("insertPlayer "+ paramReg);
  204. let url = `https://${paramReg}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${param}?api_key=${config.api_key}`;
  205. axios.get(encodeURI(url))
  206. .then(response =>{
  207. makePlayer(response.data,paramReg);
  208. fetchPlayedIn(response.data.accountId,paramReg);
  209. },
  210. err => console.log(`API PROBLEM: ${err} u insertPlayer`))
  211. .catch(err => console.log(`problem: ${err} u insertPlayer`));;
  212.  
  213. }
  214.  
  215. function makePlayer(player,paramReg){
  216. console.log("makePlayer");
  217. Player.findOne({where: {accountId: player.accountId}})
  218. .then(res => {
  219. if(res===null){
  220. Player.create({
  221. playerId: player.id,
  222. profileIconId: player.profileIconId,
  223. accountId: player.accountId,
  224. puuid: player.puuid,
  225. region: paramReg,
  226. name: player.name,
  227. summonerLevel: player.summonerLevel,
  228. createdAt: new Date(),
  229. updatedAt: new Date()
  230. })
  231. .catch(err => console.log(`Create player greska: ${err}`))
  232. }
  233. })
  234.  
  235. }
  236.  
  237.  
  238. function getMatchById(accId){
  239. console.log("getMatchById");
  240.  
  241. PlayedIn.findOne({ where: {playerId: accId} })
  242. .then(response =>{
  243. if(response === null)
  244. fetchPlayedIn(accId);
  245. else{
  246. PlayedIn.findAll({where: {playerId: accId}})
  247. .then(response =>{
  248. response.map(row =>{
  249. lookForMatch(row);
  250.  
  251. })
  252. })
  253. fetchPlayedIn(accId);
  254. }
  255. })
  256.  
  257.  
  258. }
  259.  
  260. function fetchPlayedIn(accId,paramReg){
  261. console.log("fetchPlayedIn");
  262. console.log(accId);
  263.  
  264. axios.get(`https://${paramReg}.api.riotgames.com/lol/match/v4/matchlists/by-account/${accId}?endIndex=5&api_key=${config.api_key}`)
  265. .then(response => {
  266. let matches = response.data.matches;
  267. let dataQueue = [];
  268. matches.map(match => dataQueue.push(match));
  269. console.log(`--------------------------------${dataQueue.length}-------------------------`);
  270. proccessQueue(dataQueue,accId,paramReg);
  271. },
  272. err => console.log(`API PROBLEM: ${err} u insertPlayer`))
  273. .catch(err => console.log(`${err} u fetchPlayedIn`))
  274.  
  275. }
  276.  
  277.  
  278.  
  279. function proccessQueue(queue,accId,paramReg){
  280. for (var i = 0; i < queue.length; i++) {
  281. (function (i) {
  282. setTimeout(function () {
  283. console.log(`//////////////////////////iteracija br ${i}/////////////////////////`)
  284. checkPlayedIn(queue.pop(),accId,paramReg);
  285. }, 1000*i);
  286. })(i);
  287. };
  288. }
  289.  
  290. function checkPlayedIn(match,accId,paramReg){
  291. if(match){
  292. PlayedIn.findOne({where: {gameId : match.gameId, playerId :accId}})
  293. .then(found =>{
  294. if(found === null){
  295. PlayedIn.create({
  296. playerId: accId,
  297. lane: match.lane,
  298. gameId: match.gameId,
  299. region: paramReg,
  300. champion: match.champion,
  301. role: match.role,
  302. createdAt: new Date(),
  303. updatedAt: new Date()
  304. });
  305. }
  306. lookForMatch(match,paramReg);
  307. }
  308. )
  309. .catch(err => console.log("greska u PlayedIn.findOne " + err))
  310. }
  311. }
  312.  
  313. function lookForMatch(game,paramReg){
  314. console.log("lookForMatch");
  315. Match.findOne({ where: {gameId: game.gameId} })
  316. .then(response =>{
  317. if(response === null)
  318. fetchMatch(game,paramReg);
  319.  
  320. })
  321. .catch(err=> console.log(" greska u lookForMatch " +err))
  322.  
  323. }
  324.  
  325. function fetchMatch(game,paramReg){
  326. console.log("fetchMatch");
  327. axios.get(`https://${paramReg}.api.riotgames.com/lol/match/v4/matches/${game.gameId}?api_key=${config.api_key}`)
  328. .then(response => {
  329. let responseData = response.data;
  330. Match.create({
  331. gameId: responseData.gameId,
  332. region: paramReg,
  333. mapId: responseData.mapId,
  334. gameMode: responseData.gameMode,
  335. gameType: responseData.gameType,
  336. createdAt: new Date(),
  337. updatedAt: new Date()
  338. });
  339. responseData.participants.map((player) =>{
  340. checkPlayer(responseData,player,paramReg);
  341.  
  342. })
  343.  
  344.  
  345. },err => console.log(`API PROBLEM: ${err} u insertPlayer`))
  346. .catch(err => console.log(`${err} u fetchMatch`))
  347. }
  348.  
  349. function checkPlayer(game,player,paramReg){
  350. let accId = game.participantIdentities[player.participantId-1].player.accountId;
  351. console.log(`-----------------------------------------------------checkPlayer ${accId}`);
  352. PlayedIn.create({
  353. playerId: accId,
  354. lane: player.timeline.lane,
  355. gameId: game.gameId,
  356. region: paramReg,
  357. champion: player.championId,
  358. role: player.timeline.role,
  359. createdAt: new Date(),
  360. updatedAt: new Date()
  361. })
  362. .catch(err => `problem ${err} u else checkPlayer`);
  363. let summName = game.participantIdentities[player.participantId-1].player.summonerName;
  364. console.log(summName);
  365. Player.findOne({ where: {accountId: accId} })
  366. .then(pl =>{
  367. if(pl === null){
  368. let url = `https://${paramReg}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${summName}?api_key=${config.api_key}`;
  369. axios.get(encodeURI(url))
  370. .then(response =>{
  371. makePlayer(response.data,paramReg);
  372. })
  373. .catch(err=> console.log(`greska u ceckPlayer ${err}`));
  374. }
  375. })
  376. .catch(err => console.log(`problem: ${err} sa upitom player check one u checkPlayer`));
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement