Быстрый API не может получить текст для механизма ставок в покереPython

Программы на Python
Anonymous
Быстрый API не может получить текст для механизма ставок в покере

Сообщение Anonymous »


I'm trying to collect bets from players in order

from fastapi import FastAPI, WebSocket app = FastAPI() from typing import List from HomeGame import Table, Player from fastapi import FastAPI, WebSocket, WebSocketDisconnect from fastapi.responses import HTMLResponse from fastapi.middleware.cors import CORSMiddleware from datetime import datetime import json # Dictionary to store connected WebSocket clients app.add_middleware( CORSMiddleware, allow_origins=["*"], # can alter with time allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) table=None connected_users = {} @app.websocket("/ws/{user_id}") async def websocket_endpoint(user_id: str, websocket: WebSocket): await websocket.accept() global table #add players #await websocket.send_text('enter number of players') #numplayers=await websocket.receive_text() #await websocket.send_text('enter your buy-in amount') #buyamount=await websocket.receive_text() #print(buyamount) #user_id=Player(user_id,buyamount,websocket) #connected_users[user_id] = websocket #print(connected_users) #initialize game once button is pressed #print(len(connected_users)) #if len(connected_users) == int(numplayers): # global table # await websocket.send_text('blind amounts seperated by comma') # blinds=await websocket.receive_text() # blinds=blinds.split(',') # table = Table(blinds[0],blinds[1]) # for x in connected_users: # table.addplayer(x) # print(table.list) #TEST fixing table round setup # Initialize player and add to connected users player = Player(user_id, 20, websocket) connected_users[user_id] = player # Check if enough players have connected to start the game numplayers = 3 # Example: Change this according to your game requirements if len(connected_users) == numplayers: # Set up the table blinds = [0.1, 0.2] # Example: Adjust according to your game rules table = Table(blinds[0], blinds[1]) # Add players to the table for player in connected_users.values(): table.addplayer(player) # Test the table setup print(table.order) table.pickdealer() for player in table.order: print(player.websocket) print(table.order) # Test player actions (place bets) for player in table.order: await player.placebettest(1) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)

As you can see the app initializes an instance of the Player class and stores the websocket for use in the betting function. I am playing around with the the function placebettest and I am unable to receive a bet. The weird thing is sometimes it works and sometimes it doesnt.

Here is the Player class:

class Player(): def __init__(self,name,buy,websocket): self.name=name self.balance=float(buy) self.hand=[] self.currentbet=0 self.handscore=0 self.websocket=websocket def __repr__(self): return (f'player {self.name}') async def placebet(self, current_price, valid=True): '''place a bet''' websocket = self.websocket message = f'{self.name}, price is {current_price}, place your bet (0 for check, -1 for fold): ' if not valid: message = f'{self.name}, price is {current_price}, Invalid Bet Size, what is your new bet (0 for check, -1 for fold): ' while True: try: await websocket.send_text(message) bet = float(await self.websocket.receive_text()) print(bet) if bet

Источник: https://stackoverflow.com/questions/781 ... -mechanism

Вернуться в «Python»