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
| const express = require("express"); const mysql = require("mysql2"); const cors = require("cors");
const app = express();
app.use(cors());
const connection = mysql.createConnection({ host: "localhost", user: "root", database: "test", });
const USER_ID = "1q2w3e"; const GET_USER_QUERY = "SELECT * FROM UserActivePoint WHERE user_id = ?"; const UPDATE_USER_QUERY = "UPDATE UserActivePoint SET last_update_ts = ?, active_point = ? WHERE user_id = ?"; const CHARGE_TIME = 60 * 1000; const MAX_ACTIVE_POINT = 5;
app.get("/", (req, res) => { const now = Date.now(); connection.query(GET_USER_QUERY, [USER_ID], (err, results) => { if (err) throw err; if (results.length === 0) { return connection.query( "INSERT INTO UserActivePoint(user_id, last_update_ts, active_point) VALUES (?, ?, ?)", [USER_ID, 0, 5], (err) => { if (err) throw err; return res.send( JSON.stringify({ code: 200, active_point: 5, last_update_ts: 0, server_ts: now, }) ); } ); } const activePointCalculate = activePointMiddleWare( results[0].active_point, results[0].last_update_ts ); return connection.query( UPDATE_USER_QUERY, [ activePointCalculate.last_update_ts, activePointCalculate.active_point, USER_ID, ], (err) => { if (err) throw err; const now = Date.now(); return res.send( JSON.stringify({ code: 200, active_point: activePointCalculate.active_point, last_update_ts: activePointCalculate.last_update_ts, server_ts: now, }) ); } ); }); });
app.get("/use", (req, res) => { connection.query(GET_USER_QUERY, [USER_ID], (err, results) => { if (err) throw err; if (results[0].active_point === 0) return res.send(JSON.stringify({ code: 400 })); const now = Date.now(); const activePointCalculate = activePointMiddleWare( results[0].active_point, results[0].last_update_ts > 0 ? results[0].last_update_ts : now ); connection.query( UPDATE_USER_QUERY, [ activePointCalculate.last_update_ts, activePointCalculate.active_point - 1, USER_ID, ], (err) => { if (err) throw err; return res.send( JSON.stringify({ code: 200, active_point: activePointCalculate.active_point - 1, last_update_ts: activePointCalculate.last_update_ts, server_ts: now, }) ); } ); }); });
function activePointMiddleWare(active_point, last_update_ts) { const now = Date.now(); const results = { active_point, last_update_ts, }; const chargeActionCount = Math.floor( (now - results.last_update_ts) / CHARGE_TIME ); if (chargeActionCount > 0) { results.active_point += chargeActionCount; results.last_update_ts += chargeActionCount * CHARGE_TIME; } if (chargeActionCount > 0 && results.active_point >= MAX_ACTIVE_POINT) { results.active_point = MAX_ACTIVE_POINT; results.last_update_ts = 0; } return results; }
app.listen(3000, () => console.log("서버 동작중"));
|