feat: 暂存百人赛
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import Utils from "../Pay/Utils";
|
||||
|
||||
/** Main-package bridge; the lazy bundle registers the activity implementation. */
|
||||
/** The home bundle is shipped in the main package; matching/stages load independently. */
|
||||
export default class CloudRiseService {
|
||||
private static runtime: any = null;
|
||||
private static bundleTask: Promise<cc.AssetManager.Bundle> = null;
|
||||
private static prefabTask: Promise<cc.Prefab> = null;
|
||||
private static pageTasks: { [bundle: string]: Promise<cc.Prefab> } = {};
|
||||
private static checkTask: Promise<boolean> = null;
|
||||
private static snapshot: any = null;
|
||||
private static uid = "";
|
||||
@@ -78,7 +79,7 @@ export default class CloudRiseService {
|
||||
}
|
||||
private static bundle(): Promise<cc.AssetManager.Bundle> {
|
||||
if (!this.bundleTask) this.bundleTask = new Promise<cc.AssetManager.Bundle>((resolve, reject) => {
|
||||
cc.assetManager.loadBundle("cloud_rise", (error, bundle) => error ? reject(error) : resolve(bundle));
|
||||
cc.assetManager.loadBundle("cloud_rise_home", (error, bundle) => error ? reject(error) : resolve(bundle));
|
||||
}).catch(error => { this.bundleTask = null; throw error; });
|
||||
return this.bundleTask;
|
||||
}
|
||||
@@ -90,6 +91,36 @@ export default class CloudRiseService {
|
||||
})).catch(error => { this.prefabTask = null; throw error; });
|
||||
return this.prefabTask;
|
||||
}
|
||||
static pageFragment(page: "matching" | "stage", stage = 1): Promise<cc.Prefab> {
|
||||
if (page === "stage" && ![1, 2, 3].includes(stage)) return Promise.reject(new Error("无效的百人赛阶段"));
|
||||
const name = page === "matching" ? "cloud_rise_matching" : "cloud_rise_stage" + stage;
|
||||
if (!this.pageTasks[name]) this.pageTasks[name] = Promise.all([this.bundle(), this.prepareActivityPackage()]).then(() => new Promise<cc.Prefab>((resolve, reject) => {
|
||||
cc.assetManager.loadBundle(name, (error, bundle) => {
|
||||
if (error) { reject(error); return; }
|
||||
bundle.load("prefab/CloudRise", cc.Prefab, (error, prefab: cc.Prefab) => {
|
||||
if (error) reject(error); else { prefab.addRef(); resolve(prefab); }
|
||||
});
|
||||
});
|
||||
})).catch(error => { delete this.pageTasks[name]; throw error; });
|
||||
return this.pageTasks[name];
|
||||
}
|
||||
/** The WeChat build hook supplies one download for all four page bundles. Preview keeps normal bundles. */
|
||||
private static prepareActivityPackage(): Promise<void> {
|
||||
const prepare = typeof window !== "undefined" && (window as any).__prepareCloudRisePackage;
|
||||
return prepare ? prepare() : Promise.resolve();
|
||||
}
|
||||
private static preloadActivity(): void {
|
||||
const data = this.data();
|
||||
if (!this.shouldPreload(data) && !this.unseenResult()) return;
|
||||
void Promise.all([this.pagePrefab(), this.prepareActivityPackage()]).then(() => {
|
||||
const latest = this.data(), run = this.run();
|
||||
// Download the whole package, but initialize matching assets only when another match is possible.
|
||||
if (run && run.status === "waiting" && this.challengeActive(run)
|
||||
|| latest && latest.available && this.registrationOpen(latest) && !this.lockedUntilLevel(latest)) {
|
||||
return this.pageFragment("matching").then(() => {});
|
||||
}
|
||||
}).catch(error => cc.warn("百人赛预加载失败,打开时可重试", error));
|
||||
}
|
||||
static afterLogin(): void { void this.sync(); }
|
||||
static async sync(): Promise<boolean> {
|
||||
this.ensureUser();
|
||||
@@ -97,9 +128,9 @@ export default class CloudRiseService {
|
||||
if (this.runtime) {
|
||||
const uid = this.uid;
|
||||
const ok = await this.runtime.sync();
|
||||
if (ok && String(cc.fx.GameConfig.GM_INFO.userId || "") === uid && this.shouldPreload(this.runtime.data())) {
|
||||
if (ok && String(cc.fx.GameConfig.GM_INFO.userId || "") === uid && (this.shouldPreload(this.runtime.data()) || this.unseenResult())) {
|
||||
this.autoDisplay = true;
|
||||
void this.pagePrefab().catch(error => cc.warn("百人赛预加载失败,打开时可重试", error));
|
||||
this.preloadActivity();
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
@@ -123,9 +154,10 @@ export default class CloudRiseService {
|
||||
await this.bundle();
|
||||
if (String(cc.fx.GameConfig.GM_INFO.userId || "") !== uid) return false;
|
||||
if (!this.runtime) throw new Error("百人赛分包未注册业务模块");
|
||||
// Prepare referenced textures and prefab without showing a page or delaying login.
|
||||
void this.pagePrefab().catch(error => cc.warn("百人赛预加载失败,打开时可重试", error));
|
||||
return await this.runtime.sync();
|
||||
// The runtime's first sync installs its snapshot; only then decide what to warm.
|
||||
const ok = await this.runtime.sync();
|
||||
if (ok) this.preloadActivity();
|
||||
return ok;
|
||||
} catch (error) { this.message = "活动加载失败,请重试"; cc.warn(error); this.emit(); return false; }
|
||||
})();
|
||||
this.checkTask = task;
|
||||
|
||||
Reference in New Issue
Block a user