-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyServer.js
More file actions
125 lines (102 loc) · 4.17 KB
/
Copy pathproxyServer.js
File metadata and controls
125 lines (102 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var express = require('express');
var cors = require('cors');
const axios = require('axios');
const { get } = require('http');
const { createProxyMiddleware } = require('http-proxy-middleware');
const bodyParser = require('body-parser');
require('dotenv').config();
var jsonParser = bodyParser.json();
var app = express();
app.use(cors({ origin: '*' }));
const api_key = process.env.riot_api_key;
function getPlayerPUUID(playerName) {
return axios.get("https://na1.api.riotgames.com" + "/lol/summoner/v4/summoners/by-name/" + playerName + "?api_key=" + api_key)
.then(response => {
console.log(response.data);
return response.data.puuid
}).catch(err => err);
}
function getMatchArray(PUUID) {
return axios.get("https://americas.api.riotgames.com" + "/lol/match/v5/matches/by-puuid/" + PUUID + "/ids?start=0&count=100" + "&api_key=" + api_key)
.then(response => {
console.log(response.data);
return response.data
}).catch(err => err);
}
// get the username, profile id, and level
app.get("/playerInfo", async (req, res) => {
const playerName = req.query.playerName;
const api_call = "https://na1.api.riotgames.com" + "/lol/summoner/v4/summoners/by-name/" + playerName + "?api_key=" + api_key;
const allPlayerInfo = await axios.get(api_call)
.then(response => response.data)
.catch(err => err)
console.log(allPlayerInfo);
res.json(allPlayerInfo);
})
// get profile icon number
app.get("/playerIcon", async (req, res) => {
const playerName = req.query.playerName;
const api_call = "https://na1.api.riotgames.com" + "/lol/summoner/v4/summoners/by-name/" + playerName + "?api_key=" + api_key;
try {
const response = await axios.get(api_call);
const playerInfo = response.data; // Correct property name
const playerIcon = playerInfo['profileIconId']
console.log(playerIcon);
res.json(playerIcon);
} catch (error) {
console.error("Error fetching player icon:", error);
res.status(error.response.status || 500).json({
error: "Error fetching player icon"
});
}
});
// get match data
app.get("/getMatches", async (req, res) => {
const playerName = req.query.playerName;
const PUUID = await getPlayerPUUID(playerName);
const matchArray = await getMatchArray(PUUID);
console.log("Matches for player 1:" + matchArray);
res.json(matchArray)
});
// this makes it crash
app.post("/analyzeMatches", jsonParser, async (req, res) => {
const sameMatches = req.body.sameMatches;
const playerName = req.body.playerName;
console.log("Received request to analyzeMatches for:", playerName);
console.log("Received sameMatches:", sameMatches); // Log sameMatches array
console.log("This is the req.body:" + req.body);
let count = 0;
try {
for (const match_id of sameMatches) {
const api_call = "https://americas.api.riotgames.com/lol/match/v5/matches/" + match_id + "?api_key=" + api_key;
try {
const response = await axios.get(api_call);
const matchinfo = response.data;
const participants = matchinfo.info.participants;
for (const participant of participants) {
if (participant.summonerName === playerName) {
const result = participant.win;
if (result) {
count++;
}
}
}
} catch (error) {
console.log("An error occurred:", error.message);
}
}
console.log("Sending response: " + count);
res.json(count);
} catch (error) {
console.log("An error occurred:", error.message);
res.status(500).json({ error: "An error occurred while processing the matches." });
}
});
module.exports = function(app) {
app.use(
createProxyMiddleware(["/playerInfo", "/playerIcon", "/getMatches", "/analyzeMatches"], { target: "http://localhost:8000"})
);
};
app.listen(8000, function() {
console.log("Server started on port 8000");
});