import { app } from 'electron' ; import { AxiosResponse , Request } from 'root/app/request' ; import { asMap , asString } from 'root/lib/type_utils' ; import { isMac } from 'root/lib/utils' ; import { UserRequest , UserResult } from 'root/models/userbytokenid' ; export interface TokenCheckRes { uid : string ; token : string ; nick : string ; } function parseTokenCheckRes ( data ) { try { return data as TokenCheckRes ; } catch ( _ ) { return undefined ; } } export async function tokencheck ( request ) { return parseTokenCheckRes ( await Request . post < { request : string ; } > ( `/mtg/donew2.php?cmd=cm_tokencheck&version=${ app . getVersion ( ) }${ isMac ( ) ? 'm' : 'w' }` , { request , } ) ) ; } export interface TokenRequestRes { [ index ] : string ; } function parseTokenRequestRes ( data ) { return data as TokenRequestRes ; } export async function tokenrequest ( { currentMtgaNick , currentMtgaID , } ) { return parseTokenRequestRes ( await Request . post ( `/mtg/donew2.php?cmd=cm_tokenrequest&version=${ app . getVersion ( ) }${ isMac ( ) ? 'm' : 'w' }` , { mtgaid : currentMtgaNick , mtgaplid : currentMtgaID , } ) ) ; } function parseUserResult ( data ) { const dataMap = asMap ( data ) ; if ( dataMap === undefined ) { throw new Error ( `Invalid UserResult, response is not an object (got ${ typeof data })` ) ; } const status = asString ( dataMap . status ) ; const dataString = asString ( dataMap . data ) ; if ( status === undefined ) { throw new Error ( `Invalid UserResult, status is attribute is not a string (got ${ dataMap . status })` ) ; } if ( dataString === undefined ) { throw new Error ( `Invalid UserResult, dataString is attribute is not a string (got ${ dataMap . dataString })` ) ; } return { status , data : dataString } ; } export async function userbytokenid ( tokenid ) { const usertime = ( ( - 1 * new Date ( ) . getTimezoneOffset ( ) ) / 60 ) . toString ( ) ; return parseUserResult ( await Request . post < UserRequest > ( `/mtg/donew2.php?cmd=cm_userbytokenid&version=${ app . getVersion ( ) }${ isMac ( ) ? 'm' : 'w' }` , { cm_userbytokenid : tokenid , usertime , } ) ) ; } export interface UserData { mtgaId : string ; mtgaNick : string ; token : string ; } export async function setuserdata ( userData ) { const usertime = ( ( - 1 * new Date ( ) . getTimezoneOffset ( ) ) / 60 ) . toString ( ) ; return parseUserResult ( await Request . post < UserData & { usertime : string ; } > ( `/mtg/donew2.php?cmd=cm_setuserdata&version=${ app . getVersion ( ) }${ isMac ( ) ? 'm' : 'w' }` , { ... userData , usertime , } ) ) ; }