Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.22 KB | None | 0 0
  1. // A socket.io alapú chat magja.
  2. var socketio = require('socket.io');
  3. var mysql = require('mysql');
  4. var connect = require('connect');
  5. var cookieParser = require('cookie-parser');
  6. var pool = mysql.createPool({
  7. host: 'localhost',
  8. user: 'root',
  9. password: 'root',
  10. database: 'lookingforgroup',
  11. connectionLimit: 100,
  12. debug: false,
  13. multipleStatements: true
  14. });
  15.  
  16. // A függvényt a /bin/www fájlból hívjuk meg. A függvényen belül eseményeket definiálunk.
  17. function sockets(server) {
  18. var io = socketio(server);
  19.  
  20. // Ha kapcsolódva van egy user a chathez.
  21. io.on('connection', function(socket){
  22.  
  23. // Chatről való lekapcsolódáskor ez történjen
  24. socket.on('disconnect', function(){
  25.  
  26. });
  27.  
  28. // Feliratkozás az értesítés-"szobába", illetve a chatszobába.
  29. socket.on('subscribe', function (room) {
  30. socket.join(room);
  31. });
  32.  
  33. socket.on('send_joinrequest', function(data) {
  34. pool.getConnection(function(err, connection) {
  35.  
  36. var offerMembershipColumnName = "";
  37.  
  38. switch (data.offerType) {
  39. case "guild":
  40. offerMembershipColumnName = "member_of_guild";
  41. break;
  42. case "arena2v2":
  43. offerMembershipColumnName = "member_of_2v2";
  44. break;
  45. case "arena3v3":
  46. offerMembershipColumnName = "member_of_3v3";
  47. break;
  48. case "arena5v5":
  49. offerMembershipColumnName = "member_of_5v5";
  50. break;
  51. }
  52.  
  53. connection.query('SELECT ' + offerMembershipColumnName + ' FROM wow_chars WHERE char_id = ?', [data.charData.charId], function(err, results) {
  54. if (err) {
  55. connection.release();
  56. throw err;
  57. }
  58.  
  59. if (results[0][offerMembershipColumnName] == null) {
  60. connection.query('INSERT INTO wow_joinrequests(char_id, offer_id, state) VALUES (?,?,?)', [data.charData.charId, data.offerId, 0], function(err, results2) {
  61. if (err) {
  62. connection.release();
  63. throw err;
  64. }
  65. connection.query('SELECT user_id, title FROM wow_offers WHERE offer_id = ?', [data.offerId], function(err, results3) {
  66. if (err) {
  67. connection.release();
  68. throw err;
  69. }
  70.  
  71. var userToNotify = "user_" + results3[0].user_id;
  72. console.log(userToNotify);
  73. var userToNotifyOnlyId = userToNotify.split("_")[1];
  74. var notifyMessage = "<a href='/wow/profile/" + data.userData.user_id + "'>" +
  75. data.userData.battletag + "</a> felhasználó a <a href='/wow/character/" +
  76. data.charData.charId + "'>" + data.charData.charName + "</a> karakterével fejlenetkezett a <a href='/wow/offers/" +
  77. data.offerId + "'>" + results3[0].title + "</a> című hirdetésre.";
  78.  
  79. socket.join(userToNotify);
  80. socket.broadcast.to(userToNotify).emit('receive_notify', {
  81. message: notifyMessage
  82. });
  83. socket.leave(userToNotify);
  84.  
  85. var selfNotify = "user_" + data.userData.userId;
  86. var alertMessage = "Sikeres jelentkezés!";
  87.  
  88. socket.nsp.to(selfNotify).emit('receive_alert', {
  89. message: alertMessage
  90. });
  91.  
  92. connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES (?,?,?)', [userToNotifyOnlyId, notifyMessage, 0], function(err, results4) {
  93. if (err) {
  94. connection.release();
  95. throw err;
  96. }
  97. connection.release();
  98. });
  99. });
  100. });
  101. } else {
  102. connection.release();
  103. var selfNotify = "user_" + data.userData.userId;
  104. var alertMessage = "Ezzel a karaktereddel már tagja vagy egy " + data.offerType + " típusú csoportnak.";
  105.  
  106. socket.nsp.to(selfNotify).emit('receive_alert', {
  107. message: alertMessage
  108. });
  109. }
  110. });
  111. });
  112. });
  113.  
  114. socket.on('accept_joinrequest', function(data) {
  115. pool.getConnection(function(err, connection) {
  116. connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ? AND state = ?;', [1, data.requestId, 0], function(err, results) {
  117. if (err) {
  118. connection.release();
  119. throw err;
  120. }
  121.  
  122. if (results.affectedRows > 0) {
  123. var userToNotify = "user_" + data.charUserId;
  124. var notifyMessage = "<a href='/wow/profile/" + data.offerUserId + "'>" + data.offerUserBattletag +
  125. "</a> felhasználó elfogadta a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
  126. "</a> karaktereddel tett jelentkezésed a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
  127. "</a> hirdetésre.";
  128.  
  129. socket.join(userToNotify);
  130. socket.broadcast.to(userToNotify).emit('receive_notify', {
  131. message: notifyMessage
  132. });
  133. socket.leave(userToNotify);
  134.  
  135. connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.charUserId, notifyMessage, 0], function(err, results2) {
  136. if (err) {
  137. connection.release();
  138. throw err;
  139. }
  140. connection.release();
  141. });
  142. } else {
  143. connection.release();
  144. var selfNotify = "user_" + data.offerUserId;
  145. var alertMessage = "A jelentkezés állapota az utolsó betöltés óta megváltozott. Töltsd újra az oldalt.";
  146.  
  147. socket.nsp.to(selfNotify).emit('receive_alert_jreq_fo', {
  148. message: alertMessage
  149. });
  150. }
  151. });
  152. });
  153. });
  154.  
  155. socket.on('decline_joinrequest', function(data) {
  156. pool.getConnection(function(err, connection) {
  157. connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ? AND (state = ? OR state = ?);', [2, data.requestId, 0, 1], function(err, results) {
  158. if (err) {
  159. connection.release();
  160. throw err;
  161. }
  162.  
  163. if (results.affectedRows > 0) {
  164. var userToNotify = "user_" + data.charUserId;
  165. var notifyMessage = "<a href='/wow/profile/" + data.offerUserId + "'>" + data.offerUserBattletag +
  166. "</a> felhasználó elutasította a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
  167. "</a> karaktereddel tett jelentkezésed a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
  168. "</a> hirdetésre.";
  169.  
  170. socket.join(userToNotify);
  171. socket.broadcast.to(userToNotify).emit('receive_notify', {
  172. message: notifyMessage
  173. });
  174. socket.leave(userToNotify);
  175.  
  176. connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.charUserId, notifyMessage, 0], function(err, results2) {
  177. if (err) {
  178. connection.release();
  179. throw err;
  180. }
  181. connection.release();
  182. });
  183. } else {
  184. connection.release();
  185. var selfNotify = "user_" + data.offerUserId;
  186. var alertMessage = "A jelentkezés állapota az utolsó betöltés óta megváltozott. Töltsd újra az oldalt.";
  187.  
  188. socket.nsp.to(selfNotify).emit('receive_alert_jreq_fo', {
  189. message: alertMessage
  190. });
  191. }
  192. });
  193. });
  194. });
  195.  
  196. socket.on('cancel_joinrequest', function(data) {
  197. pool.getConnection(function(err, connection) {
  198. connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ? AND (state = ? OR state = ?);', [3, data.requestId, 0, 1], function(err, results) {
  199. if (err) {
  200. connection.release();
  201. throw err;
  202. }
  203.  
  204. if (results.affectedRows > 0) {
  205. var userToNotify = "user_" + data.offerUserId;
  206. console.log(userToNotify);
  207. var notifyMessage = "<a href='/wow/profile/" + data.charUserId + "'>" + data.charUserBattletag +
  208. "</a> felhasználó visszavonta a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
  209. "</a> karakterével tett jelentkezését a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
  210. "</a> hirdetésedre.";
  211.  
  212. socket.join(userToNotify);
  213. socket.broadcast.to(userToNotify).emit('receive_notify', {
  214. message: notifyMessage
  215. });
  216. socket.leave(userToNotify);
  217.  
  218. connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.offerUserId, notifyMessage, 0], function(err, results2) {
  219. if (err) {
  220. connection.release();
  221. throw err;
  222. }
  223. connection.release();
  224. });
  225. } else {
  226. connection.release();
  227. var selfNotify = "user_" + data.charUserId;
  228. var alertMessage = "A jelentkezés állapota az utolsó betöltés óta megváltozott. Töltsd újra az oldalt.";
  229.  
  230. socket.nsp.to(selfNotify).emit('receive_alert_jreq_fm', {
  231. message: alertMessage
  232. });
  233. }
  234. });
  235. });
  236. });
  237.  
  238. socket.on('resend_joinrequest', function(data) {
  239. pool.getConnection(function(err, connection) {
  240. connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ?;', [0, data.requestId], function(err, results) {
  241. if (err) {
  242. connection.release();
  243. throw err;
  244. }
  245.  
  246. var userToNotify = "user_" + data.offerUserId;
  247. console.log(userToNotify);
  248. var notifyMessage = "<a href='/wow/profile/" + data.charUserId + "'>" + data.charUserBattletag +
  249. "</a> felhasználó újrajelentkezett a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
  250. "</a> karakterével tett jelentkezését a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
  251. "</a> hirdetésedre.";
  252.  
  253. socket.join(userToNotify);
  254. socket.broadcast.to(userToNotify).emit('receive_notify', {
  255. message: notifyMessage
  256. });
  257. socket.leave(userToNotify);
  258.  
  259. connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.offerUserId, notifyMessage, 0], function(err, results2) {
  260. if (err) {
  261. connection.release();
  262. throw err;
  263. }
  264. connection.release();
  265. });
  266. });
  267. });
  268. });
  269.  
  270. socket.on('join_offer', function(data) {
  271. pool.getConnection(function(err, connection) {
  272. connection.query('UPDATE wow_joinrequests SET state = ? WHERE request_id = ?;', [4, data.requestId], function(err, results) {
  273. if (err) {
  274. connection.release();
  275. throw err;
  276. }
  277.  
  278. var userToNotify = "user_" + data.offerUserId;
  279. var notifyMessage = "<a href='/wow/profile/" + data.charUserId + "'>" + data.charUserBattletag +
  280. "</a> felhasználó belépett a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
  281. "</a> karakterével a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
  282. "</a> csoportodba.";
  283.  
  284. socket.join(userToNotify);
  285. socket.broadcast.to(userToNotify).emit('receive_notify', {
  286. message: notifyMessage
  287. });
  288. socket.leave(userToNotify);
  289.  
  290. connection.query('INSERT INTO wow_notifications(user_id, text, seen) VALUES(?,?,?)', [data.offerUserId, notifyMessage, 0], function(err, results2) {
  291. if (err) {
  292. connection.release();
  293. throw err;
  294. }
  295.  
  296. var offerMembershipColumnName = "";
  297.  
  298. switch (data.offerType) {
  299. case "guild":
  300. offerMembershipColumnName = "member_of_guild";
  301. break;
  302. case "arena2v2":
  303. offerMembershipColumnName = "member_of_2v2";
  304. break;
  305. case "arena3v3":
  306. offerMembershipColumnName = "member_of_3v3";
  307. break;
  308. case "arena5v5":
  309. offerMembershipColumnName = "member_of_5v5";
  310. break;
  311. }
  312.  
  313. connection.query('UPDATE wow_chars SET ' + offerMembershipColumnName + ' = ? WHERE char_id = ?', [data.offerId, data.charId], function(err, results3) {
  314. if (err) {
  315. connection.release();
  316. throw err;
  317. }
  318. connection.query('UPDATE wow_joinrequests SET state = ? WHERE char_id = ? AND state <> ?;', [5, data.charId, 4], function(err, results4) {
  319. if (err) {
  320. connection.release();
  321. throw err;
  322. }
  323. connection.query('INSERT INTO wow_chatroom_memberships(chatroom_id, user_id) VALUES(?,?)', [data.offerChatroomId, data.charUserId], function(err, results5) {
  324. if (err) {
  325. connection.release();
  326. throw err;
  327. }
  328. connection.query('INSERT INTO wow_roles(char_id, offer_id, role) VALUES (?,?,?)', [data.charUserId, data.offerId, "Default"], function(err, results6) {
  329. if (err) {
  330. connection.release();
  331. throw err;
  332. }
  333. connection.release();
  334. });
  335. });
  336. });
  337. });
  338. });
  339. });
  340. });
  341. });
  342.  
  343. socket.on('leave_group', function(data) {
  344. var offerMembershipColumnName = "";
  345.  
  346. switch (data.offerType) {
  347. case "guild":
  348. offerMembershipColumnName = "member_of_guild";
  349. break;
  350. case "arena2v2":
  351. offerMembershipColumnName = "member_of_2v2";
  352. break;
  353. case "arena3v3":
  354. offerMembershipColumnName = "member_of_3v3";
  355. break;
  356. case "arena5v5":
  357. offerMembershipColumnName = "member_of_5v5";
  358. break;
  359. }
  360.  
  361. pool.getConnection(function(err, connection) {
  362. connection.query('UPDATE wow_chars SET ' + offerMembershipColumnName + ' = null WHERE char_id = ?;', [data.charId], function(err, results) {
  363. if (err) {
  364. connection.release();
  365. throw err;
  366. }
  367.  
  368. var userToNotify = "user_" + data.offerUserId;
  369. var notifyMessage = "<a href='/wow/profile/" + data.charUserId + "'>" + data.charUserBattletag +
  370. "</a> felhasználó kilépett a(z) <a href='/wow/character/" + data.charId + "'>" + data.charName +
  371. "</a> karakterével a(z) <a href='wow/offers/" + data.offerId + "'>" + data.offerTitle +
  372. "</a> csoportból.";
  373.  
  374. socket.join(userToNotify);
  375. socket.broadcast.to(userToNotify).emit('receive_notify', {
  376. message: notifyMessage
  377. });
  378. socket.leave(userToNotify);
  379.  
  380. connection.query('UPDATE wow_joinrequests SET state = ? WHERE char_id = ? AND state = ?;', [3, data.charId, 5], function(err, results) {
  381. if (err) {
  382. connection.release();
  383. throw err;
  384. }
  385. connection.release();
  386. });
  387. });
  388. });
  389. });
  390.  
  391. socket.on('notifies_seen', function(user_id) {
  392. pool.getConnection(function(err, connection) {
  393. connection.query('UPDATE wow_notifications SET seen = ? WHERE user_id = ?;', [1, user_id], function(err, results5) {
  394. if (err) {
  395. connection.release();
  396. throw err;
  397. }
  398. connection.release();
  399. });
  400. });
  401. });
  402.  
  403. // Üzenetküldés esetén ez történik: az adott chatszobába küldjük az
  404. // üzenetet úgy, hogy kiváltunk egy eseményt, amely hatására a kliens oldalon
  405. // megjelenik az üzenet.
  406. socket.on('send message', function(data) {
  407. socket.broadcast.to(data.room).emit('conversation private post', {
  408. // username == lejjebb a subjectName
  409. username: data.battletag,
  410. message: data.message
  411. });
  412.  
  413. var convSubjectName = "";
  414. var convSubjectId = "";
  415. if (data.chatroom_data.type == 0) {
  416. convSubjectId = data.user_id;
  417. convSubjectName = data.battletag;
  418. } else {
  419. convSubjectId = data.chatroom_data.offer_id;
  420. convSubjectName = data.chatroom_data.offer_title;
  421. }
  422.  
  423. // Ha privát chatünk van (négyszemközt), akkor a senderUser == subjectId és senderUserBattletag = ...
  424. for (var i = 0; i < data.users_to_notify.length; i++) {
  425. socket.join("user_" + data.users_to_notify[i].user_id);
  426. socket.broadcast.to("user_" + data.users_to_notify[i].user_id).emit('receive_messagenotify', {
  427. chatroomId: data.chatroom_data.chatroom_id,
  428. chatroomType: data.chatroom_data.type,
  429. subjectId: convSubjectId,
  430. subjectName: convSubjectName,
  431. senderUserId: data.user_id,
  432. senderUserBattletag: data.battletag,
  433. message: data.message
  434. });
  435. socket.leave("user_" + data.users_to_notify[i].user_id);
  436. }
  437.  
  438. // A notifikációt kiküldjük magunknak is. Ez arra lesz jó, hogy ha írunk egy üzenetet,
  439. // akkor a főoldalon is frissüljön a konverzációlista a - mi általunk küldött - legújabb üzenettel.
  440. if (data.chatroom_data.type == 0) {
  441. socket.broadcast.to("user_" + data.user_id).emit('receive_messagenotify', {
  442. chatroomId: data.chatroom_data.chatroom_id,
  443. chatroomType: data.chatroom_data.type,
  444. subjectId: data.users_to_notify[0].user_id,
  445. subjectName: data.users_to_notify[0].battletag,
  446. senderUserId: data.user_id,
  447. senderUserBattletag: data.battletag,
  448. message: data.message
  449. });
  450. } else {
  451. socket.broadcast.to("user_" + data.user_id).emit('receive_messagenotify', {
  452. chatroomId: data.chatroom_data.chatroom_id,
  453. chatroomType: data.chatroom_data.type,
  454. subjectId: convSubjectId,
  455. subjectName: convSubjectName,
  456. senderUserId: data.user_id,
  457. senderUserBattletag: data.battletag,
  458. message: data.message
  459. });
  460. }
  461.  
  462. var messageSeen = "";
  463. if (data.chatroom_data.type == 0) {
  464. messageSeen = 0;
  465. } else {
  466. messageSeen = 9;
  467. }
  468.  
  469. // Végül az üzenetet beillesztjük az adatbázisba, hogy később is meg lehessen tekinteni.
  470. pool.getConnection(function(err, connection) {
  471. connection.query('INSERT INTO wow_chat_messages(chatroom_id, sender_user, text, seen) VALUES (?,?,?,?)', [data.room, data.user_id, data.message, messageSeen], function(err, results) {
  472. if (err) {
  473. connection.release();
  474. throw err;
  475. }
  476. connection.release();
  477. });
  478. });
  479. });
  480.  
  481. socket.on('message_seen', function(data) {
  482. pool.getConnection(function(err, connection) {
  483. connection.query('UPDATE wow_chat_messages SET seen = ? WHERE (chatroom_id = ? AND sender_user = ?);', [1, data.room, data.the_other_user], function(err, results6) {
  484. if (err) {
  485. connection.release();
  486. throw err;
  487. }
  488. connection.release();
  489. socket.broadcast.to(data.room).emit('message_seen_2', data.the_other_user);
  490. });
  491. });
  492. });
  493.  
  494. socket.on('writing', function(data) {
  495. socket.broadcast.to(data.room).emit('writing_2', {
  496. yesOrNo: data.yesOrNo,
  497. the_other_user: data.the_other_user
  498. });
  499. });
  500.  
  501. socket.on('refresh_chat_window', function(room) {
  502. socket.broadcast.to(room).emit('refresh_chat_window_2', 'something');
  503. });
  504. });
  505. }
  506.  
  507. module.exports = sockets;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement