Skip to content

Commit 1a8801d

Browse files
committed
Update the clash of code functionalities
1 parent df11623 commit 1a8801d

File tree

5 files changed

+239
-68
lines changed

5 files changed

+239
-68
lines changed

codingame/clash_of_code/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from .clash_of_code import ClashOfCode, Solution, TestCaseResult
1+
from .clash_of_code import ClashOfCode
22
from .player import Player
3-
from .question import Contribution, Question, TestCase
3+
from .question import Contribution, Question, TestCase, TestCaseResult
4+
from .solution import Solution
45

56
__all__ = (
67
"ClashOfCode",

codingame/clash_of_code/clash_of_code.py

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,13 @@
88
from ..types.clash_of_code import LanguageIds, Mode, Modes
99
from ..utils import minified_players_to_players, to_datetime
1010
from .player import Player
11-
from .question import Question
11+
from .question import Question, TestCaseResult
12+
from .solution import Solution
1213

1314
if TYPE_CHECKING:
1415
from ..state import ConnectionState
1516

16-
__all__ = ("ClashOfCode", "Solution", "TestCaseResult")
17-
18-
19-
class TestCaseResult(BaseObject):
20-
success: bool
21-
found: str
22-
expected: str
23-
24-
__slots__ = ("success", "found", "expected")
25-
26-
def __init__(self, state: "ConnectionState", data: dict):
27-
self.success = data["comparison"]["success"]
28-
self.found = (
29-
data["output"] if self.success else data["comparison"]["success"]
30-
)
31-
self.expected = (
32-
data["output"] if self.success else data["comparison"]["success"]
33-
)
34-
35-
super().__init__(state)
36-
37-
38-
class Solution(BaseObject):
39-
__slots__ = ()
40-
41-
def __init__(self, state: "ConnectionState", data: dict):
42-
...
43-
44-
super().__init__(state)
45-
46-
def share(self):
47-
...
17+
__all__ = ("ClashOfCode",)
4818

4919

5020
class ClashOfCode(BaseObject):
@@ -456,7 +426,9 @@ async def _get_question():
456426
)
457427
)
458428
self._question = Question(
459-
self._state, test_session["currentQuestion"]["question"]
429+
self._state,
430+
self,
431+
test_session["currentQuestion"]["question"],
460432
)
461433

462434
if refetch:
@@ -480,7 +452,9 @@ def _get_question():
480452
)
481453
)
482454
self._question = Question(
483-
self._state, test_session["currentQuestion"]["question"]
455+
self._state,
456+
self,
457+
test_session["currentQuestion"]["question"],
484458
)
485459

486460
if refetch:
@@ -532,15 +506,19 @@ async def _play_test_cases():
532506
if not self._question:
533507
await self.get_question()
534508

535-
all_indexes = [tc.index for tc in self._question.test_cases]
536509
results = {}
537-
for index in all_indexes:
538-
if indexes and index not in indexes:
510+
for test_case in self._question.test_cases:
511+
if indexes and test_case.index not in indexes:
539512
continue
540513
result = await self._state.http.play_test_session_by_handle(
541-
self._test_session_handle, language_id, code, index
514+
self._test_session_handle,
515+
language_id,
516+
code,
517+
test_case.index,
518+
)
519+
results[test_case.index] = TestCaseResult(
520+
self._state, self, test_case, result
542521
)
543-
results[index] = TestCaseResult(self._state, result)
544522

545523
if refetch:
546524
await self.fetch()
@@ -553,15 +531,19 @@ def _play_test_cases():
553531
if not self._question:
554532
self.get_question()
555533

556-
all_indexes = [tc.index for tc in self._question.test_cases]
557534
results = {}
558-
for index in all_indexes:
559-
if indexes and index not in indexes:
535+
for test_case in self._question.test_cases:
536+
if indexes and test_case.index not in indexes:
560537
continue
561538
result = self._state.http.play_test_session_by_handle(
562-
self._test_session_handle, language_id, code, index
539+
self._test_session_handle,
540+
language_id,
541+
code,
542+
test_case.index,
543+
)
544+
results[test_case.index] = TestCaseResult(
545+
self._state, self, test_case, result
563546
)
564-
results[index] = TestCaseResult(self._state, result)
565547

566548
if refetch:
567549
self.fetch()
@@ -620,7 +602,7 @@ async def _submit():
620602
if refetch:
621603
await self.fetch()
622604

623-
return Solution(self._state, solution)
605+
return Solution(self._state, self, solution)
624606

625607
else:
626608

@@ -638,6 +620,6 @@ def _submit():
638620
if refetch:
639621
self.fetch()
640622

641-
return Solution(self._state, solution)
623+
return Solution(self._state, self, solution)
642624

643625
return _submit()

codingame/clash_of_code/player.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from typing import TYPE_CHECKING, Optional
33

44
from ..abc import BaseUser
5+
from ..exceptions import LoginRequired
56
from ..types.clash_of_code import LanguageId, PlayerStatus
7+
from .solution import Solution
68

79
if TYPE_CHECKING:
810
from ..state import ConnectionState
@@ -72,9 +74,9 @@ class Player(BaseUser):
7274
Only available when the Clash of Code's mode is ``SHORTEST``.
7375
7476
solution_shared: Optional :class:`bool`
75-
Whether the Player shared his code.
77+
Whether the Player shared their code.
7678
77-
submission_id: Optional :class:`int`
79+
solution_id: Optional :class:`int`
7880
ID of the player's submission.
7981
"""
8082

@@ -95,7 +97,7 @@ class Player(BaseUser):
9597
score: Optional[int]
9698
code_length: Optional[int]
9799
solution_shared: Optional[bool]
98-
submission_id: Optional[int]
100+
solution_id: Optional[int]
99101

100102
__slots__ = (
101103
"clash_of_code",
@@ -110,6 +112,7 @@ class Player(BaseUser):
110112
"score",
111113
"code_length",
112114
"solution_shared",
115+
"solution_id",
113116
"submission_id",
114117
"test_session_status",
115118
"test_session_handle",
@@ -118,12 +121,12 @@ class Player(BaseUser):
118121
def __init__(
119122
self,
120123
state: "ConnectionState",
121-
coc: "ClashOfCode",
124+
clash_of_code: "ClashOfCode",
122125
started: bool,
123126
finished: bool,
124127
data: dict,
125128
):
126-
self.clash_of_code: "ClashOfCode" = coc
129+
self.clash_of_code: "ClashOfCode" = clash_of_code
127130

128131
self.public_handle = data.get("codingamerHandle")
129132
self.id = data["codingamerId"]
@@ -148,7 +151,9 @@ def __init__(
148151
self.score = data.get("score")
149152
self.code_length = data.get("criterion")
150153
self.solution_shared = data.get("solutionShared")
151-
self.submission_id = data.get("submissionId")
154+
self.solution_id = data.get("submissionId")
155+
# TODO decprecate Player.submission_id
156+
self.submission_id = self.solution_id
152157

153158
self.test_session_status = data.get("testSessionStatus")
154159
self.test_session_handle = data.get("testSessionHandle")
@@ -161,3 +166,28 @@ def __repr__(self) -> str:
161166
"position={0.position!r} rank={0.rank!r} duration={0.duration!r} "
162167
"score={0.score!r} language_id={0.language_id!r}>".format(self)
163168
)
169+
170+
def get_solution(self) -> Solution:
171+
if not self._state.logged_in:
172+
raise LoginRequired()
173+
174+
if not self.solution_shared:
175+
raise ValueError() # TODO raise better error
176+
177+
if self._state.is_async:
178+
179+
async def _get_solution():
180+
solution = await self._state.http.get_solution_by_id(
181+
self._state.codingamer.id, self.solution_id
182+
)
183+
return Solution(self._state, self.clash_of_code, solution)
184+
185+
else:
186+
187+
def _get_solution():
188+
solution = self._state.http.get_solution_by_id(
189+
self._state.codingamer.id, self.solution_id
190+
)
191+
return Solution(self._state, self.clash_of_code, solution)
192+
193+
return _get_solution()

0 commit comments

Comments
 (0)