소수전공 최종회차 part.2) 아이돌마스터 생일 정보 1일차 결과

2017. 12. 27. 02:02프로젝트(완료)/아이마스 생일 정보(구)

날짜 계산 밑 db 처리, api 제작 완료

app.js

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const express = require('express');
const bodyParser = require('body-parser')
const app = express();
const http = require('http').Server(app)
const birth = require('./data.json');
 
app.use('/', express.static(__dirname + '/public'));
 
app.use(bodyParser.urlencoded({
    extended : true,
}));
 
app.get('/api/get/all', (req, res) => {
    res.send(birth).end()
});
 
//이름 검색 '/api/posts/name'
// name:이름 where:소속(전체일경우-1)
app.post('/api/posts/name', (req, res) => {
    let payload = [];
    if (req.body.where == -1)
      for(i = 0; i < birth.length; i++) {
          if (~birth[i].name.indexOf(req.body.name)){
            payload.push(birth[i]);
          }
      }
    else
      for(i = 0; i < birth.length; i++) {
          if (~birth[i].name.indexOf(req.body.name&& birth[i].where == req.body.where){
            payload.push(birth[i]);
          }
      }
    if(payload.length == 0)
      res.send({"log" : "등록된 아이돌 이름이 없습니다."}).end();
    else
      res.send(payload).end();
});
 
//날짜 검색 '/api/posts/day'
// month:월 day:날짜 where:소속(전체일경우-1)
app.post('/api/posts/day', (req, res) => {
    let payload = [];
    if (req.body.where == -1)
      for(let i = 0; i < birth.length; i++){
          if(birth[i].month == req.body.month && birth[i].day == req.body.day){
            payload.push(birth[i]);
          }
      }
    else
      for(let i = 0; i < birth.length; i++){
          if(birth[i].month == req.body.month && birth[i].day == req.body.day && birth[i].where == req.body.where){
            payload.push(birth[i]);
          }
      }
    if(payload.length == 0)
      res.send({"log" : "그날 생일인 아이돌이 없습니다!"}).end();
    else
      res.send(payload).end();
});
//날짜 검색 '/api/posts/day_near'
// month:월 day:날짜 where:소속(전체일경우-1) index:몇명까지(생일 같으면 1명으로 취급)
app.post('/api/posts/day_near', (req, res) => {
  let month = req.body.month;
  let date = req.body.day;
  let payload = [];
  if(req.body.where == -1)
    for (let i = 0; i<req.body.index; i++){
      nearsearch();
    }
  else
    for (let i = 0; i<req.body.index; i++){
      nearsearch_where();
    }
  res.send(payload).end();
 
  function nearsearch() {
    //만약 같은달 안에 다른 날짜인 생일이 있다면?
    let payload_in = [];
    let sameMnearday = 32;
    for(let i = 0; i<birth.length; i++){
      if(month == birth[i].month){
        if(date < birth[i].day && sameMnearday >= birth[i].day){
          sameMnearday = birth[i].day;
        }
      }
    }
    let upperMnearmonth = 13;
    let upperMnearday = [3232323232323232323232323232];
    if(sameMnearday != 32){
      for(let i = 0; i<birth.length; i++){
        if(birth[i].month == month && birth[i].day == sameMnearday){
          payload_in.push(birth[i])
        }
      }
      date = sameMnearday;
      payload.push(payload_in);
    }
    //같은 달 안에 없다면??
    //우선은 오늘날보다 큰 달 먼저 계산
    else {
      for(let i = 0; i<birth.length; i++){
        if(month < birth[i].month && birth[i].month <= upperMnearmonth){
          upperMnearmonth = birth[i].month;
          if(upperMnearday[birth[i].month] >= birth[i].day){
            upperMnearday[birth[i].month] = birth[i].day;
          }
        }
      }
      let lowerMnearmonth = 13;
      let lowerMnearday = [3232323232323232323232323232];
      if(upperMnearmonth != 13){
        for(let i = 0; i<birth.length; i++){
          if(birth[i].month == upperMnearmonth && birth[i].day == upperMnearday[upperMnearmonth]){
            payload_in.push(birth[i])
          }
        }
 
        month = upperMnearmonth;
        date = upperMnearday[upperMnearmonth];
        payload.push(payload_in);
      }
      //마지막으로 작은 달 계산
      else{
        for(let i = 0; i<birth.length; i++){
          if(month > birth[i].month && birth[i].month <= lowerMnearmonth){
            lowerMnearmonth = birth[i].month;
            if(lowerMnearday[birth[i].month] >= birth[i].day){
              lowerMnearday[birth[i].month] = birth[i].day;
            }
          }
        }
        for(let i = 0; i<birth.length; i++){
          if(birth[i].month == lowerMnearmonth && birth[i].day == lowerMnearday[lowerMnearmonth]){
            payload_in.push(birth[i]);
          }
        }
        month = lowerMnearmonth;
        date = lowerMnearday[lowerMnearmonth];
        payload.push(payload_in);
      }
    }
  }
 
  function nearsearch_where() {
    //만약 같은달 안에 다른 날짜인 생일이 있다면?
    let payload_in = [];
    let sameMnearday = 32;
    for(let i = 0; i<birth.length; i++){
      if(month == birth[i].month && req.body.where == birth[i].where){
        if(date < birth[i].day && sameMnearday >= birth[i].day ){
          sameMnearday = birth[i].day;
        }
      }
    }
    let upperMnearmonth = 13;
    let upperMnearday = [3232323232323232323232323232];
    if(sameMnearday != 32){
      for(let i = 0; i<birth.length; i++){
        if(birth[i].month == month && birth[i].day == sameMnearday && req.body.where == birth[i].where){
          payload_in.push(birth[i])
        }
      }
      date = sameMnearday;
      try {
        let check = true;
        for(let i = 0; i<payload_in.length; i++){
          for(let j = 0; j<payload.length; j++){
            for(let k = 0; k<payload[j].length; k++){
              if(payload[j][k].name == payload_in[i].name){
                check = false;
              }
            }
          }
        }
        if(check)
          payload.push(payload_in);
      } catch (e) {
        console.log(e);
        payload.push(payload_in);
      }
 
    }
    //같은 달 안에 없다면??
    //우선은 오늘날보다 큰 달 먼저 계산
    else {
      for(let i = 0; i<birth.length; i++){
        if(month < birth[i].month && birth[i].month <= upperMnearmonth && req.body.where == birth[i].where){
          upperMnearmonth = birth[i].month;
          if(upperMnearday[birth[i].month] >= birth[i].day){
            upperMnearday[birth[i].month] = birth[i].day;
          }
        }
      }
      let lowerMnearmonth = 13;
      let lowerMnearday = [3232323232323232323232323232];
      if(upperMnearmonth != 13){
        for(let i = 0; i<birth.length; i++){
          if(birth[i].month == upperMnearmonth && birth[i].day == upperMnearday[upperMnearmonth] && req.body.where == birth[i].where){
            payload_in.push(birth[i])
          }
        }
 
        month = upperMnearmonth;
        date = upperMnearday[upperMnearmonth];
        try {
          let check = true;
          for(let i = 0; i<payload_in.length; i++){
            for(let j = 0; j<payload.length; j++){
              for(let k = 0; k<payload[j].length; k++){
                if(payload[j][k].name == payload_in[i].name){
                  check = false;
                }
              }
            }
          }
          if(check)
            payload.push(payload_in);
        } catch (e) {
          console.log(e);
          payload.push(payload_in);
        }
      }
      //마지막으로 작은 달 계산
      else{
        for(let i = 0; i<birth.length; i++){
          if(month > birth[i].month && birth[i].month <= lowerMnearmonth && req.body.where == birth[i].where){
            lowerMnearmonth = birth[i].month;
            if(lowerMnearday[birth[i].month] >= birth[i].day){
              lowerMnearday[birth[i].month] = birth[i].day;
            }
          }
        }
        for(let i = 0; i<birth.length; i++){
          if(birth[i].month == lowerMnearmonth && birth[i].day == lowerMnearday[lowerMnearmonth] && req.body.where == birth[i].where){
            payload_in.push(birth[i]);
          }
        }
        month = lowerMnearmonth;
        date = lowerMnearday[lowerMnearmonth];
        try {
          let check = true;
          for(let i = 0; i<payload_in.length; i++){
            for(let j = 0; j<payload.length; j++){
              for(let k = 0; k<payload[j].length; k++){
                if(payload[j][k].name == payload_in[i].name){
                  check = false;
                }
              }
            }
          }
          if(check)
            payload.push(payload_in);
        } catch (e) {
          console.log(e);
          payload.push(payload_in);
        }
      }
    }
  }
 
});
 
app.get('*', (req, res) => {
    res.status(404).end();
});
 
app.listen(765, () => {
    console.log('localhost:80 에서 실행중');
});
 
cs

script.js(html과 연동되는 자바스크립트)

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
window.onload = () => {
  const today = new Date();
  const todayShow = document.getElementById('todayBirth');
  const soonShow = document.getElementById('soonBirth');
  const allShow = document.getElementById('allshow');
  const img_allshow = document.getElementById('allshow_with_img');
  const nonimg_allshow = document.getElementById('allshow_without_img');
  const fold_allshow = document.getElementById('allshow_fold');
  const rad = document.whereform.whereselect;
  const HOW_MANY_NEAR = 3;
  let prev = null;
  //where 0:765본가 1:765밀리 2:961레온,시이카 3:876디어리스타즈 4:346신데마스 5:315사이도엠 6:아키즈키료 특별
  function whereIs(num){
    switch (num) {
      case 0:
        return("765프로덕션 올스타즈(본가) 소속");
      case 1:
        return("765프로덕션 시어터(밀리마스) 소속");
      case 2:
        return("961프로덕션(OFA,SS) 소속");
      case 3:
        return("876프로덕션(디어리 스타즈) 소속");
      case 4:
        return("346프로덕션(신데렐라 걸즈) 소속");
      case 5:
        return("315프로덕션(sideM) 소속");
      case 6:
        return("315프로덕션(sideM),<br>876프로덕션(디어리 스타즈) 이중 소속");
      default:
        return("소속 오류");
    }
  }
  for(let i = 0; i < rad.length; i++) {
      rad[i].onclick = function() {
          if(this !== prev) {
              prev = this;
              reload_cn_show(this.value)
          }
      };
  }
 
  reload_cn_show(-1);
 
  function reload_cn_show(where) {
    loadJSON_current(where, (response) => {
      const birth = JSON.parse(response)
      let payload = ''
      for(let i = 0; i<birth.length; i++)
        payload += '<div class="idolbox" style="background-color:' + birth[i].color + '"><img src="' + birth[i].img + '"><br>' + birth[i].name + '<br>' + whereIs(birth[i].where) + '<br>' + birth[i].month + '월 ' + birth[i].day + '일' + '</div>';
      if(~Object.keys(birth).indexOf('log'))
        todayShow.innerHTML = "오늘 생일인 아이돌이 없습니다!";
      else
        todayShow.innerHTML = payload;
    });
    loadJSON_near(where, (response) => {
      const birth = JSON.parse(response)
      let payload = ''
      for(let i = 0; i<birth.length; i++){
        for(let j = 0; j<birth[i].length; j++)
          payload += '<div class="idolbox" style="background-color:' + birth[i][j].color + '"><img src="' + birth[i][j].img + '"><br>' + birth[i][j].name + '<br>' + whereIs(birth[i][j].where) + '<br>' + birth[i][j].month + '월 ' + birth[i][j].day + '일' + '</div>';
        payload += '<br>'
      }
      soonShow.innerHTML = payload;
    });
  }
 
  img_allshow.onclick = function() {
    allShow.innerHTML = "";
    loadJSON_all((response) => {
      const birth = JSON.parse(response)
      for(let i = 0; i<birth.length; i++)
        allShow.innerHTML += '<div class="idolbox" style="background-color:' + birth[i].color + '"><img src="' + birth[i].img + '"><br>' + birth[i].name + '<br>' + whereIs(birth[i].where) + '<br>' + birth[i].month + '월 ' + birth[i].day + '일' + '</div>';
    });
  };
  nonimg_allshow.onclick = function() {
    allShow.innerHTML = "";
    loadJSON_all((response) => {
      const birth = JSON.parse(response)
      for(let i = 0; i<birth.length; i++)
        allShow.innerHTML += '<div class="nonimgidolbox" style="background-color:' + birth[i].color + '">' + birth[i].name + '<br>' + whereIs(birth[i].where) + '<br>' + birth[i].month + '월 ' + birth[i].day + '일' + '</div>';
    });
  };
  fold_allshow.onclick = function() {
    allShow.innerHTML = "";
  };
 
 
  function loadJSON_current(where, callback) {
    var xobj = new XMLHttpRequest();
    xobj.open('POST''/api/posts/day'true);
    xobj.setRequestHeader("Content-type""application/x-www-form-urlencoded");
    xobj.onreadystatechange = function () {
      if (xobj.readyState == 4 && xobj.status == "200") {
        callback(xobj.responseText);
      }
    };
    xobj.send('month=' + (today.getMonth() + 1+ '&day=' + today.getDate() + '&where=' + where);
  }
  function loadJSON_near(where, callback) {
    var xobj = new XMLHttpRequest();
    xobj.open('POST''/api/posts/day_near'true);
    xobj.setRequestHeader("Content-type""application/x-www-form-urlencoded");
    xobj.onreadystatechange = function () {
      if (xobj.readyState == 4 && xobj.status == "200") {
        callback(xobj.responseText);
      }
    };
    xobj.send('month=' + (today.getMonth() + 1+ '&day=' + today.getDate() + '&where=' + where + '&index=' + HOW_MANY_NEAR);
  }
  function loadJSON_all(callback) {
    var xobj = new XMLHttpRequest();
    xobj.open('GET''/api/get/all'true);
    xobj.onreadystatechange = function () {
      if (xobj.readyState == 4 && xobj.status == "200") {
        callback(xobj.responseText);
      }
    };
    xobj.send(null);
  }
}
 
cs


실제 사용시


링크 : http://idolbirthday.oa.to


현재까지 사용 가능한 api설명


where의 value값 설명 0:765본가 1:765밀리 2:961레온,시이카(미구현 이유는 생일정보가 없음) 3:876디어리스타즈 4:346신데마스 5:315사이도엠(미구현 데이터셋 추가중) 6:기타(아키즈키료 특별)


http://ngdb.iptime.org:765/api/posts/name


이름 검색


입력값 : {

"name" : 검색할 문자열,-필수

"where" : 소속-필수

}


출력은 아이돌 객체가 1차원 배열 []에 감싸져 나온다.



http://ngdb.iptime.org:765/api/posts/day


생일 날짜 검색


입력값 : {

"month" :  월,-필수

"day" : 일,-필수

"where" : 소속-필수

}


출력은 아이돌 객체가 1차원 배열 []에 감싸져 나온다.(생일이 같은 경우)



http://ngdb.iptime.org:765/api/posts/day


다음 생일 날짜 검색


입력값 : {

"month" :  월,-필수

"day" : 일,-필수

"where" : 소속,-필수

"index" : 받을 양-필수

}


출력은 아이돌 객체가 2차원 배열 []에 감싸져 나온다. 다시 말해 날짜가 같은 것끼리 1차원 배열로 묶이고, 묶인것들이 1차원 배열로 뭉쳐 총 2차원 배열로 나온다.



http://ngdb.iptime.org:765/api/get/all


생일 날짜 검색


입력값 :  없음


모든 아이돌의 정보가 1차원 배열로 출력된다.