Showing
4 changed files
with
57 additions
and
25 deletions
| ... | @@ -9,8 +9,8 @@ var usersRouter = require('./routes/users'); | ... | @@ -9,8 +9,8 @@ var usersRouter = require('./routes/users'); |
| 9 | 9 | ||
| 10 | 10 | ||
| 11 | var app = express(), | 11 | var app = express(), |
| 12 | -server= require('http').createServer(app), | 12 | +server = require('http').createServer(app), |
| 13 | -SOCKETIO=require('./lib/socketio.js'); | 13 | +SOCKETIO = require('./lib/socketio.js'); |
| 14 | 14 | ||
| 15 | // view engine setup | 15 | // view engine setup |
| 16 | app.set('views', path.join(__dirname, 'views')); | 16 | app.set('views', path.join(__dirname, 'views')); | ... | ... |
| ... | @@ -20,12 +20,14 @@ module.exports = (server, app) => { | ... | @@ -20,12 +20,14 @@ module.exports = (server, app) => { |
| 20 | let Discomport_index = {}; | 20 | let Discomport_index = {}; |
| 21 | let Ultra_Violet_index = {}; | 21 | let Ultra_Violet_index = {}; |
| 22 | let Traffic_Accident = {}; | 22 | let Traffic_Accident = {}; |
| 23 | + let Death_Factors = []; | ||
| 23 | let sending_to_client_info = {}; | 24 | let sending_to_client_info = {}; |
| 24 | let client_send = {}; | 25 | let client_send = {}; |
| 25 | let client_name = ""; | 26 | let client_name = ""; |
| 26 | let client_birth ; | 27 | let client_birth ; |
| 27 | let Destiny; | 28 | let Destiny; |
| 28 | let sql; | 29 | let sql; |
| 30 | + | ||
| 29 | 31 | ||
| 30 | let Day3_Weather ={}; | 32 | let Day3_Weather ={}; |
| 31 | let info = {} | 33 | let info = {} |
| ... | @@ -137,6 +139,8 @@ module.exports = (server, app) => { | ... | @@ -137,6 +139,8 @@ module.exports = (server, app) => { |
| 137 | 139 | ||
| 138 | 140 | ||
| 139 | traffic: Traffic_Accident.totalCount, // 교통사고 발생횟수 | 141 | traffic: Traffic_Accident.totalCount, // 교통사고 발생횟수 |
| 142 | + death_factor: "", //사망요인 | ||
| 143 | + death_countdown: 0, //죽기까지 남은 시간(단위: 초) | ||
| 140 | death_prob: 0 //확률 | 144 | death_prob: 0 //확률 |
| 141 | } | 145 | } |
| 142 | console.log("API INFO \n", info); | 146 | console.log("API INFO \n", info); |
| ... | @@ -161,29 +165,50 @@ module.exports = (server, app) => { | ... | @@ -161,29 +165,50 @@ module.exports = (server, app) => { |
| 161 | + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) //+ (info.traffic / 5) | 165 | + info.windspd*1 + (info.rain / 10) + (Math.abs(info.current_temperature - 15) / 10) //+ (info.traffic / 5) |
| 162 | ); | 166 | ); |
| 163 | 167 | ||
| 168 | + // ------------------------------ death_factor 정의 ------------------------------ | ||
| 169 | + | ||
| 170 | + if (info.typhoon == "Y") | ||
| 171 | + Death_Factors.push("태풍에 휩쓸려 사망"); | ||
| 172 | + if (info.lightning == 1) | ||
| 173 | + Death_Factors.push("번개에 맞아 사망"); | ||
| 174 | + if (info.warning == "Y") | ||
| 175 | + Death_Factors.push("우박에 머리를 맞아 사망"); | ||
| 176 | + if (info.windspd*1 >= 2) | ||
| 177 | + Death_Factors.push("애인에게 바람맞아 그 충격으로 인해 사망"); | ||
| 178 | + if (info.heat*1 >= 100) | ||
| 179 | + Death_Factors.push("학점이 너무 낮아 화병으로 사망"); | ||
| 180 | + if (info.discomport*1 >= 40) | ||
| 181 | + Death_Factors.push("날씨가 너무 찝찝해서 사망"); | ||
| 182 | + | ||
| 183 | + Death_Factors.push("심장마비로 사망"); //반드시 하나의 요인은 추가되어야 함 | ||
| 184 | + | ||
| 185 | + info.death_factor = Death_Factors[Math.round(Math.random()*Death_Factors.length)]; //사망요인 하나를 렌덤으로 고른다. | ||
| 186 | + | ||
| 187 | + | ||
| 164 | //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보 | 188 | //이벤트 기반으로 일정 시간 간격으로 클라이언트에게 보낼 정보 |
| 165 | client_send = { | 189 | client_send = { |
| 166 | time: info.time, | 190 | time: info.time, |
| 167 | wind: info.windspd, | 191 | wind: info.windspd, |
| 168 | temperature: info.current_temperature, | 192 | temperature: info.current_temperature, |
| 169 | rain: info.rain, | 193 | rain: info.rain, |
| 170 | - death: info.death_prob | 194 | + death: info.death_prob, |
| 195 | + factor: info.death_factor, | ||
| 171 | }; | 196 | }; |
| 172 | function getRandom_add_prob(min, max) { | 197 | function getRandom_add_prob(min, max) { |
| 173 | return Math.random() * (max - min) + min; | 198 | return Math.random() * (max - min) + min; |
| 174 | - } | 199 | + } |
| 175 | 200 | ||
| 176 | 201 | ||
| 177 | - // 심장이 크게 뛰며 확률이 증가하거나 감소 할 수 있음 | 202 | + //심장이 크게 뛰며 확률이 증가하거나 감소 할 수 있음 |
| 178 | - Math.random() * 2 >= 1 ? client_send.death += getRandom_add_prob(0,5) : client_send.death -= getRandom_add_prob(0,5) ; | 203 | + Math.random() * 2 >= 1 ? client_send.death += getRandom_add_prob(0,5) : client_send.death -= getRandom_add_prob(0,5) ; |
| 179 | 204 | ||
| 180 | 205 | ||
| 181 | - //운명의 장난으로 죽을 확률이 증가하거나 감소함 | 206 | + //운명의 장난으로 죽을 확률이 증가하거나 감소함 |
| 182 | - const rand = Math.floor(Math.random() * 6) * 10//생년월일 중 한자리 뽑음 | 207 | + const rand = Math.floor(Math.random() * 6) * 10//생년월일 중 한자리 뽑음 |
| 183 | 208 | ||
| 184 | - Destiny=client_birth.charAt(rand)/3; //명시적 형 변환 | 209 | + Destiny=client_birth.charAt(rand)/3; //명시적 형 변환 |
| 185 | - if(Destiny==0)Destiny=1; //사용자 잘못 입력했을때 예외처리 | 210 | + if(Destiny==0)Destiny=1; //사용자 잘못 입력했을때 예외처리 |
| 186 | - Math.random() * 2 >= 1 ? client_send.death += Destiny : client_send.death -= Destiny ; | 211 | + Math.random() * 2 >= 1 ? client_send.death += Destiny : client_send.death -= Destiny ; |
| 187 | 212 | ||
| 188 | 213 | ||
| 189 | //만약 날이 너무 안좋아서 확률이 100을 넘긴다면 100으로 예외처리 | 214 | //만약 날이 너무 안좋아서 확률이 100을 넘긴다면 100으로 예외처리 |
| ... | @@ -197,8 +222,8 @@ module.exports = (server, app) => { | ... | @@ -197,8 +222,8 @@ module.exports = (server, app) => { |
| 197 | console.log("emit"); | 222 | console.log("emit"); |
| 198 | 223 | ||
| 199 | //db에 저장 | 224 | //db에 저장 |
| 200 | - sql = "INSERT INTO weather_info (time,wind,temperature,rain,prob) VALUES (?,?,?,?,?)"; | 225 | + sql = "INSERT INTO weather_info (time,wind,temperature,rain,prob,factor) VALUES (?,?,?,?,?,?)"; |
| 201 | - db.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death], (err, result) => { | 226 | + db.query(sql, [client_send.time, client_send.wind, client_send.temperature, client_send.rain, client_send.death, client_send.factor], (err, result) => { |
| 202 | if (err) console.log(err); | 227 | if (err) console.log(err); |
| 203 | }) | 228 | }) |
| 204 | } catch (err) { //promise err or try err catch | 229 | } catch (err) { //promise err or try err catch | ... | ... |
| ... | @@ -81,6 +81,7 @@ router.get('/Cname/:Cname/Cbirth/:Cbirth', (req,res) => { | ... | @@ -81,6 +81,7 @@ router.get('/Cname/:Cname/Cbirth/:Cbirth', (req,res) => { |
| 81 | var wsArr = new Array(); // 풍속 | 81 | var wsArr = new Array(); // 풍속 |
| 82 | var rainArr = new Array(); // 강우량 | 82 | var rainArr = new Array(); // 강우량 |
| 83 | var probArr = new Array(); // 사망 확률 | 83 | var probArr = new Array(); // 사망 확률 |
| 84 | + var factorArr = new Array(); // 사망요인 | ||
| 84 | var dataLen = 0; // 데이터 개수 | 85 | var dataLen = 0; // 데이터 개수 |
| 85 | var empty = 0; // 초기값 유뮤, 0 : 자료 있음, 1 : 자료 없음 | 86 | var empty = 0; // 초기값 유뮤, 0 : 자료 있음, 1 : 자료 없음 |
| 86 | var sql = ""; // 쿼리 | 87 | var sql = ""; // 쿼리 |
| ... | @@ -110,6 +111,7 @@ router.get('/Cname/:Cname/Cbirth/:Cbirth', (req,res) => { | ... | @@ -110,6 +111,7 @@ router.get('/Cname/:Cname/Cbirth/:Cbirth', (req,res) => { |
| 110 | ptArr.unshift(rows[i].temperature); | 111 | ptArr.unshift(rows[i].temperature); |
| 111 | wsArr.unshift(rows[i].wind); | 112 | wsArr.unshift(rows[i].wind); |
| 112 | rainArr.unshift(rows[i].rain); | 113 | rainArr.unshift(rows[i].rain); |
| 114 | + factorArr.unshift(rows[i].factor); | ||
| 113 | count = count + 1; | 115 | count = count + 1; |
| 114 | 116 | ||
| 115 | if (count == 10){ | 117 | if (count == 10){ |
| ... | @@ -128,7 +130,8 @@ router.get('/Cname/:Cname/Cbirth/:Cbirth', (req,res) => { | ... | @@ -128,7 +130,8 @@ router.get('/Cname/:Cname/Cbirth/:Cbirth', (req,res) => { |
| 128 | probArr, | 130 | probArr, |
| 129 | dataLen, | 131 | dataLen, |
| 130 | name, | 132 | name, |
| 131 | - birth | 133 | + birth, |
| 134 | + factorArr, | ||
| 132 | }); | 135 | }); |
| 133 | } | 136 | } |
| 134 | }); | 137 | }); | ... | ... |
| ... | @@ -59,7 +59,7 @@ | ... | @@ -59,7 +59,7 @@ |
| 59 | 59 | ||
| 60 | <script src="/socket.io/socket.io.js"></script> | 60 | <script src="/socket.io/socket.io.js"></script> |
| 61 | <script type="text/javascript"> | 61 | <script type="text/javascript"> |
| 62 | - let client_data = { | 62 | + let client_data = { |
| 63 | birth: "<%=birth%>", | 63 | birth: "<%=birth%>", |
| 64 | name: "<%=name%>" | 64 | name: "<%=name%>" |
| 65 | } | 65 | } |
| ... | @@ -131,7 +131,7 @@ | ... | @@ -131,7 +131,7 @@ |
| 131 | }, | 131 | }, |
| 132 | tooltip: { | 132 | tooltip: { |
| 133 | headerFormat: "<b>{series.name}</b><br/>", | 133 | headerFormat: "<b>{series.name}</b><br/>", |
| 134 | - pointFormat: "{point.x:%Y년%m월%d일 %H시%M분}<br/>의 사망률 : {point.y:.2f}%" | 134 | + pointFormat: "{point.x:%Y년%m월%d일 %H시%M분}<br/>의 사망률 : {point.y:.2f}%<br/>사망요인 : {point.factor}" |
| 135 | }, | 135 | }, |
| 136 | legend: { | 136 | legend: { |
| 137 | //enabled: false | 137 | //enabled: false |
| ... | @@ -150,7 +150,7 @@ | ... | @@ -150,7 +150,7 @@ |
| 150 | enabled: false | 150 | enabled: false |
| 151 | }, | 151 | }, |
| 152 | 152 | ||
| 153 | -plotOptions: { | 153 | + plotOptions: { |
| 154 | series: { | 154 | series: { |
| 155 | marker: { | 155 | marker: { |
| 156 | radius: 6 | 156 | radius: 6 |
| ... | @@ -170,14 +170,16 @@ plotOptions: { | ... | @@ -170,14 +170,16 @@ plotOptions: { |
| 170 | for (; j < 10 - length; j++) { | 170 | for (; j < 10 - length; j++) { |
| 171 | deathArr.push({ | 171 | deathArr.push({ |
| 172 | x: time + i * 60000, | 172 | x: time + i * 60000, |
| 173 | - y: 0 | 173 | + y: 0, |
| 174 | + factor: "없음" | ||
| 174 | }) | 175 | }) |
| 175 | i++; | 176 | i++; |
| 176 | } | 177 | } |
| 177 | 178 | ||
| 178 | - <% probArr.forEach((probArr) => {%> | 179 | + <% for(var n; n < probArr.length; n++) {%> |
| 179 | - var temp; | 180 | + var temp, temp2; |
| 180 | - temp = <%=probArr %>; | 181 | + temp = <%=probArr[n] %>; |
| 182 | + temp2 = <%=factorArr[n] %>; | ||
| 181 | 183 | ||
| 182 | // for(;j<10;j++) | 184 | // for(;j<10;j++) |
| 183 | // { | 185 | // { |
| ... | @@ -190,11 +192,12 @@ plotOptions: { | ... | @@ -190,11 +192,12 @@ plotOptions: { |
| 190 | 192 | ||
| 191 | deathArr.push({ | 193 | deathArr.push({ |
| 192 | x: time + i * 60000, | 194 | x: time + i * 60000, |
| 193 | - y: temp | 195 | + y: temp, |
| 196 | + factor: temp2 | ||
| 194 | }) | 197 | }) |
| 195 | i++; | 198 | i++; |
| 196 | 199 | ||
| 197 | - <%}) %> | 200 | + <%} %> |
| 198 | 201 | ||
| 199 | return deathArr; | 202 | return deathArr; |
| 200 | })(), | 203 | })(), |
| ... | @@ -205,7 +208,6 @@ plotOptions: { | ... | @@ -205,7 +208,6 @@ plotOptions: { |
| 205 | 208 | ||
| 206 | }); | 209 | }); |
| 207 | 210 | ||
| 208 | - | ||
| 209 | var chart2 = Highcharts.chart("container2", { | 211 | var chart2 = Highcharts.chart("container2", { |
| 210 | chart: { | 212 | chart: { |
| 211 | type: "spline", | 213 | type: "spline", |
| ... | @@ -593,16 +595,18 @@ plotOptions: { | ... | @@ -593,16 +595,18 @@ plotOptions: { |
| 593 | 595 | ||
| 594 | 596 | ||
| 595 | }); | 597 | }); |
| 598 | + | ||
| 596 | var socket = io.connect('/', { transports: ['websocket'], upgrade: false }); | 599 | var socket = io.connect('/', { transports: ['websocket'], upgrade: false }); |
| 597 | socket.emit("connection", client_data); | 600 | socket.emit("connection", client_data); |
| 598 | socket.on("weatherInfo_minutely_send_to_client", (info) => { //서버에서 client에게 메세지 전송 | 601 | socket.on("weatherInfo_minutely_send_to_client", (info) => { //서버에서 client에게 메세지 전송 |
| 599 | console.log(info); | 602 | console.log(info); |
| 600 | 603 | ||
| 601 | - var date = new Date().getTime(); | 604 | + var date = new Date().getTime(); |
| 602 | 605 | ||
| 603 | chart1.series[0].addPoint({ | 606 | chart1.series[0].addPoint({ |
| 604 | x: date, | 607 | x: date, |
| 605 | y: info.death, | 608 | y: info.death, |
| 609 | + factor: info.factor, | ||
| 606 | color:"#FFE08C" | 610 | color:"#FFE08C" |
| 607 | }); | 611 | }); |
| 608 | chart2.series[0].addPoint({ | 612 | chart2.series[0].addPoint({ | ... | ... |
-
Please register or login to post a comment