Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions apps-rps/rps-game-agent/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
class RPSGameClient:
"""Client for communicating with the RPS Game Server API"""

def __init__(self, base_url: str = None):
def __init__(self, base_url: str = None, room_id: int = 1):
if base_url is None:
base_url = os.getenv("RPS_SERVER_URL", "http://localhost:5289")
self.base_url = base_url.rstrip('/')
self.room_id = room_id
self.session = requests.Session()

def register_player(self, name: str) -> Dict[str, Any]:
"""Register a new player with the server"""
url = f"{self.base_url}/api/player/register"
data = {"Name": name}
data = {"Name": name, "RoomId": self.room_id}

try:
response = self.session.post(url, json=data)
Expand All @@ -31,7 +32,7 @@ def register_player(self, name: str) -> Dict[str, Any]:

def get_player_status(self, player_id: int) -> Dict[str, Any]:
"""Get current tournament status for a player"""
url = f"{self.base_url}/api/player/{player_id}/status"
url = f"{self.base_url}/api/player/{player_id}/status?roomId={self.room_id}"
logger.info(f"GET {url}")

try:
Expand All @@ -43,7 +44,7 @@ def get_player_status(self, player_id: int) -> Dict[str, Any]:

def submit_answer(self, player_id: int, round_number: int, answer: str, move: int) -> Dict[str, Any]:
"""Submit answer and RPS move for current round"""
url = f"{self.base_url}/api/player/submit-answer"
url = f"{self.base_url}/api/player/submit-answer?roomId={self.room_id}"
data = {
"PlayerId": player_id,
"RoundNumber": round_number,
Expand All @@ -61,7 +62,7 @@ def submit_answer(self, player_id: int, round_number: int, answer: str, move: in

def get_player_results(self, player_id: int) -> Dict[str, Any]:
"""Get all results for a specific player"""
url = f"{self.base_url}/api/player/{player_id}/results"
url = f"{self.base_url}/api/player/{player_id}/results?roomId={self.room_id}"

try:
response = self.session.get(url)
Expand Down
19 changes: 7 additions & 12 deletions apps-rps/rps-game-agent/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ def index():
show_form=False,
player_name=game_agent.player_name,
player_id=game_agent.player_id,
room_id=game_agent.room_id,
tournament_status=game_agent.tournament_status,
round_status=game_agent.round_status,
current_round=game_agent.current_round,
is_running=game_agent.is_running,
status_log=filtered_log[::-1], # Show filtered messages, latest first
status_log=filtered_log[::-1],
results=game_agent.results)

@app.route('/start', methods=['POST'])
Expand All @@ -46,20 +47,17 @@ def start_game():
global game_agent

player_name = request.form.get('player_name', '').strip() + ' *'
room_id = int(request.form.get('room_id', '1'))

if not player_name:
return redirect(url_for('index'))

# Create new game agent
game_agent = GameProcessor(player_name)
game_agent = GameProcessor(player_name, room_id)

# Register player
if game_agent.register_player():
# Start autonomous play
game_agent.start_autonomous_play()
return redirect(url_for('index'))
else:
# Registration failed, reset
game_agent = None
return redirect(url_for('index'))

Expand All @@ -69,6 +67,7 @@ def reconnect_game():
global game_agent

player_id = request.form.get('player_id', '').strip()
room_id = int(request.form.get('room_id', '1'))

if not player_id:
return redirect(url_for('index'))
Expand All @@ -78,13 +77,11 @@ def reconnect_game():
except ValueError:
return redirect(url_for('index'))

# Create game agent with existing player ID
game_agent = GameProcessor(f"Player {player_id}")
game_agent = GameProcessor(f"Player {player_id}", room_id)
game_agent.player_id = player_id

# Verify the player exists by getting status
from api_client import RPSGameClient
client = RPSGameClient()
client = RPSGameClient(room_id=room_id)
status_response = client.get_player_status(player_id)

if "error" in status_response:
Expand All @@ -94,10 +91,8 @@ def reconnect_game():

game_agent.log_status(f"Successfully reconnected as Player ID: {player_id}")

# Get current results
game_agent.get_current_results()

# Start autonomous play
game_agent.start_autonomous_play()
return redirect(url_for('index'))

Expand Down
5 changes: 3 additions & 2 deletions apps-rps/rps-game-agent/game_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
class GameProcessor:
"""Autonomous game agent for RPS Tournament"""

def __init__(self, player_name: str):
def __init__(self, player_name: str, room_id: int = 1):
self.player_name = player_name
self.client = RPSGameClient()
self.room_id = room_id
self.client = RPSGameClient(room_id=room_id)
self.agent = GameAgent()
self.player_id: Optional[int] = None
self.current_round = 1
Expand Down
16 changes: 15 additions & 1 deletion apps-rps/rps-game-agent/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,13 @@ <h1>✨ RPS Game Copilot - Your Smart Assistant</h1>
<input type="text" id="player_name" name="player_name" required
placeholder="Enter your name to join the tournament" autofocus>
</div>
<div class="form-group">
<label for="room_id">Select Room:</label>
<select id="room_id" name="room_id" required>
<option value="1">Room 1</option>
<option value="2">Room 2</option>
</select>
</div>
<button type="submit">🚀 Start Playing</button>
</form>

Expand All @@ -772,6 +779,13 @@ <h1>✨ RPS Game Copilot - Your Smart Assistant</h1>
<input type="number" id="player_id" name="player_id" required
placeholder="Enter your existing player ID">
</div>
<div class="form-group">
<label for="room_id_reconnect">Select Room:</label>
<select id="room_id_reconnect" name="room_id" required>
<option value="1">Room 1</option>
<option value="2">Room 2</option>
</select>
</div>
<button type="submit">🔗 Reconnect</button>
</form>
</div>
Expand Down Expand Up @@ -810,7 +824,7 @@ <h4>Track Progress</h4>
<!-- Game Status Display -->
<div class="status-section">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2>Player: {{ player_name }} (ID: {{ player_id }})</h2>
<h2>Player: {{ player_name }} (ID: {{ player_id }}) - Room {{ room_id }}</h2>
<a href="/reset" class="reset-btn" style="text-decoration: none; color: white;">Reset Game</a>
</div>

Expand Down
29 changes: 15 additions & 14 deletions apps-rps/rps-game-server/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,27 @@ public IActionResult Login(string passcode)
return View();
}

public IActionResult Index()
public IActionResult Index(int roomId = 1)
{
if (!IsAuthenticated())
{
return RedirectToAction("Login");
}

var tournament = _tournamentService.GetTournament();
ViewBag.RoomId = roomId;
var tournament = _tournamentService.GetTournament(roomId);
return View(tournament);
}

[HttpPost]
public IActionResult UnregisterPlayer(int playerId)
public IActionResult UnregisterPlayer(int playerId, int roomId = 1)
{
if (!IsAuthenticated())
{
return RedirectToAction("Login");
}

var success = _tournamentService.UnregisterPlayer(playerId);
var success = _tournamentService.UnregisterPlayer(playerId, roomId);
if (success)
{
TempData["Success"] = "Player unregistered successfully.";
Expand All @@ -72,18 +73,18 @@ public IActionResult UnregisterPlayer(int playerId)
TempData["Error"] = "Failed to unregister player.";
}

return RedirectToAction("Index");
return RedirectToAction("Index", new { roomId });
}

[HttpPost]
public IActionResult UnregisterAllPlayers()
public IActionResult UnregisterAllPlayers(int roomId = 1)
{
if (!IsAuthenticated())
{
return RedirectToAction("Login");
}

var success = _tournamentService.UnregisterAllPlayers();
var success = _tournamentService.UnregisterAllPlayers(roomId);
if (success)
{
TempData["Success"] = "All players unregistered successfully.";
Expand All @@ -93,18 +94,18 @@ public IActionResult UnregisterAllPlayers()
TempData["Error"] = "No players to unregister or operation failed.";
}

return RedirectToAction("Index");
return RedirectToAction("Index", new { roomId });
}

[HttpPost]
public IActionResult ResetCurrentRound()
public IActionResult ResetCurrentRound(int roomId = 1)
{
if (!IsAuthenticated())
{
return RedirectToAction("Login");
}

var success = _tournamentService.ResetCurrentRound();
var success = _tournamentService.ResetCurrentRound(roomId);
if (success)
{
TempData["Success"] = "Current round reset successfully.";
Expand All @@ -114,18 +115,18 @@ public IActionResult ResetCurrentRound()
TempData["Error"] = "Failed to reset current round.";
}

return RedirectToAction("Index");
return RedirectToAction("Index", new { roomId });
}

[HttpPost]
public IActionResult ResetTournament()
public IActionResult ResetTournament(int roomId = 1)
{
if (!IsAuthenticated())
{
return RedirectToAction("Login");
}

var success = _tournamentService.ResetTournament();
var success = _tournamentService.ResetTournament(roomId);
if (success)
{
TempData["Success"] = "Tournament reset successfully. All players remain registered.";
Expand All @@ -135,7 +136,7 @@ public IActionResult ResetTournament()
TempData["Error"] = "Failed to reset tournament.";
}

return RedirectToAction("Index");
return RedirectToAction("Index", new { roomId });
}

[HttpPost]
Expand Down
Loading