Skip to content

Commit

Permalink
Update Python CAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoWQ222 committed Mar 4, 2024
2 parents 3458499 + 0b754af commit a82b990
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 122 deletions.
46 changes: 46 additions & 0 deletions CAPI/python/PyAPI/AI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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
219 changes: 217 additions & 2 deletions CAPI/python/PyAPI/API.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,220 @@
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
8 changes: 8 additions & 0 deletions CAPI/python/PyAPI/Interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def HaveMessage(self) -> bool:
def GetMessage(self) -> Tuple[int, str]:
pass

@abstractmethod
def Move(self,timeInMilliseconds:int,angle:float)->Future[bool]:
pass

# 获取游戏目前所进行的帧数
@abstractmethod
def GetFrameCount(self) -> int:
'获取游戏目前所进行的帧数'
Expand Down Expand Up @@ -244,6 +249,9 @@ def PrintTeam(self) -> None:
def PrintSelfInfo(self) -> None:
pass

@abstractmethod
def GetSelfInfo(self) -> Union[THUAI7.Ship, THUAI7.Team]:
pass

class IShipAPI(IAPI, metaclass=ABCMeta):
@abstractmethod
Expand Down
Loading

0 comments on commit a82b990

Please sign in to comment.