inited fastapi

This commit is contained in:
Alexander Karpov 2022-08-04 16:09:21 +03:00
parent 151912b3bb
commit 2b165fed9f
4 changed files with 58 additions and 32 deletions

0
game_logic/main.py Normal file
View File

View File

@ -0,0 +1,2 @@
fastapi==0.79.0
uvicorn==0.18.2

View File

@ -337,28 +337,35 @@ class RoomConsumer(BaseConsumer):
return False return False
async def perform_move(self, data): async def perform_move(self, data):
if await move_handler( b, message = await move_handler(
data["px"], data["px"],
data["py"], data["py"],
data["x"], data["x"],
data["y"], data["y"],
self.room_name, self.room_name,
self.scope["player_in_room"], self.scope["player_in_room"],
): )
await self.send_board()
if self.scope["opponent_channel"] and self.scope["opponent_online"]: if b:
await self.channel_layer.send( await self.update_board(
self.scope["opponent_channel"], data["px"],
{ data["py"],
"type": "move", data["x"],
"x": data["x"], data["y"],
"y": data["y"],
"px": data["px"],
"py": data["py"],
},
) )
return True if self.scope["opponent_channel"] and self.scope["opponent_online"]:
return False await self.channel_layer.send(
self.scope["opponent_channel"],
{
"type": "move",
"x": data["x"],
"y": data["y"],
"px": data["px"],
"py": data["py"],
},
)
else:
await self.send_message("ERROR", message=message)
@sync_to_async @sync_to_async
def load_board(self): def load_board(self):
@ -379,6 +386,10 @@ class RoomConsumer(BaseConsumer):
self.scope["board"] = board self.scope["board"] = board
async def update_board(self, px, py, x, y):
self.scope["board"][x][y] = self.scope["board"][px][py]
self.scope["board"][px][py] = None
async def send_board(self): async def send_board(self):
# sends board to client # sends board to client
await self.send_message( await self.send_message(
@ -431,6 +442,12 @@ class RoomConsumer(BaseConsumer):
self.scope["opponent_online"] = status self.scope["opponent_online"] = status
async def move(self, event): async def move(self, event):
await self.update_board(
event["px"],
event["py"],
event["x"],
event["y"],
)
await self.send( await self.send(
text_data=json.dumps( text_data=json.dumps(
{ {

View File

@ -62,24 +62,24 @@ def _print_board(room: Room):
hero = HeroInGame.objects.get(x=x, y=y, room=room) hero = HeroInGame.objects.get(x=x, y=y, room=room)
if hero.hero.type == "KING": if hero.hero.type == "KING":
if hero.player.first: if hero.player.first:
print(colored("", 'green', attrs=['bold']), end="") print(colored("", "green", attrs=["bold"]), end="")
else: else:
print(colored("", 'red', attrs=['bold']), end="") print(colored("", "red", attrs=["bold"]), end="")
elif hero.hero.type == "WIZARD": elif hero.hero.type == "WIZARD":
if hero.player.first: if hero.player.first:
print(colored("", 'green', attrs=['bold']), end="") print(colored("", "green", attrs=["bold"]), end="")
else: else:
print(colored("", 'red', attrs=['bold']), end="") print(colored("", "red", attrs=["bold"]), end="")
elif hero.hero.type == "ARCHER": elif hero.hero.type == "ARCHER":
if hero.player.first: if hero.player.first:
print(colored("", 'green', attrs=['bold']), end="") print(colored("", "green", attrs=["bold"]), end="")
else: else:
print(colored("", 'red', attrs=['bold']), end="") print(colored("", "red", attrs=["bold"]), end="")
else: else:
if hero.player.first: if hero.player.first:
print(colored("", 'green', attrs=['bold']), end="") print(colored("", "green", attrs=["bold"]), end="")
else: else:
print(colored("", 'red', attrs=['bold']), end="") print(colored("", "red", attrs=["bold"]), end="")
except HeroInGame.DoesNotExist: except HeroInGame.DoesNotExist:
print("*", end="") print("*", end="")
print() print()
@ -88,23 +88,30 @@ def _print_board(room: Room):
@sync_to_async @sync_to_async
def move_handler( def move_handler(
prev_x: int, prev_y: int, x: int, y: int, room_slug: str, player: PlayerInRoom prev_x: int, prev_y: int, x: int, y: int, room_slug: str, player: PlayerInRoom
): ) -> (bool, str):
room = Room.objects.get(slug=room_slug) room = Room.objects.get(slug=room_slug)
_print_board(room) # TODO: Remove in production _print_board(room) # TODO: Remove in production
try: try:
hero = HeroInGame.objects.get(x=prev_x, y=prev_y, room=room, player=player) hero = HeroInGame.objects.get(x=prev_x, y=prev_y, room=room, player=player)
except HeroInGame.DoesNotExist: except HeroInGame.DoesNotExist:
return False return False, "There is no hero"
if x == prev_x and y == prev_y: if x == prev_x and y == prev_y:
return False return False, "Trying to move on same spot"
h_t = hero.hero.type h_t = hero.hero.type
if _validate_hero_movement(h_t, prev_x, prev_y, x, y, room, first=player.first): if h := HeroInGame.objects.filter(x=x, y=y, room=room, player=player):
hero.x = x if h.first().player != player:
hero.y = y # TODO: rpg encounter logic
hero.save(update_fields=["x", "y"]) return False, "Not implemented"
return True else:
return False, "Can't perform move on own figure"
else:
if _validate_hero_movement(h_t, prev_x, prev_y, x, y, room, first=player.first):
hero.x = x
hero.y = y
hero.save(update_fields=["x", "y"])
return True, "ok"
return False, "Such move can't be performed"
_print_board(room) # TODO: Remove in production