Skip to main content

List Group Tournaments

The list group tournaments method retrieves all available group tournaments for a specific game. Group tournaments allow multiple players to compete as teams.

Overview

The getGroupTournaments method:

  • Returns all group tournaments configured for a specific game
  • Provides tournament metadata including team requirements
  • Returns an array of GroupTournamentMeta objects
  • Handles empty results gracefully

Method Signature

public function getGroupTournaments(string $gameID): array

Parameters

Required Parameters

  • gameID (String) - The Game ID for which to retrieve group tournaments
    • Must be a valid Moitribe Game ID
    • Cannot be empty

Return Value

Returns an array of GroupTournamentMeta objects:

  • Empty array - No group tournaments found or game doesn't exist
  • Array with objects - Successfully retrieved group tournaments

Each GroupTournamentMeta object contains:

  • Tournament ID
  • Tournament name
  • Team size requirements
  • Start/end dates
  • Entry requirements
  • Prize information

Basic Usage

Get All Group Tournaments for a Game

use Veniso\Moitribe\Sdk\modules\classes\MoitribeApi;

$moitribe = new MoitribeApi([
'gameid' => 'your-game-id',
'channelid' => 'your-channel-id',
'playerid' => 'player-123'
]);

try {
$groupTournaments = $moitribe->groupTournRequestsHandler->getGroupTournaments('your-game-id');

echo "Found " . count($groupTournaments) . " group tournaments:\n";

foreach ($groupTournaments as $tournament) {
echo "- " . $tournament->getName() . " (ID: " . $tournament->getId() . ")\n";
echo " Team Size: " . $tournament->getMinTeamSize() . " - " . $tournament->getMaxTeamSize() . "\n";
echo " Status: " . $tournament->getStatus() . "\n";
echo " Start: " . $tournament->getStartDate() . "\n";
echo " End: " . $tournament->getEndDate() . "\n";

if ($tournament->getEntryFee()) {
echo " Entry Fee: " . $tournament->getEntryFee() . "\n";
}

if ($tournament->getPrizePool()) {
echo " Prize Pool: " . $tournament->getPrizePool() . "\n";
}

echo "---\n";
}

} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}

Filter Group Tournaments by Status

function getGroupTournamentsByStatus($gameId, $status = null) {
global $moitribe;

try {
$allGroupTournaments = $moitribe->groupTournRequestsHandler->getGroupTournaments($gameId);

if ($status === null) {
return $allGroupTournaments;
}

$filteredTournaments = array_filter($allGroupTournaments, function($tournament) use ($status) {
return $tournament->getStatus() === $status;
});

return array_values($filteredTournaments);

} catch (Exception $e) {
error_log("Error getting group tournaments: " . $e->getMessage());
return [];
}
}

// Usage examples
$activeTournaments = getGroupTournamentsByStatus('your-game-id', 'active');
$upcomingTournaments = getGroupTournamentsByStatus('your-game-id', 'upcoming');
$completedTournaments = getGroupTournamentsByStatus('your-game-id', 'completed');

echo "Active group tournaments: " . count($activeTournaments) . "\n";
echo "Upcoming group tournaments: " . count($upcomingTournaments) . "\n";
echo "Completed group tournaments: " . count($completedTournaments) . "\n";

Integration Examples

Group Tournament Selection Interface

function displayGroupTournamentSelection($gameId) {
global $moitribe;

try {
$groupTournaments = $moitribe->groupTournRequestsHandler->getGroupTournaments($gameId);

if (empty($groupTournaments)) {
echo "<p>No group tournaments available for this game.</p>";
return;
}

// Group tournaments by status
$groupedTournaments = [];
foreach ($groupTournaments as $tournament) {
$status = $tournament->getStatus();
if (!isset($groupedTournaments[$status])) {
$groupedTournaments[$status] = [];
}
$groupedTournaments[$status][] = $tournament;
}

echo "<div class='group-tournament-selection'>";
echo "<h2>Choose a Group Tournament</h2>";

foreach ($groupedTournaments as $status => $tournaments) {
$statusLabel = ucfirst($status);
$statusClass = $status === 'active' ? 'active' : 'inactive';

echo "<div class='tournament-group $statusClass'>";
echo "<h3>$statusLabel Group Tournaments</h3>";

foreach ($tournaments as $tournament) {
$teamSize = $tournament->getMinTeamSize() . '-' . $tournament->getMaxTeamSize();
$entryFee = $tournament->getEntryFee() ?: 'Free';
$prizePool = $tournament->getPrizePool() ?: 'N/A';

echo "<div class='tournament-item' data-id='{$tournament->getId()}'>";
echo "<h4>" . htmlspecialchars($tournament->getName()) . "</h4>";
echo "<div class='tournament-details'>";
echo "<span class='team-size'>Team: $teamSize</span>";
echo "<span class='entry-fee'>Entry: $entryFee</span>";
echo "<span class='prize-pool'>Prizes: $prizePool</span>";
echo "<span class='dates'>" . date('M j', strtotime($tournament->getStartDate())) . " - " . date('M j', strtotime($tournament->getEndDate())) . "</span>";
echo "</div>";

if ($status === 'active') {
echo "<button class='join-btn' onclick='joinGroupTournament(\"{$tournament->getId()}\")'>Join Tournament</button>";
} else {
echo "<button class='join-btn disabled'>" . getJoinButtonText($status) . "</button>";
}

echo "</div>";
}

echo "</div>";
}

echo "</div>";

} catch (Exception $e) {
echo "<div class='error'>Failed to load group tournaments. Please try again later.</div>";
error_log("Group tournament selection error: " . $e->getMessage());
}
}

function getJoinButtonText($status) {
switch ($status) {
case 'active':
return 'Join Tournament';
case 'upcoming':
return 'Starting Soon';
case 'completed':
return 'Tournament Ended';
case 'full':
return 'Tournament Full';
default:
return 'Not Available';
}
}

// Display on your page
displayGroupTournamentSelection('your-game-id');

Group Tournament Management System

class GroupTournamentManager {
private $moitribe;
private $gameId;
private $cache = [];
private $cacheExpiry = 300; // 5 minutes

public function __construct($moitribe, $gameId) {
$this->moitribe = $moitribe;
$this->gameId = $gameId;
}

public function getAvailableTournaments($useCache = true) {
$cacheKey = "group_tournaments_{$this->gameId}";

// Check cache first
if ($useCache && isset($this->cache[$cacheKey])) {
$cached = $this->cache[$cacheKey];
if (time() - $cached['timestamp'] < $this->cacheExpiry) {
return $cached['data'];
}
}

try {
$tournaments = $this->moitribe->groupTournRequestsHandler->getGroupTournaments($this->gameId);

// Cache the result
$this->cache[$cacheKey] = [
'data' => $tournaments,
'timestamp' => time()
];

return $tournaments;

} catch (Exception $e) {
error_log("Error getting group tournaments: " . $e->getMessage());
return [];
}
}

public function getTournamentsByTeamSize($minSize, $maxSize = null) {
$tournaments = $this->getAvailableTournaments();

$filteredTournaments = array_filter($tournaments, function($tournament) use ($minSize, $maxSize) {
$tournamentMinSize = $tournament->getMinTeamSize();
$tournamentMaxSize = $tournament->getMaxTeamSize();

$sizeMatch = $tournamentMinSize >= $minSize;

if ($maxSize !== null) {
$sizeMatch = $sizeMatch && $tournamentMaxSize <= $maxSize;
}

return $sizeMatch;
});

return array_values($filteredTournaments);
}

public function getTournamentsByEntryFee($isFree = null) {
$tournaments = $this->getAvailableTournaments();

$filteredTournaments = array_filter($tournaments, function($tournament) use ($isFree) {
$entryFee = $tournament->getEntryFee();

if ($isFree === null) {
return true; // Include all if not specified
}

return $isFree ? ($entryFee === null || $entryFee == 0) : ($entryFee > 0);
});

return array_values($filteredTournaments);
}

public function getTournamentById($tournamentId) {
$tournaments = $this->getAvailableTournaments();

foreach ($tournaments as $tournament) {
if ($tournament->getId() === $tournamentId) {
return $tournament;
}
}

return null;
}

public function refreshCache() {
$cacheKey = "group_tournaments_{$this->gameId}";
unset($this->cache[$cacheKey]);

return $this->getAvailableTournaments(false);
}
}

// Usage
$tournamentManager = new GroupTournamentManager($moitribe, 'your-game-id');

// Get all tournaments (with caching)
$allTournaments = $tournamentManager->getAvailableTournaments();
echo "Total group tournaments: " . count($allTournaments) . "\n";

// Get tournaments for specific team size
$teamTournaments = $tournamentManager->getTournamentsByTeamSize(2, 4);
echo "2-4 player tournaments: " . count($teamTournaments) . "\n";

// Get free tournaments
$freeTournaments = $tournamentManager->getTournamentsByEntryFee(true);
echo "Free tournaments: " . count($freeTournaments) . "\n";

// Get specific tournament
$tournament = $tournamentManager->getTournamentById('tournament-456');
if ($tournament) {
echo "Found: " . $tournament->getName() . "\n";
} else {
echo "Tournament not found\n";
}

Web API Endpoint for Group Tournaments

function getGroupTournamentsAPI() {
global $moitribe;

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
return;
}

$gameId = $_GET['game_id'] ?? 'your-game-id';
$status = $_GET['status'] ?? null;
$teamSize = $_GET['team_size'] ?? null;
$isFree = $_GET['is_free'] ?? null;

try {
if (empty($gameId)) {
throw new InvalidArgumentException('Game ID is required');
}

$tournaments = $moitribe->groupTournRequestsHandler->getGroupTournaments($gameId);

// Apply filters
if ($status) {
$tournaments = array_filter($tournaments, function($tournament) use ($status) {
return $tournament->getStatus() === $status;
});
}

if ($teamSize !== null) {
$tournaments = array_filter($tournaments, function($tournament) use ($teamSize) {
$tournamentMinSize = $tournament->getMinTeamSize();
$tournamentMaxSize = $tournament->getMaxTeamSize();

return $teamSize >= $tournamentMinSize &&
($teamSize <= $tournamentMaxSize || $tournamentMaxSize === null);
});
}

if ($isFree !== null) {
$tournaments = array_filter($tournaments, function($tournament) use ($isFree) {
$entryFee = $tournament->getEntryFee();
return $isFree ? ($entryFee === null || $entryFee == 0) : ($entryFee > 0);
});
}

// Format response
$formattedTournaments = array_map(function($tournament) {
return [
'id' => $tournament->getId(),
'name' => $tournament->getName(),
'status' => $tournament->getStatus(),
'min_team_size' => $tournament->getMinTeamSize(),
'max_team_size' => $tournament->getMaxTeamSize(),
'entry_fee' => $tournament->getEntryFee(),
'prize_pool' => $tournament->getPrizePool(),
'start_date' => $tournament->getStartDate(),
'end_date' => $tournament->getEndDate(),
'is_free' => ($tournament->getEntryFee() === null || $tournament->getEntryFee() == 0)
];
}, $tournaments);

http_response_code(200);
echo json_encode([
'success' => true,
'data' => $formattedTournaments,
'count' => count($formattedTournaments)
]);

} catch (InvalidArgumentException $e) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Server error occurred']);
error_log("Group tournaments API error: " . $e->getMessage());
}
}

// Route handler
if (strpos($_SERVER['REQUEST_URI'], '/api/group-tournaments') !== false) {
getGroupTournamentsAPI();
}

Error Handling

Common Error Scenarios

  • Invalid game ID - Game doesn't exist
  • Access denied - No permission to view group tournaments
  • Network issues - API communication problems

Error Handling Example

function safeGetGroupTournaments($gameId) {
global $moitribe;

try {
if (empty($gameId)) {
throw new InvalidArgumentException("Game ID cannot be empty");
}

$tournaments = $moitribe->groupTournRequestsHandler->getGroupTournaments($gameId);

return [
'success' => true,
'data' => $tournaments,
'count' => count($tournaments)
];

} catch (InvalidArgumentException $e) {
return [
'success' => false,
'error' => 'Invalid input: ' . $e->getMessage()
];
} catch (Exception $e) {
error_log("Group tournaments API error: " . $e->getMessage());
return [
'success' => false,
'error' => 'Failed to retrieve group tournaments'
];
}
}

Best Practices

  1. Implement Caching

    • Group tournament lists don't change frequently
    • Cache for 5-15 minutes to improve performance
    • Invalidate cache when tournaments are updated
  2. Handle Empty Results

    • Always check if returned array is empty
    • Provide user-friendly messages when no tournaments are available
  3. Provide Filtering Options

    • Filter by status (active, upcoming, completed)
    • Filter by team size requirements
    • Filter by entry fee (free vs paid)
  4. Display Team Requirements

    • Show minimum and maximum team sizes
    • Display entry fee information clearly
    • Include prize pool details when available