Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- socket.on('SENDFIGHTINGUNIT', function (data)
- {
- //Basic
- //var arraytest2 = [];
- currentSENDFIGHTINGUNIT =
- {
- UnitOrderA:data.UnitOrderA,
- UnitOrderB:data.UnitOrderB,
- idUnitInLocationsA:data.idUnitInLocationsA,
- idUnitInLocationsB:data.idUnitInLocationsB,
- UserNameA:data.UserNameA,
- UserNameB:data.UserNameB,
- }
- pool.getConnection(function(err,connection)
- {
- d = new Date();
- //d = new Date();
- createPositionTimelast = Math.floor(d.getTime() / 1000);
- //update position click
- connection.query("SELECT `idUnitInLocations`,`UserName`,`HealthEach`,`HealthRemain`, `Damage`,`DamageEach`, `Defend`,`DefendEach`, `Quality`,`UnitOrder` FROM `unitinlocations` where `idUnitInLocations` = '"+currentSENDFIGHTINGUNIT.idUnitInLocationsA
- +"'UNION ALL SELECT `idUnitInLocations`,`UserName`,`HealthEach`,`HealthRemain`, `Damage`,`DamageEach`, `Defend`,`DefendEach`,`Quality`,`UnitOrder` FROM `unitinlocations` where `idUnitInLocations` = '"+currentSENDFIGHTINGUNIT.idUnitInLocationsB
- +"'",function(error, rows,field)
- {
- if (!!error) {
- console.log('Error in the queryjhg2323');
- }else
- {
- //console.log('chieu dài row select: '+rows.length);
- if(rows.length > 1)
- {
- QualityUnEqual = 0;
- //QualityUnEqual = parseInt(rows[0].Quality,10)/parseInt(rows[1].Quality,10);
- if (parseInt(rows[0].Quality,10)/parseInt(rows[1].Quality,10) < 1)
- {
- QualityUnEqual=1;
- }
- // Tính Defent tổng
- DefendSum = 0;
- DefendSum = parseInt(rows[0].DamageEach,10)/parseInt(rows[1].HealthEach,10);
- if ((DefendSum % 1 ) >= 0.5)
- {
- DefendSum = Number((DefendSum).toFixed(0));
- }else if ((DefendSum % 1 ) < 0.5 && (DefendSum % 1 ) > 0)
- {
- DefendSum = Number((DefendSum).toFixed(0))+1;
- }else
- {
- DefendSum = parseInt(DefendSum,10);
- }
- HealthRemain = parseInt(rows[1].HealthRemain,10) -(parseFloat(QualityUnEqual)*parseInt(rows[0].DamageEach,10) - parseInt(DefendSum,10)* parseInt(rows[1].DefendEach,10));
- if (HealthRemain<=0)
- {
- connection.query('DELETE FROM unitinlocations WHERE idUnitInLocations = ? ',
- [rows[1].idUnitInLocations],function(error, result, field)
- {
- if(!!error)
- {
- console.log('Error in the queryfgfg');
- }else
- {
- if(result.affectedRows > 0)
- {
- connection.release();
- io.emit('RECEIVEFIGHTINGUNIT',
- {
- idUnitInLocations: parseInt(rows[1].idUnitInLocations,10),
- HealthRemain:0,
- Quality:0,
- });
- //update lại unit order
- connection.query("SELECT * FROM `unitinlocations` where `UserName` = '"+rows[1].UserName
- +"'AND `UnitOrder` > '"+rows[1].UnitOrder+"'",function(error, rows,field)
- {
- if (!!error)
- {
- console.log('Error in the query1');
- }else
- {
- if(rows.length > 0)
- {
- for (var i = 0; i < rows.length; i++)
- {
- connection.query('UPDATE unitinlocations SET UnitOrder = ? WHERE UserName = ? AND UnitOrder = ?',
- [(parseInt(rows[i].UnitOrder, 10)-1),currentSENDFIGHTINGUNIT.UserNameB, (parseInt(rows[i].UnitOrder, 10))],function(error, result, field)
- {
- if(!!error)
- {
- console.log('Error in the query11');
- }else
- {
- }
- })
- }
- }
- }
- })
- }else
- {
- console.log('khong co gi update');
- }
- }
- })
- }else
- {
- QualityRemain = 0;
- QualityRemain = parseInt(HealthRemain,10)/parseInt(rows[1].HealthEach,10);
- if ((QualityRemain % 1 ) >= 0.5)
- {
- QualityEnd = Number((QualityRemain).toFixed(0));
- }else if ((QualityRemain % 1 ) < 0.5 && (QualityRemain % 1 ) > 0)
- {
- QualityEnd = Number((QualityRemain).toFixed(0))+1;
- }else
- {
- QualityEnd = parseInt(QualityRemain,10);
- }
- d = new Date();
- io.emit('RECEIVEFIGHTINGUNIT',
- {
- idUnitInLocations:parseInt(rows[1].idUnitInLocations),
- HealthRemain:HealthRemain,
- Quality:QualityEnd,
- });
- DamageRemain = parseInt(QualityEnd,10)*parseInt(rows[1].DamageEach,10);
- DefendRemain = parseInt(QualityEnd,10)*parseInt(rows[1].DefendEach,10);
- connection.query('UPDATE unitinlocations SET HealthRemain = ? , Damage = ? , Defend = ? , Quality = ? WHERE idUnitInLocations = ?',
- [HealthRemain,DamageRemain,DefendRemain, QualityEnd,rows[1].idUnitInLocations],function(error, result, field)
- {
- if(!!error)
- {
- console.log('Error in the query11');
- }else
- {
- //console.log(currentSENDFIGHTINGUNIT.idUnitInLocationsB+" vao func 4 "+HealthRemain+" _ "+ QualityEnd);
- if(result.affectedRows > 0)
- {
- connection.release();
- }
- }
- })
- }
- }else
- {
- console.log("lỗi select");
- }
- }
- })
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement