'use strict'; var Api = { csrf: null, me: null, configured: false, local: false, defaults: null, timezone: 'Europe/Amsterdam', serverNow: null, syncedAtMs: null }; Api._url = function (action, query) { var u = 'api.php?action=' + encodeURIComponent(action); if (query) { Object.keys(query).forEach(function (k) { if (query[k] != null) { u += '&' + encodeURIComponent(k) + '=' + encodeURIComponent(query[k]); } }); } return u; }; Api.call = function (action, body, method) { method = method || (body ? 'POST' : 'GET'); var opts = { method: method, credentials: 'same-origin', headers: { Accept: 'application/json' } }; if (method !== 'GET') { opts.headers['Content-Type'] = 'application/json'; if (this.csrf) { opts.headers['X-CSRF-TOKEN'] = this.csrf; } var payload = Object.assign({ action: action }, body || {}); opts.body = JSON.stringify(payload); } var url = method === 'GET' ? this._url(action, body) : this._url(action); return fetch(url, opts).then(function (res) { return res.json().then(function (data) { if (!data || typeof data !== 'object') { throw new Error('generic'); } if (data.csrf) { Api.csrf = data.csrf; } if (typeof data.server_now === 'number') { Api.serverNow = data.server_now; Api.syncedAtMs = Date.now(); } if (!data.ok && data.error) { var err = new Error(data.error); err.code = data.error; err.http = res.status; throw err; } if (!res.ok) { var e2 = new Error('serverStatus'); e2.code = 'serverStatus'; e2.status = res.status; throw e2; } return data; }, function () { var e = new Error('serverStatus'); e.status = res.status; throw e; }); }); }; Api.now = function () { if (this.serverNow == null || this.syncedAtMs == null) { return Math.floor(Date.now() / 1000); } return this.serverNow + (Date.now() - this.syncedAtMs) / 1000; }; Api.loadMe = function () { var self = this; return this.call('me').then(function (data) { self.me = data.me || null; self.configured = !!data.configured; self.local = !!data.local; self.defaults = data.defaults || Rules.defaultRules(); self.timezone = data.timezone || 'Europe/Amsterdam'; return data; }); }; Api.setLocale = function (id) { if (!this.me) { return Promise.resolve(); } return this.call('set_locale', { locale: id }); }; Api.createLobby = function (rules) { return this.call('lobby_create', { rules: rules }); }; Api.join = function (code) { return this.call('lobby_join', { code: code }); }; Api.leave = function (gameId) { return this.call('lobby_leave', { game: gameId }); }; Api.setRules = function (gameId, rules) { return this.call('lobby_rules', { game: gameId, rules: rules }); }; Api.start = function (gameId) { return this.call('lobby_start', { game: gameId }); }; Api.act = function (gameId, action) { return this.call('act', Object.assign({ game: gameId }, action)); }; Api.note = function (gameId, subject, text) { return this.call('note', { game: gameId, subject: subject, text: text }); }; Api.message = function (gameId, to, text) { return this.call('message', { game: gameId, to: to, text: text }); }; Api.sync = function (gameId, version) { return this.call('sync', { game: gameId, v: version }, 'GET'); }; Api.game = function (gameId) { return this.call('game', { game: gameId }, 'GET'); }; Api.games = function () { return this.call('games'); }; Api.debugAdvance = function (gameId, days) { return this.call('debug_advance', { game: gameId, days: days || 1 }); }; if (typeof module !== 'undefined' && module.exports) { module.exports = Api; } else if (typeof globalThis !== 'undefined') { globalThis.Api = Api; }