Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. socket.on('SENDFIGHTINGUNIT', function (data)
  2. {
  3. //Basic
  4. //var arraytest2 = [];
  5. currentSENDFIGHTINGUNIT =
  6. {
  7. UnitOrderA:data.UnitOrderA,
  8. UnitOrderB:data.UnitOrderB,
  9. idUnitInLocationsA:data.idUnitInLocationsA,
  10. idUnitInLocationsB:data.idUnitInLocationsB,
  11. UserNameA:data.UserNameA,
  12. UserNameB:data.UserNameB,
  13. }
  14.  
  15.  
  16. pool.getConnection(function(err,connection)
  17. {
  18.  
  19. d = new Date();
  20.  
  21. //d = new Date();
  22. createPositionTimelast = Math.floor(d.getTime() / 1000);
  23. //update position click
  24. connection.query("SELECT `idUnitInLocations`,`UserName`,`HealthEach`,`HealthRemain`, `Damage`,`DamageEach`, `Defend`,`DefendEach`, `Quality`,`UnitOrder` FROM `unitinlocations` where `idUnitInLocations` = '"+currentSENDFIGHTINGUNIT.idUnitInLocationsA
  25. +"'UNION ALL SELECT `idUnitInLocations`,`UserName`,`HealthEach`,`HealthRemain`, `Damage`,`DamageEach`, `Defend`,`DefendEach`,`Quality`,`UnitOrder` FROM `unitinlocations` where `idUnitInLocations` = '"+currentSENDFIGHTINGUNIT.idUnitInLocationsB
  26. +"'",function(error, rows,field)
  27. {
  28. if (!!error) {
  29. console.log('Error in the queryjhg2323');
  30. }else
  31. {
  32. //console.log('chieu dài row select: '+rows.length);
  33. if(rows.length > 1)
  34. {
  35. QualityUnEqual = 0;
  36. //QualityUnEqual = parseInt(rows[0].Quality,10)/parseInt(rows[1].Quality,10);
  37. if (parseInt(rows[0].Quality,10)/parseInt(rows[1].Quality,10) < 1)
  38. {
  39. QualityUnEqual=1;
  40. }
  41.  
  42. // Tính Defent tổng
  43. DefendSum = 0;
  44. DefendSum = parseInt(rows[0].DamageEach,10)/parseInt(rows[1].HealthEach,10);
  45. if ((DefendSum % 1 ) >= 0.5)
  46. {
  47. DefendSum = Number((DefendSum).toFixed(0));
  48.  
  49.  
  50. }else if ((DefendSum % 1 ) < 0.5 && (DefendSum % 1 ) > 0)
  51. {
  52. DefendSum = Number((DefendSum).toFixed(0))+1;
  53.  
  54. }else
  55. {
  56. DefendSum = parseInt(DefendSum,10);
  57.  
  58. }
  59.  
  60. HealthRemain = parseInt(rows[1].HealthRemain,10) -(parseFloat(QualityUnEqual)*parseInt(rows[0].DamageEach,10) - parseInt(DefendSum,10)* parseInt(rows[1].DefendEach,10));
  61.  
  62.  
  63. if (HealthRemain<=0)
  64. {
  65. connection.query('DELETE FROM unitinlocations WHERE idUnitInLocations = ? ',
  66. [rows[1].idUnitInLocations],function(error, result, field)
  67. {
  68. if(!!error)
  69. {
  70. console.log('Error in the queryfgfg');
  71. }else
  72. {
  73. if(result.affectedRows > 0)
  74. {
  75.  
  76. connection.release();
  77.  
  78.  
  79. io.emit('RECEIVEFIGHTINGUNIT',
  80. {
  81. idUnitInLocations: parseInt(rows[1].idUnitInLocations,10),
  82. HealthRemain:0,
  83. Quality:0,
  84. });
  85.  
  86.  
  87. //update lại unit order
  88. connection.query("SELECT * FROM `unitinlocations` where `UserName` = '"+rows[1].UserName
  89. +"'AND `UnitOrder` > '"+rows[1].UnitOrder+"'",function(error, rows,field)
  90. {
  91. if (!!error)
  92. {
  93. console.log('Error in the query1');
  94. }else
  95. {
  96. if(rows.length > 0)
  97. {
  98. for (var i = 0; i < rows.length; i++)
  99. {
  100. connection.query('UPDATE unitinlocations SET UnitOrder = ? WHERE UserName = ? AND UnitOrder = ?',
  101. [(parseInt(rows[i].UnitOrder, 10)-1),currentSENDFIGHTINGUNIT.UserNameB, (parseInt(rows[i].UnitOrder, 10))],function(error, result, field)
  102. {
  103. if(!!error)
  104. {
  105. console.log('Error in the query11');
  106. }else
  107. {
  108. }
  109. })
  110.  
  111. }
  112.  
  113. }
  114. }
  115. })
  116.  
  117.  
  118. }else
  119. {
  120. console.log('khong co gi update');
  121. }
  122. }
  123. })
  124.  
  125.  
  126. }else
  127. {
  128.  
  129. QualityRemain = 0;
  130. QualityRemain = parseInt(HealthRemain,10)/parseInt(rows[1].HealthEach,10);
  131.  
  132. if ((QualityRemain % 1 ) >= 0.5)
  133. {
  134. QualityEnd = Number((QualityRemain).toFixed(0));
  135.  
  136.  
  137. }else if ((QualityRemain % 1 ) < 0.5 && (QualityRemain % 1 ) > 0)
  138. {
  139. QualityEnd = Number((QualityRemain).toFixed(0))+1;
  140.  
  141. }else
  142. {
  143. QualityEnd = parseInt(QualityRemain,10);
  144.  
  145. }
  146.  
  147. d = new Date();
  148.  
  149. io.emit('RECEIVEFIGHTINGUNIT',
  150. {
  151. idUnitInLocations:parseInt(rows[1].idUnitInLocations),
  152. HealthRemain:HealthRemain,
  153. Quality:QualityEnd,
  154. });
  155. DamageRemain = parseInt(QualityEnd,10)*parseInt(rows[1].DamageEach,10);
  156. DefendRemain = parseInt(QualityEnd,10)*parseInt(rows[1].DefendEach,10);
  157.  
  158. connection.query('UPDATE unitinlocations SET HealthRemain = ? , Damage = ? , Defend = ? , Quality = ? WHERE idUnitInLocations = ?',
  159. [HealthRemain,DamageRemain,DefendRemain, QualityEnd,rows[1].idUnitInLocations],function(error, result, field)
  160. {
  161. if(!!error)
  162. {
  163. console.log('Error in the query11');
  164. }else
  165. {
  166. //console.log(currentSENDFIGHTINGUNIT.idUnitInLocationsB+" vao func 4 "+HealthRemain+" _ "+ QualityEnd);
  167. if(result.affectedRows > 0)
  168. {
  169. connection.release();
  170.  
  171.  
  172. }
  173. }
  174. })
  175.  
  176. }
  177.  
  178.  
  179. }else
  180. {
  181. console.log("lỗi select");
  182. }
  183.  
  184.  
  185. }
  186. })
  187.  
  188. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement