Initial commit.

This commit is contained in:
Matúš Tejiščák 2024-02-13 10:55:47 +01:00
commit bb4f259921
2 changed files with 94 additions and 0 deletions

76
words.py Normal file
View File

@ -0,0 +1,76 @@
import time
import httpx
import random
import sqlite3
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
http = httpx.Client(headers={
'Referer': 'https://neal.fun/infinite-craft/',
})
db = sqlite3.connect('words.sqlite3')
db.execute('pragma foreign_keys = on')
r = db.execute('''
select x.word, x.rank, y.word, y.rank
from words x, words y
where not exists (
select *
from combos c
where c.x = x.word and c.y = y.word
)
''')
Q: set[tuple[tuple[str, int], tuple[str, int]]] = {
((x, xr), (y, yr)) for x, xr, y, yr in r.fetchall()
}
while Q:
log.info('%d items queued', len(Q))
# (x, x_rank), (y, y_rank) = random.choice(list(Q))
((x, x_rank), (y, y_rank)), = random.sample(
population=list(Q),
k=1,
counts=[max(xr, yr) for (_, xr), (_, yr) in Q],
)
Q.remove(((x, x_rank), (y, y_rank)))
resp = http.get(
'https://neal.fun/api/infinite-craft/pair',
params={
'first': x,
'second': y,
},
)
resp.raise_for_status()
z = resp.json()['result']
log.info('%s (%d) + %s (%d) -> %s', x, x_rank, y, y_rank, z)
z_rank = 1+max(x_rank, y_rank)
db.execute('''
insert into words (word, rank) values (?, ?)
on conflict(word) do update set rank = ? where rank > ?
''', (z, z_rank, z_rank, z_rank))
db.executemany(
'insert or ignore into combos (x, y, z) values (?, ?, ?)',
[(x, y, z), (y, x, z)],
)
db.commit()
r = db.execute('''
select q.word, q.rank
from words q
where not exists (
select *
from combos c
where c.x = q.word and c.y = ?
)
''', (z,))
for q, q_rank in r.fetchall():
Q.add(((z, z_rank), (q, q_rank)))
time.sleep(5)

18
words.sql Normal file
View File

@ -0,0 +1,18 @@
CREATE TABLE words (
word text not null,
rank integer not null,
primary key (word)
);
CREATE TABLE combos (
x text not null,
y text not null,
z text not null,
primary key (x, y),
foreign key (x) references words(word),
foreign key (y) references words(word),
foreign key (z) references words(word)
);
insert into words (word, rank)
values ('Earth', 0), ('Wind', 0), ('Fire', 0), ('Water', 0);