-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
214 lines (164 loc) · 6.48 KB
/
bot.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import signal
import time
import sys
import random
from autobahn.asyncio.component import Component
from autobahn.asyncio.component import run
from demo_config import create_autobahn_component_config
from demo_config import MARKET_BIDDER_ADD
from demo_config import MARKET_GET
from demo_config import MARKET_ITEM_ADDED
from demo_config import MARKET_ITEM_GET
from demo_config import MARKET_ITEM_BID
from demo_config import MARKET_ITEM_NEW_PRICE
from demo_config import MARKET_OPENED
from item import Item
class Bot:
def __init__(self, name=None, incr=None, limit=None, lag=None):
if name is None:
with open("names.txt", 'r') as f:
names = f.readlines()
self._name = random.choice(names).strip()
else:
self._name = name
if incr is None:
self._incr = round(random.uniform(1, 5), 2)
else:
self._incr = incr
if limit is None or limit == "0":
self._limit = random.uniform(1000, 5000)
else:
self._limit = limit
if lag is None:
self._lag = random.uniform(0.1, 1)
else:
self._lag = lag
self._incr = round(float(self._incr ), 2)
self._limit = round(float(self._limit), 2)
self._lag = round(float(self._lag), 2)
print(f"My name is '{self._name}'. I will start bidding with limit='{self._limit}', incr='{self._incr}', lag='{self._lag}'")
ab_component_config = create_autobahn_component_config(user_id="bot")
self._component = Component(**ab_component_config)
self._component.on("join", self._on_join)
self._session = None
self._bids = set()
# def pick_name():
# with open("names.txt", 'r') as f:
# names = f.readlines()
# return random.choice(names).strip()
def run(self):
run([self._component])
async def _identify(self):
basename = self._name
counter = 0
while not await self._session.call(MARKET_BIDDER_ADD, self._name):
prev_name = self._name
counter += 1
self._name = f"{basename}_{counter:02}"
print(f"'{prev_name}' already taken, trying '{self._name}'")
async def _on_join(self, session, details):
self._session = session
self._session.subscribe(self._on_market_opening, MARKET_OPENED)
try:
await self._on_market_opening()
except BaseException:
print("Market is not opened yet, waiting for the signal...")
async def _on_market_opening(self):
await self._identify()
print(
f"""
{self._name} joined the '{self._session.realm}' marketplace.
{self._name} will try to buy any item cheaper than ${self._limit+self._incr}.
{self._name} will bid adding ${self._incr}.
To make the bot more 'human' like:
- {self._name} will only bid one item at a time
- It takes {self._lag}s for {self._name} to perform a bid
- After 3 consecutive bid failutes, {self._name} gives up on the item
"""
)
await self._first_bids()
self._session.subscribe(self._on_new_item, MARKET_ITEM_ADDED)
self._session.subscribe(self._on_bid, MARKET_ITEM_NEW_PRICE)
async def _first_bids(self):
print(f"{self._name} queries the market...")
market = await self._session.call(MARKET_GET)
items = [
item
for item in map(Item.wamp_unpack, market)
if item.is_on_offer() and item.price < self._limit
]
n_items = len(items)
if 0 == n_items:
print(f"{self._name} did not find anything interesting.")
print(f"{self._name} is waiting for new items...\n")
else:
print(
f"{self._name} found {n_items} interesting item{'s' if 1 < n_items else ''}"
)
for item in items:
await self._start_bidding(item.name)
async def _start_bidding(self, item_name):
print(f"{self._name} is looking at the item '{item_name}'")
tries = 0
while tries < 3:
item_details = await self._session.call(MARKET_ITEM_GET, item_name)
item = Item.wamp_unpack(item_details)
if not item.is_on_offer():
print(
f"'{item.name}' deadline is expired. {self._name} discard the item.\n"
)
return
if self._limit < item.price:
print(
f"'{item.name}' is too expensive, ${item.price}. {self._name} gives up.\n"
)
return
print(f"It takes {self._lag}s for {self._name} to submit the new price...")
time.sleep(self._lag)
new_price = item.price + self._incr
print(f"{self._name} bids on {item.name} at ${new_price}...")
if await self._session.call(
MARKET_ITEM_BID, item.name, new_price, self._name
):
print(f"Bid accepted! {self._name} is happy.\n")
return
else:
print(f"Bid rejected, {self._name} tries again")
tries += 1
print(
f"After 3 failures, {self._name} gives up bidding on item '{item.name}'\n"
)
async def _on_new_item(self, name, price, deadline):
item = Item(name, price, deadline)
print(
f"{self._name} is notified a new item '{name}' is on offer at ${item.price}."
)
if name in self._bids:
print(f"{self._name} is already bidding on item '{name}'")
return
self._bids.add(name)
await self._start_bidding(name)
self._bids.remove(name)
async def _on_bid(self, name, price, deadline, winner):
if winner == self._name:
return
item = Item(name, price, deadline, winner)
print(
f"{self._name} is notified of a new bid at ${item.price} on time '{item.name}'"
)
if self._limit < item.price:
print(f"Too expensive, {self._name} ignores the item.\n")
return
if name in self._bids:
print(f"{self._name} is already bidding on item '{name}'")
return
self._bids.add(name)
await self._start_bidding(name)
self._bids.remove(name)
if __name__ == "__main__":
# Handle Ctrl+C gracefully
def signal_handler(sig, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
bot = Bot(*sys.argv[1:])
bot.run()