mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-23 15:10:40 +00:00
## Description: Implements 2v2 ranked matchmaking end-to-end against the matchmaking API's 2v2 queues (API PR #419): core team pinning, the server's second checkin loop and game creation, and the client UI. ## Core — deterministic team pinning The matcher's assignment specifies exactly who plays with whom (`teams: [[a,d],[b,c]]`), but team assignment previously only did clan/friend balancing and could scramble the ELO-balanced split. - `PlayerSchema`/`PlayerInfo` gain an optional **`teamIndex`** — a server-stamped index into the game's team list, part of `GameStartInfo` so it's identical on every client (same category as `clanTag`/`friends`, which already feed deterministic team assignment). - `assignTeams` honors pins **unconditionally** — before clan/friend grouping and past `maxTeamSize` (the matcher's balancing is authoritative) — and seeds the counts that balancing of any unpinned players sees. Pinned players still participate in the friend graph, so an unpinned friend is pulled toward a pinned player's team. - publicIds never enter core: the game server resolves publicId → teamIndex per client at game start. ## Server - **One checkin long-poll per mode.** Both loops send `mode` explicitly (the API deployed ahead of the client, so no omit-for-back-compat needed). - **`get2v2Config()`**: Team mode, `playerTeams: 2`, `maxPlayers: 4`, always-compact map, donations enabled (matching public team games), `rankedType: "2v2"` (the API's 2v2 ingestion has shipped; `RankedType` gains `TwoVTwo`). - **The assignment payload is now used** (it was previously discarded): `players` → `allowedPublicIds` so only the matched accounts can take the slots (also hardens 1v1), and `teams` → `teamIndex` stamps at game start. A malformed assignment logs a warning and falls back to creating the game without pins rather than stranding matched players. - The 3-clients-per-IP cap on public games applies to matchmade games too (an allowlist doesn't stop one person multi-tabbing multiple accounts). It is now skipped in dev, where local testing (multi-tab, the 4-player e2e) is inherently same-IP — matching the existing dev/prod gating of Turnstile and the duplicate-account kick. ## Client - Ranked modal's 2v2 card is enabled; it passes the mode through `open-matchmaking` (dispatchers without a detail — homepage button, requeue URL — still mean 1v1). - Matchmaking modal joins with `&mode=1v1`/`&mode=2v2`, shows a 2v2 title (`matchmaking_modal.title_2v2` in en.json), and shows the real 2v2 ELO from the new `leaderboard.twoVtwo` field in `/users/@me` (the ranked modal's 2v2 card does too). - WinModal shows requeue for any ranked game and carries the mode back into the right queue (`/?requeue=2v2`). - 2v2 ranked stats surface in the player stats tree (labeled via `player_stats_tree.ranked_2v2`). ## Harnesses (`tests/matchmaking/`) - Contained: the fake server captures the `mode` query param; asserts each queue sends its mode explicitly. **10/10.** - E2E: `MM_MODE=2v2` runs four real browser players through the real local worker's 2v2 queue and rides the flow into the started game. Asserts same gameId for all four, the 2v2 config, allowlist admission, and a **deterministic 2 vs 2 in-game split read from each client's GameView** (the software-WebGL gate is spoofed in test pages only). **8/8.** 1v1 e2e still **6/6.** ## Verification - `npm test`: 2,053 tests pass, including 7 new (6 `assignTeams` pinning unit tests + a full-game pinned-split test through `setup()`). - `npx tsc --noEmit`, ESLint clean. - Live e2e against a local `wrangler dev` API worker: 1v1 (6/6) and 2v2 (8/8) as above. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory (UI changes — the ranked modal's 1v1/2v2 cards — were verified with before/after screenshots in the live app during development.) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
329 lines
9.4 KiB
TypeScript
329 lines
9.4 KiB
TypeScript
import { html, LitElement, TemplateResult } from "lit";
|
|
import { customElement, state } from "lit/decorators.js";
|
|
import {
|
|
getGamesPlayed,
|
|
isInIframe,
|
|
translateText,
|
|
TUTORIAL_VIDEO_URL,
|
|
} from "../../../client/Utils";
|
|
import { EventBus } from "../../../core/EventBus";
|
|
import { RankedType } from "../../../core/game/Game";
|
|
import { GameUpdateType } from "../../../core/game/GameUpdates";
|
|
import { getUserMe } from "../../Api";
|
|
import "../../components/CosmeticButton";
|
|
import { Controller } from "../../Controller";
|
|
import {
|
|
fetchCosmetics,
|
|
purchaseCosmetic,
|
|
resolveCosmetics,
|
|
} from "../../Cosmetics";
|
|
import { crazyGamesSDK } from "../../CrazyGamesSDK";
|
|
import { Platform } from "../../Platform";
|
|
import { SendWinnerEvent } from "../../Transport";
|
|
import { GameView } from "../../view";
|
|
|
|
@customElement("win-modal")
|
|
export class WinModal extends LitElement implements Controller {
|
|
public game: GameView;
|
|
public eventBus: EventBus;
|
|
|
|
private hasShownDeathModal = false;
|
|
|
|
@state()
|
|
isVisible = false;
|
|
|
|
@state()
|
|
showButtons = false;
|
|
|
|
@state()
|
|
private isWin = false;
|
|
|
|
@state()
|
|
private isRankedGame = false;
|
|
|
|
@state()
|
|
private patternContent: TemplateResult | null = null;
|
|
|
|
private _title: string;
|
|
|
|
private rand = Math.random();
|
|
|
|
// Override to prevent shadow DOM creation
|
|
createRenderRoot() {
|
|
return this;
|
|
}
|
|
|
|
constructor() {
|
|
super();
|
|
}
|
|
|
|
render() {
|
|
return html`
|
|
<div
|
|
class="${this.isVisible
|
|
? "fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 bg-gray-800/70 p-6 shrink-0 rounded-lg z-[10010] shadow-2xl backdrop-blur-xs text-white w-87.5 max-w-[90%] md:w-175"
|
|
: "hidden"}"
|
|
>
|
|
<h2 class="m-0 mb-4 text-[26px] text-center text-white">
|
|
${this._title || ""}
|
|
</h2>
|
|
${this.innerHtml()}
|
|
<div
|
|
class="${this.showButtons
|
|
? "flex justify-between gap-2.5"
|
|
: "hidden"}"
|
|
>
|
|
<o-button
|
|
variant="primary"
|
|
width="block"
|
|
class="flex-1"
|
|
translationKey="win_modal.exit"
|
|
@click=${this._handleExit}
|
|
></o-button>
|
|
${this.isRankedGame
|
|
? html`
|
|
<o-button
|
|
variant="primary"
|
|
width="block"
|
|
class="flex-1"
|
|
translationKey="win_modal.requeue"
|
|
@click=${this._handleRequeue}
|
|
></o-button>
|
|
`
|
|
: null}
|
|
<o-button
|
|
variant="primary"
|
|
width="block"
|
|
class="flex-1"
|
|
.title=${this.game?.myPlayer()?.isAlive()
|
|
? translateText("win_modal.keep")
|
|
: translateText("win_modal.spectate")}
|
|
@click=${this.hide}
|
|
></o-button>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
innerHtml() {
|
|
if (isInIframe()) {
|
|
return this.steamWishlist();
|
|
}
|
|
|
|
if (!this.isWin && getGamesPlayed() < 3) {
|
|
return this.renderYoutubeTutorial();
|
|
}
|
|
if (this.rand < 0.25) {
|
|
return this.steamWishlist();
|
|
} else if (this.rand < 0.5) {
|
|
return this.discordDisplay();
|
|
} else {
|
|
return this.renderPatternButton();
|
|
}
|
|
}
|
|
|
|
renderYoutubeTutorial() {
|
|
return html`
|
|
<div class="text-center mb-6 bg-black/30 p-2.5 rounded-sm">
|
|
<h3 class="text-xl font-semibold text-white mb-3">
|
|
${translateText("win_modal.youtube_tutorial")}
|
|
</h3>
|
|
<!-- 56.25% = 9:16 -->
|
|
<div class="relative w-full pb-[56.25%]">
|
|
<iframe
|
|
class="absolute top-0 left-0 w-full h-full rounded-sm"
|
|
src="${this.isVisible ? TUTORIAL_VIDEO_URL : ""}"
|
|
title="YouTube video player"
|
|
frameborder="0"
|
|
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
|
allowfullscreen
|
|
></iframe>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
renderPatternButton() {
|
|
return html`
|
|
<div class="text-center mb-6 bg-black/30 p-2.5 rounded-sm">
|
|
<h3 class="text-xl font-semibold text-white mb-3">
|
|
${translateText("win_modal.support_openfront")}
|
|
</h3>
|
|
<p class="text-white mb-3">
|
|
${translateText("win_modal.territory_pattern")}
|
|
</p>
|
|
<div class="flex justify-center">${this.patternContent}</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
async loadPatternContent() {
|
|
const me = await getUserMe();
|
|
const cosmetics = await fetchCosmetics();
|
|
|
|
const purchasable = resolveCosmetics(cosmetics, me, null).filter(
|
|
(r) => r.type === "pattern" && r.relationship === "purchasable",
|
|
);
|
|
|
|
if (purchasable.length === 0) {
|
|
this.patternContent = html``;
|
|
return;
|
|
}
|
|
|
|
// Shuffle the array and take patterns based on screen size
|
|
const shuffled = [...purchasable].sort(() => Math.random() - 0.5);
|
|
const maxPatterns = Platform.isMobileWidth ? 1 : 3;
|
|
const selected = shuffled.slice(0, Math.min(maxPatterns, shuffled.length));
|
|
|
|
this.patternContent = html`
|
|
<div class="flex gap-4 flex-wrap justify-start">
|
|
${selected.map(
|
|
(r) => html`
|
|
<cosmetic-button
|
|
.resolved=${r}
|
|
.onPurchase=${purchaseCosmetic}
|
|
></cosmetic-button>
|
|
`,
|
|
)}
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
steamWishlist(): TemplateResult {
|
|
return html`<p class="m-0 mb-5 text-center bg-black/30 p-2.5 rounded-sm">
|
|
<a
|
|
href="https://store.steampowered.com/app/3560670"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
class="text-[#4a9eff] underline font-medium transition-colors duration-200 text-2xl hover:text-[#6db3ff]"
|
|
>
|
|
${translateText("win_modal.wishlist")}
|
|
</a>
|
|
</p>`;
|
|
}
|
|
|
|
discordDisplay(): TemplateResult {
|
|
return html`
|
|
<div class="text-center mb-6 bg-black/30 p-2.5 rounded-sm">
|
|
<h3 class="text-xl font-semibold text-white mb-3">
|
|
${translateText("win_modal.join_discord")}
|
|
</h3>
|
|
<p class="text-white mb-3">
|
|
${translateText("win_modal.discord_description")}
|
|
</p>
|
|
<a
|
|
href="https://discord.com/invite/openfront"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
class="inline-block px-6 py-3 bg-indigo-600 text-white rounded-sm font-semibold transition-all duration-200 hover:bg-indigo-700 hover:-translate-y-px no-underline"
|
|
>
|
|
${translateText("win_modal.join_server")}
|
|
</a>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
async show() {
|
|
crazyGamesSDK.gameplayStop();
|
|
await this.loadPatternContent();
|
|
// Check if this is a ranked game
|
|
this.isRankedGame =
|
|
this.game.config().gameConfig().rankedType !== undefined;
|
|
this.isVisible = true;
|
|
this.requestUpdate();
|
|
setTimeout(() => {
|
|
this.showButtons = true;
|
|
this.requestUpdate();
|
|
}, 3000);
|
|
}
|
|
|
|
hide() {
|
|
this.isVisible = false;
|
|
this.showButtons = false;
|
|
this.requestUpdate();
|
|
}
|
|
|
|
private _handleExit() {
|
|
this.hide();
|
|
window.location.href = "/";
|
|
}
|
|
|
|
private _handleRequeue() {
|
|
this.hide();
|
|
// Navigate to homepage and open matchmaking modal for the same mode
|
|
const requeue =
|
|
this.game.config().gameConfig().rankedType === RankedType.TwoVTwo
|
|
? "/?requeue=2v2"
|
|
: "/?requeue";
|
|
window.location.href = requeue;
|
|
}
|
|
|
|
init() {}
|
|
|
|
tick() {
|
|
const myPlayer = this.game.myPlayer();
|
|
if (
|
|
!this.hasShownDeathModal &&
|
|
myPlayer &&
|
|
!myPlayer.isAlive() &&
|
|
!this.game.inSpawnPhase() &&
|
|
myPlayer.hasSpawned()
|
|
) {
|
|
this.hasShownDeathModal = true;
|
|
this._title = translateText("win_modal.died");
|
|
this.show();
|
|
}
|
|
const updates = this.game.updatesSinceLastTick();
|
|
const winUpdates = updates !== null ? updates[GameUpdateType.Win] : [];
|
|
winUpdates.forEach((wu) => {
|
|
if (wu.winner === undefined) {
|
|
// ...
|
|
} else if (wu.winner[0] === "team") {
|
|
this.eventBus.emit(new SendWinnerEvent(wu.winner, wu.allPlayersStats));
|
|
if (wu.winner[1] === this.game.myPlayer()?.team()) {
|
|
this._title = translateText("win_modal.your_team");
|
|
this.isWin = true;
|
|
crazyGamesSDK.happytime();
|
|
} else {
|
|
this._title = translateText("win_modal.other_team", {
|
|
team: wu.winner[1],
|
|
});
|
|
this.isWin = false;
|
|
}
|
|
history.replaceState(null, "", `${window.location.pathname}?replay`);
|
|
this.show();
|
|
} else if (wu.winner[0] === "nation") {
|
|
this._title = translateText("win_modal.nation_won", {
|
|
nation: wu.winner[1],
|
|
});
|
|
this.isWin = false;
|
|
this.show();
|
|
} else {
|
|
const winner = this.game.playerByClientID(wu.winner[1]);
|
|
if (!winner?.isPlayer()) return;
|
|
const winnerClient = winner.clientID();
|
|
if (winnerClient !== null) {
|
|
this.eventBus.emit(
|
|
new SendWinnerEvent(["player", winnerClient], wu.allPlayersStats),
|
|
);
|
|
}
|
|
if (
|
|
winnerClient !== null &&
|
|
winnerClient === this.game.myPlayer()?.clientID()
|
|
) {
|
|
this._title = translateText("win_modal.you_won");
|
|
this.isWin = true;
|
|
crazyGamesSDK.happytime();
|
|
} else {
|
|
this._title = translateText("win_modal.other_won", {
|
|
player: winner.displayName(),
|
|
});
|
|
this.isWin = false;
|
|
}
|
|
history.replaceState(null, "", `${window.location.pathname}?replay`);
|
|
this.show();
|
|
}
|
|
});
|
|
}
|
|
}
|