Skip to content

Commit 647d616

Browse files
authored
Merge pull request #197 from asdawej/patch-pyapi
fix wrong `ShipPlay` in `TeamDebugAPI` & improve log file name
2 parents 3006964 + 6d1d13b commit 647d616

File tree

4 files changed

+32
-47
lines changed

4 files changed

+32
-47
lines changed

CAPI/python/PyAPI/DebugAPI.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class ShipDebugAPI(IShipAPI, IGameTimer):
1212
def __init__(
13-
self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, playerID: int
13+
self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, playerID: int, teamID: int
1414
) -> None:
1515
self.__logic = logic
1616
self.__pool = ThreadPoolExecutor(20)
@@ -31,9 +31,7 @@ def __init__(
3131

3232
fileHandler = logging.FileHandler(
3333
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
34-
+ "/logs/api-"
35-
+ str(playerID)
36-
+ "-log.txt",
34+
+ f"/logs/api-{teamID}-{playerID}-log.txt",
3735
mode="w+",
3836
encoding="utf-8",
3937
)
@@ -302,7 +300,7 @@ def Play(self, ai: IAI) -> None:
302300

303301
class TeamDebugAPI(ITeamAPI, IGameTimer):
304302
def __init__(
305-
self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, playerID: int
303+
self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, playerID: int, teamID: int
306304
) -> None:
307305
self.__logic = logic
308306
self.__pool = ThreadPoolExecutor(20)
@@ -323,9 +321,7 @@ def __init__(
323321

324322
fileHandler = logging.FileHandler(
325323
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
326-
+ "/logs/api-"
327-
+ str(playerID)
328-
+ "-log.txt",
324+
+ f"/logs/api-{teamID}-{playerID}-log.txt",
329325
mode="w+",
330326
encoding="utf-8",
331327
)
@@ -518,4 +514,4 @@ def EndTimer(self) -> None:
518514
self.__logger.info(f"Time elapsed: {self.__GetTime()}ms")
519515

520516
def Play(self, ai: IAI) -> None:
521-
ai.ShipPlay(self)
517+
ai.TeamPlay(self)

CAPI/python/PyAPI/logic.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,7 @@ def Main(
843843

844844
fileHandler = logging.FileHandler(
845845
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
846-
+ "/logs/logic"
847-
+ str(self.__playerID)
848-
+ "-log.txt",
846+
+ f"/logs/logic-{self.__teamID}-{self.__playerID}-log.txt",
849847
"w+",
850848
encoding="utf-8",
851849
)
@@ -881,11 +879,11 @@ def Main(
881879
else:
882880
if self.__playerID == 0:
883881
self.__timer = TeamDebugAPI(
884-
self, file, screen, warnOnly, self.__playerID
882+
self, file, screen, warnOnly, self.__playerID, self.__teamID
885883
)
886884
else:
887885
self.__timer = ShipDebugAPI(
888-
self, file, screen, warnOnly, self.__playerID
886+
self, file, screen, warnOnly, self.__playerID, self.__teamID
889887
)
890888

891889
# 构建AI线程

CAPI/python_exp/PyAPI/DebugAPI.py

+21-28
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111

1212
class ShipDebugAPI(IShipAPI, IGameTimer):
13-
def __init__(self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, playerID: int) -> None:
13+
def __init__(self, logic: ILogic,
14+
file: bool, screen: bool, warnOnly: bool,
15+
playerID: int, teamID: int) -> None:
1416
self.__logic = logic
1517
self.__pool = ThreadPoolExecutor(20)
1618
self.__startPoint = datetime.datetime.now()
@@ -25,11 +27,9 @@ def __init__(self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, play
2527

2628
fileHandler = logging.FileHandler(
2729
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
28-
+ "/logs/api-"
29-
+ str(playerID)
30-
+ "-log.txt",
30+
+ f"/logs/api-{teamID}-{playerID}-log.txt",
3131
mode="w+",
32-
encoding="utf-8",
32+
encoding="utf-8"
3333
)
3434
screenHandler = logging.StreamHandler()
3535
if file:
@@ -224,17 +224,16 @@ def GetScore(self) -> int:
224224

225225
def HaveView(self, gridX: int, gridY: int) -> bool:
226226
return self.__logic.HaveView(
227-
gridX,
228-
gridY,
229-
self.GetSelfInfo().x,
230-
self.GetSelfInfo().y,
227+
gridX, gridY,
228+
self.GetSelfInfo().x, self.GetSelfInfo().y,
231229
self.GetSelfInfo().viewRange,
232230
)
233231

234232
def Print(self, cont: str) -> None:
235233
self.__logger.info(cont)
236234

237235
def PrintShip(self) -> None:
236+
self.__logger.info("START PrintShip")
238237
for ship in self.__logic.GetShips():
239238
self.__logger.info("******ship Info******")
240239
self.__logger.info(
@@ -253,6 +252,7 @@ def PrintShip(self) -> None:
253252
f"armorType:{ship.armorType} shieldType:{ship.shieldType} weaponType:{ship.weaponType}"
254253
)
255254
self.__logger.info("************************\n")
255+
self.__logger.info("END PrintShip")
256256

257257
def PrintTeam(self) -> None:
258258
pass
@@ -295,33 +295,26 @@ def Play(self, ai: IAI) -> None:
295295

296296

297297
class TeamDebugAPI(ITeamAPI, IGameTimer):
298-
def __init__(
299-
self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, playerID: int
300-
) -> None:
298+
def __init__(self, logic: ILogic,
299+
file: bool, screen: bool, warnOnly: bool,
300+
playerID: int, teamID: int) -> None:
301301
self.__logic = logic
302302
self.__pool = ThreadPoolExecutor(20)
303303
self.__startPoint = datetime.datetime.now()
304304
self.__logger = logging.getLogger("api " + str(playerID))
305305
self.__logger.setLevel(logging.DEBUG)
306306
formatter = logging.Formatter(
307-
"[%(name)s] [%(asctime)s.%(msecs)03d] [%(levelname)s] %(message)s",
308-
"%H:%M:%S",
307+
"[%(name)s] [%(asctime)s.%(msecs)03d] [%(levelname)s] %(message)s", "%H:%M:%S"
309308
)
310309
# 确保文件存在
311-
if not os.path.exists(
312-
os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/logs"
313-
):
314-
os.makedirs(
315-
os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/logs"
316-
)
310+
if not os.path.exists(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/logs"):
311+
os.makedirs(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/logs")
317312

318313
fileHandler = logging.FileHandler(
319314
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
320-
+ "/logs/api-"
321-
+ str(playerID)
322-
+ "-log.txt",
315+
+ f"/logs/api-{teamID}-{playerID}-log.txt",
323316
mode="w+",
324-
encoding="utf-8",
317+
encoding="utf-8"
325318
)
326319
screenHandler = logging.StreamHandler()
327320
if file:
@@ -469,6 +462,7 @@ def Print(self, cont: str) -> None:
469462
self.__logger.info(cont)
470463

471464
def PrintShip(self) -> None:
465+
self.__logger.info("START PrintShip")
472466
for ship in self.__logic.GetShips():
473467
self.__logger.info("******ship Info******")
474468
self.__logger.info(
@@ -487,6 +481,7 @@ def PrintShip(self) -> None:
487481
f"armorType:{ship.armorType} shieldType:{ship.shieldType} weaponType:{ship.weaponType}"
488482
)
489483
self.__logger.info("************************\n")
484+
self.__logger.info("END PrintShip")
490485

491486
def PrintTeam(self) -> None:
492487
self.PrintSelfInfo()
@@ -499,9 +494,7 @@ def PrintSelfInfo(self) -> None:
499494
self.__logger.info("************************\n")
500495

501496
def __GetTime(self) -> float:
502-
return (datetime.datetime.now() - self.__startPoint) / datetime.timedelta(
503-
milliseconds=1
504-
)
497+
return (datetime.datetime.now() - self.__startPoint) / datetime.timedelta(milliseconds=1)
505498

506499
def StartTimer(self) -> None:
507500
self.__startPoint = datetime.datetime.now()
@@ -512,4 +505,4 @@ def EndTimer(self) -> None:
512505
self.__logger.info(f"Time elapsed: {self.__GetTime()}ms")
513506

514507
def Play(self, ai: IAI) -> None:
515-
ai.ShipPlay(self)
508+
ai.TeamPlay(self)

CAPI/python_exp/PyAPI/logic.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,7 @@ def Main(self, createAI: Callable) -> None:
720720

721721
fileHandler = logging.FileHandler(
722722
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
723-
+ "/logs/logic"
724-
+ str(self.__playerID)
725-
+ "-log.txt",
723+
+ f"/logs/logic-{self.__teamID}-{self.__playerID}-log.txt",
726724
"w+",
727725
encoding="utf-8",
728726
)
@@ -762,14 +760,14 @@ def Main(self, createAI: Callable) -> None:
762760
self.__processEnv.file,
763761
self.__processEnv.screen,
764762
self.__processEnv.warnOnly,
765-
self.__playerID)
763+
self.__playerID, self.__teamID)
766764
else:
767765
self.__timer = ShipDebugAPI(
768766
self,
769767
self.__processEnv.file,
770768
self.__processEnv.screen,
771769
self.__processEnv.warnOnly,
772-
self.__playerID)
770+
self.__playerID, self.__teamID)
773771

774772
# 构建AI线程
775773
def AIThread():

0 commit comments

Comments
 (0)