Suyeon Jung

Add grade function to category

......@@ -5,12 +5,4 @@
.card-body {
text-align: center;
}
.alert{
text-align : center;
width : 50%;
display: inline-block;
margin-left: auto;
margin-right: auto;
}
\ No newline at end of file
......
......@@ -13,144 +13,199 @@ let user_dong;
let userLocation;
let user_latitude;
let user_longitude;
// 등급을 위한 변수
let food_A = 2;
let food_B = 1;
let fast_food_A = 9;
let fast_food_B = 5;
let library_A = 1;
let institute_A = 60;
let institute_B = 20;
let park_A = 10;
let park_B = 5;
let cctv_A = 30;
let cctv_B = 10;
let light_A = 2;
let light_B = 1;
let culture_A = 25;
let culture_B = 10;
let shop_A = 5;
let shop_B = 2;
let gym_A = 20;
let gym_B = 10;
// 결과 중 사용자 근처인지 검사하기 위한 변수
let user_target;
let sigun = [{
sigun_name: '가평군',
sigun_code: 41820
},
{
sigun_name: '경기도',
sigun_code: 41000
},
{
sigun_name: '고양시',
sigun_code: 41280
},
{
sigun_name: '과천시',
sigun_code: 41290
},
{
sigun_name: '광명시',
sigun_code: 41210
},
{
sigun_name: '광주시',
sigun_code: 41610
},
{
sigun_name: '구리시',
sigun_code: 41310
},
{
sigun_name: '군포시',
sigun_code: 41410
},
{
sigun_name: '김포시',
sigun_code: 41570
},
{
sigun_name: '남양주시',
sigun_code: 41360
},
{
sigun_name: '동두천시',
sigun_code: 41250
},
{
sigun_name: '부천시',
sigun_code: 41190
},
{
sigun_name: '성남시',
sigun_code: 41130
},
{
sigun_name: '수원시',
sigun_code: 41110
},
{
sigun_name: '시흥시',
sigun_code: 41390
},
{
sigun_name: '안산시',
sigun_code: 41270
},
{
sigun_name: '안성시',
sigun_code: 41550
},
{
sigun_name: '안양시',
sigun_code: 41170
},
{
sigun_name: '양주시',
sigun_code: 41630
},
{
sigun_name: '양평군',
sigun_code: 41830
},
{
sigun_name: '여주시',
sigun_code: 41670
},
{
sigun_name: '연천군',
sigun_code: 41800
},
{
sigun_name: '오산시',
sigun_code: 41370
},
{
sigun_name: '용인시',
sigun_code: 41460
},
{
sigun_name: '의왕시',
sigun_code: 41430
},
{
sigun_name: '의정부시',
sigun_code: 41150
},
{
sigun_name: '이천시',
sigun_code: 41500
},
{
sigun_name: '파주시',
sigun_code: 41480
},
{
sigun_name: '평택시',
sigun_code: 41220
},
{
sigun_name: '포천시',
sigun_code: 41650
},
{
sigun_name: '하남시',
sigun_code: 41450
},
{
sigun_name: '화성시',
sigun_code: 41590
},
]
sigun_name: '가평군',
sigun_code: 41820
},
{
sigun_name: '경기도',
sigun_code: 41000
},
{
sigun_name: '고양시',
sigun_code: 41280
},
{
sigun_name: '과천시',
sigun_code: 41290
},
{
sigun_name: '광명시',
sigun_code: 41210
},
{
sigun_name: '광주시',
sigun_code: 41610
},
{
sigun_name: '구리시',
sigun_code: 41310
},
{
sigun_name: '군포시',
sigun_code: 41410
},
{
sigun_name: '김포시',
sigun_code: 41570
},
{
sigun_name: '남양주시',
sigun_code: 41360
},
{
sigun_name: '동두천시',
sigun_code: 41250
},
{
sigun_name: '부천시',
sigun_code: 41190
},
{
sigun_name: '성남시',
sigun_code: 41130
},
{
sigun_name: '수원시',
sigun_code: 41110
},
{
sigun_name: '시흥시',
sigun_code: 41390
},
{
sigun_name: '안산시',
sigun_code: 41270
},
{
sigun_name: '안성시',
sigun_code: 41550
},
{
sigun_name: '안양시',
sigun_code: 41170
},
{
sigun_name: '양주시',
sigun_code: 41630
},
{
sigun_name: '양평군',
sigun_code: 41830
},
{
sigun_name: '여주시',
sigun_code: 41670
},
{
sigun_name: '연천군',
sigun_code: 41800
},
{
sigun_name: '오산시',
sigun_code: 41370
},
{
sigun_name: '용인시',
sigun_code: 41460
},
{
sigun_name: '의왕시',
sigun_code: 41430
},
{
sigun_name: '의정부시',
sigun_code: 41150
},
{
sigun_name: '이천시',
sigun_code: 41500
},
{
sigun_name: '파주시',
sigun_code: 41480
},
{
sigun_name: '평택시',
sigun_code: 41220
},
{
sigun_name: '포천시',
sigun_code: 41650
},
{
sigun_name: '하남시',
sigun_code: 41450
},
{
sigun_name: '화성시',
sigun_code: 41590
},
]
// 등급 평가하는 함수
let evalGrade = function(A, B, count) {
if (count >= A) {
return 'A';
} else if (count >= B) {
return 'B';
} else {
return 'C';
}
}
let gradetoScore = function(grade) {
if (grade == 'A') {
return 3;
} else if (grade == 'B') {
return 2;
} else {
return 1;
}
}
let scoretoGrade = function(score) {
if (score == 3) {
return 'A';
} else if (score == 2) {
return 'B';
} else {
return 'C';
}
}
let avgGrade = function(grade1, grade2) {
score1 = gradetoScore(grade1);
score2 = gradetoScore(grade2);
return scoretoGrade(Math.round((score1 + score2) / 2));
}
// 결과 중 사용자 근처인지 검사하기 위한 함수
let userArea = function(target, targetKey, user) {
if (target[targetKey] != null && target[targetKey].includes(user)) {
console.log('param1', target[targetKey]);
console.log('param2', user);
console.log('result', target[targetKey].includes(user))
return true;
} else {
return false;
......@@ -159,7 +214,7 @@ let userArea = function(target, targetKey, user) {
// 사용자가 index 페이지에서 주소를 입력하고 넘어오면
router.post('/', function(req, res) {
console.log(req.body);
user_sigun = req.body.sigun;
user_gu = req.body.gu;
user_dong = req.body.dong;
......@@ -188,7 +243,7 @@ router.post('/', function(req, res) {
// sigun code 받는 부분
for (var key in sigun) {
if (userLocation.includes(sigun[key].sigun_name)) {
console.log('hey!');
SIGUN_CODE = sigun[key].sigun_code;
}
console.log(sigun[key].sigun_name);
......@@ -220,18 +275,18 @@ router.get('/food', function(req, res) {
}, function(err, response, body) {
if (!err && res.statusCode == 200) {
let food_result = JSON.parse(body);
// console.log('food_result', food_result.PlaceThatDoATasteyFoodSt[1]);
let stores = [];
for (let i = 0; i < food_result.PlaceThatDoATasteyFoodSt[1].row.length; i++) {
// 상호명, 음식명, 전화번호, 도로명주소(REFINE_ROADNM_ADDR), 지번 주소(REFINE_LOTNO_ADDR)
store = food_result.PlaceThatDoATasteyFoodSt[1].row[i];
if (userArea(store, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('store', store);
stores.push(store);
}
}
console.log('food_result', food_result);
console.log('stores', stores);
food_grade = evalGrade(food_A, food_B, stores.length);
request({
url: fastfood_url + qs,
method: 'GET'
......@@ -243,11 +298,13 @@ router.get('/food', function(req, res) {
fast_food_store = fast_food_result.Genrestrtfastfood[1].row[i];
// 폐업인 지점 제외
if (fast_food_store['BSN_STATE_NM'] != '폐업' && userArea(fast_food_store, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('fast_store_name', fast_food_store);
fast_food_stores.push(fast_food_store);
}
}
res.render('result', { category: 'food', stores: stores, fast_food_stores: fast_food_stores, userLocation: userLocation, grade: 'A' });
fast_food_grade = evalGrade(fast_food_A, fast_food_B, fast_food_stores.length);
grade = avgGrade(food_grade, fast_food_grade);
res.render('result', { category: 'food', stores: stores, fast_food_stores: fast_food_stores, userLocation: userLocation, grade: grade });
}
})
......@@ -267,32 +324,31 @@ router.get('/school', function(req, res) {
}, function(err, response, body) {
if (!err && res.statusCode == 200) {
let library_result = JSON.parse(body);
console.log(library_result);
let librarys = [];
for (let i = 0; i < library_result.Tbggibllbrm[1].row.length; i++) {
library = library_result.Tbggibllbrm[1].row[i];
if (userArea(library, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('library', library);
librarys.push(library);
}
}
library_grade = evalGrade(library_A, 0.5, librarys.length);
request({
url: institute_url + qs,
method: 'GET'
}, function(err, response, body) {
if (!err && res.statusCode == 200) {
let institute_result = JSON.parse(body);
console.log(institute_result);
let institutes = [];
for (let i = 0; i < institute_result.TninsttInstutM[1].row.length; i++) {
institute = institute_result.TninsttInstutM[1].row[i];
if (userArea(institute, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('institute', institute);
institutes.push(institute);
}
}
res.render('result', { category: 'school', librarys: librarys, institutes: institutes, userLocation: userLocation, grade: 'A' });
institute_grade = evalGrade(institute_A, institute_B, institutes);
grade = avgGrade(library_grade, institute_grade);
res.render('result', { category: 'school', librarys: librarys, institutes: institutes, userLocation: userLocation, grade: grade });
}
})
}
......@@ -307,10 +363,8 @@ router.get('/park', function(req, res) {
url: citypark_url + qs,
method: 'GET'
}, function(err, response, body) {
// console.log('park_result', park_result.CityPark[0].head);
if (!err && res.statusCode == 200) {
let city_park_result = JSON.parse(body);
console.log('park', city_park_result)
let city_parks = [];
for (let i = 0; i < city_park_result.CityPark[1].row.length; i++) {
city_park = city_park_result.CityPark[1].row[i];
......@@ -318,7 +372,8 @@ router.get('/park', function(req, res) {
city_parks.push(city_park);
}
}
res.render('result', { category: 'park', city_parks: city_parks, userLocation: userLocation, grade: 'A' });
grade = evalGrade(park_A, park_B, city_parks.length);
res.render('result', { category: 'park', city_parks: city_parks, userLocation: userLocation, grade: grade });
}
})
});
......@@ -331,7 +386,6 @@ router.get('/transport', function(req, res) {
let qs = `?lang=0&x=${user_latitude}&y=${user_longitude}&radius=1000&stationClass=2&apiKey=${ODSAY_API_KEY}`;
let busCnt = 0;
let subwayCnt = 0;
console.log(bus_url);
request({
url: bus_url + qs,
......@@ -343,13 +397,10 @@ router.get('/transport', function(req, res) {
let bus_result = JSON.parse(body);
// console.log('bus_result', bus_result.result.station);
let busStations = [];
console.log(bus_result.result)
for (let i = 0; i < bus_result.result.station.length; i++) {
station = bus_result.result.station[i];
console.log('station', station)
busStations.push(station);
}
console.log('busStations', busStations);
request({
url: subway_url + qs,
method: 'GET'
......@@ -357,14 +408,13 @@ router.get('/transport', function(req, res) {
if (!err && res.statusCode == 200) {
let subway_result = JSON.parse(body);
let subwayStations = [];
console.log('subway_result', subway_result);
for (let i = 0; i < subway_result.result.station.length; i++) {
station = subway_result.result.station[i];
subwayStations.push(station);
}
console.log(busStations);
console.log('subwayStaions', subwayStations);
res.render('result', { category: 'transport', busStations: busStations, subwayStations: subwayStations, userLocation: userLocation, grade: 'A' })
res.render('result', { category: 'transport', busStations: busStations, subwayStations: subwayStations, userLocation: userLocation, grade: grade })
}
})
......@@ -384,14 +434,13 @@ router.get('/safe', function(req, res) {
if (!err && res.statusCode == 200) {
let cctv_result = JSON.parse(body);
let cctvs = [];
console.log(cctv_result);
for (let i = 0; i < cctv_result.CCTV[1].row.length; i++) {
cctv = cctv_result.CCTV[1].row[i];
if (userArea(cctv, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('cctv', cctv);
cctvs.push(cctv);
}
}
cctv_grade = evalGrade(cctv_A, cctv_B, cctvs.length);
request({
url: light_url + qs,
method: 'GET'
......@@ -399,15 +448,15 @@ router.get('/safe', function(req, res) {
if (!err && res.statusCode == 200) {
let light_result = JSON.parse(body);
let lights = [];
console.log(light_result);
for (let i = 0; i < light_result.SECRTLGT[1].row.length; i++) {
light = light_result.SECRTLGT[1].row[i];
if (userArea(light, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('light', light);
lights.push(light);
}
}
res.render('result', { category: 'safe', cctvs: cctvs, lights: lights, userLocation: userLocation, grade: 'A' });
light_grade = evalGrade(light_A, light_B, lights.length);
grade = avgGrade(cctv_grade, light_grade);
res.render('result', { category: 'safe', cctvs: cctvs, lights: lights, userLocation: userLocation, grade: grade });
}
})
}
......@@ -425,17 +474,17 @@ router.get('/culture', function(req, res) {
}, function(err, response, body) {
if (!err && res.statusCode == 200) {
let movie_result = JSON.parse(body);
console.log(movie_result);
let movies = [];
for (let i = 0; i < movie_result.MovieTheater[1].row.length; i++) {
movie = movie_result.MovieTheater[1].row[i];
// 폐업인 지점 제외
if (!movie['BSN_STATE_NM'].includes('폐업') && userArea(movie, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('movie', movie);
movies.push(movie);
}
}
res.render('result', { category: 'culture', movies: movies, userLocation: userLocation, grade: 'A' });
grade = evalGrade(culture_A, culture_B, movies.length);
res.render('result', { category: 'culture', movies: movies, userLocation: userLocation, grade: grade });
}
});
});
......@@ -456,11 +505,11 @@ router.get('/shop', function(req, res) {
shop = shop_result.MrktStoreM[1].row[i];
// 폐업인 지점 제외
if (userArea(shop, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('shop', shop);
shops.push(shop);
}
}
res.render('result', { category: 'shop', shops: shops, userLocation: userLocation, grade: 'A' });
grade = evalGrade(shop_A, shop_B, shops.length)
res.render('result', { category: 'shop', shops: shops, userLocation: userLocation, grade: grade });
}
})
......@@ -476,16 +525,14 @@ router.get('/gym', function(req, res) {
if (!err && res.statusCode == 200) {
let gym_result = JSON.parse(body);
let gyms = [];
console.log(gym_result.PhysicaFitnessTrainingPlace[1]);
for (let i = 0; i < gym_result.PhysicaFitnessTrainingPlace[1].row.length; i++) {
gym = gym_result.PhysicaFitnessTrainingPlace[1].row[i];
console.log(gym);
if (userArea(gym, 'REFINE_LOTNO_ADDR', user_target)) {
console.log('gym', gym);
gyms.push(gym);
}
}
res.render('result', { category: 'gym', gyms: gyms, userLocation: userLocation, grade: 'A' });
grade = evalGrade(gym_A, gym_B, gyms.length)
res.render('result', { category: 'gym', gyms: gyms, userLocation: userLocation, grade: grade });
}
})
});
......
<div class="alert alert-primary" role="alert">
<h3>평가 등급 : <%=grade%></h3>
</div>
\ No newline at end of file
<%if(grade == 'A'){%>
<div class="alert alert-primary" role="alert">
<h3>평가 등급 :
<%=grade%>
</h3>
</div>
<%} else if(grade == 'B'){%>
<div class="alert alert-warning" role="alert">
<h3>평가 등급 :
<%=grade%>
</h3>
</div>
<%} else{%>
<div class="alert alert-danger" role="alert">
<h3>평가 등급 :
<%=grade%>
</h3>
</div>
<%}%>
\ No newline at end of file
......