aboutsummaryrefslogtreecommitdiffstats
path: root/bot/seasons/evergreen/snakes/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot/seasons/evergreen/snakes/utils.py')
-rw-r--r--bot/seasons/evergreen/snakes/utils.py46
1 files changed, 23 insertions, 23 deletions
diff --git a/bot/seasons/evergreen/snakes/utils.py b/bot/seasons/evergreen/snakes/utils.py
index e8d2ee44..24e71227 100644
--- a/bot/seasons/evergreen/snakes/utils.py
+++ b/bot/seasons/evergreen/snakes/utils.py
@@ -11,7 +11,7 @@ from typing import List, Tuple
from PIL import Image
from PIL.ImageDraw import ImageDraw
from discord import File, Member, Reaction
-from discord.ext.commands import Context
+from discord.ext.commands import Cog, Context
SNAKE_RESOURCES = Path("bot/resources/snakes").absolute()
@@ -116,12 +116,12 @@ def get_resource(file: str) -> List[dict]:
return json.load(snakefile)
-def smoothstep(t):
+def smoothstep(t: float) -> float:
"""Smooth curve with a zero derivative at 0 and 1, making it useful for interpolating."""
return t * t * (3. - 2. * t)
-def lerp(t, a, b):
+def lerp(t: float, a: float, b: float) -> float:
"""Linear interpolation between a and b, given a fraction t."""
return a + t * (b - a)
@@ -138,7 +138,7 @@ class PerlinNoiseFactory(object):
Licensed under ISC
"""
- def __init__(self, dimension, octaves=1, tile=(), unbias=False):
+ def __init__(self, dimension: int, octaves: int = 1, tile: Tuple[int] = (), unbias: bool = False):
"""
Create a new Perlin noise factory in the given number of dimensions.
@@ -152,7 +152,7 @@ class PerlinNoiseFactory(object):
This will produce noise that tiles every 3 units vertically, but never tiles horizontally.
- If ``unbias`` is true, the smoothstep function will be applied to the output before returning
+ If ``unbias`` is True, the smoothstep function will be applied to the output before returning
it, to counteract some of Perlin noise's significant bias towards the center of its output range.
"""
self.dimension = dimension
@@ -166,7 +166,7 @@ class PerlinNoiseFactory(object):
self.gradient = {}
- def _generate_gradient(self):
+ def _generate_gradient(self) -> Tuple[float, ...]:
"""
Generate a random unit vector at each grid point.
@@ -186,7 +186,7 @@ class PerlinNoiseFactory(object):
scale = sum(n * n for n in random_point) ** -0.5
return tuple(coord * scale for coord in random_point)
- def get_plain_noise(self, *point):
+ def get_plain_noise(self, *point) -> float:
"""Get plain noise for a single point, without taking into account either octaves or tiling."""
if len(point) != self.dimension:
raise ValueError("Expected {0} values, got {1}".format(
@@ -234,7 +234,7 @@ class PerlinNoiseFactory(object):
return dots[0] * self.scale_factor
- def __call__(self, *point):
+ def __call__(self, *point) -> float:
"""
Get the value of this Perlin noise function at the given point.
@@ -367,7 +367,7 @@ GAME_SCREEN_EMOJI = [
class SnakeAndLaddersGame:
"""Snakes and Ladders game Cog."""
- def __init__(self, snakes, context: Context):
+ def __init__(self, snakes: Cog, context: Context):
self.snakes = snakes
self.ctx = context
self.channel = self.ctx.channel
@@ -382,14 +382,14 @@ class SnakeAndLaddersGame:
self.positions = None
self.rolls = []
- async def open_game(self):
+ async def open_game(self) -> None:
"""
Create a new Snakes and Ladders game.
Listen for reactions until players have joined,
and the game has been started.
"""
- def startup_event_check(reaction_: Reaction, user_: Member):
+ def startup_event_check(reaction_: Reaction, user_: Member) -> bool:
"""Make sure that this reaction is what we want to operate on."""
return (
all((
@@ -454,7 +454,7 @@ class SnakeAndLaddersGame:
self.cancel_game(self.author)
return # We're done, no reactions for the last 5 minutes
- async def _add_player(self, user: Member):
+ async def _add_player(self, user: Member) -> None:
self.players.append(user)
self.player_tiles[user.id] = 1
@@ -462,7 +462,7 @@ class SnakeAndLaddersGame:
im = Image.open(io.BytesIO(avatar_bytes)).resize((BOARD_PLAYER_SIZE, BOARD_PLAYER_SIZE))
self.avatar_images[user.id] = im
- async def player_join(self, user: Member):
+ async def player_join(self, user: Member) -> None:
"""
Handle players joining the game.
@@ -488,7 +488,7 @@ class SnakeAndLaddersGame:
delete_after=10
)
- async def player_leave(self, user: Member):
+ async def player_leave(self, user: Member) -> None:
"""
Handle players leaving the game.
@@ -518,7 +518,7 @@ class SnakeAndLaddersGame:
return
await self.channel.send(user.mention + " You are not in the match.", delete_after=10)
- async def cancel_game(self, user: Member):
+ async def cancel_game(self, user: Member) -> None:
"""Allow the game author to cancel the running game."""
if not user == self.author:
await self.channel.send(user.mention + " Only the author of the game can cancel it.", delete_after=10)
@@ -526,7 +526,7 @@ class SnakeAndLaddersGame:
await self.channel.send("**Snakes and Ladders**: Game has been canceled.")
self._destruct()
- async def start_game(self, user: Member):
+ async def start_game(self, user: Member) -> None:
"""
Allow the game author to begin the game.
@@ -550,9 +550,9 @@ class SnakeAndLaddersGame:
await self.channel.send("**Snakes and Ladders**: The game is starting!\nPlayers: " + player_list)
await self.start_round()
- async def start_round(self):
+ async def start_round(self) -> None:
"""Begin the round."""
- def game_event_check(reaction_: Reaction, user_: Member):
+ def game_event_check(reaction_: Reaction, user_: Member) -> bool:
"""Make sure that this reaction is what we want to operate on."""
return (
all((
@@ -642,7 +642,7 @@ class SnakeAndLaddersGame:
# Round completed
await self._complete_round()
- async def player_roll(self, user: Member):
+ async def player_roll(self, user: Member) -> None:
"""Handle the player's roll."""
if user.id not in self.player_tiles:
await self.channel.send(user.mention + " You are not in the match.", delete_after=10)
@@ -674,7 +674,7 @@ class SnakeAndLaddersGame:
self.player_tiles[user.id] = min(100, next_tile)
self.round_has_rolled[user.id] = True
- async def _complete_round(self):
+ async def _complete_round(self) -> None:
self.state = 'post_round'
# check for winner
@@ -694,13 +694,13 @@ class SnakeAndLaddersGame:
return next((player for player in self.players if self.player_tiles[player.id] == 100),
None)
- def _check_all_rolled(self):
+ def _check_all_rolled(self) -> bool:
return all(rolled for rolled in self.round_has_rolled.values())
- def _destruct(self):
+ def _destruct(self) -> None:
del self.snakes.active_sal[self.channel]
- def _board_coordinate_from_index(self, index: int):
+ def _board_coordinate_from_index(self, index: int) -> Tuple[float, float]:
# converts the tile number to the x/y coordinates for graphical purposes
y_level = 9 - math.floor((index - 1) / 10)
is_reversed = math.floor((index - 1) / 10) % 2 != 0