Skip to content

Commit dfc3b35

Browse files
committed
crazygames
1 parent e1d31ef commit dfc3b35

File tree

9 files changed

+125
-13
lines changed

9 files changed

+125
-13
lines changed

src/client/CrazyGamesSDK.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ declare global {
33
CrazyGames?: {
44
SDK: {
55
init: () => Promise<void>;
6+
user: {
7+
getUser(): Promise<{
8+
username: string;
9+
profilePictureUrl: string;
10+
} | null>;
11+
};
12+
ad: {
13+
requestAd: (
14+
adType: string,
15+
callbacks: {
16+
adStarted: () => void;
17+
adFinished: () => void;
18+
adError: (error: any) => void;
19+
},
20+
) => void;
21+
};
622
game: {
723
gameplayStart: () => Promise<void>;
824
gameplayStop: () => Promise<void>;
@@ -15,6 +31,7 @@ declare global {
1531
}) => string;
1632
hideInviteButton: () => void;
1733
getInviteParam: (paramName: string) => string | null;
34+
isInstantMultiplayer?: boolean;
1835
};
1936
};
2037
};
@@ -24,6 +41,24 @@ declare global {
2441
export class CrazyGamesSDK {
2542
private initialized = false;
2643
private isGameplayActive = false;
44+
private readyPromise: Promise<void>;
45+
private resolveReady!: () => void;
46+
47+
constructor() {
48+
this.readyPromise = new Promise((resolve) => {
49+
this.resolveReady = resolve;
50+
});
51+
}
52+
53+
async ready(): Promise<boolean> {
54+
const timeout = new Promise<boolean>((resolve) => {
55+
setTimeout(() => resolve(false), 3000);
56+
});
57+
58+
const ready = this.readyPromise.then(() => true);
59+
60+
return Promise.race([ready, timeout]);
61+
}
2762

2863
isOnCrazyGames(): boolean {
2964
try {
@@ -70,12 +105,29 @@ export class CrazyGamesSDK {
70105
try {
71106
await window.CrazyGames.SDK.init();
72107
this.initialized = true;
108+
this.resolveReady();
73109
console.log("CrazyGames SDK initialized");
74110
} catch (error) {
75111
console.error("Failed to initialize CrazyGames SDK:", error);
76112
}
77113
}
78114

115+
async getUsername(): Promise<string | null> {
116+
const isReady = await this.ready();
117+
if (!isReady) {
118+
return null;
119+
}
120+
return (await window.CrazyGames!.SDK.user.getUser())?.username ?? null;
121+
}
122+
123+
async isInstantMultiplayer(): Promise<boolean> {
124+
const isReady = await this.ready();
125+
if (!isReady) {
126+
return false;
127+
}
128+
return window.CrazyGames!.SDK.game.isInstantMultiplayer ?? false;
129+
}
130+
79131
async gameplayStart(): Promise<void> {
80132
if (!this.isReady()) {
81133
return;
@@ -200,6 +252,33 @@ export class CrazyGamesSDK {
200252
return null;
201253
}
202254
}
255+
256+
requestMidgameAd(): Promise<void> {
257+
return new Promise((resolve) => {
258+
if (!this.isReady()) {
259+
resolve();
260+
return;
261+
}
262+
263+
try {
264+
const callbacks = {
265+
adFinished: () => {
266+
console.log("End midgame ad");
267+
resolve();
268+
},
269+
adError: (error: any) => {
270+
console.log("Error midgame ad", error);
271+
resolve();
272+
},
273+
adStarted: () => console.log("Start midgame ad"),
274+
};
275+
window.CrazyGames!.SDK.ad.requestAd("midgame", callbacks);
276+
} catch (error) {
277+
console.error("Failed to request midgame ad:", error);
278+
resolve();
279+
}
280+
});
281+
}
203282
}
204283

205284
export const crazyGamesSDK = new CrazyGamesSDK();

src/client/Main.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class Client {
214214
private usernameInput: UsernameInput | null = null;
215215
private flagInput: FlagInput | null = null;
216216

217+
private hostModal: HostPrivateLobbyModal;
217218
private joinModal: JoinPrivateLobbyModal;
218219
private publicLobby: PublicLobby;
219220
private userSettings: UserSettings = new UserSettings();
@@ -522,10 +523,10 @@ class Client {
522523
}
523524
});
524525

525-
const hostModal = document.querySelector(
526+
this.hostModal = document.querySelector(
526527
"host-lobby-modal",
527528
) as HostPrivateLobbyModal;
528-
if (!hostModal || !(hostModal instanceof HostPrivateLobbyModal)) {
529+
if (!this.hostModal || !(this.hostModal instanceof HostPrivateLobbyModal)) {
529530
console.warn("Host private lobby modal element not found");
530531
}
531532
const hostLobbyButton = document.getElementById("host-lobby-button");
@@ -641,6 +642,14 @@ class Client {
641642
return;
642643
}
643644
}
645+
crazyGamesSDK.isInstantMultiplayer().then((isInstant) => {
646+
if (isInstant) {
647+
console.log(
648+
`CrazyGames: joining instant multiplayer lobby from CrazyGames`,
649+
);
650+
this.hostModal.open();
651+
}
652+
});
644653

645654
const strip = () =>
646655
history.replaceState(

src/client/SinglePlayerModal.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import "./components/FluentSlider";
2727
import "./components/Maps";
2828
import { modalHeader } from "./components/ui/ModalHeader";
2929
import { fetchCosmetics } from "./Cosmetics";
30+
import { crazyGamesSDK } from "./CrazyGamesSDK";
3031
import { FlagInput } from "./FlagInput";
3132
import { JoinLobbyEvent } from "./Main";
3233
import { UsernameInput } from "./UsernameInput";
@@ -839,6 +840,8 @@ export class SinglePlayerModal extends BaseModal {
839840

840841
const selectedColor = this.userSettings.getSelectedColor();
841842

843+
await crazyGamesSDK.requestMidgameAd();
844+
842845
this.dispatchEvent(
843846
new CustomEvent("join-lobby", {
844847
detail: {

src/client/UsernameInput.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
MIN_USERNAME_LENGTH,
99
validateUsername,
1010
} from "../core/validations/username";
11+
import { crazyGamesSDK } from "./CrazyGamesSDK";
1112

1213
const usernameKey: string = "username";
1314

@@ -39,7 +40,7 @@ export class UsernameInput extends LitElement {
3940

4041
connectedCallback() {
4142
super.connectedCallback();
42-
const stored = this.getStoredUsername();
43+
const stored = this.getUsername();
4344
this.parseAndSetUsername(stored);
4445
}
4546

@@ -161,7 +162,14 @@ export class UsernameInput extends LitElement {
161162
}
162163
}
163164

164-
private getStoredUsername(): string {
165+
private getUsername(): string {
166+
crazyGamesSDK.getUsername().then((username) => {
167+
if (username) {
168+
this.baseUsername = username;
169+
this.requestUpdate();
170+
}
171+
return null;
172+
});
165173
const storedUsername = localStorage.getItem(usernameKey);
166174
if (storedUsername) {
167175
return storedUsername;

src/client/graphics/layers/GameRightSidebar.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,26 @@ export class GameRightSidebar extends LitElement implements Layer {
105105

106106
private onPauseButtonClick() {
107107
this.isPaused = !this.isPaused;
108+
if (this.isPaused) {
109+
crazyGamesSDK.gameplayStop();
110+
} else {
111+
crazyGamesSDK.gameplayStart();
112+
}
108113
this.eventBus.emit(new PauseGameIntentEvent(this.isPaused));
109114
}
110115

111-
private onExitButtonClick() {
116+
private async onExitButtonClick() {
112117
const isAlive = this.game.myPlayer()?.isAlive();
113118
if (isAlive) {
114119
const isConfirmed = confirm(
115120
translateText("help_modal.exit_confirmation"),
116121
);
117122
if (!isConfirmed) return;
118123
}
119-
crazyGamesSDK.gameplayStop().then(() => {
120-
// redirect to the home page
121-
window.location.href = "/";
122-
});
124+
await crazyGamesSDK.requestMidgameAd();
125+
await crazyGamesSDK.gameplayStop();
126+
// redirect to the home page
127+
window.location.href = "/";
123128
}
124129

125130
private onSettingsButtonClick() {

src/client/graphics/layers/SettingsModal.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { html, LitElement } from "lit";
22
import { customElement, property, query, state } from "lit/decorators.js";
3+
import { crazyGamesSDK } from "src/client/CrazyGamesSDK";
4+
import { PauseGameIntentEvent } from "src/client/Transport";
35
import { EventBus } from "../../../core/EventBus";
46
import { UserSettings } from "../../../core/game/UserSettings";
57
import { AlternateViewEvent, RefreshGraphicsEvent } from "../../InputHandler";
6-
import { PauseGameIntentEvent } from "../../Transport";
78
import { translateText } from "../../Utils";
89
import SoundManager from "../../sound/SoundManager";
910
import { Layer } from "./Layer";
@@ -105,8 +106,14 @@ export class SettingsModal extends LitElement implements Layer {
105106
}
106107

107108
private pauseGame(pause: boolean) {
108-
if (this.shouldPause && !this.wasPausedWhenOpened)
109+
if (this.shouldPause && !this.wasPausedWhenOpened) {
110+
if (pause) {
111+
crazyGamesSDK.gameplayStop();
112+
} else {
113+
crazyGamesSDK.gameplayStart();
114+
}
109115
this.eventBus.emit(new PauseGameIntentEvent(pause));
116+
}
110117
}
111118

112119
private onTerrainButtonClick() {

src/client/graphics/layers/UnitDisplay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class UnitDisplay extends LitElement implements Layer {
130130

131131
return html`
132132
<div
133-
class="hidden 2xl:flex lg:flex fixed bottom-4 left-1/2 transform -translate-x-1/2 z-1100 2xl:flex-row xl:flex-col lg:flex-col 2xl:gap-5 xl:gap-2 lg:gap-2 justify-center items-center"
133+
class="hidden min-[1200px]:flex fixed bottom-4 left-1/2 transform -translate-x-1/2 z-[1100] 2xl:flex-row xl:flex-col min-[1200px]:flex-col 2xl:gap-5 xl:gap-2 min-[1200px]:gap-2 justify-center items-center"
134134
>
135135
<div class="bg-gray-800/70 backdrop-blur-xs rounded-lg p-0.5">
136136
<div class="grid grid-rows-1 auto-cols-max grid-flow-col gap-1 w-fit">

src/client/graphics/layers/WinModal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export class WinModal extends LitElement implements Layer {
250250
}
251251

252252
async show() {
253+
crazyGamesSDK.gameplayStop();
253254
await this.loadPatternContent();
254255
this.isVisible = true;
255256
this.requestUpdate();

startup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export CLOUDFLARE_TUNNEL_TOKEN=${TUNNEL_TOKEN}
8686

8787
# Start supervisord
8888
if [ "$DOMAIN" = openfront.dev ] && [ "$SUBDOMAIN" != main ]; then
89-
exec timeout 18h /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
89+
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
9090
else
9191
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
9292
fi

0 commit comments

Comments
 (0)