Skip to content

Commit

Permalink
Added initpy and get/post
Browse files Browse the repository at this point in the history
  • Loading branch information
JJMN22 committed Mar 24, 2024
1 parent 61e2c3e commit 2c886f3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
Empty file added __init__.py
Empty file.
58 changes: 40 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
from fastapi import FastAPI, WebSocket
from starlette.websockets import WebSocketDisconnect
# from game import Game
from contextlib import asynccontextmanager
from fastapi import FastAPI
from pydantic import BaseModel
from game.board import Board
from game.game import Game
from typing import List, Dict, Tuple


game = Game(1)
app = FastAPI()

# Define a Pydantic model for the item
class SBoard(BaseModel):
board: List[List[Tuple[int, str, int]]] # 2D list of (owner, type, troops)

class SMoves(BaseModel):
moves: Tuple[int, int, int, int]

@app.get("/")
async def root():
return {"message": "Hello, world!"}
async def read_root():
return {"Hello": "World"}

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
except WebSocketDisconnect:
print("Client disconnected")

# game = Game(10, 10, 2)
def serialize_board(board):
b = board.board
M, N = len(b), len(b[0])
ret = []
for r in range(M):
next_row = []
for c in range(N):
tile = b[r][c]
owner = tile.owner or -1
type = tile.type
troops = tile.troops or 0
next_row.append((owner, type, troops))
ret.append(next_row)
return ret

# @app.post("/execute_move/", response_model=Move, status_code=201)
# def execute_move(move: Move):
# global game

@app.get("/board")
async def get_boardstate():
serialized_board = serialize_board(game.board)
return SBoard(board=serialized_board)

@app.post("/move")
async def update_item(moves: SMoves):
# Make the move

print(moves)
Empty file added game/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion game/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class Tile:
def __init__(self, owner, type):
def __init__(self, owner: int, type):
self.owner = owner
self.type = type
self.troops = 0
Expand Down
6 changes: 3 additions & 3 deletions game/game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time, random
from player import Player
from board import Board
from .player import Player
from .board import Board


class Game:
Expand Down Expand Up @@ -50,5 +50,5 @@ def update(self):


if __name__ == "__main__":
game = Game()
game = Game(1)
game.start()

0 comments on commit 2c886f3

Please sign in to comment.