UNPKG

rune-sdk

Version:

Build a multiplayer game played by millions! Your game runs inside the Rune app with 10 million installs across [iOS](https://apps.apple.com/app/rune-games-and-voice-chat/id1450358364) and [Android](https://play.google.com/store/apps/details?id=ai.rune.ti

10 lines (8 loc) 15.5 kB
/* Copyright (c) 2024 Rune AI Inc. All rights reserved. This code is proprietary to Rune AI Inc. The code may be used solely for accessing the Service provided by Rune AI Inc. following the Rune AI Inc. Terms of Service ("Terms") accessible at rune.ai/eula. You may not use this code for any use or purpose other than as expressly permitted by the Terms. Restrictions set forth in the Terms include, but is not limited to, that you may not copy, adapt, modify, prepare derivative works based upon, distribute, license, sell, transfer, publicly display, publicly perform, transmit, stream, broadcast, attempt to discover any source code, reverse engineer, decompile, dissemble, or otherwise exploit the code as a whole or any portion of the code. */ "use strict";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){if(!e)throw t instanceof Error?t:new Error(null!=t?t:"Assertion failed")}function r(e){if("object"!=typeof e||null===e)return!1;var t=Object.getPrototypeOf(e);return!(null!==t&&t!==Object.prototype&&null!==Object.getPrototypeOf(t)||Symbol.toStringTag in e||Symbol.iterator in e)}function i(e,i){var n,o;if(void 0!==e){if(t("object"==typeof e&&null!==e&&r(e),"must be an object"),t("players"in e||"everyone"in e,"must have players or everyone property"),t(!("players"in e&&"everyone"in e),"players and everyone option are not allowed at the same time"),"players"in e){t("object"==typeof e.players&&null!==e.players&&r(e.players),"players must be an object"),t(Object.keys(e.players).length>0,"players must not be empty"),t(Object.keys(e.players).sort().toString()===i.sort().toString(),"all game players must be mentioned in the players object");var s=Object.values(e.players),a=0;try{for(var f=function(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],i=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&i>=e.length&&(e=void 0),{value:e&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(s),u=f.next();!u.done;u=f.next()){var c=u.value;t("number"==typeof c&&!isNaN(c)&&Number.isInteger(c)&&c>=0||["WON","LOST","TIE"].includes(c),"only number >= 0, WON, LOST, TIE are allowed as player results"),"TIE"===c&&a++}}catch(e){n={error:e}}finally{try{u&&!u.done&&(o=f.return)&&o.call(f)}finally{if(n)throw n.error}}t(1!=a,"There must be no TIE or at least 2 TIEs in the game."),t(s.every((function(e){return["WON","LOST","TIE"].includes(e)}))||s.every((function(e){return"number"==typeof e})),"all players must have either WON, LOST, TIE or all players must have a score")}else t("number"==typeof e.everyone&&!isNaN(e.everyone)&&Number.isInteger(e.everyone)&&e.everyone>=0||["WON","LOST","TIE"].includes(e.everyone),"only number >= 0, WON, LOST, TIE are allowed as everyone value"),"TIE"===e.everyone&&t(i.length>1,"There must be at least 2 players in the game to have a TIE");"delayPopup"in e&&t("boolean"==typeof e.delayPopup,"delayPopup must be a boolean"),"minimizePopUp"in e&&t("boolean"==typeof e.minimizePopUp,"minimizePopUp must be a boolean")}}var n,o,s;function a(e,t,r){var i=Math.floor(r/4294967296),n=r;e.setUint32(t,i),e.setUint32(t+4,n)}var f=("undefined"==typeof process||"never"!==(null===(n=null===process||void 0===process?void 0:process.env)||void 0===n?void 0:n.TEXT_ENCODING))&&"undefined"!=typeof TextEncoder&&"undefined"!=typeof TextDecoder;function u(e){for(var t=e.length,r=0,i=0;i<t;){var n=e.charCodeAt(i++);if(0!=(4294967168&n))if(0==(4294965248&n))r+=2;else{if(n>=55296&&n<=56319&&i<t){var o=e.charCodeAt(i);56320==(64512&o)&&(++i,n=((1023&n)<<10)+(1023&o)+65536)}r+=0==(4294901760&n)?3:4}else r++}return r}var c=f?new TextEncoder:void 0,h=f?"undefined"!=typeof process&&"force"!==(null===(o=null===process||void 0===process?void 0:process.env)||void 0===o?void 0:o.TEXT_ENCODING)?200:0:4294967295;var l=(null==c?void 0:c.encodeInto)?function(e,t,r){c.encodeInto(e,t.subarray(r))}:function(e,t,r){t.set(c.encode(e),r)};f&&new TextDecoder,!f||"undefined"!=typeof process&&(null===(s=null===process||void 0===process?void 0:process.env)||void 0===s||s.TEXT_DECODER);var p,y=function(e,t){this.type=e,this.data=t},d=(p=function(e,t){return p=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},p(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}p(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}),w=function(e){function t(r){var i=e.call(this,r)||this,n=Object.create(t.prototype);return Object.setPrototypeOf(i,n),Object.defineProperty(i,"name",{configurable:!0,enumerable:!1,value:t.name}),i}return d(t,e),t}(Error);var v={type:-1,encode:function(e){var t,r,i,n;return e instanceof Date?function(e){var t,r=e.sec,i=e.nsec;if(r>=0&&i>=0&&r<=17179869183){if(0===i&&r<=4294967295){var n=new Uint8Array(4);return(t=new DataView(n.buffer)).setUint32(0,r),n}var o=r/4294967296,s=4294967295&r;return n=new Uint8Array(8),(t=new DataView(n.buffer)).setUint32(0,i<<2|3&o),t.setUint32(4,s),n}return n=new Uint8Array(12),(t=new DataView(n.buffer)).setUint32(0,i),a(t,4,r),n}((t=e.getTime(),r=Math.floor(t/1e3),i=1e6*(t-1e3*r),n=Math.floor(i/1e9),{sec:r+n,nsec:i-1e9*n})):null},decode:function(e){var t=function(e){var t=new DataView(e.buffer,e.byteOffset,e.byteLength);switch(e.byteLength){case 4:return{sec:i=t.getUint32(0),nsec:0};case 8:var r=t.getUint32(0);return{sec:i=4294967296*(3&r)+t.getUint32(4),nsec:r>>>2};case 12:var i=function(e,t){return 4294967296*e.getInt32(t)+e.getUint32(t+4)}(t,4);return{sec:i,nsec:t.getUint32(0)};default:throw new w("Unrecognized data size for timestamp (expected 4, 8, or 12): ".concat(e.length))}}(e);return new Date(1e3*t.sec+t.nsec/1e6)}},b=function(){function e(){this.builtInEncoders=[],this.builtInDecoders=[],this.encoders=[],this.decoders=[],this.register(v)}return e.prototype.register=function(e){var t=e.type,r=e.encode,i=e.decode;if(t>=0)this.encoders[t]=r,this.decoders[t]=i;else{var n=1+t;this.builtInEncoders[n]=r,this.builtInDecoders[n]=i}},e.prototype.tryToEncode=function(e,t){for(var r=0;r<this.builtInEncoders.length;r++){if(null!=(i=this.builtInEncoders[r]))if(null!=(n=i(e,t)))return new y(-1-r,n)}for(r=0;r<this.encoders.length;r++){var i,n;if(null!=(i=this.encoders[r]))if(null!=(n=i(e,t)))return new y(r,n)}return e instanceof y?e:null},e.prototype.decode=function(e,t,r){var i=t<0?this.builtInDecoders[-1-t]:this.decoders[t];return i?i(e,t,r):new y(t,e)},e.defaultCodec=new e,e}();var U=function(){function t(e,t,r,i,n,o,s,a){void 0===e&&(e=b.defaultCodec),void 0===t&&(t=void 0),void 0===r&&(r=100),void 0===i&&(i=2048),void 0===n&&(n=!1),void 0===o&&(o=!1),void 0===s&&(s=!1),void 0===a&&(a=!1),this.extensionCodec=e,this.context=t,this.maxDepth=r,this.initialBufferSize=i,this.sortKeys=n,this.forceFloat32=o,this.ignoreUndefined=s,this.forceIntegerToFloat=a,this.pos=0,this.view=new DataView(new ArrayBuffer(this.initialBufferSize)),this.bytes=new Uint8Array(this.view.buffer)}return t.prototype.reinitializeState=function(){this.pos=0},t.prototype.encodeSharedRef=function(e){return this.reinitializeState(),this.doEncode(e,1),this.bytes.subarray(0,this.pos)},t.prototype.encode=function(e){return this.reinitializeState(),this.doEncode(e,1),this.bytes.slice(0,this.pos)},t.prototype.doEncode=function(e,t){if(t>this.maxDepth)throw new Error("Too deep objects in depth ".concat(t));null===e?this.encodeNil():void 0===e?this.encodeUndefined():"boolean"==typeof e?this.encodeBoolean(e):"number"==typeof e?this.encodeNumber(e):"string"==typeof e?this.encodeString(e):this.encodeObject(e,t)},t.prototype.ensureBufferSizeToWrite=function(e){var t=this.pos+e;this.view.byteLength<t&&this.resizeBuffer(2*t)},t.prototype.resizeBuffer=function(e){var t=new ArrayBuffer(e),r=new Uint8Array(t),i=new DataView(t);r.set(this.bytes),this.view=i,this.bytes=r},t.prototype.encodeNil=function(){this.writeU8(192)},t.prototype.encodeUndefined=function(){this.writeU8(193)},t.prototype.encodeBoolean=function(e){!1===e?this.writeU8(194):this.writeU8(195)},t.prototype.encodeNumber=function(e){Number.isSafeInteger(e)&&!this.forceIntegerToFloat?e>=0?e<128?this.writeU8(e):e<256?(this.writeU8(204),this.writeU8(e)):e<65536?(this.writeU8(205),this.writeU16(e)):e<4294967296?(this.writeU8(206),this.writeU32(e)):(this.writeU8(207),this.writeU64(e)):e>=-32?this.writeU8(224|e+32):e>=-128?(this.writeU8(208),this.writeI8(e)):e>=-32768?(this.writeU8(209),this.writeI16(e)):e>=-2147483648?(this.writeU8(210),this.writeI32(e)):(this.writeU8(211),this.writeI64(e)):this.forceFloat32?(this.writeU8(202),this.writeF32(e)):(this.writeU8(203),this.writeF64(e))},t.prototype.writeStringHeader=function(e){if(e<32)this.writeU8(160+e);else if(e<256)this.writeU8(217),this.writeU8(e);else if(e<65536)this.writeU8(218),this.writeU16(e);else{if(!(e<4294967296))throw new Error("Too long string: ".concat(e," bytes in UTF-8"));this.writeU8(219),this.writeU32(e)}},t.prototype.encodeString=function(e){if(e.length>h){var t=u(e);this.ensureBufferSizeToWrite(5+t),this.writeStringHeader(t),l(e,this.bytes,this.pos),this.pos+=t}else{t=u(e);this.ensureBufferSizeToWrite(5+t),this.writeStringHeader(t),function(e,t,r){for(var i=e.length,n=r,o=0;o<i;){var s=e.charCodeAt(o++);if(0!=(4294967168&s)){if(0==(4294965248&s))t[n++]=s>>6&31|192;else{if(s>=55296&&s<=56319&&o<i){var a=e.charCodeAt(o);56320==(64512&a)&&(++o,s=((1023&s)<<10)+(1023&a)+65536)}0==(4294901760&s)?(t[n++]=s>>12&15|224,t[n++]=s>>6&63|128):(t[n++]=s>>18&7|240,t[n++]=s>>12&63|128,t[n++]=s>>6&63|128)}t[n++]=63&s|128}else t[n++]=s}}(e,this.bytes,this.pos),this.pos+=t}},t.prototype.encodeObject=function(t,r){var i=this.extensionCodec.tryToEncode(t,this.context);if(null!=i)this.encodeExtension(i);else if(Array.isArray(t))this.encodeArray(t,r);else if(ArrayBuffer.isView(t))this.encodeBinary(t);else{if("object"!==e(t))throw new Error("Unrecognized object: ".concat(Object.prototype.toString.apply(t)));this.encodeMap(t,r)}},t.prototype.encodeBinary=function(e){var t=e.byteLength;if(t<256)this.writeU8(196),this.writeU8(t);else if(t<65536)this.writeU8(197),this.writeU16(t);else{if(!(t<4294967296))throw new Error("Too large binary: ".concat(t));this.writeU8(198),this.writeU32(t)}var r,i=(r=e)instanceof Uint8Array?r:ArrayBuffer.isView(r)?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):r instanceof ArrayBuffer?new Uint8Array(r):Uint8Array.from(r);this.writeU8a(i)},t.prototype.encodeArray=function(e,t){var r=e.length;if(r<16)this.writeU8(144+r);else if(r<65536)this.writeU8(220),this.writeU16(r);else{if(!(r<4294967296))throw new Error("Too large array: ".concat(r));this.writeU8(221),this.writeU32(r)}for(var i=0,n=e;i<n.length;i++){var o=n[i];this.doEncode(o,t+1)}},t.prototype.countWithoutUndefined=function(e,t){for(var r=0,i=0,n=t;i<n.length;i++){void 0!==e[n[i]]&&r++}return r},t.prototype.encodeMap=function(e,t){var r=Object.keys(e);this.sortKeys&&r.sort();var i=this.ignoreUndefined?this.countWithoutUndefined(e,r):r.length;if(i<16)this.writeU8(128+i);else if(i<65536)this.writeU8(222),this.writeU16(i);else{if(!(i<4294967296))throw new Error("Too large map object: ".concat(i));this.writeU8(223),this.writeU32(i)}for(var n=0,o=r;n<o.length;n++){var s=o[n],a=e[s];this.ignoreUndefined&&void 0===a||(this.encodeString(s),this.doEncode(a,t+1))}},t.prototype.encodeExtension=function(e){var t=e.data.length;if(1===t)this.writeU8(212);else if(2===t)this.writeU8(213);else if(4===t)this.writeU8(214);else if(8===t)this.writeU8(215);else if(16===t)this.writeU8(216);else if(t<256)this.writeU8(199),this.writeU8(t);else if(t<65536)this.writeU8(200),this.writeU16(t);else{if(!(t<4294967296))throw new Error("Too large extension object: ".concat(t));this.writeU8(201),this.writeU32(t)}this.writeI8(e.type),this.writeU8a(e.data)},t.prototype.writeU8=function(e){this.ensureBufferSizeToWrite(1),this.view.setUint8(this.pos,e),this.pos++},t.prototype.writeU8a=function(e){var t=e.length;this.ensureBufferSizeToWrite(t),this.bytes.set(e,this.pos),this.pos+=t},t.prototype.writeI8=function(e){this.ensureBufferSizeToWrite(1),this.view.setInt8(this.pos,e),this.pos++},t.prototype.writeU16=function(e){this.ensureBufferSizeToWrite(2),this.view.setUint16(this.pos,e),this.pos+=2},t.prototype.writeI16=function(e){this.ensureBufferSizeToWrite(2),this.view.setInt16(this.pos,e),this.pos+=2},t.prototype.writeU32=function(e){this.ensureBufferSizeToWrite(4),this.view.setUint32(this.pos,e),this.pos+=4},t.prototype.writeI32=function(e){this.ensureBufferSizeToWrite(4),this.view.setInt32(this.pos,e),this.pos+=4},t.prototype.writeF32=function(e){this.ensureBufferSizeToWrite(4),this.view.setFloat32(this.pos,e),this.pos+=4},t.prototype.writeF64=function(e){this.ensureBufferSizeToWrite(8),this.view.setFloat64(this.pos,e),this.pos+=8},t.prototype.writeU64=function(e){this.ensureBufferSizeToWrite(8),function(e,t,r){var i=r/4294967296,n=r;e.setUint32(t,i),e.setUint32(t+4,n)}(this.view,this.pos,e),this.pos+=8},t.prototype.writeI64=function(e){this.ensureBufferSizeToWrite(8),a(this.view,this.pos,e),this.pos+=8},t}(),m={};function g(e,t){var r;return void 0===t&&(t={}),function(e,t){return void 0===t&&(t=m),new U(t.extensionCodec,t.context,t.maxDepth,t.initialBufferSize,t.sortKeys,t.forceFloat32,t.ignoreUndefined,t.forceIntegerToFloat).encodeSharedRef(e)}(e,{maxDepth:25,sortKeys:null!==(r=t.sortKeys)&&void 0!==r&&r})}function T(e){t(null!==e&&"object"==typeof e);var r=e;return g(r),r}function S(e){if(null===e)return e;t("object"==typeof e),t("reason"in e);var r=e.reason;return t("gameEnded"===r||"playerLeft"===r||"minPlayers"===r||"err"===r),t("players"in e),t("object"==typeof e.players&&null!==e.players),"options"in e&&i(e.options,Object.keys(e.players)),{reason:r,players:e.players,options:"options"in e&&e.options?e.options:void 0}}function E(e){t(null!==e&&"object"==typeof e),t("gameOver"in e),t("orderNumber"in e),t("sessionId"in e),t("gameId"in e),"serverStartedAt"in e&&t("number"==typeof e.serverStartedAt);var r=e.gameOver,i=e.orderNumber,n=e.sessionId,o=e.gameId;return t("number"==typeof i),t("string"==typeof n),t("number"==typeof o),{gameOver:S(r),orderNumber:i,sessionId:n,gameId:o,serverStartedAt:"serverStartedAt"in e?e.serverStartedAt:0}}function I(e){t(null!==e&&"object"==typeof e),t("seed"in e),t("actionCount"in e);var r=e.seed,i=e.actionCount;return t("number"==typeof r),t("number"==typeof i),{seed:r,actionCount:i}}function O(e){t(null!==e&&"object"==typeof e);var r=Object.entries(e).map((function(e){var t=function(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var i,n,o=r.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(i=o.next()).done;)s.push(i.value)}catch(e){n={error:e}}finally{try{i&&!i.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}return s}(e,2);return[t[0],I(t[1])]}));return Object.fromEntries(r)}exports.validateServerSerializationData=function(e){t(null!==e&&"object"==typeof e),t("game"in e),t("context"in e),t("random"in e),t("gameTime"in e);var r=e.game,i=e.context,n=e.random,o=e.gameTime,s="updateCount"in e?e.updateCount:0;return t("number"==typeof o&&o>=0),t("number"==typeof s&&s>=0),{game:T(r),context:E(i),random:O(n),gameTime:o,updateCount:s}};