Guest User

Untitled

a guest
Feb 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. server.get('/topScore', async function(req, res){
  2. const group = req.query.group;
  3. const leagueid = req.query.leagueid;
  4. let api_key;
  5. const items = [];
  6. let match;
  7. let row = {};
  8. let masuk;
  9. if(group != 'other'){
  10. api_key = 'wpvzxf6d2wkjdkaqauyf4juk';
  11. }else{
  12. api_key = '3kdegdxesfebr8udhnyp5z68';
  13. }
  14. if(leagueid == '1015'){
  15. res.send([]);
  16. }
  17. const key = 'topscore_' + leagueid + '_' + group;
  18.  
  19. try {
  20. client.get(key, function(error, result){
  21. if(result){
  22. res.send(JSON.parse(result));
  23. }else{
  24. const url = config.API_HOST + group + '/en/tournaments/sr:tournament:'+ leagueid +'/leaders.json?api_key=' +api_key ;
  25. axios.get(url)
  26. .then(function (response){
  27. if(_.has(response, 'data.top_points') && _.size(response.data.top_points) > 0){
  28. for (let i=0; i < 5; i++){
  29. const player = response.data.top_points[i].player.id;
  30. const seasonId = response.data.tournament.current_season.id;
  31. let row = response.data.top_points[i];
  32. console.log("ini kesatu");
  33. const url2 = config.API_HOST + group + '/en/players/'+player+'/profile.json?api_key=' +api_key;
  34. // *biar blok ini bisa diakses*
  35. axios.get(url2)
  36. .then(function(responses){
  37.  
  38. if(_.size(responses.data.statistics.seasons) > 0){
  39. for (let j=0; j < responses.data.statistics.seasons.length; j++){
  40. if(responses.data.statistics.seasons[j].id == seasonId){
  41. masuk = seasonId;
  42. }
  43. }
  44. }
  45. row.tambah = masuk;
  46. })
  47. //===============================================================================
  48. console.log(row.tambah); //karena ini undefined
  49. items.push(row);
  50. }
  51. items.sort(function(a, b){
  52. return b.goals-a.goals;
  53. })
  54. res.setHeader('content-type', 'application/json');
  55. res.send({tournament: response.data.tournament, top_goals: items});
  56. // client.set(key, JSON.stringify({
  57. // tournament: response.data.tournament,
  58. // top_goals: items
  59. // }), 'NX');
  60. // res.send({tournament: response.data.tournament, top_goals: items});
  61. }else {
  62. throw new Error('Error');
  63. }
  64. }).catch(function(response) {
  65. res.send({
  66. code: 404,
  67. message: 'Request failed'
  68. });
  69. });
  70. }
  71. })
  72. }catch (error){
  73. res.send({
  74. code: 400,
  75. message: 'There is something wrong!'
  76. });
  77. }
  78. });
Add Comment
Please, Sign In to add comment