Commit 860b4b42 authored by 范雪寒's avatar 范雪寒

feat: 单机可以玩了

parent f62de039
...@@ -13,32 +13,32 @@ export declare interface AvailableRoomsInfo { ...@@ -13,32 +13,32 @@ export declare interface AvailableRoomsInfo {
hasNext: 0 | 1; hasNext: 0 | 1;
} }
declare class Base { /**
* Base 类
* @public
*/
export declare class Base {
protected get state(): StateCode; protected get state(): StateCode;
protected get serverEventCode(): ServerEventCode; protected get serverEventCode(): ServerEventCode;
protected get accessToken(): string; protected get appId(): string;
protected get openId(): string;
protected get serviceToken(): string; protected get serviceToken(): string;
get appId(): string; get playerId(): string;
get clientId(): string; get lastRoomId(): string;
get clientSecret(): string;
get roomId(): string; get roomId(): string;
get groupId(): string; get groupId(): string;
get openId(): string;
get playerId(): string;
protected constructor(); protected constructor();
protected setState(state: StateCode): void; protected setState(state: StateCode): void;
protected setServerEvent(code: ServerEventCode, param?: string): void; protected setServerEvent(code: ServerEventCode, param?: string): void;
protected setAccessToken(token: string): void;
protected setServiceToken(token: string): void;
protected setAppId(id: string): void; protected setAppId(id: string): void;
protected setOpenId(id: string): void; protected setOpenId(id: string): void;
protected setClientId(id: string): void; protected setServiceToken(token: string): void;
protected setClientSecret(secret: string): void; protected setPlayerId(id: string): void;
protected setLastRoomId(roomId: string): void;
protected setRoomId(id: string): void; protected setRoomId(id: string): void;
protected setGroupId(id: string): void; protected setGroupId(id: string): void;
protected setPlayerId(id: string): void;
protected onStateChange(latter: StateCode, former: StateCode): void; protected onStateChange(latter: StateCode, former: StateCode): void;
protected onServerEvent(latter: ServerEvent, former: ServerEvent): void; protected onServerEventChange(latter: ServerEvent, former: ServerEvent): void;
} }
/** /**
...@@ -46,11 +46,13 @@ declare class Base { ...@@ -46,11 +46,13 @@ declare class Base {
* @public * @public
*/ */
export declare class Client extends Base { export declare class Client extends Base {
private _auth;
private _room; private _room;
private _group; private _group;
private _lastRoomId; private _pollInterval;
private _isMatching; private _isMatching;
private _isCancelMatch; private _isCancelMatch;
private _loginTimestamp;
/** /**
* 获取对应房间实例 * 获取对应房间实例
* @readonly * @readonly
...@@ -62,10 +64,10 @@ export declare class Client extends Base { ...@@ -62,10 +64,10 @@ export declare class Client extends Base {
*/ */
get group(): Group | null; get group(): Group | null;
/** /**
* 上次断线的房间 ID * 获取玩家登录时间戳
* @readonly * @readonly
*/ */
get lastRoomId(): string; get loginTimestamp(): number;
/** /**
* 创建客户端 * 创建客户端
* @param config - 创建客户端参数 * @param config - 创建客户端参数
...@@ -75,43 +77,27 @@ export declare class Client extends Base { ...@@ -75,43 +77,27 @@ export declare class Client extends Base {
* 初始化客户端 * 初始化客户端
* @remarks 必须先初始化客户端,才能创建/加入/匹配房间 * @remarks 必须先初始化客户端,才能创建/加入/匹配房间
*/ */
init(): Promise<void>; init(): Promise<Client>;
/** /**
* 创建房间 * 创建房间
* @remarks 创建成功也意味着加入了该房间 * @remarks 创建成功也意味着加入了该房间
* @param roomConfig - 房间信息参数 * @param createRoomConfig - 房间信息参数
* @param playerConfig - 玩家信息参数 * @param playerConfig - 玩家信息参数
*/ */
createRoom(roomConfig: CreateRoomConfig, playerConfig?: PlayerConfig): Promise<Room>; createRoom(createRoomConfig: CreateRoomConfig, playerConfig?: PlayerConfig): Promise<Room>;
/** /**
* 创建队伍 * 创建队伍
* @remarks 创建成功也意味着加入了该队伍 * @remarks 创建成功也意味着加入了该队伍
* @param groupConfig - 队伍信息参数 * @param groupConfig - 队伍信息参数
* @param playerConfig - 玩家信息参数 * @param playerConfig - 玩家信息参数
*/ */
createGroup(this: Client, groupConfig: CreateGroupConfig, playerConfig?: PlayerConfig): Promise<Group>; createGroup(groupConfig: CreateGroupConfig, playerConfig?: PlayerConfig): Promise<Group>;
/** /**
* 加入房间 * 加入房间
* @param roomIdentity - 房间身份标识(房间Id或者房间Code) * @param roomIdentity - 房间身份标识(房间Id或者房间Code)
* @param playerConfig - 玩家信息参数 * @param playerConfig - 玩家信息参数
*/ */
joinRoom(roomIdentity: string, playerConfig?: PlayerConfig): Promise<Room>; joinRoom(roomIdentity: string, playerConfig?: PlayerConfig): Promise<Room>;
/**
* 根据房间 ID 加入房间
* @param roomId - 房间 ID
* @param playerConfig - 玩家信息参数
*/
joinRoomById(roomId: string, playerConfig?: PlayerConfig): Promise<Room>;
/**
* 根据房间 roomCode 加入房间
* @param roomCode - 房间 code
* @param playerConfig - 玩家信息参数
*/
joinRoomByCode(roomCode: string, playerConfig?: PlayerConfig): Promise<Room>;
/**
* 重新加入房间
*/
reconnectRoom(playerConfig?: PlayerConfig): Promise<Room>;
/** /**
* 根据队伍ID加入队伍 * 根据队伍ID加入队伍
* @param groupId - 队伍 ID * @param groupId - 队伍 ID
...@@ -147,30 +133,36 @@ export declare class Client extends Base { ...@@ -147,30 +133,36 @@ export declare class Client extends Base {
getAvailableRooms(getAvailableRoomsConfig: GetAvailableRoomsConfig): Promise<AvailableRoomsInfo>; getAvailableRooms(getAvailableRoomsConfig: GetAvailableRoomsConfig): Promise<AvailableRoomsInfo>;
/** /**
* 房间匹配 * 房间匹配
* @param matchRoomConfig -房间匹配参数 * @param matchRoomConfig - 房间匹配参数
* @param playerConfig - 玩家信息参数 * @param playerConfig - 玩家信息参数
*/ */
matchRoom(matchRoomConfig: MatchRoomConfig, playerConfig?: PlayerConfig): Promise<Room>; matchRoom(matchRoomConfig: MatchRoomConfig, playerConfig?: PlayerConfig): Promise<Room>;
/** /**
* 在线匹配 * 在线匹配
* @param matchPlayerConfig -在线匹配参数 * @param matchPlayerConfig - 在线匹配参数
* @param playerConfig - 玩家信息参数 * @param playerConfig - 玩家信息参数
* @remark 轮询时间间隔2s
*/ */
matchPlayer(matchPlayerConfig: MatchPlayerConfig, playerConfig?: PlayerConfig): Promise<Room>; matchPlayer(matchPlayerConfig: MatchPlayerConfig, playerConfig?: PlayerConfig): Promise<Room>;
/** /**
* 组队匹配 * 组队匹配
* @param matchGroupConfig -组队匹配参数 * @param matchGroupConfig - 组队匹配参数
* @param playerConfig - 玩家信息参数 * @param playerConfig - 玩家信息参数
* @remark 轮询时间间隔2s * @remarks 队长才能主动发起组队匹配,队员通过广播通知被动调起组队匹配
*/ */
matchGroup(matchGroupConfig: MatchGroupConfig, playerConfig?: PlayerConfig): Promise<Room>; matchGroup(matchGroupConfig: MatchGroupConfig, playerConfig?: PlayerConfig): Promise<Room>;
/** /**
* 取消匹配 * 取消匹配
* @remark 组队匹配模式中,当前只有队长可以取消匹配 * @remarks 组队匹配模式中,当前只有队长可以取消匹配
* 该方法会修改_isCancelMatch状态 在matchPolling中请求真实的取消匹配接口并通过匹配接口返回取消结果
*/ */
cancelMatch(): void; cancelMatch(): void;
/**
* 取消匹配
*/
private requestCancelMatch;
/**
* 轮询匹配
*/
private matchPolling;
protected onStateChange(state: StateCode, former: StateCode): void; protected onStateChange(state: StateCode, former: StateCode): void;
private checkInit; private checkInit;
private checkCreateOrJoin; private checkCreateOrJoin;
...@@ -181,33 +173,23 @@ export declare class Client extends Base { ...@@ -181,33 +173,23 @@ export declare class Client extends Base {
private checkCreateGroupConfig; private checkCreateGroupConfig;
private checkJoinRoomConfig; private checkJoinRoomConfig;
private checkMatching; private checkMatching;
/**
* 请求匹配接口
*/
private requestMatch;
/**
* 请求取消匹配接口
*/
private requestCancelMatch;
/**
* @remark 匹配轮询方法 包含取消匹配
*/
private matchPolling;
} }
/** /**
* 客户端类构造方法参数 * Client 类构造方法参数
* @param clientId - 客户端ID * @param clientId - 客户端ID
* @param clientSecret - 客户端密钥 * @param clientSecret - 客户端密钥
* @param appId - 应用ID * @param appId - 应用ID
* @param openId - 玩家ID * @param openId - 玩家ID
* @param createSignature - 签名函数
* @public * @public
*/ */
export declare interface ClientConfig { export declare interface ClientConfig {
clientId: string; clientId: string;
clientSecret: string; clientSecret: string;
appId: string;
openId: string; openId: string;
appId: string;
createSignature?: CreateSignature;
} }
/** /**
...@@ -246,6 +228,12 @@ export declare interface CreateRoomConfig { ...@@ -246,6 +228,12 @@ export declare interface CreateRoomConfig {
customRoomProperties?: string; customRoomProperties?: string;
} }
/**
* 签名函数
* @public
*/
export declare type CreateSignature = () => Promise<Signature>;
/** /**
* 错误码 * 错误码
* @public * @public
...@@ -257,21 +245,55 @@ export declare const enum ErrorCode { ...@@ -257,21 +245,55 @@ export declare const enum ErrorCode {
COMMON_REQUEST_PARAM_ERR = 1001, COMMON_REQUEST_PARAM_ERR = 1001,
DATABASE_OPERATION_ERR = 1002, DATABASE_OPERATION_ERR = 1002,
DCS_OPERATION_ERR = 1003, DCS_OPERATION_ERR = 1003,
FAILED_TO_VERIFY_THE_INTERFACE_SIGNATURE = 1011,
REPEAT_RUQUEST = 4003, REPEAT_RUQUEST = 4003,
TOKEN_AUTH_FAILED = 100003, TOKEN_AUTH_FAILED = 100103,
SERVICE_NOT_ENABLED = 100004, SERVICE_NOT_ENABLED = 100104,
PLAYER_INFO_MIASSING = 100005, PLAYER_INFO_MIASSING = 100105,
PROJECT_NOT_EXIST = 100008, PROJECT_NOT_EXIST = 100108,
IP_AREA_ERR = 101001, QUERY_APP_INFO_FROM_AGC = 100112,
PLAYER_AND_ROOM_MISMATCH = 101004, APP_NOT_BELONG_CURRENT_PROJECT = 100113,
ROOM_INFO_NOT_EXIST = 101005, INVALID_APP_SIGNATURE_VERIFICATION_PARAMETER = 100114,
CANNOT_DESTORY_ROOM_IN_GAME = 101006, FAILED_TO_VERIFY_THE_APP = 100115,
PLAYER_NOT_IN_CURRENT_ROOM = 101007, ROOM_PLAYER_NOT_IN_ROOM = 101101,
ENTERED_ROOM_OR_NOT_EXIST = 101008, PLAYER_AND_ROOM_MISMATCH = 101102,
PLAYERS_EXCEEDS_ROOM_MAX = 101009, ROOM_INFO_NOT_EXIST = 101103,
PLAYER_INFO_NOT_EXIST = 101010, CANNOT_DESTORY_ROOM_IN_GAME = 101104,
OWNER_AND_ROOM_MISMATCH = 101011, PLAYER_NOT_IN_CURRENT_ROOM = 101105,
MAXPLAYER_TOO_LARGE = 101015, ENTERED_ROOM_OR_NOT_EXIST = 101106,
PLAYERS_EXCEEDS_ROOM_MAX = 101107,
PLAYER_INFO_NOT_EXIST = 101108,
ROOM_OWNER_AND_PLAYER_MISMATCH = 101109,
GENERATE_SECURE_RANDOM_NUM_FAILED = 101110,
GET_SIGNATURE_INFO_FAILED = 101111,
CREATE_GRPC_CHANNEL_FAILED = 101112,
MAXPLAYER_TOO_LARGE = 101113,
ROOM_STARTED_FRAME_SYNC = 101114,
ROOM_STOPPED_FRAME_SYNC = 101115,
OTHER_PLAYER_OFFLINE = 101116,
INVALID_ROOM = 101117,
FRAME_SYNC_OPERATION_IS_INVALID_ROOM_STATE = 101118,
FRAME_SYNC_OPERATION_IS_INVALID_PLAYER_STATE = 101119,
INVALID_ROOM_STATUS = 101120,
REPEATS_PLAYER_ID = 101121,
PARSE_TIME_FAILED = 101122,
TOO_MANY_MATCHING_PARAMETERS = 101123,
PLAYER_STATUS_CONNOT_MODIFY = 101124,
ROOM_OWNER_STATUS_CANNOT_BE_KICKED = 101125,
PLAYER_INFO_CANNOT_EMPTY = 101126,
SOME_PLAYER_DADABASES_NOT_EXIST = 101127,
PLAYERS_NUMBER_EXCEEDS_THE_LIMIT = 101128,
THE_PLAYER_NOT_EXIST_DADABASES = 101129,
THE_PLAYER_ALREADY_IN_THE_ROOM = 101130,
ROUTE_INFO_CANNOT_BE_FOUND = 101131,
PLAYER_NOT_IN_CURRENT_GROUP = 101201,
GROUP_NOT_EXIST = 101202,
PLAYERS_ALREADY_IN_OTHER_GROUP = 101203,
THE_GROUP_CONNOT_BE_DISBANDED = 101204,
THE_PLAYER_NOT_CURRENT_GROUP_LEADER = 101205,
CURRENT_GROUP_IS_LOCKED = 101206,
CURRENT_GROUP_IS_FULL = 101207,
NEW_PLAYER_NOT_IN_GROUP = 101208,
ROOM_NOT_BEGIN_FRAME_SYNC = 102003, ROOM_NOT_BEGIN_FRAME_SYNC = 102003,
PLAYER_NOT_IN_ROOM = 102005, PLAYER_NOT_IN_ROOM = 102005,
ROOM_NOT_EXIST = 102008, ROOM_NOT_EXIST = 102008,
...@@ -283,18 +305,18 @@ export declare const enum ErrorCode { ...@@ -283,18 +305,18 @@ export declare const enum ErrorCode {
CLIENT_TRAFFIC_CONTROL = 103003, CLIENT_TRAFFIC_CONTROL = 103003,
NOT_LOGGED_IN = 103004, NOT_LOGGED_IN = 103004,
EXCEED_MAX_CONNECTIONSS = 103006, EXCEED_MAX_CONNECTIONSS = 103006,
ROOM_MATCH_FAILED = 104001, ROOM_MATCH_FAILED = 104101,
ROOM_MATCHING = 104002, ROOM_MATCHING = 104102,
ROOM_MATCH_TIMEOUT = 104003, ROOM_MATCH_TIMEOUT = 104103,
PLAYER_MATCH_FAILED = 306001, PLAYER_MATCH_FAILED = 104201,
PLAYER_MATCHING = 306002, PLAYER_MATCHING = 104202,
PLAYER_MATCH_TIMEOUT = 306003, PLAYER_MATCH_TIMEOUT = 104203,
PLAYER_MATCH_CANCEL_NO_PERMISSION = 306004, PLAYER_MATCH_CANCEL_NO_PERMISSION = 104204,
PLAYER_ID_NOT_CONSISTENT = 306005, PLAYER_MATCH_CANCELED = 104205,
PLAYER_MATCH_CANCEL_WHEN_SUCCESS = 306006, PLAYER_MATCH_CANCEL_WHEN_SUCCESS = 104206,
PLAYER_MATCH_GET_RULE_FAIL = 306007, PLAYER_MATCH_GET_RULE_FAIL = 104207,
PLAYER_MATCH_ROOM_IS_NULL = 306008, PLAYER_MATCH_ROOM_IS_NULL = 104208,
PLAYER_MATCH_CANCELED = 306009, PLAYER_MATCH_INVALID_TEAM = 104209,
SDK_NOT_IN_GROUP = 80001, SDK_NOT_IN_GROUP = 80001,
SDK_GROUP_NAME_TOO_LONG = 80002, SDK_GROUP_NAME_TOO_LONG = 80002,
SDK_NO_PERMISSION_UPDATE_GROUP = 80003, SDK_NO_PERMISSION_UPDATE_GROUP = 80003,
...@@ -307,10 +329,17 @@ export declare const enum ErrorCode { ...@@ -307,10 +329,17 @@ export declare const enum ErrorCode {
SDK_IN_FRAME_SYNC = 90006, SDK_IN_FRAME_SYNC = 90006,
SDK_INVALID_ROOM_IDENTITY = 90007, SDK_INVALID_ROOM_IDENTITY = 90007,
SDK_IN_MATCHING = 90008, SDK_IN_MATCHING = 90008,
SDK_NOT_IN_MATCHING = 90009,
SDK_IN_Requesting = 90010,
SDK_GROUP_MEMBERS_ERROR = 90011,
SDK_ROOM_NAME_TOO_LONG = 10001 SDK_ROOM_NAME_TOO_LONG = 10001
} }
declare class EventEmitter<T extends (...args: any[]) => any> { /**
* 事件触发器
* @public
*/
export declare class EventEmitter<T extends (...args: any[]) => any> {
handlers: Array<T>; handlers: Array<T>;
on(handler: T): this; on(handler: T): this;
emit(...args: FunctionParam<T>): void; emit(...args: FunctionParam<T>): void;
...@@ -343,7 +372,11 @@ export declare interface FramePlayerInfo { ...@@ -343,7 +372,11 @@ export declare interface FramePlayerInfo {
playerId: string; playerId: string;
} }
declare type FunctionParam<T> = T extends (...args: infer P) => any ? P : never; /**
* 函数参数类型
* @public
*/
export declare type FunctionParam<T> = T extends (...args: infer P) => any ? P : never;
/** /**
* 获取可匹配房间列表参数 * 获取可匹配房间列表参数
...@@ -358,6 +391,15 @@ export declare interface GetAvailableRoomsConfig { ...@@ -358,6 +391,15 @@ export declare interface GetAvailableRoomsConfig {
limit?: number; limit?: number;
} }
/**
* 自定义错误类
* @public
*/
export declare class GOBEError extends Error {
code: number;
constructor(code: number, message?: string);
}
/** /**
* 队伍类 * 队伍类
* @public * @public
...@@ -393,12 +435,6 @@ export declare class Group extends Base { ...@@ -393,12 +435,6 @@ export declare class Group extends Base {
off(cb: (serverEvent: ServerEvent) => void): void; off(cb: (serverEvent: ServerEvent) => void): void;
clear(): void; clear(): void;
}; };
onMatchComplete: {
(this: any, cb: (serverEvent: ServerEvent, room?: Room | undefined) => void): EventEmitter<(serverEvent: ServerEvent, room?: Room | undefined) => void>;
emit(serverEvent: ServerEvent, room?: Room | undefined): void;
off(cb: (serverEvent: ServerEvent, room?: Room | undefined) => void): void;
clear(): void;
};
private config; private config;
private _player; private _player;
private _client; private _client;
...@@ -442,7 +478,7 @@ export declare class Group extends Base { ...@@ -442,7 +478,7 @@ export declare class Group extends Base {
* 队伍 * 队伍
* @param config - 创建客户端参数 * @param config - 创建客户端参数
*/ */
constructor(client: Client, config: GroupInfo, playerConfig?: PlayerConfig); constructor(client: Client, config: GroupInfo);
/** /**
* 队伍信息查询 * 队伍信息查询
*/ */
...@@ -463,7 +499,8 @@ export declare class Group extends Base { ...@@ -463,7 +499,8 @@ export declare class Group extends Base {
*/ */
updateGroup(config: UpdateGroupConfig): Promise<void>; updateGroup(config: UpdateGroupConfig): Promise<void>;
private checkUpdatePermission; private checkUpdatePermission;
protected onServerEvent(serverEvent: ServerEvent): Promise<void>; protected onServerEventChange(serverEvent: ServerEvent): Promise<void>;
removeAllListeners(): void;
} }
/** /**
...@@ -492,35 +529,42 @@ export declare interface GroupInfo { ...@@ -492,35 +529,42 @@ export declare interface GroupInfo {
/** /**
* 组队匹配参数 * 组队匹配参数
* @param playerInfos - 带匹配规则的玩家信息列表 * @param playerInfos - 带匹配规则的玩家信息列表
* @param teamInfo - 带匹配规则队伍信息,非对称匹配场景必填,存放队伍参数
* @param matchCode - 匹配规则编号 * @param matchCode - 匹配规则编号
* @public
*/ */
declare interface MatchGroupConfig { export declare interface MatchGroupConfig {
playerInfos: MatchPlayerInfoParam[]; playerInfos: MatchPlayerInfoParam[];
teamInfo?: MatchTeamInfoParam;
matchCode: string; matchCode: string;
} }
/** /**
* 在线匹配参数 * 在线匹配参数
* @param playerInfo - 带匹配规则的玩家信息 * @param playerInfo - 带匹配规则的玩家信息
* @param teamInfo - 带匹配规则队伍信息,非对称匹配场景必填,存放队伍参数
* @param matchCode - 匹配规则编号 * @param matchCode - 匹配规则编号
* @public
*/ */
declare interface MatchPlayerConfig { export declare interface MatchPlayerConfig {
playerInfo: MatchPlayerInfoParam; playerInfo: MatchPlayerInfoParam;
teamInfo?: MatchTeamInfoParam;
matchCode: string; matchCode: string;
} }
/** /**
* 带匹配规则的玩家信息 * 带匹配规则的玩家信息
* @param matchParams - 自定义匹配参数,JSON对象格式 * @param matchParams - 自定义匹配参数
* @public
*/ */
declare interface MatchPlayerInfoParam { export declare interface MatchPlayerInfoParam {
playerId: string; playerId: string;
matchParams: Record<string, string>; matchParams: Record<string, number>;
} }
/** /**
* 房间匹配参数 * 房间匹配参数
* @param matchParams - 自定义匹配参数,JSON对象格式,最多支持5条匹配规则 * @param matchParams - 自定义匹配参数,最多支持5条匹配规则
* @param maxPlayers - 房间最大支持人数 * @param maxPlayers - 房间最大支持人数
* @param roomType - 房间类型 * @param roomType - 房间类型
* @param customRoomProperties - 自定义房间属性 * @param customRoomProperties - 自定义房间属性
...@@ -534,20 +578,27 @@ export declare interface MatchRoomConfig { ...@@ -534,20 +578,27 @@ export declare interface MatchRoomConfig {
customRoomProperties?: string; customRoomProperties?: string;
} }
/**
* 带匹配规则队伍信息,非对称匹配场景必填,存放队伍参数
* @param matchParams - 自定义匹配参数
* @public
*/
export declare interface MatchTeamInfoParam {
matchParams: Record<string, number>;
}
/** /**
* 玩家类 * 玩家类
* @public * @public
*/ */
export declare class Player extends Base { export declare class Player extends Base {
private _customStatus?; customStatus?: number;
private _customProperties?; customProperties?: string;
constructor(customStatus?: number, customProperties?: string); constructor(customStatus?: number, customProperties?: string);
get customPlayerStatus(): number | undefined;
get customPlayerProperties(): string | undefined;
/** /**
* 修改玩家自定义状态 * 更新玩家自定义状态
*/ */
setCustomStatus(status: number): Promise<Player>; updateCustomStatus(status: number): Promise<Player>;
} }
/** /**
...@@ -606,7 +657,7 @@ export declare const enum Protocol { ...@@ -606,7 +657,7 @@ export declare const enum Protocol {
* 基于「线性同余」的伪随机数生成器 * 基于「线性同余」的伪随机数生成器
* @public * @public
*/ */
export declare class Random { export declare class RandomUtils {
private mask; private mask;
private m; private m;
private a; private a;
...@@ -621,54 +672,67 @@ export declare class Random { ...@@ -621,54 +672,67 @@ export declare class Random {
*/ */
export declare class Room extends Base { export declare class Room extends Base {
onJoin: { onJoin: {
(this: any, cb: (player: FramePlayerInfo) => void): EventEmitter<(player: FramePlayerInfo) => void>; (this: any, cb: (player: FramePlayerInfo) => any): EventEmitter<(player: FramePlayerInfo) => any>;
emit(player: FramePlayerInfo): void; emit(player: FramePlayerInfo): void;
off(cb: (player: FramePlayerInfo) => void): void; off(cb: (player: FramePlayerInfo) => any): void;
clear(): void; clear(): void;
}; };
onLeave: { onLeave: {
(this: any, cb: (player: FramePlayerInfo) => void): EventEmitter<(player: FramePlayerInfo) => void>; (this: any, cb: (player: FramePlayerInfo) => any): EventEmitter<(player: FramePlayerInfo) => any>;
emit(player: FramePlayerInfo): void; emit(player: FramePlayerInfo): void;
off(cb: (player: FramePlayerInfo) => void): void; off(cb: (player: FramePlayerInfo) => any): void;
clear(): void; clear(): void;
}; };
onDismiss: { onDismiss: {
(this: any, cb: () => void): EventEmitter<() => void>; (this: any, cb: () => any): EventEmitter<() => any>;
emit(): void; emit(): void;
off(cb: () => void): void; off(cb: () => any): void;
clear(): void; clear(): void;
}; };
onDisconnect: { onDisconnect: {
(this: any, cb: (player: FramePlayerInfo) => void): EventEmitter<(player: FramePlayerInfo) => void>; (this: any, cb: (player: FramePlayerInfo, event?: CloseEvent | undefined) => any): EventEmitter<(player: FramePlayerInfo, event?: CloseEvent | undefined) => any>;
emit(player: FramePlayerInfo): void; emit(player: FramePlayerInfo, event?: CloseEvent | undefined): void;
off(cb: (player: FramePlayerInfo) => void): void; off(cb: (player: FramePlayerInfo, event?: CloseEvent | undefined) => any): void;
clear(): void; clear(): void;
}; };
onStartFrameSync: { onStartFrameSync: {
(this: any, cb: () => void): EventEmitter<() => void>; (this: any, cb: () => any): EventEmitter<() => any>;
emit(): void; emit(): void;
off(cb: () => void): void; off(cb: () => any): void;
clear(): void; clear(): void;
}; };
onStopFrameSync: { onStopFrameSync: {
(this: any, cb: () => void): EventEmitter<() => void>; (this: any, cb: () => any): EventEmitter<() => any>;
emit(): void; emit(): void;
off(cb: () => void): void; off(cb: () => any): void;
clear(): void; clear(): void;
}; };
onRecvFrame: { onRecvFrame: {
(this: any, cb: (msg: ServerFrameMessage | ServerFrameMessage[]) => void): EventEmitter<(msg: ServerFrameMessage | ServerFrameMessage[]) => void>; (this: any, cb: (msg: ServerFrameMessage | ServerFrameMessage[]) => any): EventEmitter<(msg: ServerFrameMessage | ServerFrameMessage[]) => any>;
emit(msg: ServerFrameMessage | ServerFrameMessage[]): void; emit(msg: ServerFrameMessage | ServerFrameMessage[]): void;
off(cb: (msg: ServerFrameMessage | ServerFrameMessage[]) => void): void; off(cb: (msg: ServerFrameMessage | ServerFrameMessage[]) => any): void;
clear(): void;
};
onRequestFrameError: {
(this: any, cb: (error: GOBEError) => any): EventEmitter<(error: GOBEError) => any>;
emit(error: GOBEError): void;
off(cb: (error: GOBEError) => any): void;
clear(): void; clear(): void;
}; };
private connection; private connection;
private config; private config;
private frameId; private frameId;
private readonly frameRequestMaxSize;
private frameRequesting;
private frameRequestSize;
private frameRequestList;
private autoFrameRequesting;
private autoFrameRequestCacheList;
private endpoint; private endpoint;
private heartbeatTimer; private wsHeartbeatTimer;
private _isSyncing; private _isSyncing;
private _player; private _player;
private _client;
/** /**
* 房间 ID * 房间 ID
*/ */
...@@ -725,12 +789,27 @@ export declare class Room extends Base { ...@@ -725,12 +789,27 @@ export declare class Room extends Base {
* 房间 * 房间
* @param config - 创建客户端参数 * @param config - 创建客户端参数
*/ */
constructor(config: RoomInfo, playerConfig?: PlayerConfig); constructor(client: Client, config: RoomInfo);
/** /**
* websocket 建链 * websocket 建链
* @param endpoint - 接入地址 * @param endpoint - 接入地址
*/ */
connect(routerAddr: string, ticket: string): void; connect(routerAddr: string, ticket: string): void;
/**
* 发送帧数据
* @param frameData - 帧数据
*/
sendFrame(frameData: string | string[]): void;
/**
* 请求补帧
* @param beginFrameId - 起始帧号
* @param size - 请求帧号
*/
requestFrame(beginFrameId: number, size: number): void;
/**
* 移除所有事件监听
*/
removeAllListeners(): void;
/** /**
* 重连 * 重连
*/ */
...@@ -743,17 +822,6 @@ export declare class Room extends Base { ...@@ -743,17 +822,6 @@ export declare class Room extends Base {
* 结束帧同步 * 结束帧同步
*/ */
stopFrameSync(): Promise<void>; stopFrameSync(): Promise<void>;
/**
* 发送帧数据
* @param frameData - 帧数据
*/
sendFrame(frameData: string | string[]): void;
/**
* 请求补帧
* @param beginFrameId - 起始帧号
* @param size - 请求帧号
*/
requestFrame(beginFrameId: number, size: number): void;
/** /**
* 玩家房间信息查询 * 玩家房间信息查询
*/ */
...@@ -768,23 +836,21 @@ export declare class Room extends Base { ...@@ -768,23 +836,21 @@ export declare class Room extends Base {
*/ */
dismiss(): Promise<void>; dismiss(): Promise<void>;
/** /**
* 踢人 * 移除房间内玩家
* @param playerId - 被踢出的玩家ID * @param playerId - 被移除出的玩家ID
* @remarks 房主才能踢人 * @remarks 只有房主有权限移除其他玩家
* @remarks 房间在帧同步中,不能踢人 * @remarks 房间在帧同步中,不能移除其他玩家
*/ */
kickPlayer(playerId: string): Promise<void>; removePlayer(playerId: string): Promise<void>;
/**
* 移除所有事件监听
*/
removeAllListeners(): void;
private onMessageCallback; private onMessageCallback;
private startHeartbeat; private clearRequestFrame;
private doHeartbeat; private startWSHeartbeat;
private stopHeartbeat; private doWSHeartbeat;
private stopWSHeartbeat;
private buildEndpoint; private buildEndpoint;
private checkIsInSync; private checkInSync;
private checkNotInSync; private checkNotInSync;
private checkNotInRequesting;
} }
/** /**
...@@ -831,9 +897,17 @@ export declare interface RouterInfo { ...@@ -831,9 +897,17 @@ export declare interface RouterInfo {
} }
/** /**
* 上报接口返回服务端事件 * 服务端 ACK 消息
* @param eventType - 事件类型 1:匹配开始;2:匹配成功;3:匹配失败;4:匹配取消;5:匹配超时; * @public
* 6:加入小队;7:离开小队;8:解散小队;9:更新小队; */
export declare interface ServerAckMessage {
rtnCode: number;
msg: string;
}
/**
* 服务端事件
* @param eventType - 1:匹配开始;2:匹配成功;3:匹配失败;4:匹配取消;5:匹配超时;6:加入小队;7:离开小队;8:解散小队;9:更新小队;
* @param eventParam - 事件相关信息 * @param eventParam - 事件相关信息
* @public * @public
*/ */
...@@ -842,7 +916,11 @@ export declare interface ServerEvent { ...@@ -842,7 +916,11 @@ export declare interface ServerEvent {
eventParam?: string; eventParam?: string;
} }
declare const enum ServerEventCode { /**
* 服务端事件码
* @public
*/
export declare const enum ServerEventCode {
DEFAULT = 0, DEFAULT = 0,
MATCH_START = 1, MATCH_START = 1,
MATCH_SUCCESS = 2, MATCH_SUCCESS = 2,
...@@ -865,7 +943,24 @@ export declare interface ServerFrameMessage { ...@@ -865,7 +943,24 @@ export declare interface ServerFrameMessage {
ext: FrameExtInfo; ext: FrameExtInfo;
} }
declare const enum StateCode { /**
* 初始化签名
* @param sign - 签名
* @param nonce - 随机正整数
* @param timestamp - 时间戳(秒)
* @public
*/
export declare interface Signature {
sign: string;
nonce: number;
timestamp: number;
}
/**
* SDK 状态码
* @public
*/
export declare const enum StateCode {
UNINITIALIZED = 0, UNINITIALIZED = 0,
INITIALIZED = 1, INITIALIZED = 1,
INROOM = 2, INROOM = 2,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
"type": "software", "type": "software",
"scope": "game", "scope": "game",
"name": "GameOBESDK", "name": "GameOBESDK",
"version": "1.1.4.301", "version": "1.1.5.300",
"playbook": "" "playbook": ""
} }
\ No newline at end of file
...@@ -64,7 +64,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -64,7 +64,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
console.log("房主"); console.log("房主");
this.isRoomOwner = true; this.isRoomOwner = true;
this.server = new GameServer(2, this.networkHelper); this.server = new GameServer(2, this.networkHelper);
await this.networkHelper.startFrameSync();
this.server.addPlayer({ this.server.addPlayer({
uuid: this.playerId, uuid: this.playerId,
isAI: false, isAI: false,
...@@ -647,8 +646,10 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -647,8 +646,10 @@ export default class SceneComponent extends MyCocosSceneComponent {
dataList.push(frameData); dataList.push(frameData);
} }
dataList.forEach(async (item) => { dataList.forEach(async (item) => {
console.log(item); if (!item.data) {
return; return;
}
console.log(item);
if (this.server) { if (this.server) {
this.server.onFrameEvent(item.data); this.server.onFrameEvent(item.data);
......
...@@ -33,7 +33,6 @@ export class NetworkHelper { ...@@ -33,7 +33,6 @@ export class NetworkHelper {
appId: '105879509',// 应用的ID appId: '105879509',// 应用的ID
openId: 'op_16' + Math.random(),// 玩家ID openId: 'op_16' + Math.random(),// 玩家ID
}); });
console.log(this.client);
await this.client.init(); await this.client.init();
} }
...@@ -41,41 +40,24 @@ export class NetworkHelper { ...@@ -41,41 +40,24 @@ export class NetworkHelper {
async joinRoom(roomType: string, maxPlayers: number) { async joinRoom(roomType: string, maxPlayers: number) {
const matchRoomConfig = { const matchRoomConfig = {
matchParams: { matchParams: {
matchRule: '', param1: 'param1',
matchRule2: '',
}, },
roomType: roomType, roomType: roomType,
customRoomProperties: '',
maxPlayers: maxPlayers, maxPlayers: maxPlayers,
}; };
// 在线匹配参数 this.room = await this.client.matchRoom(matchRoomConfig);
const matchPlayerConfig = {
playerInfo: {
playerId: 'xxxxxx',
matchParams: {
test: 1, // 匹配参数的属性需要与matchCode所属规则中配置的属性对应
},
},
matchCode: 'cc4dc617d3aa4f849824a12c3e8290c8',
};
const playerConfig = {
customPlayerStatus: 0,
customPlayerProperties: ''
};
console.log('汪汪汪1');
// this.room = await this.client.createRoom(matchRoomConfig, playerConfig);
this.room = await this.client.matchPlayer(matchPlayerConfig, playerConfig);
console.log('汪汪汪2' + JSON.stringify(this.room));
this.player = this.room.player; this.player = this.room.player;
console.log('汪汪汪: ' + this.room.players.length);
await this.startFrameSync();
console.log('汪汪汪2');
this.room.onJoin(this.onJoinRoom.bind(this)); this.room.onJoin(this.onJoinRoom.bind(this));
this.room.onLeave(this.onLeaveRoom.bind(this)); this.room.onLeave(this.onLeaveRoom.bind(this));
this.room.onStartFrameSync(this.onStartFrameSync.bind(this)); // this.room.onStartFrameSync(this.onStartFrameSync.bind(this));
this.room.onStopFrameSync(this.onStopFrameSync.bind(this)); this.room.onStopFrameSync(this.onStopFrameSync.bind(this));
this.room.onRecvFrame(this.onRecvFrame.bind(this)); this.room.onRecvFrame(this.onRecvFrame.bind(this));
this.room.onDisconnect(this.onDisconnect.bind(this)); this.room.onDisconnect(this.onDisconnect.bind(this));
this.room.onDismiss(this.onDisconnect.bind(this)); this.room.onDismiss(this.onDisconnect.bind(this));
console.log('汪汪汪3' + this.player.playerId);
return this.player.playerId; return this.player.playerId;
} }
...@@ -92,30 +74,32 @@ export class NetworkHelper { ...@@ -92,30 +74,32 @@ export class NetworkHelper {
} }
} }
closeRoom() { async closeRoom() {
return; await this.client.cancelMatch();
}
async startFrameSync() {
await this.room.startFrameSync();
// return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
// this.room.changeRoom({ isForbidJoin: true }, (event) => { // this.room.onStartFrameSync(() => {
// console.log("关门"); // cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
// // console.log(JSON.stringify(event))
// resolve(null); // resolve(null);
// }); // });
// }); // });
} }
async startFrameSync() {
await this.room.startFrameSync();
cc.log("开始帧同步成功,请到控制台查看具体帧同步信息");
}
async stopFrameSync() { async stopFrameSync() {
await this.room.stopFrameSync(); await this.room.stopFrameSync();
console.log("停止帧同步成功"); console.log("停止帧同步成功");
} }
sendFrame(data: any) { sendFrame(data: any) {
console.log('sendFrame: ' + JSON.stringify(data)); console.warn('sendFrame: ' + JSON.stringify(data));
if (this.room._isSyncing) {
this.room.sendFrame(JSON.stringify(data)); this.room.sendFrame(JSON.stringify(data));
} else {
console.log('is syncing = false');
this.onRecvFrame({data});
}
} }
onJoinRoom(event) { onJoinRoom(event) {
...@@ -123,6 +107,9 @@ export class NetworkHelper { ...@@ -123,6 +107,9 @@ export class NetworkHelper {
this._eventListeners["playerJoin"](event); this._eventListeners["playerJoin"](event);
} }
console.log("新玩家加入", JSON.stringify(event)); console.log("新玩家加入", JSON.stringify(event));
this.room.players.forEach(player => {
console.log('player.playerId = ', player.playerId);
});
} }
onLeaveRoom(event) { onLeaveRoom(event) {
console.log("onLeaveRoom"); console.log("onLeaveRoom");
...@@ -146,6 +133,7 @@ export class NetworkHelper { ...@@ -146,6 +133,7 @@ export class NetworkHelper {
} }
} }
onStartFrameSync(event) { onStartFrameSync(event) {
console.log('onStartFrameSync');
if (this._eventListeners["gameStart"]) { if (this._eventListeners["gameStart"]) {
this._eventListeners["gameStart"](event); this._eventListeners["gameStart"](event);
} }
......
...@@ -37,11 +37,12 @@ export class GameServer { ...@@ -37,11 +37,12 @@ export class GameServer {
this.headUrls = this.headUrls.sort((a, b) => RandomInt(2) - 1); this.headUrls = this.headUrls.sort((a, b) => RandomInt(2) - 1);
this.nameList = this.nameList.sort((a, b) => RandomInt(2) - 1); this.nameList = this.nameList.sort((a, b) => RandomInt(2) - 1);
this.networkHelper.startFrameSync();
} }
async addPlayer(player) { async addPlayer(player) {
if (this.playerList.find(p => p.uuid == player.uuid)) {
return;
}
console.log("addPlayer"); console.log("addPlayer");
this.playerList.push({ this.playerList.push({
name: this.nameList.splice(0, 1)[0], name: this.nameList.splice(0, 1)[0],
...@@ -206,7 +207,7 @@ export class GameServer { ...@@ -206,7 +207,7 @@ export class GameServer {
startAI() { startAI() {
this.playerList this.playerList
.filter((player) => player.isAI == true) .filter((player) => player.isAI == true)
.forEach((player) => {}); .forEach((player) => { });
} }
_timeoutIds = null; _timeoutIds = null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment