Skip to content

Commit 496e5bb

Browse files
authored
Merge pull request #109 from GuoWQ222/dev
Update Python CAPI
2 parents 4311459 + 0ac1283 commit 496e5bb

20 files changed

+9893
-8843
lines changed

CAPI/cpp/proto/Message2Clients.pb.cc

100755100644
+3,977-2,806
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CAPI/cpp/proto/Message2Clients.pb.h

100755100644
+2,695-3,315
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CAPI/cpp/proto/Message2Server.pb.cc

100755100644
+1,558-1,151
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CAPI/cpp/proto/Message2Server.pb.h

100755100644
+820-1,123
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CAPI/cpp/proto/MessageType.pb.cc

100755100644
+231-132
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CAPI/cpp/proto/MessageType.pb.h

100755100644
+206-237
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CAPI/cpp/proto/Services.grpc.pb.cc

100755100644
File mode changed.

CAPI/cpp/proto/Services.grpc.pb.h

100755100644
File mode changed.

CAPI/cpp/proto/Services.pb.cc

100755100644
+4-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CAPI/cpp/proto/Services.pb.h

100755100644
+10-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CAPI/python/PyAPI/AI.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import PyAPI.structures as THUAI7
2+
from PyAPI.Interface import IShipAPI, ITeamAPI, IAI
3+
from typing import Union, Final, cast, List
4+
from PyAPI.constants import Constants
5+
import queue
6+
7+
import time
8+
9+
10+
class Setting:
11+
# 为假则play()期间确保游戏状态不更新,为真则只保证游戏状态在调用相关方法时不更新,大致一帧更新一次
12+
@staticmethod
13+
def asynchronous() -> bool:
14+
return False
15+
16+
17+
numOfGridPerCell: Final[int] = 1000
18+
19+
20+
class AssistFunction:
21+
@staticmethod
22+
def CellToGrid(cell: int) -> int:
23+
return cell * numOfGridPerCell + numOfGridPerCell // 2
24+
25+
@staticmethod
26+
def GridToCell(grid: int) -> int:
27+
return grid // numOfGridPerCell
28+
29+
30+
class AI(IAI):
31+
def __init__(self, pID: int):
32+
self.__playerID = pID
33+
34+
def ShipPlay(self, api: IShipAPI) -> None:
35+
# 公共操作
36+
37+
if self.__playerID == 0:
38+
# player0的操作
39+
return
40+
elif self.__playerID == 1:
41+
# player1的操作
42+
return
43+
elif self.__playerID == 2:
44+
# player2的操作
45+
return
46+
return
47+
48+
def TeamPlay(self, api: ITeamAPI) -> None:
49+
# 操作
50+
return

CAPI/python/PyAPI/API.py

+219-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,222 @@
1-
import PyAPI.structures as THUAI6
2-
from PyAPI.Interface import ILogic, IStudentAPI, ITrickerAPI, IGameTimer, IAI
1+
import PyAPI.structures as THUAI7
2+
from PyAPI.Interface import ILogic, IShipAPI, ITeamAPI, IGameTimer, IAI
33
from math import pi
44
from concurrent.futures import ThreadPoolExecutor, Future
55
from typing import List, cast, Tuple, Union
6+
7+
8+
class ShipAPI(IShipAPI, IGameTimer):
9+
def __init__(self, logic: ILogic) -> None:
10+
self.__logic = logic
11+
self.__pool = ThreadPoolExecutor(20)
12+
13+
def Move(self, timeInMilliseconds: int, angle: float) -> Future[bool]:
14+
return self.__pool.submit(self.__logic.move, timeInMilliseconds, angle)
15+
16+
def MoveRight(self, timeInMilliseconds: int) -> Future[bool]:
17+
return self.Move(timeInMilliseconds, pi * 0.5)
18+
19+
def MoveLeft(self, timeInMilliseconds: int) -> Future[bool]:
20+
return self.Move(timeInMilliseconds, pi * 1.5)
21+
22+
def MoveUp(self, timeInMilliseconds: int) -> Future[bool]:
23+
return self.Move(timeInMilliseconds, pi)
24+
25+
def MoveDown(self, timeInMilliseconds: int) -> Future[bool]:
26+
return self.Move(timeInMilliseconds, 0)
27+
28+
def Attack(self, angle: float) -> Future[bool]:
29+
return self.__pool.submit(self.__logic.Attack, angle)
30+
31+
def Recover(self) -> Future[bool]:
32+
return self.__pool.submit(self.__logic.Recover)
33+
34+
def Produce(self) -> Future[bool]:
35+
return self.__pool.submit(self.__logic.Produce)
36+
37+
def Rebuild(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
38+
return self.__pool.submit(self.__logic.Rebuild, constructionType)
39+
40+
def Construct(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
41+
return self.__pool.submit(self.__logic.Construct, constructionType)
42+
43+
def GetFrameCount(self) -> int:
44+
return self.__logic.GetFrameCount()
45+
46+
def Wait(self) -> bool:
47+
if self.__logic.GetCounter() == -1:
48+
return False
49+
else:
50+
return self.__logic.WaitThread()
51+
52+
def EndAllAction(self) -> Future[bool]:
53+
return self.__pool.submit(self.__logic.EndAllAction)
54+
55+
def SendMessage(self, toID: int, message: Union[str, bytes]) -> Future[bool]:
56+
return self.__pool.submit(self.__logic.SendMessage, toID, message)
57+
58+
def HaveMessage(self) -> bool:
59+
return self.__logic.HaveMessage()
60+
61+
def GetMessage(self) -> Tuple[int, Union[str, bytes]]:
62+
return self.__logic.GetMessage()
63+
64+
def GetShips(self) -> List[THUAI7.Ship]:
65+
return self.__logic.GetShips()
66+
67+
def GetEnemyShips(self) -> List[THUAI7.Ship]:
68+
return self.__logic.GetEnemyShips()
69+
70+
def GetBullets(self) -> List[THUAI7.Bullet]:
71+
return self.__logic.GetBullets()
72+
73+
def GetFullMap(self) -> List[List[THUAI7.PlaceType]]:
74+
return self.__logic.GetFullMap()
75+
76+
def GetPlaceType(self, cellX: int, cellY: int) -> THUAI7.PlaceType:
77+
return self.__logic.GetPlaceType(cellX, cellY)
78+
79+
def GetConstructionHp(self, cellX: int, cellY: int) -> int:
80+
return self.__logic.GetConstructionHp(cellX, cellY)
81+
82+
def GetWormHp(self, cellX: int, cellY: int) -> int:
83+
return self.__logic.GetWormHp(cellX, cellY)
84+
85+
def GetResouceState(self, cellX: int, cellY: int) -> int:
86+
return self.__logic.GetResouceState(cellX, cellY)
87+
88+
def GetHomeHp(self) -> int:
89+
return self.__logic.GetHomeHp()
90+
91+
def GetGameInfo(self) -> THUAI7.GameInfo:
92+
return self.__logic.GetGameInfo()
93+
94+
def GetPlayerGUIDs(self) -> List[int]:
95+
return self.__logic.GetPlayerGUIDs()
96+
97+
def GetSelfInfo(self) -> THUAI7.Ship:
98+
return cast(THUAI7.Ships, self.__logic.GetSelfInfo())
99+
100+
def GetMoney(self) -> int:
101+
return self.__logic.GetMoney()
102+
103+
def GetScore(self) -> int:
104+
return self.__logic.GetScore()
105+
106+
def HaveView(self, gridX: int, gridY: int) -> bool:
107+
return self.__logic.HaveView(gridX, gridY, self.GetSelfInfo().x,
108+
self.GetSelfInfo().y, self.GetSelfInfo().viewRange)
109+
110+
def Print(self, cont: str) -> None:
111+
pass
112+
113+
def PrintShip(self) -> None:
114+
pass
115+
116+
def PrintSelfInfo(self) -> None:
117+
pass
118+
119+
def StartTimer(self) -> None:
120+
pass
121+
122+
def EndTimer(self) -> None:
123+
pass
124+
125+
def Play(self, ai: IAI) -> None:
126+
ai.ShipPlay(self)
127+
128+
129+
class TeamAPI(ITeamAPI, IGameTimer):
130+
def __init__(self, logic: ILogic) -> None:
131+
self.__logic = logic
132+
self.__pool = ThreadPoolExecutor(20)
133+
134+
def StartTimer(self) -> None:
135+
pass
136+
137+
def EndTimer(self) -> None:
138+
pass
139+
140+
def Play(self, ai: IAI) -> None:
141+
ai.TeamPlay(self)
142+
143+
def SendMessage(self, toID: int, message: Union[str, bytes]) -> Future[bool]:
144+
return self.__pool.submit(self.__logic.SendMessage, toID, message)
145+
146+
def HaveMessage(self) -> bool:
147+
return self.__logic.HaveMessage()
148+
149+
def GetMessage(self) -> Tuple[int, Union[str, bytes]]:
150+
return self.__logic.GetMessage()
151+
152+
def GetFrameCount(self) -> int:
153+
return self.__logic.GetFrameCount()
154+
155+
def Wait(self) -> bool:
156+
if self.__logic.GetCounter() == -1:
157+
return False
158+
else:
159+
return self.__logic.WaitThread()
160+
161+
def EndAllAction(self) -> Future[bool]:
162+
return self.__pool.submit(self.__logic.EndAllAction)
163+
164+
def GetBullets(self) -> List[THUAI7.Bullet]:
165+
return self.__logic.GetBullets()
166+
167+
def GetShips(self) -> List[THUAI7.Ship]:
168+
return self.__logic.GetShips()
169+
170+
def GetEnemyShips(self) -> List[THUAI7.Ship]:
171+
return self.__logic.GetEnemyShips()
172+
173+
def GetFullMap(self) -> List[List[THUAI7.PlaceType]]:
174+
return self.__logic.GetFullMap()
175+
176+
def GetPlaceType(self, cellX: int, cellY: int) -> THUAI7.PlaceType:
177+
return self.__logic.GetPlaceType(cellX, cellY)
178+
179+
def GetConstructionHp(self, cellX: int, cellY: int) -> int:
180+
return self.__logic.GetConstructionHp(cellX, cellY)
181+
182+
def GetWormHp(self, cellX: int, cellY: int) -> int:
183+
return self.__logic.GetWormHp(cellX, cellY)
184+
185+
def GetResouceState(self, cellX: int, cellY: int) -> int:
186+
return self.__logic.GetResouceState(cellX, cellY)
187+
188+
def GetHomeHp(self) -> int:
189+
return self.__logic.GetHomeHp()
190+
191+
def GetGameInfo(self) -> THUAI7.GameInfo:
192+
return self.__logic.GetGameInfo()
193+
194+
def GetPlayerGUIDs(self) -> List[int]:
195+
return self.__logic.GetPlayerGUIDs()
196+
197+
def GetSelfInfo(self) -> THUAI7.Home:
198+
return cast(THUAI7.Home, self.__logic.GetSelfInfo())
199+
200+
def GetScore(self) -> int:
201+
return self.__logic.GetScore()
202+
203+
def GetMoney(self) -> int:
204+
return self.__logic.GetMoney()
205+
206+
def InstallModule(self, ID: int, type: THUAI7.ModuleType) -> Future[bool]:
207+
return self.__pool.submit(self.__logic.InstallModule, ID, type)
208+
209+
def Recycle(self, ID: int) -> Future[bool]:
210+
return self.__pool.submit(self.__logic.Recycle, ID)
211+
212+
def BuildShip(self, shipType: THUAI7.ShipType, cellX: int, cellY: int) -> Future[bool]:
213+
return self.__pool.submit(self.__logic.BuildShip, shipType, cellX, cellY)
214+
215+
def Print(self, string: str) -> None:
216+
pass
217+
218+
def PrintTeam(self) -> None:
219+
pass
220+
221+
def PrintSelfInfo(self) -> None:
222+
pass

0 commit comments

Comments
 (0)