moved general player functions to super class

This commit is contained in:
Bibin Muttappillil 2020-07-15 16:50:26 +02:00
parent e7b37217f9
commit 812ae8a990
2 changed files with 78 additions and 69 deletions

View File

@ -6,4 +6,74 @@ import discord
class Player:
"""This (abstract) class is a template for player objects for games"""
@classmethod
async def make(cls, member, game):
p = cls()
p.member = member
p.dm = member.dm_channel or await member.create_dm()
p.game = game
return p
def name(self):
return self.member.name
def __str__(self):
return self.name()
def reset(self):
pass
def other_players(self):
return [p for p in self.game.player_list if p != self]
async def send_normal(self, message):
await self.dm.send(message)
async def send_embed(self, desc, color):
await self.dm.send(embed=discord.Embed(description=desc, color=color))
async def send_wrong(self, message):
await self.send_embed(message, 0xff8000)
async def send_confirmation(self, message):
await self.send_embed(message, 0x00ff00)
async def send_info(self, message):
await self.send_embed(message, 0x00ffff)
# TODO: refactor this function to make it understandable
async def ask_choice(self, question, options):
text = f"{question}\n" + f"{'='*len(question)}\n\n" + '\n'.join(f"[{str(i)}]({str(options[i])})" for i in range(len(options)))
await self.dm.send(f"```md\n{text}```")
# TODO: Basic Converters (https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#basic-converters)
def check_num(self, choice, N):
if not choice.isdigit():
raise ValueError(f"Your choice {choice} is not a number")
if not 0 <= int(choice) < N:
raise ValueError(f"Your choice {choice} is not in range 0 - {N-1}")
# TODO: seems hacky, figure out a nicer way
async def receive_choice(self, options, n_ans=1):
while True:
def check(choice):
return choice.channel == self.dm and choice.author == self.member
choice = (await self.game.bot.wait_for('message', check=check)).content.split()
if not len(choice) == n_ans:
await self.send_wrong(f"Please give {n_ans} numbers not {len(choice)}")
continue
try:
for c in choice:
self.check_num(c, len(options))
except ValueError as error:
await self.send_wrong(str(error))
continue
await self.send_confirmation(f"Received: {', '.join(choice)}")
return [int(c) for c in choice]
async def get_choice(self, question, options):
await self.ask_choice(question, options)
return (await self.receive_choice(options))[0]

View File

@ -1,86 +1,24 @@
import discord
"""Has a single class: Werewolf_player"""
# local import
from .player import Player
class Werewolf_player(Player):
"""This class is for simulating non-role-specific werewolf players"""
@staticmethod
async def make(member, game):
p = Player()
p.member = member
p.dm = member.dm_channel or await member.create_dm()
p.game = game
return p
def setRole(self, role):
def set_role(self, role):
self.day_role = self.night_role = role
def reset(self):
self.tally = 0
self.won = self.dead = False
def name(self):
return self.member.name
def __str__(self):
return self.name()
def swap(self, player_B):
self.day_role, player_B.day_role = player_B.day_role, self.day_role
def other(self):
return [p for p in self.game.player_list if p != self]
async def send_normal(self, message):
await self.dm.send(message)
async def send_embed(self, desc, color):
await self.dm.send(embed=discord.Embed(description=desc, color=color))
async def send_wrong(self, message):
await self.send_embed(message, 0xff8000)
async def send_confirmation(self, message):
await self.send_embed(message, 0x00ff00)
async def send_info(self, message):
await self.send_embed(message, 0x00ffff)
async def ask_choice(self, question, options):
text = f"{question}\n" + f"{'='*len(question)}\n\n" + '\n'.join(f"[{str(i)}]({str(options[i])})" for i in range(len(options)))
await self.dm.send(f"```md\n{text}```")
# TODO: Basic Converters (https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#basic-converters)
def check_num(self, choice, N):
if not choice.isdigit():
raise ValueError(f"Your choice {choice} is not a number")
if not 0 <= int(choice) < N:
raise ValueError(f"Your choice {choice} is not in range 0 - {N-1}")
async def receive_choice(self, options, n_ans=1):
while True:
def check(choice):
return choice.channel == self.dm and choice.author == self.member
choice = (await self.game.bot.wait_for('message', check=check)).content.split()
if not len(choice) == n_ans:
await self.send_wrong(f"Please give {n_ans} numbers not {len(choice)}")
continue
try:
for c in choice:
self.check_num(c, len(options))
except ValueError as error:
await self.send_wrong(str(error))
continue
await self.send_confirmation(f"Received: {', '.join(choice)}")
return [int(c) for c in choice]
async def get_choice(self, question, options):
await self.ask_choice(question, options)
return (await self.receive_choice(options))[0]
async def get_double_choice(self, question, options):
await self.ask_choice(question, options)
return await self.receive_choice(options, 2)
@ -95,6 +33,7 @@ class Werewolf_player(Player):
await self.send_confirmation("You are ready to vote")
class No_player(Player):
# TODO: this seems hacky, find another approach
class No_player(Werewolf_player):
def name(self):
return "no one"