Skip to content

Commit

Permalink
Merge pull request eesast#109 from GuoWQ222/dev
Browse files Browse the repository at this point in the history
Update Python CAPI
  • Loading branch information
asdawej authored Mar 4, 2024
2 parents 4311459 + 0ac1283 commit 496e5bb
Show file tree
Hide file tree
Showing 20 changed files with 9,893 additions and 8,843 deletions.
6,783 changes: 3,977 additions & 2,806 deletions CAPI/cpp/proto/Message2Clients.pb.cc
100755 → 100644

Large diffs are not rendered by default.

6,010 changes: 2,695 additions & 3,315 deletions CAPI/cpp/proto/Message2Clients.pb.h
100755 → 100644

Large diffs are not rendered by default.

2,709 changes: 1,558 additions & 1,151 deletions CAPI/cpp/proto/Message2Server.pb.cc
100755 → 100644

Large diffs are not rendered by default.

1,943 changes: 820 additions & 1,123 deletions CAPI/cpp/proto/Message2Server.pb.h
100755 → 100644

Large diffs are not rendered by default.

363 changes: 231 additions & 132 deletions CAPI/cpp/proto/MessageType.pb.cc
100755 → 100644

Large diffs are not rendered by default.

443 changes: 206 additions & 237 deletions CAPI/cpp/proto/MessageType.pb.h
100755 → 100644

Large diffs are not rendered by default.

Empty file modified CAPI/cpp/proto/Services.grpc.pb.cc
100755 → 100644
Empty file.
Empty file modified CAPI/cpp/proto/Services.grpc.pb.h
100755 → 100644
Empty file.
14 changes: 4 additions & 10 deletions CAPI/cpp/proto/Services.pb.cc
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 10 additions & 21 deletions CAPI/cpp/proto/Services.pb.h
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions CAPI/python/PyAPI/AI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import PyAPI.structures as THUAI7
from PyAPI.Interface import IShipAPI, ITeamAPI, IAI
from typing import Union, Final, cast, List
from PyAPI.constants import Constants
import queue

import time


class Setting:
# 为假则play()期间确保游戏状态不更新,为真则只保证游戏状态在调用相关方法时不更新,大致一帧更新一次
@staticmethod
def asynchronous() -> bool:
return False


numOfGridPerCell: Final[int] = 1000


class AssistFunction:
@staticmethod
def CellToGrid(cell: int) -> int:
return cell * numOfGridPerCell + numOfGridPerCell // 2

@staticmethod
def GridToCell(grid: int) -> int:
return grid // numOfGridPerCell


class AI(IAI):
def __init__(self, pID: int):
self.__playerID = pID

def ShipPlay(self, api: IShipAPI) -> None:
# 公共操作

if self.__playerID == 0:
# player0的操作
return
elif self.__playerID == 1:
# player1的操作
return
elif self.__playerID == 2:
# player2的操作
return
return

def TeamPlay(self, api: ITeamAPI) -> None:
# 操作
return
221 changes: 219 additions & 2 deletions CAPI/python/PyAPI/API.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,222 @@
import PyAPI.structures as THUAI6
from PyAPI.Interface import ILogic, IStudentAPI, ITrickerAPI, IGameTimer, IAI
import PyAPI.structures as THUAI7
from PyAPI.Interface import ILogic, IShipAPI, ITeamAPI, IGameTimer, IAI
from math import pi
from concurrent.futures import ThreadPoolExecutor, Future
from typing import List, cast, Tuple, Union


class ShipAPI(IShipAPI, IGameTimer):
def __init__(self, logic: ILogic) -> None:
self.__logic = logic
self.__pool = ThreadPoolExecutor(20)

def Move(self, timeInMilliseconds: int, angle: float) -> Future[bool]:
return self.__pool.submit(self.__logic.move, timeInMilliseconds, angle)

def MoveRight(self, timeInMilliseconds: int) -> Future[bool]:
return self.Move(timeInMilliseconds, pi * 0.5)

def MoveLeft(self, timeInMilliseconds: int) -> Future[bool]:
return self.Move(timeInMilliseconds, pi * 1.5)

def MoveUp(self, timeInMilliseconds: int) -> Future[bool]:
return self.Move(timeInMilliseconds, pi)

def MoveDown(self, timeInMilliseconds: int) -> Future[bool]:
return self.Move(timeInMilliseconds, 0)

def Attack(self, angle: float) -> Future[bool]:
return self.__pool.submit(self.__logic.Attack, angle)

def Recover(self) -> Future[bool]:
return self.__pool.submit(self.__logic.Recover)

def Produce(self) -> Future[bool]:
return self.__pool.submit(self.__logic.Produce)

def Rebuild(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
return self.__pool.submit(self.__logic.Rebuild, constructionType)

def Construct(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
return self.__pool.submit(self.__logic.Construct, constructionType)

def GetFrameCount(self) -> int:
return self.__logic.GetFrameCount()

def Wait(self) -> bool:
if self.__logic.GetCounter() == -1:
return False
else:
return self.__logic.WaitThread()

def EndAllAction(self) -> Future[bool]:
return self.__pool.submit(self.__logic.EndAllAction)

def SendMessage(self, toID: int, message: Union[str, bytes]) -> Future[bool]:
return self.__pool.submit(self.__logic.SendMessage, toID, message)

def HaveMessage(self) -> bool:
return self.__logic.HaveMessage()

def GetMessage(self) -> Tuple[int, Union[str, bytes]]:
return self.__logic.GetMessage()

def GetShips(self) -> List[THUAI7.Ship]:
return self.__logic.GetShips()

def GetEnemyShips(self) -> List[THUAI7.Ship]:
return self.__logic.GetEnemyShips()

def GetBullets(self) -> List[THUAI7.Bullet]:
return self.__logic.GetBullets()

def GetFullMap(self) -> List[List[THUAI7.PlaceType]]:
return self.__logic.GetFullMap()

def GetPlaceType(self, cellX: int, cellY: int) -> THUAI7.PlaceType:
return self.__logic.GetPlaceType(cellX, cellY)

def GetConstructionHp(self, cellX: int, cellY: int) -> int:
return self.__logic.GetConstructionHp(cellX, cellY)

def GetWormHp(self, cellX: int, cellY: int) -> int:
return self.__logic.GetWormHp(cellX, cellY)

def GetResouceState(self, cellX: int, cellY: int) -> int:
return self.__logic.GetResouceState(cellX, cellY)

def GetHomeHp(self) -> int:
return self.__logic.GetHomeHp()

def GetGameInfo(self) -> THUAI7.GameInfo:
return self.__logic.GetGameInfo()

def GetPlayerGUIDs(self) -> List[int]:
return self.__logic.GetPlayerGUIDs()

def GetSelfInfo(self) -> THUAI7.Ship:
return cast(THUAI7.Ships, self.__logic.GetSelfInfo())

def GetMoney(self) -> int:
return self.__logic.GetMoney()

def GetScore(self) -> int:
return self.__logic.GetScore()

def HaveView(self, gridX: int, gridY: int) -> bool:
return self.__logic.HaveView(gridX, gridY, self.GetSelfInfo().x,
self.GetSelfInfo().y, self.GetSelfInfo().viewRange)

def Print(self, cont: str) -> None:
pass

def PrintShip(self) -> None:
pass

def PrintSelfInfo(self) -> None:
pass

def StartTimer(self) -> None:
pass

def EndTimer(self) -> None:
pass

def Play(self, ai: IAI) -> None:
ai.ShipPlay(self)


class TeamAPI(ITeamAPI, IGameTimer):
def __init__(self, logic: ILogic) -> None:
self.__logic = logic
self.__pool = ThreadPoolExecutor(20)

def StartTimer(self) -> None:
pass

def EndTimer(self) -> None:
pass

def Play(self, ai: IAI) -> None:
ai.TeamPlay(self)

def SendMessage(self, toID: int, message: Union[str, bytes]) -> Future[bool]:
return self.__pool.submit(self.__logic.SendMessage, toID, message)

def HaveMessage(self) -> bool:
return self.__logic.HaveMessage()

def GetMessage(self) -> Tuple[int, Union[str, bytes]]:
return self.__logic.GetMessage()

def GetFrameCount(self) -> int:
return self.__logic.GetFrameCount()

def Wait(self) -> bool:
if self.__logic.GetCounter() == -1:
return False
else:
return self.__logic.WaitThread()

def EndAllAction(self) -> Future[bool]:
return self.__pool.submit(self.__logic.EndAllAction)

def GetBullets(self) -> List[THUAI7.Bullet]:
return self.__logic.GetBullets()

def GetShips(self) -> List[THUAI7.Ship]:
return self.__logic.GetShips()

def GetEnemyShips(self) -> List[THUAI7.Ship]:
return self.__logic.GetEnemyShips()

def GetFullMap(self) -> List[List[THUAI7.PlaceType]]:
return self.__logic.GetFullMap()

def GetPlaceType(self, cellX: int, cellY: int) -> THUAI7.PlaceType:
return self.__logic.GetPlaceType(cellX, cellY)

def GetConstructionHp(self, cellX: int, cellY: int) -> int:
return self.__logic.GetConstructionHp(cellX, cellY)

def GetWormHp(self, cellX: int, cellY: int) -> int:
return self.__logic.GetWormHp(cellX, cellY)

def GetResouceState(self, cellX: int, cellY: int) -> int:
return self.__logic.GetResouceState(cellX, cellY)

def GetHomeHp(self) -> int:
return self.__logic.GetHomeHp()

def GetGameInfo(self) -> THUAI7.GameInfo:
return self.__logic.GetGameInfo()

def GetPlayerGUIDs(self) -> List[int]:
return self.__logic.GetPlayerGUIDs()

def GetSelfInfo(self) -> THUAI7.Home:
return cast(THUAI7.Home, self.__logic.GetSelfInfo())

def GetScore(self) -> int:
return self.__logic.GetScore()

def GetMoney(self) -> int:
return self.__logic.GetMoney()

def InstallModule(self, ID: int, type: THUAI7.ModuleType) -> Future[bool]:
return self.__pool.submit(self.__logic.InstallModule, ID, type)

def Recycle(self, ID: int) -> Future[bool]:
return self.__pool.submit(self.__logic.Recycle, ID)

def BuildShip(self, shipType: THUAI7.ShipType, cellX: int, cellY: int) -> Future[bool]:
return self.__pool.submit(self.__logic.BuildShip, shipType, cellX, cellY)

def Print(self, string: str) -> None:
pass

def PrintTeam(self) -> None:
pass

def PrintSelfInfo(self) -> None:
pass
Loading

0 comments on commit 496e5bb

Please sign in to comment.