from tinydb import TinyDB, Query import pandas as pd import numpy as np from scipy import stats import matplotlib.pyplot as plt from datetime import datetime,date MarketDB = TinyDB('market.json') PlayerDB = TinyDB('player.json') def getDF(card=None,aDate=None,pruneOutliers=True): """ Supply either a card identifier or a date for filter by. card can be an integer, which will pull from the card-num, or a string which will pull from the name field. It can also be a list of ints or strings, which will pull multiple cards histories. date can be either a datetime or string date formatted 'yyyy-mm-dd' """ MarketDB = TinyDB('market.json') if card is not None: if isinstance(card,list): if isinstance(card[0],int): d=MarketDB.search(Query()['card-num'].one_of(card)) else: d=MarketDB.search(Query()['name'].one_of(card)) else: if isinstance(card,int): d=MarketDB.search(Query()['card-num']==card) else: d=MarketDB.search(Query()['name']==card) else: if isinstance(aDate,(datetime,date)): d=MarketDB.search(Query()['date']==aDate.strftime('%Y-%m-%d')) else: d=MarketDB.search(Query()['date']==aDate) df=pd.DataFrame(d) if pruneOutliers: df=df[np.abs(stats.zscore(df['cost'])) < 1.5] df.sort_values(by=['card-num','date'], ascending=[True,True], inplace=True) return df.round({'cost':2,'cost-min':0,'cost-max':0}) def _hist(card=None,date=None,columns=['date','card-num','name','cost']): return getDF(card,date)[columns].to_string(index=False) def hist(card=None,date=None,columns=['date','card-num','name','cost']): print(_hist(card,date,columns)) def plot(card=None,columns=['cost','cost-min','cost-max'],maxCost=None): """must use either card or date. card cand be card-num or name date can be a datetime type or a string like 'yyy-mm-dd' columns must be a list of any numeric fields in the MarketDB """ df=getDF(card).plot(x='date',y=columns) plt.title(str(card)) plt.xticks(rotation=45) plt.xlabel("Date") plt.ylabel("Cost") plt.ylim(0,maxCost) plt.show() def buyout(): today=datetime.utcnow().strftime('%Y-%m-%d') PlayerDB = TinyDB('player.json') PlayerCards=PlayerDB.search(Query().fragment({'date':today})) Stock=[None]*152 for card in PlayerCards: Stock[card['card-num']]=card['stock'] MarketDF = getDF(aDate=today) MarketCards=MarketDF.to_dict('records') total=0 cardCount=0 NeededCards=[] print(f"Cards Needed Today:") print(f' Name Needed Cost') for card in MarketCards: if 'CRYSTAL' not in Stock[card['card-num']]: card['stock']=int(Stock[card['card-num']]) card['needed']=10-card['stock'] card['card-num']=card['card-num'] card['total-cost']=card['needed']*card['cost'] NeededCards.append(card) total+=card['total-cost'] cardCount+=card['needed'] print(f"{card['card-num']: >3} {card['name']: <20} {card['needed']: >2} {round(card['total-cost']): >5}") print(f" Remaining Cards: {cardCount}") print(f"Total Gems Needed: {round(total)}")