Skip to content

Commit

Permalink
Merge pull request #95 from gamultong/enhancement/cursor-revive-at
Browse files Browse the repository at this point in the history
cursor revive at 관리를 cursor 내부로 변경
  • Loading branch information
onee-only authored Dec 29, 2024
2 parents 039b92a + 4d9666b commit 522f862
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
9 changes: 3 additions & 6 deletions cursor/data/handler/test/cursor_handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,23 @@ def setUp(self):
pointer=None,
height=6,
width=6,
color=Color.BLUE,
revive_at=None
color=Color.BLUE
),
"B": Cursor(
conn_id="B",
position=Point(-3, -4),
pointer=None,
height=7,
width=7,
color=Color.BLUE,
revive_at=None
color=Color.BLUE
),
"C": Cursor(
conn_id="C",
position=Point(2, -1),
pointer=None,
height=4,
width=4,
color=Color.BLUE,
revive_at=None
color=Color.BLUE
)
}

Expand Down
16 changes: 13 additions & 3 deletions cursor/data/internal/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ class Cursor:
color: Color
width: int
height: int
revive_at: datetime | None
_revive_at: datetime | None = None

@property
def revive_at(self) -> datetime | None:
if (self._revive_at is not None) and (self._revive_at <= datetime.now()):
self._revive_at = None

return self._revive_at

@revive_at.setter
def revive_at(self, v: datetime) -> None:
self._revive_at = v

def set_size(self, width: int, height: int):
self.width = width
Expand Down Expand Up @@ -47,6 +58,5 @@ def create(conn_id: str):
pointer=None,
color=Color.get_random(),
width=0,
height=0,
revive_at=None
height=0
)
26 changes: 20 additions & 6 deletions cursor/data/test/cursor_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from board.data import Point
from cursor.data import Cursor, Color
from datetime import datetime, timedelta
import unittest


Expand All @@ -16,6 +17,22 @@ def test_cursor_create(self):
self.assertEqual(cursor.width, 0)
self.assertEqual(cursor.height, 0)

def test_cursor_revive_at(self):
conn_id = "some id"
cursor = Cursor.create(conn_id)

self.assertIsNone(cursor.revive_at)

# revive_at이 지나지 않음
hour_later = datetime.now() + timedelta(hours=1)
cursor.revive_at = hour_later
self.assertEqual(cursor.revive_at, hour_later)

# revive_at이 지남
hour_earlier = datetime.now() - timedelta(hours=1)
cursor.revive_at = hour_earlier
self.assertIsNone(cursor.revive_at)

def test_check_interactable(self):
# 0, 0
cursor = Cursor.create("")
Expand Down Expand Up @@ -46,26 +63,23 @@ def test_check_in_view(self):
pointer=None,
height=6,
width=6,
color=Color.BLUE,
revive_at=None
color=Color.BLUE
)
cur_b = Cursor(
conn_id="B",
position=Point(-3, -4),
pointer=None,
height=7,
width=7,
color=Color.BLUE,
revive_at=None
color=Color.BLUE
)
cur_c = Cursor(
conn_id="C",
position=Point(2, -1),
pointer=None,
height=4,
width=4,
color=Color.BLUE,
revive_at=None
color=Color.BLUE
)

self.assertTrue(cur_a.check_in_view(cur_c.position))
Expand Down
4 changes: 1 addition & 3 deletions cursor/event/handler/internal/cursor_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ async def receive_pointing(message: Message[PointingPayload]):

# 커서 부활시간 확인
if cursor.revive_at is not None:
if cursor.revive_at >= datetime.now():
return
cursor.revive_at = None
return

# 뷰 바운더리 안에서 포인팅하는지 확인
if not cursor.check_in_view(new_pointer):
Expand Down
11 changes: 8 additions & 3 deletions cursor/event/handler/test/cursor_event_handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ async def sleep(_):

@patch("event.EventBroker.publish")
async def test_receive_new_cursor_candidate_with_cursors(self, mock: AsyncMock):
c_revive_at = datetime.now()
# /docs/example/cursor-location.png
# But B is at 0,0
CursorHandler.cursor_dict = {
Expand All @@ -195,7 +194,6 @@ async def test_receive_new_cursor_candidate_with_cursors(self, mock: AsyncMock):
width=6,
# color 중요. 이따 비교에 써야 함.
color=Color.RED,
revive_at=None
),
"C": Cursor(
conn_id="C",
Expand All @@ -205,9 +203,15 @@ async def test_receive_new_cursor_candidate_with_cursors(self, mock: AsyncMock):
width=4,
# color 중요.
color=Color.BLUE,
revive_at=c_revive_at
)
}
# 지남. cursors에서 안 보여야 함.
a_revive_at = datetime(year=1000, month=1, day=1)
CursorHandler.cursor_dict["A"].revive_at = a_revive_at

# 안 지남. cursors에서 보여야 함.
c_revive_at = datetime(year=2200, month=1, day=1)
CursorHandler.cursor_dict["C"].revive_at = c_revive_at

original_cursors_len = 2
original_cursors = [c.conn_id for c in list(CursorHandler.cursor_dict.values())]
Expand Down Expand Up @@ -266,6 +270,7 @@ async def test_receive_new_cursor_candidate_with_cursors(self, mock: AsyncMock):
self.assertEqual(type(got.payload), CursorsPayload)
self.assertEqual(len(got.payload.cursors), 2)
self.assertEqual(got.payload.cursors[0].color, Color.RED)
self.assertIsNone(got.payload.cursors[0].revive_at)
self.assertEqual(got.payload.cursors[1].color, Color.BLUE)
self.assertEqual(got.payload.cursors[1].revive_at, c_revive_at.astimezone().isoformat())

Expand Down
9 changes: 3 additions & 6 deletions cursor/event/handler/test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@ def setup_cursor_locations() -> tuple[Cursor]:
pointer=None,
height=6,
width=6,
color=Color.YELLOW,
revive_at=None
color=Color.YELLOW
),
"B": Cursor(
conn_id="B",
position=Point(-3, -4),
pointer=None,
height=7,
width=7,
color=Color.BLUE,
revive_at=None
color=Color.BLUE
),
"C": Cursor(
conn_id="C",
position=Point(2, -1),
pointer=None,
height=4,
width=4,
color=Color.PURPLE,
revive_at=None
color=Color.PURPLE
)
}

Expand Down

0 comments on commit 522f862

Please sign in to comment.