Repositories | Polyback | Log in to see private repositories
4,003 bytes | parent directory
<?php
namespace pizzaboxer\ProjectPolygon;
// Client versions a place can target: the polygon-engine era modes, a year plus a letter
// for the part of the year (E = early, M = mid, L = late).
class ClientVersions
{
const ALL = ["2010M", "2011M", "2012L", "2014M", "2016M"];
const DEFAULT = "2014M";
// Accept an era ("2012L") or a legacy bare year ("2012") and return the era, or null.
static function Normalize($value)
{
$value = strtoupper(trim((string) $value));
if (in_array($value, self::ALL, true)) return $value;
foreach (self::ALL as $era) if (substr($era, 0, 4) === $value) return $era;
return null;
}
// A place's versions: assets.Version is its main one (what Studio opens it in), and
// assets.Versions says what it can be played on: NULL = only the main one, "All" = every
// version there is (later ones too), else a comma-separated list that includes the main one.
// The versions a place can be played on, oldest first (never empty).
static function Supported($place)
{
$main = self::Normalize($place->Version) ?? self::DEFAULT;
$versions = (string) ($place->Versions ?? "");
if ($versions === "All") return self::ALL;
$listed = explode(",", $versions);
return array_values(array_filter(self::ALL, fn($v) => $v === $main || in_array($v, $listed, true)));
}
// "2014M", "2014M, 2016M" or "All"
static function Label($place)
{
return ($place->Versions ?? null) === "All" ? "All" : implode(", ", self::Supported($place));
}
// The assets columns for a choice ("All", or a list of versions), keeping the main version
// if it's still among them: ["Version" => ..., "Versions" => ...], or null if it's invalid.
static function Columns($choice, $main = null)
{
$main = self::Normalize($main);
if ($choice === "All") return ["Version" => $main ?? self::DEFAULT, "Versions" => "All"];
if (!is_array($choice) || !$choice) return null;
foreach ($choice as $v) if (!in_array($v, self::ALL, true)) return null;
$chosen = array_values(array_intersect(self::ALL, $choice));
return [
"Version" => in_array($main, $chosen, true) ? $main : $chosen[0],
"Versions" => count($chosen) > 1 ? implode(",", $chosen) : null,
];
}
// The columns after the main version changes (Studio publishes in the version the file is
// in): a place for one version moves to it, one for several also gets it.
static function ColumnsForMain($place, $main)
{
if (empty($place->Versions)) return self::Columns([$main], $main);
if ($place->Versions === "All") return self::Columns("All", $main);
return self::Columns(array_merge(self::Supported($place), [$main]), $main);
}
// The choice made in the place forms (see FormFields)
static function FormChoice($post)
{
return ($post["AllVersions"] ?? "") === "on" ? "All" : ($post["Versions"] ?? null);
}
// Checkboxes for the place forms; $place is null for a new place
static function FormFields($place = null)
{
$all = $place && ($place->Versions ?? null) === "All";
$supported = $place ? self::Supported($place) : [self::DEFAULT];
$html = '<label>Versions:</label><div class="place-versions">';
foreach (self::ALL as $v)
{
$html .= '<div class="form-check form-check-inline"><input type="checkbox" class="form-check-input" name="Versions[]" value="'.$v.'" id="version_'.$v.'"'
.(in_array($v, $supported, true) ? ' checked="checked"' : '').($all ? ' disabled="disabled"' : '').'>'
.'<label class="form-check-label" for="version_'.$v.'">'.$v.'</label></div>';
}
return $html.'<div class="form-check mt-1"><input type="checkbox" class="form-check-input" name="AllVersions" id="version_all"'.($all ? ' checked="checked"' : '')
.' onchange="$(\'.place-versions input[name="Versions[]"]\').prop(\'disabled\', this.checked)">'
.'<label class="form-check-label" for="version_all">All</label></div></div>'
.'<small class="text-muted">With more than one, players choose a version when they click Play. Each version gets its own servers.</small>';
}
}