feat: modify for smart ads

feature/ads-smart-display
tiendat3699 2024-07-15 09:25:19 +07:00
parent e73cf76c00
commit aa6dcfad2c
7 changed files with 673 additions and 610 deletions

File diff suppressed because it is too large Load Diff

View File

@ -12,10 +12,12 @@ import {
Vec3,
} from 'cc';
import ControllerSide from '../Enum/ControllerSide';
import GameState from '../Enum/GameState';
import TimeConfig from '../Enum/TimeConfig';
import GameEvent from '../Events/GameEvent';
import AudioManager from '../Manager/AudioManager';
import { EventManger } from '../Manager/EventManger';
import P4PSDK, { EventType } from '../P4PSDK';
const { ccclass, property } = _decorator;
@ccclass('Flipper')
@ -38,13 +40,14 @@ export class Flipper extends Component {
private _isAnimationPlaying;
protected onLoad(): void {
input.on(Input.EventType.KEY_DOWN, this.onKeyInputDown, this);
input.on(Input.EventType.KEY_UP, this.onKeyInputUp, this);
EventManger.instance.on(GameEvent.ControlTouchStart, this.onTouchStart, this);
EventManger.instance.on(GameEvent.ControlTouchEnd, this.onTouchEnd, this);
EventManger.instance.on(GameEvent.GameStateChange, this.gameStateChange, this);
}
protected start(): void {
input.on(Input.EventType.KEY_DOWN, this.onKeyInputDown, this);
input.on(Input.EventType.KEY_UP, this.onKeyInputUp, this);
this._animation.play();
}
@ -56,8 +59,53 @@ export class Flipper extends Component {
}
}
private gameStateChange(state: GameState) {
if (state == GameState.Init) {
P4PSDK.broadCast.on(EventType.OnKeyDown, this.onSDKKeyDown, this);
P4PSDK.broadCast.on(EventType.OnKeyUp, this.onSDKKeyUp, this);
}
}
//#region Input Handler
private onSDKKeyDown(key: number) {
switch (key) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
if (this.side == ControllerSide.Left) this.activeFlipper();
this._timer = 0;
if (this._animation.getState(this._animation.defaultClip.name).isPlaying) {
this._animation.stop();
}
break;
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
if (this.side == ControllerSide.Right) this.activeFlipper();
this._timer = 0;
if (this._isAnimationPlaying) {
this._animation.stop();
}
break;
default:
break;
}
}
private onSDKKeyUp(key: number) {
switch (key) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
if (this.side == ControllerSide.Left) this.deActiveFlipper();
break;
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
if (this.side == ControllerSide.Right) this.deActiveFlipper();
break;
default:
break;
}
}
private onKeyInputDown(event: EventKeyboard) {
switch (event.keyCode) {
case KeyCode.KEY_A:
@ -116,7 +164,7 @@ export class Flipper extends Component {
//#endregion
private activeFlipper(): void {
AudioManager.playSfx(this._activeSound, { volume: 0.5 });
// AudioManager.playSfx(this._activeSound, { volume: 0.5 });
this._hingeJoint.motorSpeed = this._motorSpeedActive;
}
private deActiveFlipper(): void {

View File

@ -12,7 +12,6 @@ import {
Vec2,
Vec3,
} from 'cc';
import { EDITOR, PREVIEW } from 'cc/env';
import Timer, { TimerType } from '../Base/Timer';
import { BoosterBase } from '../Booster/BoosterBase';
import BoosterType from '../Enum/BoosterType';
@ -98,13 +97,13 @@ export class GameManager extends Singleton<GameManager>() {
}
protected async start(): Promise<void> {
await P4PSDK.init(this.onBoughtTicket, this);
if (P4PSDK.getGameTime()) {
this._timePlay = P4PSDK.getGameTime();
P4PSDK.isUseApi = false;
await P4PSDK.init();
if (P4PSDK.gameTime) {
this._timePlay = P4PSDK.gameTime;
}
P4PSDK.setCallAPI(this._callAPI);
await P4PSDK.authenticate();
this.changeGameState(GameState.Init);
this.Ready();
}
protected update(dt: number): void {
@ -114,8 +113,8 @@ export class GameManager extends Singleton<GameManager>() {
}
private onBoughtTicket() {
this.gameRelive();
EventManger.instance.emit(GameEvent.TicketUpdate, P4PSDK.getUserTicket());
// this.gameRelive();
// EventManger.instance.emit(GameEvent.TicketUpdate, P4PSDK.getUserTicket());
}
private async changeGameState(state: GameState) {
@ -306,27 +305,29 @@ export class GameManager extends Singleton<GameManager>() {
private _minusTicketLoading = false;
public async replay(): Promise<void> {
if (this._minusTicketLoading) return;
this._minusTicketLoading = true;
if (!PREVIEW && !EDITOR) {
const checkGameScoreTicket = await P4PSDK.checkGameScoreTicket();
if (checkGameScoreTicket) {
const success = await P4PSDK.minusTicket('revive');
if (success) {
this.gameRelive();
} else {
P4PSDK.callPayPalModal();
}
} else {
this.gameOver();
}
} else {
this.gameRelive();
}
this._minusTicketLoading = false;
// if (this._minusTicketLoading) return;
// this._minusTicketLoading = true;
// if (!PREVIEW && !EDITOR) {
// const checkGameScoreTicket = await P4PSDK.checkGameScoreTicket();
// if (checkGameScoreTicket) {
// const success = await P4PSDK.minusTicket('revive');
// if (success) {
// this.gameRelive();
// } else {
// P4PSDK.callPayPalModal();
// }
// } else {
// this.gameOver();
// }
// } else {
// this.gameRelive();
// }
// this._minusTicketLoading = false;
}
public async play() {
await P4PSDK.startGame();
const coin = await P4PSDK.getUserCoin();
this._timer.time = this._timePlay;
this._score = 0;
this._currentBallInGame = 0;
@ -335,8 +336,7 @@ export class GameManager extends Singleton<GameManager>() {
await Utils.delay(TimeConfig.DelayPLay);
this._timer.startCount();
this.spawnBall(true);
await P4PSDK.minusTicket('auth');
EventManger.instance.emit(GameEvent.TicketUpdate, P4PSDK.getUserTicket());
EventManger.instance.emit(GameEvent.TicketUpdate, coin);
}
public async gameRelive() {

View File

@ -1,48 +1,91 @@
export type postMessageType = 'paypal_modal' | 'game_tournament';
export interface IObject {
[key: string]: any;
}
export type minusTicketType = 'auth' | 'revive';
export interface IUserInfo {
id: string;
displayName: string;
promoCode: string;
isSubscriber: boolean;
avatarLink: string;
}
export interface IParticipationInfo {
id: string;
totalScore: number;
myRank: number;
gameStates: IObject;
}
export interface ILogger {
log(...data: (number | string | boolean | undefined | null)[]): void;
info(...data: (number | string | boolean | undefined | null)[]): void;
warn(...data: (number | string | boolean | undefined | null)[]): void;
error(...data: (number | string | boolean | undefined | null)[]): void;
addBadge(badge: string, style?: { color?: string; backgroundColor?: string }): ILogger;
}
export interface IAnalyticsModule {
logger: ILogger;
}
export interface IDataModule {
getItem<T>(key: string): T | null;
setItem(key: string, value: any): void;
removeItem(key: string): void;
clear(): void;
}
export interface IBroadCast {
emit(eventType: EventType, data?: any): void;
on<K extends keyof EventMap>(eventType: K, listener: EventMap[K], thisArg?: any): void;
off<K extends keyof EventMap>(eventType: K, listener: EventMap[K], thisArg?: any): void;
}
export enum EventType {
OnKeyDown = 'OnKeyDown',
OnKeyUp = 'OnKeyUp',
OnMouse = 'OnMouse',
OnMouseDown = 'OnMouseDown',
OnMouseUp = 'OnMouseUp',
OnGameStart = 'OnGameStart',
OnGameEnd = 'OnGameEnd',
}
export interface EventMap {
[EventType.OnKeyDown]: (key: number) => void;
[EventType.OnKeyUp]: (key: number) => void;
[EventType.OnMouse]: (position: IVector2) => void;
[EventType.OnMouseDown]: (button: number) => void;
[EventType.OnMouseUp]: (button: number) => void;
[EventType.OnGameStart]: () => void;
[EventType.OnGameEnd]: () => void;
}
interface IVector2 {
x: number;
y: number;
}
export interface SDK {
setCallAPI(value: boolean): void;
getEnv(): string;
getUserTicket(): number;
getTopScore(): number;
getLatestScore(): number;
getGameScore(): number;
getUserId(): string;
getGameTime(): number | null;
getTicketNeedToContinue(): number;
init(buyTicketCallBack: () => any, thisArg?: any): void;
previousScore: number;
currentScore: number;
totalScore: number;
userRank: number;
isUseApi: boolean;
tournamentData: IDataModule;
userData: IDataModule;
gameData: IDataModule;
analytics: IAnalyticsModule;
broadCast: IBroadCast;
gameTime: number;
updateScore(score: number): void;
authenticate(): Promise<boolean>;
checkGameScoreTicket(): Promise<boolean>;
buyMoreTicket(): void;
postScoreToServer(): void;
minusTicket(type: minusTicketType): Promise<boolean>;
callPayPalModal(): void;
canRelive(): boolean;
spinGacha(id: string): Promise<Reward>;
getLeaderBoard(): Promise<PlayerInfo[]>;
}
export enum Env {
development,
staging,
production,
}
export interface PlayerInfo {
userId: string;
displayName: string;
score: number;
}
export interface Reward {
id: string;
name: string;
description: string;
quantity: number;
thumbnailLink: string;
init(): Promise<void>;
getUserInfo(): Promise<IUserInfo>;
getLeaderBoard(start: number, count: number): Promise<IParticipationInfo[]>;
startGame(): Promise<boolean>;
endGame(): Promise<boolean>;
getUserCoin(): Promise<number>;
}
enum InitState {
@ -50,54 +93,73 @@ enum InitState {
Initialized,
}
export default class P4PSDK {
private static _initState: InitState = InitState.UnInitialized;
private static _sdk: SDK;
(window as any).P4P = {
isUseApi: true,
};
//#region PUBLIC METHOD
class _P4PSDK implements SDK {
private _initState: InitState = InitState.UnInitialized;
private _sdk: SDK;
public static getEnv(): Env {
return Env[this._sdk?.getEnv() as keyof typeof Env];
private get sdk() {
if (this._initState !== InitState.Initialized || !this._sdk) {
throw new Error('P4P SDK is not initialized yet. Please call P4PSDK.init() first.');
}
return this._sdk;
}
public static getUserTicket(): number {
return this._sdk?.getUserTicket();
public get tournamentData(): IDataModule {
return this.sdk.tournamentData;
}
public get userData(): IDataModule {
return this.sdk.userData;
}
public get gameData(): IDataModule {
return this.sdk.gameData;
}
public get analytics(): IAnalyticsModule {
return this.sdk.analytics;
}
public get broadCast(): IBroadCast {
return this.sdk.broadCast;
}
public get previousScore(): number {
return this.sdk.previousScore;
}
public get currentScore(): number {
return this.sdk.currentScore;
}
public get totalScore(): number {
return this.sdk.totalScore;
}
public get userRank(): number {
return this.sdk.userRank;
}
public get isUseApi(): boolean {
return this.sdk.isUseApi;
}
public set isUseApi(value: boolean) {
(window as any).P4P.isUseApi = value;
}
public get gameTime(): number {
return this.sdk.gameTime;
}
public static getTopScore(): number {
return this._sdk?.getTopScore();
}
public static getLatestScore(): number {
return this._sdk?.getLatestScore();
}
public static getGameScore(): number {
return this._sdk?.getGameScore();
}
public static getUserId(): string {
return this._sdk.getUserId();
}
public static getGameTime(): number | null {
return this._sdk.getGameTime();
}
public static getTicketNeedToContinue(): number {
return this._sdk?.getTicketNeedToContinue();
}
private static async loadSDK(): Promise<void> {
private async loadSDK(): Promise<void> {
return new Promise((resolve, reject) => {
const tag = document.createElement('script');
tag.type = 'module';
tag.src = 'https://firebasestorage.googleapis.com/v0/b/play-now-1aef8.appspot.com/o/SDK%2Fsdk.js?alt=media';
tag.src =
'https://firebasestorage.googleapis.com/v0/b/play-now-1aef8.appspot.com/o/SDKTEST%2Fsdk.js?alt=media';
tag.async = true;
tag.onload = async () => {
console.log('P4P SDK loaded');
this._sdk = (window as any).P4P.SDK;
resolve();
this._sdk = (globalThis as any).P4P.SDK;
if (this._sdk) {
await this._sdk.init();
resolve();
} else {
reject('P4P SDK is undefined');
}
};
tag.onerror = (e) => {
console.error('Failed to load P4PSDK JS. Please check your internet connection.');
@ -107,55 +169,39 @@ export default class P4PSDK {
});
}
public static setCallAPI(value: boolean) {
this._sdk?.setCallAPI(value);
}
public static async init(buyTicketCallBack: () => any, thisArg?: any): Promise<void> {
public async init(): Promise<void> {
if (this._initState == InitState.Initialized) return;
await this.loadSDK();
this._sdk.init(buyTicketCallBack, thisArg);
this._initState = InitState.Initialized;
}
public static updateScore(score: number) {
this._sdk?.updateScore(score);
public setIsUseApi(value: boolean) {
(window as any).P4P.isUseApi = value;
}
public static async authenticate(): Promise<boolean> {
return this._sdk?.authenticate();
public updateScore(score: number): void {
this.sdk.updateScore(score);
}
public static async checkGameScoreTicket(): Promise<boolean> {
return this._sdk?.checkGameScoreTicket();
public getUserInfo(): Promise<IUserInfo> {
return this.sdk.getUserInfo();
}
public static buyMoreTicket() {
this._sdk?.buyMoreTicket();
public getLeaderBoard(start: number, count: number) {
return this.sdk.getLeaderBoard(start, count);
}
public static async postScoreToServer() {
this._sdk?.postScoreToServer();
public startGame(): Promise<boolean> {
return this.sdk.startGame();
}
public static async minusTicket(type: minusTicketType): Promise<boolean> {
return this._sdk?.minusTicket(type);
public endGame(): Promise<boolean> {
return this.sdk.endGame();
}
public static callPayPalModal() {
this._sdk?.callPayPalModal();
public getUserCoin(): Promise<number> {
return this.sdk.getUserCoin();
}
public static canRelive(): boolean {
return this._sdk?.canRelive();
}
public static async spinGacha(id: string): Promise<Reward> {
return this._sdk?.spinGacha(id);
}
public static async getLeaderBoard(): Promise<PlayerInfo[]> {
return this._sdk?.getLeaderBoard();
}
//#endregion
}
const P4PSDK: SDK = new _P4PSDK();
export default P4PSDK;

View File

@ -48,11 +48,12 @@ export class GameOverPanel extends Component {
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
}
public show(end: boolean): void {
this._ticketMinus.string = P4PSDK.getTicketNeedToContinue().toString();
const currentScore = P4PSDK.getLatestScore();
const gameScore = P4PSDK.getGameScore();
this.topScore.string = P4PSDK.getTopScore().toString();
public async show(end: boolean): Promise<void> {
// this._ticketMinus.string = P4PSDK.getTicketNeedToContinue().toString();
const currentScore = P4PSDK.previousScore;
const gameScore = P4PSDK.currentScore;
const top = await P4PSDK.getLeaderBoard(0, 1);
this.topScore.string = top.length > 0 ? top[0].totalScore.toString() : '0';
this.yourScore.string = currentScore.toString();
this.playCollectEffect(gameScore, currentScore);
this.scheduleOnce(this.endGame, 60);
@ -79,7 +80,7 @@ export class GameOverPanel extends Component {
this._quitBtn.active = false;
if (this._active) {
await Utils.delay(1);
P4PSDK.postScoreToServer();
P4PSDK.endGame();
}
break;
case GameState.Relive:
@ -161,7 +162,7 @@ export class GameOverPanel extends Component {
.start();
if (!this._end) return;
await Utils.delay(1);
P4PSDK.postScoreToServer();
P4PSDK.endGame();
}
}
}

View File

@ -17,6 +17,7 @@ import TimeConfig from '../Enum/TimeConfig';
import GameEvent from '../Events/GameEvent';
import { EventManger } from '../Manager/EventManger';
import { GameManager } from '../Manager/GameManager';
import P4PSDK, { EventType } from '../P4PSDK';
import Utils from '../Utilities';
const { ccclass, property } = _decorator;
@ -37,13 +38,11 @@ export class TutorialController extends Component {
private _canShow = true;
private _playing = false;
protected onLoad(): void {
protected onLoad() {
this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
input.on(Input.EventType.KEY_UP, this.onKeyInputUpStart, this);
P4PSDK.broadCast.on(EventType.OnKeyDown, this.onSDKKeyInputStart, this);
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
}
protected start() {
this.playTutorial();
}
@ -115,9 +114,11 @@ export class TutorialController extends Component {
this.stopTutorial();
this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
input.off(Input.EventType.KEY_UP, this.onKeyInputUpStart, this);
P4PSDK.broadCast.off(EventType.OnKeyDown, this.onSDKKeyInputStart, this);
this.node.on(Input.EventType.TOUCH_START, this.onTouch, this);
input.on(Input.EventType.KEY_UP, this.onKeyInputUp, this);
P4PSDK.broadCast.on(EventType.OnKeyDown, this.onSDKKeyInput, this);
GameManager.instance.play();
}
@ -130,6 +131,19 @@ export class TutorialController extends Component {
this.startGame();
}
private onSDKKeyInputStart(key: number) {
switch (key) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
this.startGame();
break;
default:
break;
}
}
private onKeyInputUpStart(event: EventKeyboard) {
switch (event.keyCode) {
case KeyCode.KEY_A:
@ -155,4 +169,17 @@ export class TutorialController extends Component {
break;
}
}
private onSDKKeyInput(key: number) {
switch (key) {
case KeyCode.KEY_A:
case KeyCode.ARROW_LEFT:
case KeyCode.KEY_D:
case KeyCode.ARROW_RIGHT:
this.stopTutorial();
break;
default:
break;
}
}
}

View File

@ -65,9 +65,10 @@ export class UIController extends Component {
private async onGameStateChange(state: GameState) {
switch (state) {
case GameState.Init:
const coin = await P4PSDK.getUserCoin();
this.playButton.interactable = true;
this.loadingScreen.active = false;
this._ticketLabel.string = P4PSDK.getUserTicket().toString();
this._ticketLabel.string = coin.toString();
this._scoreLabel.string = '0';
break;
case GameState.Ready: