This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
min-bin.js
175 lines (146 loc) · 6.15 KB
/
min-bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* globals
window $ document
*/
import { analytics, BaseScript, SettingsEntry } from '../core';
import { TransferMarket, priceTiers } from '../../fut';
export class MinBinSettings extends SettingsEntry {
static id = 'min-bin';
constructor() {
super('min-bin', 'Search minimum BIN');
this.addSetting('Amount of lowest BINs to determine minimum on', 'mean-count', 3, 'number');
this.addSetting('Adjust quicklist panel price automatically based on minimum BIN', 'adjust-list-price', true, 'checkbox');
this.addSettingUnder('adjust-list-price', 'Start price percentage (0 to 100%)', 'start-price-percentage', 90, 'number');
this.addSettingUnder('adjust-list-price', 'Buy now price percentage (0 to 100%)', 'buy-now-price-percentage', 110, 'number');
}
}
class MinBin extends BaseScript {
constructor() {
super(MinBinSettings.id);
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
this._observer = new MutationObserver(this._mutationHandler.bind(this));
this._playerPrices = [];
}
activate(state) {
super.activate(state);
const obsConfig = {
childList: true,
characterData: true,
attributes: false,
subtree: true,
};
setTimeout(() => {
this._observer.observe($(document)[0], obsConfig);
}, 0);
}
deactivate(state) {
super.deactivate(state);
this._observer.disconnect();
}
_mutationHandler(mutationRecords) {
mutationRecords.forEach(function (mutation) {
if ($(mutation.target).hasClass('DetailView') && $(mutation.target)
.find('.DetailPanel') && mutation.addedNodes.length > 0) {
const searchMinBin = $(mutation.target).find('#searchMinBin');
searchMinBin.remove();
let selectedItem = this._getSelectedItem();
if (selectedItem == null || selectedItem.resourceId === 0) {
return;
}
const knownPlayerPrice = this._playerPrices
.find(p => p.resourceId === selectedItem.resourceId);
let price = '';
if (knownPlayerPrice != null) {
price = `(${knownPlayerPrice.minimumBin})`;
this._updateListPrice(knownPlayerPrice.minimumBin);
}
$(mutation.target).find('.DetailPanel > .ut-button-group').prepend(`<button id="searchMinBin" data-resource-id="${selectedItem.resourceId}" class="list"><span class="btn-text">Search minimum BIN ${price}</span><span class="btn-subtext"></span></button>`);
$('#searchMinBin').bind('click', async () => {
const btn = $('#searchMinBin');
btn.find('.btn-text').html('Searching minimum BIN...');
analytics.trackEvent('Min BIN', 'Search Min BIN', btn.data('resource-id'));
const settings = this.getSettings();
const minimumBin = await new TransferMarket().searchMinBuy(selectedItem, parseInt(settings['mean-count'], 10));
const playerPrice = this._playerPrices.find(p => p.resourceId === btn.data('resource-id'));
if (playerPrice != null) {
this._playerPrices.splice(this._playerPrices.indexOf(playerPrice), 1);
}
this._playerPrices.push({
resourceId: btn.data('resource-id'),
minimumBin,
});
selectedItem = this._getSelectedItem();
let notificationText = `Minimum BIN found for ${selectedItem._staticData.name} is ${minimumBin}`;
if (btn.data('resource-id') === selectedItem.resourceId) {
if (minimumBin === 0) {
btn.find('.btn-text').html('Search minimum BIN (extinct)');
notificationText = `Minimum BIN not found for ${selectedItem._staticData.name}, card may be extinct`;
} else {
btn.find('.btn-text').html(`Search minimum BIN (${minimumBin})`);
this._updateListPrice(minimumBin);
}
}
GM_notification({
text: notificationText,
title: 'FUT 20 Web App',
timeout: 5000,
onclick: () => window.focus(),
});
});
}
}, this);
}
_updateListPrice(minimumBin) {
const settings = this.getSettings();
const quicklistPanel = getAppMain().getRootViewController()
.getPresentedViewController()
.getCurrentViewController()
.getCurrentController()
._rightController._currentController._quickListPanel;
if (settings['adjust-list-price'] && quicklistPanel) {
const quicklistpanelView = quicklistPanel._view;
const listPrice = priceTiers.determineListPrice(
minimumBin * (settings['start-price-percentage'] / 100),
minimumBin * (settings['buy-now-price-percentage'] / 100),
);
if (quicklistPanel._item) {
// sets the values when the quicklistpanel hasn't been initialized
const auction = quicklistPanel._item._auction;
if (auction.tradeState === 'closed') {
// item is sold
return;
}
if (auction.tradeState !== 'active') {
auction.startingBid = listPrice.start;
auction.buyNowPrice = listPrice.buyNow;
quicklistPanel._item.setAuctionData(auction);
}
}
const bidSpinner = quicklistpanelView._bidNumericStepper;
const buySpinner = quicklistpanelView._buyNowNumericStepper;
bidSpinner.setValue(listPrice.start);
buySpinner.setValue(listPrice.buyNow);
}
}
/* eslint-disable class-methods-use-this */
_getSelectedItem() {
const listController = getAppMain().getRootViewController()
.getPresentedViewController()
.getCurrentViewController()
.getCurrentController()._listController;
if (listController) {
return listController.getIterator().current();
}
const detailController = getAppMain().getRootViewController()
.getPresentedViewController()
.getCurrentViewController()
.getCurrentController()._rightController;
if (detailController && detailController._currentController._viewmodel) {
const current = detailController
._currentController._viewmodel.current();
return current._item ? current._item : current;
}
return null;
}
/* eslint-enable class-methods-use-this */
}
new MinBin(); // eslint-disable-line no-new