PageSourceSearch

https://gamengai.com/controllers/hitCtrl.js

js gamengai.com collected 2026-10-02 23:37:59 UTC 16,241 bytes, 508 lines download raw bytes

1app.controller('hitListCtrl', ['$scope','$http','$location','$routeParams','$interval','$templateCache','$window','ModalService','SITE','IMGURL',
2  function($scope, $http, $location, $routeParams, $interval, $templateCache, $window, ModalService, SITE, IMGURL) {
3
4    let site = SITE
5    let alertHitsEndpoint = 'alert-hits';
6    $scope.hitsPerCat = 25;
7
8    // 1) Get all alert strings that have hits for this mode
9    // 2) Populate the scope with these strings (which show as categories)
10    // 3) Hit endpoint for each string, getting al hits for given string for this mode/user
11
12    $scope.title        = '';
13    $scope.busyImage    = IMGURL + "spinner-transparent.gif";
14    
15    // TODO routeParams should take care of this
16    let modePattern     = /.+?mode=(.+)/;
17    let modeMatch       = modePattern.exec($location.absUrl());
18    $scope.mode         = 'regular';
19
20    let winsPattern     = /.+?wins/;
21    let winsMatch       = winsPattern.exec($location.absUrl());
22    $scope.wins         = false;
23    if (winsMatch) {
24      $scope.wins       = true;
25    }
26
27    $scope.activeBids   = [];
28   
29    $scope.hasAction    = true;
30    $scope.actionIcon   = 'glyphicon glyphicon-star';
31    $scope.actionButton = 'btn btn-xs btn-success';
32    
33    if (modeMatch && modeMatch.length > 1) {
34      $scope.mode = modeMatch[1];
35    }
36
37    if ($scope.mode == 'save' || $scope.mode == 'trash') {
38      $scope.actionIcon  = 'glyphicon glyphicon-repeat';
39      $scope.actionButton = 'btn btn-xs btn-info';
40    }
41
42    if ($scope.wins) {
43      $scope.title = 'Wins';
44    } else if ($scope.mode == 'regular') {
45      $scope.title = 'Alert Hits';
46    } else if ($scope.mode == 'save') {
47      $scope.title = 'Saved Hits';
48    } else if ($scope.mode == 'trash') {
49      $scope.title = 'Deleted Hits';
50    }
51
52    $scope.hits = {};
53    $scope.categories   = {};
54    $scope.addedDates   = [];
55    $scope.addedDates.push('Select');
56    $scope.addedDate    = $scope.addedDates[0];
57    $scope.endDates     = [];
58    $scope.endDates.push('Select');
59    $scope.endDate      = $scope.endDates[0];
60    $scope.endingSoon   = [];
61    $scope.lastDateType = "";
62    $scope.lastShowDate = "";
63    $scope.busy_image   = IMGURL + "spinner-transparent.gif";
64
65    // Get the list of hits, and store them in an associate array.
66    // Keys are auction category 
67
68    // Do this in a loop for each string
69    $scope.getAlertHits = function(string, loadMore) {
70      var url = "hits-index?mode=" + $scope.mode + "&string=" + string;
71      if (loadMore) {
72        let start = $scope.hits[string].LoadMoreCount++ * 25;
73        url += `&start=${start}&limit=${$scope.hitsPerCat}`;
74      }
75
76      $http.get(url)
77        .then(function(res) {
78          let unbusy = [];
79          let added = 0;
80          for (let item of res.data) {
81
82		        if (!item.Url) {
83			        // Something went wrong w/ this auction in the database
84			        continue;
85			      }
86
87            var have = false;
88            var key = item.String;
89
90            // Check if we've loaded his one, as we get dupes when we "load more"
91            if (typeof $scope.hits[key] !== 'undefined' ) {
92              for (let oldItem of $scope.hits[key].items) {
93                if (oldItem.Url == item.Url) {
94                  have = true;
95                  break;
96                }
97              }
98            }
99
100            if (have) {
101              continue;
102            }
103
104            item.Visible  = true;
105            item.Expanded = false;
106            item.Checked  = false;
107            item.fontColor = 'inherit';
108            item.backgroundColor = 'inherit';
109            item.RemainColor ='inherit';
110
111            var endDateTimeTokens = item.EndDateTime.split(' ');
112            var endDate = endDateTimeTokens.length ? endDateTimeTokens[0] : ""; 
113            
114            var addedDateTimeTokens = item.addedDate.split(' ');
115            var addedDate = addedDateTimeTokens.length ? addedDateTimeTokens[0] : "";
116
117            if ($scope.addedDates.indexOf(addedDate) == -1) {
118              $scope.addedDates.push(addedDate);
119            }
120            
121            if ($scope.endDates.indexOf(endDate) == -1) {
122              $scope.endDates.push(endDate);
123            }
124
125            item.EndDate = endDate;
126            item.AddedDate = addedDate;
127
128            let remainingTokens = item.RemainingTime.split(":");
129            if (parseInt(remainingTokens[0]) > 0) {
130              item.DaysRemaining = Math.round(parseInt(remainingTokens[0]) / 24);
131              item.HoursRemaining = Math.ceil(parseInt(remainingTokens[0]) - (item.DaysRemaining * 24));
132              item.MinutesRemaining = Math.ceil(parseInt(remainingTokens[1]));
133              item.SecondsRemaining = Math.ceil(parseInt(remainingTokens[2]));
134              item.RemainingString = `${item.DaysRemaining} Days, ${item.HoursRemaining} Hours, ${item.MinutesRemaining} Minutes`;
135              if (item.DaysRemaining < 1) {
136                item.RemainColor ='red';
137                item.color ='red;';
138                item.fontColor ='red;';
139              }
140            } else {
141              item.RemainingString = 'Ended';
142              item.RemainColor ='#A5A5A5';
143              item.color ='#A5A5A5;';
144              item.fontColor ='#A5A5A5;';
145            }
146              
147            // Get thumbnail
148            var images = item.Images.split(',');
149            if (images.length) {
150              item.thumbnail = 'https://wing-auctions.c.yimg.jp/sim?furl=' + images[0] + '&dc=1&sr.fs=20000';
151            }
152
153            added++;
154            $scope.hits[key].items.push(item);
155            $scope.hits[key].Visible   = true;
156            // $scope.hits[key].Expanded  = false;
157            $scope.hits[key].Expanded  = true;
158            $scope.hits[key].busy      = false;
159            $scope.hits[key].Remaining = $scope.hits[key].Total - $scope.hits[key].items.length;
160            if (unbusy.indexOf(key) == -1) {
161              unbusy.push(key);
162            }
163          }
164
165          $scope.addedDates.sort();
166          $scope.addedDate = 'Select';
167          $scope.endDates.sort();
168          $scope.endDate = 'Select';
169
170          for (var i = 0; i < unbusy.length; i++) {
171            $scope.hits[key].busy = false;
172          }
173          
174        }, function(err) {
175          console.error(err);
176        }
177      );
178    }
179    
180    // Get category strings
181    $scope.getCategories = function(next) {
182      $http.get("/" + alertHitsEndpoint + "?mode=" + $scope.mode)
183        .then(function(res) {
184          for (let item of res.data) {
185            let key = item.string;
186            if (!$scope.hits[key]) {
187              $scope.hits[key]               = {};
188              $scope.hits[key].items         = [];
189              $scope.hits[key].Visible       = true;
190              $scope.hits[key].busy          = true;
191              $scope.hits[key].Total         = item.Total;
192              $scope.hits[key].LoadMoreCount = 0;
193            }
194          }
195          next();
196        }, function(err) {
197          console.error(JSON.stringify(err));
198        }
199      );
200    }
201    
202    $scope.toggleExpanded = function(key,userHitId) {
203      for (let item of $scope.hits[key].items) {
204        if (item.userHitId == userHitId ) {
205          item.Expanded = !item.Expanded;
206          console.log(`${item.userHitId} Expanded: ${item.Expanded}`);
207          if (item.Expanded) {
208
209            var images = [];
210            if (item.Images.length) {
211              images = item.Images.split(',');
212            }
213
214            item.slides = [];
215            item.bidStatus = 'None';
216            item.bidResult = 'None';
217            item.bidProgress = 'None';
218          
219            for (var j = 0; j < images.length; j++) {
220              item.slides.push({
221                image: images[j],
222                text: '',
223                id: j 
224              });
225            }
226
227          }
228          return;
229        }
230      }
231    }
232    
233    $scope.toggleChecked = function(key,userHitId) {
234      for (let item of $scope.hits[key].items) {
235        if (item.userHitId === userHitId ) {
236          item.Checked = !item.Checked;
237          item.backgroundColor = item.Checked ? '#c8e2e3' : 'inherit';
238        }
239      }
240    }
241
242    $scope.switchDate = function(dateType, showDate) {
243      for (let key in $scope.hits) {
244        let visible = 0;
245        for ( let item of $scope.hits[key].items ) {
246          let endDateTimeTokens = item.EndDateTime.split(' ');
247          let endDate = endDateTimeTokens[0];
248          if (showDate == 'Select') {
249            item.Visible = true;
250          } else if ((dateType == 'added') && (item.addedDate.split(' ')[0] !== showDate)) {
251            item.Visible = false;
252          } else if ((dateType == 'end') && (endDate !== showDate)) {
253            item.Visible = false;
254          } else {
255            item.Visible = true;
256            visible++;
257          }
258        }
259        // TODO ??
260        $scope.hits[key].items.Visible = visible ? true : false;
261      }
262      
263      if ( showDate === 'Select' ) {
264        $scope.addedDate = 'Select';
265        $scope.endDate = 'Select';
266      } 
267
268      $scope.lastShowDate = showDate;
269      $scope.lastDateType = dateType;
270      
271    }
272       
273    $scope.handleDelete = function(key, ids) {
274      var deletedIds = [];
275      if (Array.isArray(ids) ) {
276        deletedIds = ids;
277      } else {
278        deletedIds.push(ids);
279      }
280
281      for (let deleted of deletedIds) {
282        for (var i = 0; i < $scope.hits[key].items.length; i++) {
283          if ($scope.hits[key].items[i].userHitId == deleted) {
284            // Set it to invisible.  Will be gone next refresh
285            // TODO remove these, not just make invisible
286            // $scope.hits[key].items[i].Expanded = false;
287            // $scope.hits[key].items[i].Visible = false;
288            $scope.hits[key].items.splice(i, 1);
289            break;
290          }
291        }
292      } 
293        
294      $scope.checkVisible(key);
295    };
296    
297    // Save hit
298    $scope.handleAction = function(key, userHitId) {
299      $http.get('/hits-save?mode=' + $scope.mode + '&ids=' + userHitId)
300        .then(
301          function(res) {
302            let pattern = new RegExp("Success");
303            if (pattern.test(res.data) ) {
304              $scope.handleDelete(key,userHitId);
305            } else {
306              console.error(`Error ${res.data}`);
307            }
308          },
309          function(err) {
310            $window.alert(err.data);
311            console.error(err);
312          }
313      );
314    }
315
316    // Trash hit (single)
317    $scope.handleTrash = function(key, userHitIds) {
318      $http.get("/hits-trash?ids=" + userHitIds + "&mode=" + $scope.mode)
319        .then(function(res) {
320          let pattern = new RegExp("Success");
321          if (pattern.test(res.data) ) {
322            $scope.handleDelete(key,userHitIds);
323          } else {
324            console.log(`Error ${res.data}`);
325          }
326      });
327    };
328    
329    // Expand multiple
330    $scope.handleExpandSelected = function(key) {
331
332      let ids = [];
333      $scope.hits[key].Expanded = !$scope.hits[key].Expanded;
334      for (let item of $scope.hits[key].items) {
335        if (item.Checked) {
336          ids.push(item.userHitId);
337        }
338      }
339      
340      if (ids.length) {
341        for (let id of ids) {
342          $scope.toggleExpanded(key, id);
343        }
344      }
345
346    };
347    
348    // Trash multiple
349    $scope.handleTrashSelected = function(key) {
350      
351      let ids = [];
352      for (let item of $scope.hits[key].items) {
353        if (item.Checked) {
354          ids.push(item.userHitId);
355        }
356      }
357      
358      if (ids.length) {
359        $scope.handleTrash(key, ids);
360      }
361    };
362
363    $scope.checkVisible = function(key) {
364      let visible = false;
365      for (let item of $scope.hits[key].items) {
366        if (item.Visible) {
367          visible = true;
368          break;
369        }
370      }
371      $scope.hits[key].Visible = visible;
372    };
373    
374    $scope.handleActionSelected = function(key) {
375      
376      let ids = [];
377      for (let item of $scope.hits[key].items) {
378        if (item.Checked) {
379          ids.push(item.userHitId);
380        }
381      }
382      
383      if (ids.length) {
384        $scope.handleAction(key, ids);
385      }
386    };
387    
388    $scope.selectAll = function(key) {
389      if (!key) { return; }
390      for (let item of $scope.hits[key].items) {
391        item.Checked = true;
392      }
393    };
394
395    $scope.loadMore = function(key) {
396      if (!key) { return; }
397      $scope.getAlertHits(key, true);
398    };
399
400    // TODO these three functions should be a service
401    $scope.processBidData = function(url, bidStatus, bidProgress, bidResult) {
402      // console.log('processBidData(' + url + ',' + bidStatus + ',' + bidProgress + ',' + bidResult);
403      for (var key in $scope.hits) {
404        $scope.hits[key].items.forEach(function(alert) {
405          if (alert.Url == url) {
406               
407            alert.startedBid = true;
408            console.log(`Marking bid ${alert.url} as started`);
409
410            if (bidStatus.length) {
411              console.log('Updating bid status to ' + bidStatus);
412              alert.bidStatus = bidStatus;
413              // Bidding is finished, so we can remove this from active bids
414            }
415
416            if (bidResult.length) {
417              console.log('Result: ' + bidResult);
418              alert.bidResult = bidResult;
419            }
420
421            if (bidProgress.length) {
422              alert.bidProgress = bidProgress;
423              console.log('set BidProgress to ' + alert.bidProgress);
424            }
425              
426            // If bidding is done we can stop checking
427            if (bidProgress == "Failed To Confirm Bid" || bidProgress == "Confirmed Bid") {
428              if ( bidStatus == "Bid Confirmed" || bidStatus == "Bid Unconfirmed" ) {
429                var ix = $scope.activeBids.indexOf(url);
430                $scope.activeBids.splice(ix, 1);
431                console.log('Getting rid of active bid.  Now have ' + $scope.activeBids.length);
432              }
433            }
434          }
435        });
436      }
437    }
438
439    $scope.checkBids = function() {
440      for (var i = 0; i < $scope.activeBids.length; i++) {
441        var getUrl = '/get-bid?url=' + $scope.activeBids[i];  // prolly bad
442        console.log('url ' + $scope.activeBids[i]);
443        $http.get(getUrl)
444          .then(function(res) {
445            res.data.forEach(function(element) {
446              console.log(element);
447              $scope.processBidData(element.Url, element.BidStatus, element.BidProgress, element.BidResult);
448            });
449          });
450      }
451    };
452
453    $scope.placeBid = function(bidAmount, url) {
454      console.log('placeBid(' + bidAmount + ',' + url + ')');
455      $scope.activeBids.push(url);
456      var resetUrl = '/reset-bid?url=' + url + '&bidAmount=' + bidAmount;
457      $http.get(resetUrl)
458        .then(function(res) {
459
460          console.log('resetting bid finished');
461
462          // We've successfully reset the bid.  We can kick off the bidder now
463          // var getUrl = '/place-bid?url=' + url + '&bidAmount=' + bidAmount;
464          var body = { url : url, bidAmount : bidAmount };
465          $http.post('/place-bid', body)
466            .then(function(res) {
467              console.log('place bid finished');
468            });
469      
470          $scope.activeBids.push(url);
471          console.log('activeBids ' + $scope.activeBids.length);
472     
473          var promise = $interval(function() {
474            console.log('activeBids ' + $scope.activeBids.length);
475            if (!$scope.activeBids.length) {
476              console.log('No more active bids.  Killing timer...');
477              $interval.cancel(promise);
478            }
479            $scope.checkBids();
480          }, 2000 );
481        });
482    };
483
484    $scope.emptyTrash = function() {
485      console.log('empty trash');
486      let url = '/empty-trash';
487      $http.post(url, {}) 
488        .then(function(res) {
489          console.log('Trash emptied');
490          $scope.hits = {};
491        }, function(res) {
492          console.error(`error.`);
493        });
494    }
495
496    $scope.remainingTime = function(endTime) {
497
498    }
499    
500    let populateCategories = function() {
501      for (let key in $scope.hits) {
502        $scope.getAlertHits(key, false);
503      }
504    };
505
506    $scope.getCategories(populateCategories);
507
508}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.