<commit_before>// @flow
/* global window, fetch */

import {appConst} from '../const';
import type {BuildingAttrTypeType, BuildingType, MapType, UnitType} from '../maps/type';
import {mapGuide} from '../maps/map-guide';
import type {PathType} from '../component/game/model/unit/path-master';
import type {AttackResultUnitType} from '../component/game/model/helper';
import {isOnLineRoomType} from '../component/game/model/helper';
import {localGet, localPost, localServerUrl} from './server-local-api';
import {localSocketIoClient} from './socket-local';
import {isString} from '../lib/is/is';
import {socket} from './socket';
import type {SocketMessageType} from './socket';
import isEqual from 'lodash/isEqual';

const {api} = appConst;
const {url} = api;

type ServerApiErrorType = {
    +error: {
        +id: string | number,
        +message: string,
    },
};

export type ServerUserType = {|
    socketId: string,
    userId: string,
    teamId?: string,
    type: 'human' | 'bot',
|};

export type RoomTypeType = 'on-line' | 'off-line';

export type CreateRoomType = {
    roomId: string,
};

const headers = {Accept: 'application/json', 'Content-Type': 'application/json'};

export function createRoom(): Promise<CreateRoomType> {
    if (isOnLineRoomType()) {
        return fetch(url + '/api/room/create')
            .then((blob: Response): Promise<CreateRoomType> => blob.json())
            .then(
                (result: CreateRoomType): CreateRoomType => ({
                    roomId: isString(result.roomId) ? result.roomId : '',
                })
            )
            .catch(
                (error: Error): CreateRoomType => {
                    console.error(error);
                    return {roomId: ''};
                }
            );
    }

    return localGet(localServerUrl + '/api/room/create').then((result: string): CreateRoomType => JSON.parse(result));
}

export type JoinRoomType = {
    roomId: string,
};

export function joinRoom(roomId: string, userId: string, socketId: string): Promise<JoinRoomType> {
    if (isOnLineRoomType()) {
        return fetch(url + '/api/room/join/' + [roomId, userId, socketId].join('/'))
            .then((blob: Response): Promise<JoinRoomType> => blob.json())
            .then(
                (result: JoinRoomType): JoinRoomType => ({
                    roomId: isString(result.roomId) ? result.roomId : '',
                })
            )
            .catch(
                (error: Error): JoinRoomType => {
                    console.error(error);
                    return {roomId: ''};
                }
            );
    }

    return localGet(localServerUrl + '/api/room/join/' + [roomId, userId, localSocketIoClient.id].join('/')).then(
        (result: string): JoinRoomType => JSON.parse(result)
    );
}

export function makeUser(type: 'human' | 'bot', roomId: string): Promise<JoinRoomType> {
    if (isOnLineRoomType()) {
        return fetch(url + '/api/room/make/' + type + '/' + roomId)
            .then((blob: Response): Promise<JoinRoomType> => blob.json())
            .then(
                (result: JoinRoomType): JoinRoomType => ({
                    roomId: isString(result.roomId) ? result.roomId : '',
                })
            );
    }

    return localGet(localServerUrl + '/api/room/make/' + type + '/' + roomId).then(
        (result: string): JoinRoomType => JSON.parse(result)
    );
}

export type LeaveRoomType = {
    roomId: string,
};

export function leaveRoom(roomId: string, userId: string): Promise<LeaveRoomType> {
    if (isOnLineRoomType()) {
        return fetch(url + '/api/room/leave/' + [roomId, userId].join('/'))
            .then((blob: Response): Promise<LeaveRoomType | ServerApiErrorType> => blob.json())
            .then(
                (result: LeaveRoomType | ServerApiErrorType): LeaveRoomType => {
                    if (typeof result.roomId === 'string') {
                        return {
                            roomId: result.roomId,
                        };
                    }

                    return {
                        roomId: '',
                    };
                }
            )
            .catch(
                (error: Error): LeaveRoomType => {
                    console.error(error);

                    return {
                        roomId: '',
                    };
                }
            );
    }

    return localGet(localServerUrl + '/api/room/leave/' + [roomId, userId].join('/')).then(
        (result: string): LeaveRoomType => JSON.parse(result)<commit_msg>bot - refactor<commit_after>// @flow
/* global window, fetch */

import {appConst} from '../const';
import type {BuildingAttrTypeType, BuildingType, MapType, UnitType} from '../maps/type';
import {mapGuide} from '../maps/map-guide';
import type {PathType} from '../component/game/model/unit/path-master';
import type {AttackResultUnitType} from '../component/game/model/helper';
import {isOnLineRoomType} from '../component/game/model/helper';
import {localGet, localPost, localServerUrl} from './server-local-api';
import {localSocketIoClient} from './socket-local';
import {isString} from '../lib/is/is';
import {socket} from './socket';
import type {SocketMessageType} from './socket';
import isEqual from 'lodash/isEqual';

const {api} = appConst;
const {url} = api;

type ServerApiErrorType = {
    +error: {
        +id: string | number,
        +message: string,
    },
};

export type ServerUserType = {|
    socketId: string,
    userId: string,
    teamId?: string,
    type: 'human' | 'bot',
|};

export type RoomTypeType = 'on-line' | 'off-line';

export type CreateRoomType = {
    roomId: string,
};

const headers = {Accept: 'application/json', 'Content-Type': 'application/json'};

export function createRoom(): Promise<CreateRoomType> {
    if (isOnLineRoomType()) {
        return fetch(url + '/api/room/create')
            .then((blob: Response): Promise<CreateRoomType> => blob.json())
            .then(
                (result: CreateRoomType): CreateRoomType => ({
                    roomId: isString(result.roomId) ? result.roomId : '',
                })
            )
            .catch(
                (error: Error): CreateRoomType => {
                    console.error(error);
                    return {roomId: ''};
                }
            );
    }

    return localGet(localServerUrl + '/api/room/create').then((result: string): CreateRoomType => JSON.parse(result));
}

export type JoinRoomType = {
    roomId: string,
};

export function joinRoom(roomId: string, userId: string, socketId: string): Promise<JoinRoomType> {
    if (isOnLineRoomType()) {
        return fetch(url + '/api/room/join/' + [roomId, userId, socketId].join('/'))
            .then((blob: Response): Promise<JoinRoomType> => blob.json())
            .then(
                (result: JoinRoomType): JoinRoomType => ({
                    roomId: isString(result.roomId) ? result.roomId : '',
                })
            )
            .catch(
                (error: Error): JoinRoomType => {
                    console.error(error);
                    return {roomId: ''};
                }
            );
    }

    return localGet(localServerUrl + '/api/room/join/' + [roomId, userId, localSocketIoClient.id].join('/')).then(
        (result: string): JoinRoomType => JSON.parse(result)
    );
}

export function makeUser(type: 'human' | 'bot', roomId: string): Promise<JoinRoomType> {
    if (isOnLineRoomType()) {
        return fetch(url + '/api/room/make/' + type + '/' + roomId)
            .then((blob: Response): Promise<JoinRoomType> => blob.json())
            .then(
                (result: JoinRoomType): JoinRoomType => ({
                    roomId: isString(result.roomId) ? result.roomId : '',
                })
            );
    }

    return localGet(localServerUrl + '/api/room/make/' + type + '/' + roomId).then(
        (result: string): JoinRoomType => JSON.parse(result)
    );
}

export type LeaveRoomType = {
    roomId: string,
};

export function leaveRoom(roomId: string, userId: string): Promise<LeaveRoomType> {
    if (isOnLineRoomType()) {
        return fetch(url + '/api/room/leave/' + [roomId, userId].join('/'))
            .then((blob: Response): Promise<LeaveRoomType | ServerApiErrorType> => blob.json())
            .then(
                (result: LeaveRoomType | ServerApiErrorType): LeaveRoomType => {
                    if (typeof result.roomId === 'string') {
                        return {
                            roomId: result.roomId,
                        };
                    }

                    return {
                        roomId: '',
                    };
                }
            )
            .catch(
                (error: Error): LeaveRoomType => {
                    console.error(error);

                    return {
                        roomId: '',
                    };
                }
            );
    }

    return localGet(localServerUrl + '/api/room/leave/' + [roomId, userId].join('/')).then(
        (result: string): LeaveRoomType => {
            const parsedResult = JSON.parse(result);

            if (typeof parsedResult.roomId === 'string') {
                return {
                    roomId: parsedResult.roomId,
                };
            }

            return {
                roomId: '',
            };
        }
{'fraction_non_alphanumeric': 0.14735503021308174, 'fraction_numerical': 0.0, 'mean_word_length': 1.8727161997563946, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4794948', 'n_tokens_mistral': 2657, 'n_tokens_neox': 2583, 'n_words': 563}