@@ -2,13 +2,16 @@ import { GameBase, IAPGameState, IClickResult, IIndividualState, IScores, IStatu
22import { APGamesInformation } from "../schemas/gameinfo" ;
33import { APRenderRep , AreaPieces , Glyph , RowCol } from "@abstractplay/renderer/src/schemas/schema" ;
44import { APMoveResult } from "../schemas/moveresults" ;
5- import { reviver , UserFacingError } from "../common" ;
5+ import { reviver , replacer , UserFacingError } from "../common" ;
66import i18next from "i18next" ;
77import { Card , Deck , cardSortAsc , cardsBasic , cardsExtended } from "../common/decktet" ;
88import { BiscuitBoard } from "./biscuit/board" ;
99import { BiscuitCard } from "./biscuit/card" ;
1010// eslint-disable-next-line @typescript-eslint/no-require-imports
1111const deepclone = require ( "rfdc/default" ) ;
12+ // eslint-disable-next-line @typescript-eslint/no-require-imports
13+ const Buffer = require ( 'buffer/' ) . Buffer // note: the trailing slash is important!
14+ import pako , { Data } from "pako" ;
1215
1316export type playerid = 1 | 2 | 3 ;
1417
@@ -188,7 +191,14 @@ export class BiscuitGame extends GameBase {
188191 this . stack = [ fresh ] ;
189192 } else {
190193 if ( typeof state === "string" ) {
191- state = JSON . parse ( state , reviver ) as IBiscuitState ;
194+ // is the state a raw JSON obj
195+ if ( state . startsWith ( "{" ) ) {
196+ state = JSON . parse ( state , reviver ) as IBiscuitState ;
197+ } else {
198+ const decoded = Buffer . from ( state , "base64" ) as Data ;
199+ const decompressed = pako . ungzip ( decoded , { to : "string" } ) ;
200+ state = JSON . parse ( decompressed , reviver ) as IBiscuitState ;
201+ }
192202 }
193203 if ( state . game !== BiscuitGame . gameinfo . uid ) {
194204 throw new Error ( `The Biscuit engine cannot process a game of '${ state . game } '.` ) ;
@@ -1201,6 +1211,14 @@ export class BiscuitGame extends GameBase {
12011211 return resolved ;
12021212 }
12031213
1214+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1215+ public serialize ( opts ?: { strip ?: boolean , player ?: number } ) : string {
1216+ const json = JSON . stringify ( this . state ( ) , replacer ) ;
1217+ const compressed = pako . gzip ( json ) ;
1218+
1219+ return Buffer . from ( compressed ) . toString ( "base64" ) as string ;
1220+ }
1221+
12041222 public clone ( ) : BiscuitGame {
12051223 return Object . assign ( new BiscuitGame ( this . numplayers ) , deepclone ( this ) as BiscuitGame ) ;
12061224 }
0 commit comments