-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
44 additions
and
22 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters