| 1 | 'use strict'; |
| 2 | |
| 3 | var Api = { |
| 4 | csrf: null, |
| 5 | me: null, |
| 6 | configured: false, |
| 7 | local: false, |
| 8 | defaults: null, |
| 9 | timezone: 'Europe/Amsterdam', |
| 10 | serverNow: null, |
| 11 | syncedAtMs: null |
| 12 | }; |
| 13 | |
| 14 | Api._url = function (action, query) |
| 15 | { |
| 16 | var u = 'api.php?action=' + encodeURIComponent(action); |
| 17 | if (query) |
| 18 | { |
| 19 | Object.keys(query).forEach(function (k) |
| 20 | { |
| 21 | if (query[k] != null) |
| 22 | { u += '&' + encodeURIComponent(k) + '=' + encodeURIComponent(query[k]); } |
| 23 | }); |
| 24 | } |
| 25 | return u; |
| 26 | }; |
| 27 | |
| 28 | Api.call = function (action, body, method) |
| 29 | { |
| 30 | method = method || (body ? 'POST' : 'GET'); |
| 31 | var opts = { |
| 32 | method: method, |
| 33 | credentials: 'same-origin', |
| 34 | headers: { Accept: 'application/json' } |
| 35 | }; |
| 36 | if (method !== 'GET') |
| 37 | { |
| 38 | opts.headers['Content-Type'] = 'application/json'; |
| 39 | if (this.csrf) |
| 40 | { opts.headers['X-CSRF-TOKEN'] = this.csrf; } |
| 41 | var payload = Object.assign({ action: action }, body || {}); |
| 42 | opts.body = JSON.stringify(payload); |
| 43 | } |
| 44 | var url = method === 'GET' ? this._url(action, body) : this._url(action); |
| 45 | return fetch(url, opts).then(function (res) |
| 46 | { |
| 47 | return res.json().then(function (data) |
| 48 | { |
| 49 | if (!data || typeof data !== 'object') |
| 50 | { throw new Error('generic'); } |
| 51 | if (data.csrf) |
| 52 | { Api.csrf = data.csrf; } |
| 53 | if (typeof data.server_now === 'number') |
| 54 | { |
| 55 | Api.serverNow = data.server_now; |
| 56 | Api.syncedAtMs = Date.now(); |
| 57 | } |
| 58 | if (!data.ok && data.error) |
| 59 | { |
| 60 | var err = new Error(data.error); |
| 61 | err.code = data.error; |
| 62 | err.http = res.status; |
| 63 | throw err; |
| 64 | } |
| 65 | if (!res.ok) |
| 66 | { |
| 67 | var e2 = new Error('serverStatus'); |
| 68 | e2.code = 'serverStatus'; |
| 69 | e2.status = res.status; |
| 70 | throw e2; |
| 71 | } |
| 72 | return data; |
| 73 | }, function () |
| 74 | { |
| 75 | var e = new Error('serverStatus'); |
| 76 | e.status = res.status; |
| 77 | throw e; |
| 78 | }); |
| 79 | }); |
| 80 | }; |
| 81 | |
| 82 | Api.now = function () |
| 83 | { |
| 84 | if (this.serverNow == null || this.syncedAtMs == null) |
| 85 | { return Math.floor(Date.now() / 1000); } |
| 86 | return this.serverNow + (Date.now() - this.syncedAtMs) / 1000; |
| 87 | }; |
| 88 | |
| 89 | Api.loadMe = function () |
| 90 | { |
| 91 | var self = this; |
| 92 | return this.call('me').then(function (data) |
| 93 | { |
| 94 | self.me = data.me || null; |
| 95 | self.configured = !!data.configured; |
| 96 | self.local = !!data.local; |
| 97 | self.defaults = data.defaults || Rules.defaultRules(); |
| 98 | self.timezone = data.timezone || 'Europe/Amsterdam'; |
| 99 | return data; |
| 100 | }); |
| 101 | }; |
| 102 | |
| 103 | Api.setLocale = function (id) |
| 104 | { |
| 105 | if (!this.me) { return Promise.resolve(); } |
| 106 | return this.call('set_locale', { locale: id }); |
| 107 | }; |
| 108 | |
| 109 | Api.createLobby = function (rules) |
| 110 | { |
| 111 | return this.call('lobby_create', { rules: rules }); |
| 112 | }; |
| 113 | |
| 114 | Api.join = function (code) |
| 115 | { |
| 116 | return this.call('lobby_join', { code: code }); |
| 117 | }; |
| 118 | |
| 119 | Api.leave = function (gameId) |
| 120 | { |
| 121 | return this.call('lobby_leave', { game: gameId }); |
| 122 | }; |
| 123 | |
| 124 | Api.setRules = function (gameId, rules) |
| 125 | { |
| 126 | return this.call('lobby_rules', { game: gameId, rules: rules }); |
| 127 | }; |
| 128 | |
| 129 | Api.start = function (gameId) |
| 130 | { |
| 131 | return this.call('lobby_start', { game: gameId }); |
| 132 | }; |
| 133 | |
| 134 | Api.act = function (gameId, action) |
| 135 | { |
| 136 | return this.call('act', Object.assign({ game: gameId }, action)); |
| 137 | }; |
| 138 | |
| 139 | Api.note = function (gameId, subject, text) |
| 140 | { |
| 141 | return this.call('note', { game: gameId, subject: subject, text: text }); |
| 142 | }; |
| 143 | |
| 144 | Api.message = function (gameId, to, text) |
| 145 | { |
| 146 | return this.call('message', { game: gameId, to: to, text: text }); |
| 147 | }; |
| 148 | |
| 149 | Api.sync = function (gameId, version) |
| 150 | { |
| 151 | return this.call('sync', { game: gameId, v: version }, 'GET'); |
| 152 | }; |
| 153 | |
| 154 | Api.game = function (gameId) |
| 155 | { |
| 156 | return this.call('game', { game: gameId }, 'GET'); |
| 157 | }; |
| 158 | |
| 159 | Api.games = function () |
| 160 | { |
| 161 | return this.call('games'); |
| 162 | }; |
| 163 | |
| 164 | Api.debugAdvance = function (gameId, days) |
| 165 | { |
| 166 | return this.call('debug_advance', { game: gameId, days: days || 1 }); |
| 167 | }; |
| 168 | |
| 169 | if (typeof module !== 'undefined' && module.exports) |
| 170 | { module.exports = Api; } |
| 171 | else if (typeof globalThis !== 'undefined') |
| 172 | { globalThis.Api = Api; } |
projects-timfalken-com allianties