Skip to content

Commit

Permalink
ref: unify
Browse files Browse the repository at this point in the history
  • Loading branch information
emptybutton committed Dec 29, 2024
1 parent eb5b337 commit 65903dd
Show file tree
Hide file tree
Showing 64 changed files with 284 additions and 279 deletions.
18 changes: 9 additions & 9 deletions src/pixel_battle/application/interactors/recolor_pixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
from pixel_battle.application.ports.broker import Broker
from pixel_battle.application.ports.clock import Clock
from pixel_battle.application.ports.user_data_signing import UserDataSigning
from pixel_battle.entities.core.pixel import Pixel, recolored_by
from pixel_battle.entities.core.pixel import Pixel, recolored_by_user
from pixel_battle.entities.core.user import (
user_temporarily_without_recoloring_right_when,
)
from pixel_battle.entities.quantities.color import (
from pixel_battle.entities.geometry.vector import Vector
from pixel_battle.entities.space.color import (
RGBColor,
RGBColorValue,
unknown_color,
)
from pixel_battle.entities.quantities.vector import Vector


@dataclass(kw_only=True, frozen=True, slots=True)
Expand Down Expand Up @@ -59,19 +59,19 @@ async def __call__(
pixel = Pixel(position=pixel_position, color=unknown_color)

new_pixel_color = RGBColor(
red=RGBColorValue(number=new_color_red_value_number),
green=RGBColorValue(number=new_color_green_value_number),
blue=RGBColorValue(number=new_color_blue_value_number),
red_value=RGBColorValue(number=new_color_red_value_number),
green_value=RGBColorValue(number=new_color_green_value_number),
blue_value=RGBColorValue(number=new_color_blue_value_number),
)

result = recolored_by(
user,
result = recolored_by_user(
pixel,
user=user,
new_color=new_pixel_color,
current_time=current_time,
)

await self.broker.push_new_event_with(pixel=result.pixel)
await self.broker.push_event_with(pixel=result.pixel)

signed_user_data = await self.user_data_signing.signed_user_data_when(
user=result.user
Expand Down
12 changes: 6 additions & 6 deletions src/pixel_battle/application/interactors/update_chunk_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pixel_battle.application.ports.broker import Broker
from pixel_battle.application.ports.chunk_view import (
ChunkView,
DefaultChunkViewWhere,
DefaultChunkViewWhen,
)
from pixel_battle.application.ports.chunk_views import ChunkViews
from pixel_battle.application.ports.lock import Lock
Expand All @@ -17,7 +17,7 @@ class UpdateChunkView[ChunkViewT: ChunkView, OffsetT]:
offsets_of_latest_compressed_events: Offsets[OffsetT]
lock: Lock
chunk_views: ChunkViews[ChunkViewT]
default_chunk_view_where: DefaultChunkViewWhere[ChunkViewT]
default_chunk_view_when: DefaultChunkViewWhen[ChunkViewT]

async def __call__(self, chunk_number_x: int, chunk_number_y: int) -> None:
chunk = Chunk(number=ChunkNumber(x=chunk_number_x, y=chunk_number_y))
Expand All @@ -26,25 +26,25 @@ async def __call__(self, chunk_number_x: int, chunk_number_y: int) -> None:
last_compressed_event_offset = (
await self
.offsets_of_latest_compressed_events
.offset_where(chunk=chunk)
.offset_when(chunk=chunk)
)

if last_compressed_event_offset is not None:
not_compressed_events = await self.broker.events_after(
last_compressed_event_offset, chunk=chunk
)
else:
not_compressed_events = await self.broker.all_events_where(
not_compressed_events = await self.broker.events_when(
chunk=chunk
)

if len(not_compressed_events) == 0:
return

chunk_view = await self.chunk_views.chunk_view_where(chunk=chunk)
chunk_view = await self.chunk_views.chunk_view_when(chunk=chunk)

if chunk_view is None:
chunk_view = await self.default_chunk_view_where(chunk=chunk)
chunk_view = await self.default_chunk_view_when(chunk=chunk)

pixels = (event.pixel for event in not_compressed_events)
await chunk_view.redraw_by_pixels(pixels)
Expand Down
14 changes: 7 additions & 7 deletions src/pixel_battle/application/interactors/view_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from pixel_battle.application.ports.broker import Broker
from pixel_battle.application.ports.chunk_view import (
ChunkView,
DefaultChunkViewWhere,
DefaultChunkViewWhen,
)
from pixel_battle.application.ports.chunk_views import ChunkViews
from pixel_battle.application.ports.offsets import Offsets
from pixel_battle.entities.core.chunk import Chunk, ChunkNumber
from pixel_battle.entities.core.pixel import Pixel
from pixel_battle.entities.quantities.color import RGBColor
from pixel_battle.entities.space.color import RGBColor


@dataclass(kw_only=True, frozen=True, slots=True)
Expand All @@ -24,25 +24,25 @@ class ViewChunk[ChunkViewT: ChunkView, OffsetT]:
broker: Broker[OffsetT]
offsets_of_latest_compressed_events: Offsets[OffsetT]
chunk_views: ChunkViews[ChunkViewT]
default_chunk_view_where: DefaultChunkViewWhere[ChunkViewT]
default_chunk_view_when: DefaultChunkViewWhen[ChunkViewT]

async def __call__(
self, chunk_number_x: int, chunk_number_y: int
) -> Output[ChunkViewT]:
chunk = Chunk(number=ChunkNumber(x=chunk_number_x, y=chunk_number_y))

offset = await self.offsets_of_latest_compressed_events.offset_where(
offset = await self.offsets_of_latest_compressed_events.offset_when(
chunk=chunk
)
chunk_view = await self.chunk_views.chunk_view_where(chunk=chunk)
chunk_view = await self.chunk_views.chunk_view_when(chunk=chunk)

if offset is not None:
events = await self.broker.events_after(offset, chunk=chunk)
else:
events = await self.broker.all_events_where(chunk=chunk)
events = await self.broker.events_when(chunk=chunk)

if chunk_view is None:
chunk_view = await self.default_chunk_view_where(chunk=chunk)
chunk_view = await self.default_chunk_view_when(chunk=chunk)

pixels = tuple(event.pixel for event in events)
return Output(pixels=pixels, chunk_view=chunk_view)
6 changes: 3 additions & 3 deletions src/pixel_battle/application/interactors/view_chunk_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pixel_battle.application.ports.broker import Broker
from pixel_battle.entities.core.chunk import Chunk, ChunkNumber
from pixel_battle.entities.core.pixel import Pixel
from pixel_battle.entities.quantities.color import RGBColor
from pixel_battle.entities.space.color import RGBColor


@dataclass(kw_only=True, frozen=True, slots=True)
Expand All @@ -21,6 +21,6 @@ async def __call__(
) -> Output:
chunk = Chunk(number=ChunkNumber(x=chunk_number_x, y=chunk_number_y))

async with self.broker.pulled_events_where(chunk=chunk) as new_events:
new_pixels = tuple(event.pixel for event in new_events)
async with self.broker.pulled_events_when(chunk=chunk) as events:
new_pixels = tuple(event.pixel for event in events)
return Output(new_pixels=new_pixels)
8 changes: 4 additions & 4 deletions src/pixel_battle/application/ports/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pixel_battle.entities.core.chunk import Chunk
from pixel_battle.entities.core.pixel import Pixel
from pixel_battle.entities.quantities.color import RGBColor
from pixel_battle.entities.space.color import RGBColor


@dataclass(kw_only=True, frozen=True, slots=True)
Expand All @@ -15,7 +15,7 @@ class NewPixelColorEvent[OffsetT]:

class Broker[OffsetT](ABC):
@abstractmethod
async def push_new_event_with(self, *, pixel: Pixel[RGBColor]) -> None: ...
async def push_event_with(self, *, pixel: Pixel[RGBColor]) -> None: ...

@abstractmethod
async def events_after(
Expand All @@ -24,12 +24,12 @@ async def events_after(
...

@abstractmethod
async def all_events_where(
async def events_when(
self, *, chunk: Chunk
) -> Sequence[NewPixelColorEvent[OffsetT]]: ...

@abstractmethod
def pulled_events_where(
def pulled_events_when(
self, *, chunk: Chunk
) -> AsyncContextManager[Sequence[NewPixelColorEvent[OffsetT]]]:
...
4 changes: 2 additions & 2 deletions src/pixel_battle/application/ports/chunk_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pixel_battle.entities.core.chunk import Chunk
from pixel_battle.entities.core.pixel import Pixel
from pixel_battle.entities.quantities.color import RGBColor
from pixel_battle.entities.space.color import RGBColor


class ChunkView(ABC):
Expand All @@ -12,6 +12,6 @@ async def redraw_by_pixels(self, pixels: Iterable[Pixel[RGBColor]]) -> None:
...


class DefaultChunkViewWhere[ChunkViewT: ChunkView](ABC):
class DefaultChunkViewWhen[ChunkViewT: ChunkView](ABC):
@abstractmethod
async def __call__(self, *, chunk: Chunk) -> ChunkViewT: ...
2 changes: 1 addition & 1 deletion src/pixel_battle/application/ports/chunk_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ChunkViews[ChunkViewT: ChunkView](ABC):
@abstractmethod
async def chunk_view_where(self, *, chunk: Chunk) -> ChunkViewT | None: ...
async def chunk_view_when(self, *, chunk: Chunk) -> ChunkViewT | None: ...

@abstractmethod
async def put(self, view: ChunkViewT, *, chunk: Chunk) -> None: ...
2 changes: 1 addition & 1 deletion src/pixel_battle/application/ports/clock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod

from pixel_battle.entities.quantities.time import Time
from pixel_battle.entities.space.time import Time


class Clock(ABC):
Expand Down
2 changes: 1 addition & 1 deletion src/pixel_battle/application/ports/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class Offsets[OffsetT](ABC):
async def put(self, offset: OffsetT, *, chunk: Chunk) -> None: ...

@abstractmethod
async def offset_where(self, *, chunk: Chunk) -> OffsetT | None: ...
async def offset_when(self, *, chunk: Chunk) -> OffsetT | None: ...
3 changes: 2 additions & 1 deletion src/pixel_battle/application/ports/user_data_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

class UserDataSigning[SignedUserDataT](ABC):
@abstractmethod
async def signed_user_data_when(self, *, user: User) -> SignedUserDataT: ...
async def signed_user_data_when(self, *, user: User) -> SignedUserDataT:
...

@abstractmethod
async def user_when(
Expand Down
6 changes: 3 additions & 3 deletions src/pixel_battle/entities/core/canvas.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dataclasses import dataclass

from pixel_battle.entities.quantities.rectangle import Rectangle, rectangle_with
from pixel_battle.entities.quantities.size import Size
from pixel_battle.entities.quantities.vector import Vector
from pixel_battle.entities.geometry.rectangle import Rectangle, rectangle_with
from pixel_battle.entities.geometry.size import Size
from pixel_battle.entities.geometry.vector import Vector


@dataclass(kw_only=True, frozen=True, slots=True)
Expand Down
8 changes: 4 additions & 4 deletions src/pixel_battle/entities/core/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from typing import ClassVar

from pixel_battle.entities.core.canvas import canvas
from pixel_battle.entities.quantities.rectangle import Rectangle, rectangle_with
from pixel_battle.entities.quantities.size import Size
from pixel_battle.entities.quantities.vector import Vector
from pixel_battle.entities.geometry.rectangle import Rectangle, rectangle_with
from pixel_battle.entities.geometry.size import Size
from pixel_battle.entities.geometry.vector import Vector


@dataclass(kw_only=True, frozen=True, slots=True)
Expand Down Expand Up @@ -46,7 +46,7 @@ def __post_init__(self) -> None:
is_y_valid = 0 <= self.y <= ChunkNumber.max_y

if not is_x_valid or not is_y_valid:
raise ExtremeChunkNumberValuesError
raise ExtremeChunkNumberValuesError(self)


def chunk_where(position: Vector) -> Chunk:
Expand Down
24 changes: 10 additions & 14 deletions src/pixel_battle/entities/core/pixel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass
from dataclasses import dataclass, field

from pixel_battle.entities.core.canvas import canvas
from pixel_battle.entities.core.chunk import Chunk, chunk_where
Expand All @@ -7,9 +7,9 @@
has_recoloring_right,
user_temporarily_without_recoloring_right_when,
)
from pixel_battle.entities.quantities.color import Color, RGBColor
from pixel_battle.entities.quantities.time import Time
from pixel_battle.entities.quantities.vector import Vector
from pixel_battle.entities.geometry.vector import Vector
from pixel_battle.entities.space.color import Color, RGBColor
from pixel_battle.entities.space.time import Time


class PixelError(Exception): ...
Expand All @@ -23,10 +23,6 @@ class Pixel[ColorT: Color]:
position: Vector
color: ColorT

@property
def id(self) -> Vector:
return self.position

@property
def chunk(self) -> Chunk:
return chunk_where(self.position)
Expand Down Expand Up @@ -58,21 +54,21 @@ def recolored[ColorT: Color](


@dataclass(kw_only=True, frozen=True, slots=True)
class PixelRecoloringByUser:
pixel: Pixel[RGBColor]
class RecoloredPixelByUser:
pixel: Pixel[RGBColor] = field(kw_only=False)
user: User


class UserHasNoRightToRecolorError(Exception): ...


def recolored_by[ColorT: Color](
user: User,
def recolored_by_user[ColorT: Color](
pixel: Pixel[ColorT],
*,
user: User,
new_color: RGBColor,
current_time: Time,
) -> PixelRecoloringByUser:
) -> RecoloredPixelByUser:
if not has_recoloring_right(user, current_time=current_time):
raise UserHasNoRightToRecolorError

Expand All @@ -81,4 +77,4 @@ def recolored_by[ColorT: Color](
current_time=current_time
)

return PixelRecoloringByUser(pixel=pixel, user=user)
return RecoloredPixelByUser(pixel, user=user)
2 changes: 1 addition & 1 deletion src/pixel_battle/entities/core/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from datetime import timedelta

from pixel_battle.entities.quantities.time import Time
from pixel_battle.entities.space.time import Time


@dataclass(kw_only=True, frozen=True, slots=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass

from pixel_battle.entities.quantities.size import Size
from pixel_battle.entities.quantities.vector import Vector
from pixel_battle.entities.geometry.size import Size
from pixel_battle.entities.geometry.vector import Vector


@dataclass(kw_only=True, frozen=True, slots=True, eq=False)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from dataclasses import dataclass

from pixel_battle.entities.quantities.vector import Vector
from pixel_battle.entities.geometry.vector import Vector


class SizeError(Exception): ...


class InvalidSizeError(Exception): ...
class NegativeSizeValuesError(SizeError): ...


@dataclass(kw_only=True, frozen=True, slots=True)
Expand All @@ -22,4 +22,4 @@ def to_number_set_vector(self) -> Vector:

def __post_init__(self) -> None:
if self.width <= 0 or self.height <= 0:
raise InvalidSizeError
raise NegativeSizeValuesError
Loading

0 comments on commit 65903dd

Please sign in to comment.