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
async def perform_move(self, data):
if await move_handler(
b, message = await move_handler(
data["px"],
data["py"],
data["x"],
data["y"],
self.room_name,
self.scope["player_in_room"],
):
await self.send_board()
if self.scope["opponent_channel"] and self.scope["opponent_online"]:
await self.channel_layer.send(
self.scope["opponent_channel"],
{
"type": "move",
"x": data["x"],
"y": data["y"],
"px": data["px"],
"py": data["py"],
},
)
if b:
await self.update_board(
data["px"],
data["py"],
data["x"],
data["y"],
)
return True
return False
if self.scope["opponent_channel"] and self.scope["opponent_online"]:
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
def load_board(self):
@ -379,6 +386,10 @@ class RoomConsumer(BaseConsumer):
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):
# sends board to client
await self.send_message(
@ -431,6 +442,12 @@ class RoomConsumer(BaseConsumer):
self.scope["opponent_online"] = status
async def move(self, event):
await self.update_board(
event["px"],
event["py"],
event["x"],
event["y"],
)
await self.send(
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)
if hero.hero.type == "KING":
if hero.player.first:
print(colored("", 'green', attrs=['bold']), end="")
print(colored("", "green", attrs=["bold"]), end="")
else:
print(colored("", 'red', attrs=['bold']), end="")
print(colored("", "red", attrs=["bold"]), end="")
elif hero.hero.type == "WIZARD":
if hero.player.first:
print(colored("", 'green', attrs=['bold']), end="")
print(colored("", "green", attrs=["bold"]), end="")
else:
print(colored("", 'red', attrs=['bold']), end="")
print(colored("", "red", attrs=["bold"]), end="")
elif hero.hero.type == "ARCHER":
if hero.player.first:
print(colored("", 'green', attrs=['bold']), end="")
print(colored("", "green", attrs=["bold"]), end="")
else:
print(colored("", 'red', attrs=['bold']), end="")
print(colored("", "red", attrs=["bold"]), end="")
else:
if hero.player.first:
print(colored("", 'green', attrs=['bold']), end="")
print(colored("", "green", attrs=["bold"]), end="")
else:
print(colored("", 'red', attrs=['bold']), end="")
print(colored("", "red", attrs=["bold"]), end="")
except HeroInGame.DoesNotExist:
print("*", end="")
print()
@ -88,23 +88,30 @@ def _print_board(room: Room):
@sync_to_async
def move_handler(
prev_x: int, prev_y: int, x: int, y: int, room_slug: str, player: PlayerInRoom
):
) -> (bool, str):
room = Room.objects.get(slug=room_slug)
_print_board(room) # TODO: Remove in production
try:
hero = HeroInGame.objects.get(x=prev_x, y=prev_y, room=room, player=player)
except HeroInGame.DoesNotExist:
return False
return False, "There is no hero"
if x == prev_x and y == prev_y:
return False
return False, "Trying to move on same spot"
h_t = hero.hero.type
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
if h := HeroInGame.objects.filter(x=x, y=y, room=room, player=player):
if h.first().player != player:
# TODO: rpg encounter logic
return False, "Not implemented"
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