added real player names to draft pool (slow) and fixed #238

This commit is contained in:
Sakimori 2021-06-27 19:15:56 -04:00
parent ca8e1d44fc
commit 0953f05471
5 changed files with 63 additions and 16 deletions

1
.gitignore vendored
View file

@ -358,3 +358,4 @@ env
/data/leagues /data/leagues
/simmadome/build /simmadome/build
/simmadome/.eslintcache /simmadome/.eslintcache
/matteo_env/Lib/site-packages

24
real_players.py Normal file
View file

@ -0,0 +1,24 @@
from lxml import html
import requests, json
import onomancer as ono
def random_real_player():
name = []
while name == []:
page = requests.get("http://www.baseball-reference.com/rand.fcgi")
tree = html.fromstring(page.content)
name = tree.xpath("//h1[@itemprop='name']/span/text()")
if len(name) > 0 and len(name[0]) == 4:
name = [] #gets rid of years
name = name[0]
return name
def get_real_players(num):
names = []
for i in range(0, num):
names.append(random_real_player())
players = {}
for name in names:
players[name] = json.loads(ono.get_stats(name))
return players

View file

@ -46,6 +46,9 @@
<Compile Include="games.py"> <Compile Include="games.py">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="real_players.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="roman.py" /> <Compile Include="roman.py" />
<Compile Include="the_draft.py" /> <Compile Include="the_draft.py" />
<Compile Include="the_prestige.py" /> <Compile Include="the_prestige.py" />

View file

@ -2,7 +2,7 @@ from collections import namedtuple
import games import games
import json import json
import uuid import uuid
import real_players
import onomancer import onomancer
Participant = namedtuple('Participant', ['handle', 'team']) Participant = namedtuple('Participant', ['handle', 'team'])
@ -16,19 +16,22 @@ class Draft:
""" """
@classmethod @classmethod
def make_draft(cls, teamsize, draftsize, minsize, pitchers): def make_draft(cls, teamsize, draftsize, minsize, pitchers, ono_ratio):
draft = cls(teamsize, draftsize, minsize, pitchers) draft = cls(teamsize, draftsize, minsize, pitchers, ono_ratio)
return draft return draft
def __init__(self, teamsize, draftsize, minsize, pitchers): def __init__(self, teamsize, draftsize, minsize, pitchers, ono_ratio):
self.DRAFT_SIZE = draftsize self.DRAFT_SIZE = int(draftsize * ono_ratio)
self.REAL_SIZE = draftsize - self.DRAFT_SIZE
self.REFRESH_DRAFT_SIZE = minsize # fewer players remaining than this and the list refreshes self.REFRESH_DRAFT_SIZE = minsize # fewer players remaining than this and the list refreshes
self.DRAFT_ROUNDS = teamsize self.DRAFT_ROUNDS = teamsize
self.pitchers = pitchers self.pitchers = pitchers
self._id = str(uuid.uuid4())[:6] self._id = str(uuid.uuid4())[:6]
self._participants = [] self._participants = []
self._active_participant = BOOKMARK # draft mutex self._active_participant = BOOKMARK # draft mutex
self._players = onomancer.get_names(limit=self.DRAFT_SIZE) nameslist = onomancer.get_names(limit=self.DRAFT_SIZE)
nameslist.update(real_players.get_real_players(self.REAL_SIZE))
self._players = nameslist
self._round = 0 self._round = 0
@property @property
@ -68,7 +71,9 @@ class Draft:
self.advance_draft() self.advance_draft()
def refresh_players(self): def refresh_players(self):
self._players = onomancer.get_names(limit=self.DRAFT_SIZE) nameslist = onomancer.get_names(limit=self.DRAFT_SIZE)
nameslist.update(real_players.get_real_players(self.REAL_SIZE))
self._players = nameslist
def advance_draft(self): def advance_draft(self):
""" """

View file

@ -1,4 +1,4 @@
import discord, json, math, os, roman, games, asyncio, random, main_controller, threading, time, urllib, leagues, datetime, gametext import discord, json, math, os, roman, games, asyncio, random, main_controller, threading, time, urllib, leagues, datetime, gametext, real_players
import database as db import database as db
import onomancer as ono import onomancer as ono
from league_storage import league_exists, season_save, season_restart from league_storage import league_exists, season_save, season_restart
@ -646,6 +646,8 @@ class StartDraftCommand(Command):
draftsize = 20 draftsize = 20
minsize = 4 minsize = 4
pitchers = 3 pitchers = 3
ono_ratio = 0.5
timeout = 120
for flag in flags: for flag in flags:
try: try:
@ -657,10 +659,16 @@ class StartDraftCommand(Command):
minsize = int(flag[1]) minsize = int(flag[1])
elif flag[0] == "p": elif flag[0] == "p":
pitchers = int(flag[1]) pitchers = int(flag[1])
elif flag[0] == "c":
ono_ratio = float(flag[1])
if ono_ratio > 1 or ono_ratio < 0:
raise CommandError("The Chaos value needs to be between 0 and 1, chief. Probability has rules.")
elif flag[0] == "w": #wait
timeout = int(flag[1])
else: else:
raise CommandError(f"We don't recognize that {flag[0]} flag.") raise CommandError(f"We don't recognize that {flag[0]} flag.")
except ValueError: except ValueError:
raise CommandError(f"Your {flag[0]} flag isn't a valid integer, boss.") raise CommandError(f"Your {flag[0]} flag isn't a valid nummber, boss.")
if teamsize-pitchers > 20 or pitchers > 8: if teamsize-pitchers > 20 or pitchers > 8:
raise CommandError("You can't fit that many players on a team, chief. Slow your roll.") raise CommandError("You can't fit that many players on a team, chief. Slow your roll.")
@ -669,7 +677,13 @@ class StartDraftCommand(Command):
if draftsize > 40: if draftsize > 40:
raise CommandError("40 players is the max. We're not too confident about pushing for more.") raise CommandError("40 players is the max. We're not too confident about pushing for more.")
draft = Draft.make_draft(teamsize, draftsize, minsize, pitchers) await msg.channel.send("Got it, boss. Give me a sec to find all the paperwork.")
try:
draft = Draft.make_draft(teamsize, draftsize, minsize, pitchers, ono_ratio)
except ConnectionError:
await msg.channel.send("Baseball Reference isn't playing nice. Could you try again for us in a minute or so?")
mentions = {f'<@!{m.id}>' for m in msg.mentions} mentions = {f'<@!{m.id}>' for m in msg.mentions}
content = msg.content.split('\n')[1:] # drop command out of message content = msg.content.split('\n')[1:] # drop command out of message
if not content or len(content) % 3: if not content or len(content) % 3:
@ -683,7 +697,7 @@ class StartDraftCommand(Command):
handle = mention handle = mention
break break
else: else:
await msg.channel.send(f"I don't recognize {handle_token}") await msg.channel.send(f"I don't recognize {handle_token}.")
return return
team_name = content[i + 1].strip() team_name = content[i + 1].strip()
if games.get_team(team_name): if games.get_team(team_name):
@ -727,7 +741,7 @@ class StartDraftCommand(Command):
embed=build_draft_embed(draft.get_draftees(), footer=footer), embed=build_draft_embed(draft.get_draftees(), footer=footer),
) )
try: try:
draft_message = await self.wait_draft(msg.channel, draft) draft_message = await self.wait_draft(msg.channel, draft, timeout)
draft.draft_player(f'<@!{draft_message.author.id}>', draft_message.content.split(' ', 1)[1]) draft.draft_player(f'<@!{draft_message.author.id}>', draft_message.content.split(' ', 1)[1])
except SlowDraftError: except SlowDraftError:
player = random.choice(draft.get_draftees()) player = random.choice(draft.get_draftees())
@ -777,7 +791,7 @@ class StartDraftCommand(Command):
return False return False
return False return False
async def wait_draft(self, channel, draft): async def wait_draft(self, channel, draft, timeout):
def check(m): def check(m):
if m.channel != channel: if m.channel != channel:
@ -790,9 +804,9 @@ class StartDraftCommand(Command):
return False return False
try: try:
draft_message = await client.wait_for('message', timeout=120.0, check=check) draft_message = await client.wait_for('message', timeout=timeout, check=check)
except asyncio.TimeoutError: except asyncio.TimeoutError:
raise SlowDraftError('Too slow') raise SlowDraftError('Too slow, boss.')
return draft_message return draft_message