@jetthai/cuora 1.0.168 → 1.0.170

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.0.170](https://github.com/jetthai/cuora-module/compare/v1.0.169...v1.0.170) (2022-09-19)
6
+
7
+ ### [1.0.169](https://github.com/jetthai/cuora-module/compare/v1.0.168...v1.0.169) (2022-09-19)
8
+
5
9
  ### [1.0.168](https://github.com/jetthai/cuora-module/compare/v1.0.167...v1.0.168) (2022-09-12)
6
10
 
7
11
  ### [1.0.167](https://github.com/jetthai/cuora-module/compare/v1.0.166...v1.0.167) (2022-09-07)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.0.168",
3
+ "version": "1.0.170",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
package/src/api/game.js CHANGED
@@ -2,7 +2,7 @@ export default {
2
2
  getGameSHA: () => ({ method: 'get', url: 'game/list/v2/sha' }),
3
3
  getOptionMap: (code) => ({ method: 'get', url: 'game/list/v2/options', params: { code } }),
4
4
  getGameStatus: () => ({ method: 'get', url: 'game/list/v2/status' }),
5
- getGameCategory: ({ id, first }) => ({ method: 'get', url: 'game/list/v2/category', params: { id, first } }),
5
+ getGameCategory: ({ id, first } = {}) => ({ method: 'get', url: 'game/list/v2/category', params: { id, first } }),
6
6
  addFavoriteGame: (code) => ({ method: 'post', url: 'game/myfavorite/add', params: { games: [code] } }),
7
7
  removeFavoriteGame: (code) => ({ method: 'post', url: 'game/myfavorite/remove', params: { games: [code] } }),
8
8
  getStatements: () => ({ url: 'game/statements', method: 'get' }),
@@ -43,4 +43,34 @@ export default {
43
43
  url: 'game/history/options',
44
44
  method: 'get',
45
45
  }),
46
+ getListRoot: ({ timestamp } = {}) => ({
47
+ url: 'game/list/v3/root',
48
+ method: 'get',
49
+ params: { timestamp },
50
+ }),
51
+ getListCategory: ({ categoryId, timestamp, index }) => ({
52
+ url: 'game/list/v3/category',
53
+ method: 'get',
54
+ params: { categoryId, timestamp, index },
55
+ }),
56
+ getListAddition: ({ categoryId, timestamp, index }) => ({
57
+ url: 'game/list/v3/addition',
58
+ method: 'get',
59
+ params: { categoryId, timestamp, index },
60
+ }),
61
+ getListCategoryByRange: ({ categoryId, timestamp, end }) => ({
62
+ url: 'game/list/v3/categoryByRange',
63
+ method: 'get',
64
+ params: { categoryId, timestamp, end },
65
+ }),
66
+ getListAdditionByRange: ({ categoryId, timestamp, end }) => ({
67
+ url: 'game/list/v3/additionByRange',
68
+ method: 'get',
69
+ params: { categoryId, timestamp, end },
70
+ }),
71
+ getListMyFavorite: ({ timestamp } = {}) => ({
72
+ url: 'game/list/v3/myFavorite',
73
+ method: 'get',
74
+ params: { timestamp },
75
+ }),
46
76
  }
package/src/index.js CHANGED
@@ -2,5 +2,6 @@ import api from './api/index.js'
2
2
  import machine from './machine/index.js'
3
3
  import Request from './request/request.js'
4
4
  import Config from './request/config.js'
5
- import util from './util/index.js'
6
- export { api, machine, Request, Config, util }
5
+ import util, { constant } from './util/index.js'
6
+
7
+ export { api, machine, Request, Config, util, constant }
@@ -0,0 +1,327 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import api from '../api/game.js'
3
+ import UAParser from 'ua-parser-js'
4
+ import { MY_FAVORITE } from '../util/constant.js'
5
+
6
+ export default function (request) {
7
+ return createMachine(
8
+ {
9
+ id: 'game',
10
+ type: 'parallel',
11
+ context: {
12
+ room: null,
13
+ index: 0,
14
+ content: {
15
+ data: [],
16
+ timestamp: null,
17
+ parentCategoryId: null,
18
+ totalPage: 1,
19
+ },
20
+ favoriteList: [],
21
+ // play params
22
+ game: {
23
+ providerId: null,
24
+ gameId: null,
25
+ gameTypeId: null,
26
+ mappingId: null,
27
+ portrait: null,
28
+ },
29
+ lang: 'en_US',
30
+ isMobile: new UAParser().getResult().device.type,
31
+ gameURL: null,
32
+ },
33
+ states: {
34
+ flow: {
35
+ initial: 'idle',
36
+ states: {
37
+ idle: {
38
+ enter: 'clearGameInfo',
39
+ on: {
40
+ ENTER_ROOM: {
41
+ target: 'loading',
42
+ cond: 'isDifferentRoom',
43
+ actions: ['setRoom', 'resetContent'],
44
+ },
45
+ NEXT_PAGE: {
46
+ target: 'loading',
47
+ cond: 'hasNext',
48
+ actions: 'increaseIndex',
49
+ },
50
+ ENTER_GAME: {
51
+ target: 'connecting',
52
+ actions: ['setGameInfo', 'setLang'],
53
+ },
54
+ ADD_FAVORITE: {
55
+ target: 'adding',
56
+ cond: 'isNotFavorite',
57
+ actions: 'addToFavoriteList',
58
+ },
59
+ REMOVE_FAVORITE: {
60
+ target: 'removing',
61
+ cond: 'isFavorite',
62
+ actions: 'removeFromFavoriteList',
63
+ },
64
+ },
65
+ },
66
+ loading: {
67
+ invoke: {
68
+ src: 'getPageContent',
69
+ onDone: {
70
+ target: 'idle',
71
+ actions: 'appendContent',
72
+ },
73
+ onError: {
74
+ target: 'idle',
75
+ },
76
+ },
77
+ },
78
+ connecting: {
79
+ invoke: {
80
+ src: 'connectGame',
81
+ onDone: {
82
+ target: 'playing',
83
+ actions: 'setGameURL',
84
+ },
85
+ onError: {
86
+ target: 'idle',
87
+ },
88
+ },
89
+ },
90
+ playing: {
91
+ on: {
92
+ EXIT_GAME: {
93
+ target: 'disconnect',
94
+ },
95
+ },
96
+ },
97
+ disconnect: {
98
+ invoke: {
99
+ src: 'disconnectGame',
100
+ onDone: {
101
+ target: 'idle',
102
+ },
103
+ onError: {
104
+ target: 'idle',
105
+ },
106
+ },
107
+ },
108
+ adding: {
109
+ invoke: {
110
+ src: 'addFavorite',
111
+ onDone: {
112
+ target: 'idle',
113
+ },
114
+ onError: {
115
+ target: 'idle',
116
+ actions: 'removeFromFavoriteList',
117
+ },
118
+ },
119
+ },
120
+ removing: {
121
+ invoke: {
122
+ src: 'removeFavorite',
123
+ onDone: {
124
+ target: 'idle',
125
+ },
126
+ onError: {
127
+ target: 'idle',
128
+ actions: 'addToFavoriteList',
129
+ },
130
+ },
131
+ },
132
+ },
133
+ },
134
+ timer: {
135
+ initial: 'favorite',
136
+ states: {
137
+ favorite: {
138
+ invoke: {
139
+ src: 'getFavorite',
140
+ onDone: {
141
+ target: 'pending',
142
+ actions: 'setFavoriteList',
143
+ },
144
+ onError: {
145
+ target: 'pending',
146
+ },
147
+ },
148
+ },
149
+ pending: {
150
+ after: {
151
+ 20000: [
152
+ {
153
+ target: 'updating',
154
+ cond: 'hasRoom',
155
+ },
156
+ {
157
+ target: 'pending',
158
+ },
159
+ ],
160
+ },
161
+ },
162
+ updating: {
163
+ invoke: {
164
+ src: 'updatePageContent',
165
+ onDone: {
166
+ target: 'pending',
167
+ actions: 'swapContent',
168
+ },
169
+ onError: {
170
+ target: 'pending',
171
+ },
172
+ },
173
+ },
174
+ },
175
+ },
176
+ },
177
+ },
178
+ {
179
+ actions: {
180
+ setFavoriteList: assign((context, event) => {
181
+ return {
182
+ favoriteList: event.data.data || [],
183
+ }
184
+ }),
185
+ setRoom: assign((context, event) => {
186
+ return {
187
+ room: event.data,
188
+ }
189
+ }),
190
+ resetContent: assign((context, event) => {
191
+ return {
192
+ index: 0,
193
+ content: {
194
+ data: [],
195
+ timestamp: null,
196
+ parentCategoryId: null,
197
+ totalPage: 1,
198
+ },
199
+ }
200
+ }),
201
+ appendContent: assign((context, event) => {
202
+ const { room, favoriteList } = context
203
+ const { data, ...rest } = event.data
204
+ return {
205
+ favoriteList: room.categoryId === MY_FAVORITE ? data : favoriteList,
206
+ content: {
207
+ ...context.content,
208
+ ...rest,
209
+ data: context.content.data.concat(data),
210
+ },
211
+ }
212
+ }),
213
+ swapContent: assign((context, event) => {
214
+ const { room, favoriteList } = context
215
+ if (event.data.data.length) {
216
+ return {
217
+ favoriteList: room.categoryId === MY_FAVORITE ? event.data.data : favoriteList,
218
+ content: event.data,
219
+ }
220
+ }
221
+ }),
222
+ increaseIndex: assign((context, event) => {
223
+ return {
224
+ index: context.index + 1,
225
+ }
226
+ }),
227
+ setGameURL: assign({
228
+ gameURL: (context, event) => {
229
+ return event.data.gameURL
230
+ },
231
+ }),
232
+ clearGameInfo: assign((context, event) => {
233
+ return { game: { providerId: null, gameId: null, gameTypeId: null, mappingId: null } }
234
+ }),
235
+ setGameInfo: assign((context, event) => {
236
+ const { providerId, gameId, gameTypeId, mappingId, portrait } = event.data.game
237
+ return { game: { providerId, gameId, gameTypeId, mappingId, portrait: portrait ? 1 : 0 } }
238
+ }),
239
+ setLang: assign((context, event) => {
240
+ const lang = event.data.locale.replace('-', '_')
241
+ return { lang }
242
+ }),
243
+ addToFavoriteList: assign((context, event) => {
244
+ return {
245
+ favoriteList: [...context.favoriteList, event.data],
246
+ }
247
+ }),
248
+ removeFromFavoriteList: assign((context, event) => {
249
+ return {
250
+ favoriteList: context.favoriteList.filter((item) => item.gameCode !== event.data.gameCode),
251
+ }
252
+ }),
253
+ },
254
+ guards: {
255
+ isDifferentRoom: (context, event) => context.room?.categoryId !== event.data?.categoryId,
256
+ hasNext: (context, event) => context.index < context?.content?.totalPage - 1,
257
+ hasRoom: (context, event) => context.room?.categoryId,
258
+ isFavorite: (context, event) => context.favoriteList.some((item) => item.gameCode === event.data.gameCode),
259
+ isNotFavorite: (context, event) => !context.favoriteList.some((item) => item.gameCode === event.data.gameCode),
260
+ },
261
+ services: {
262
+ getFavorite: async (context, event) => {
263
+ return await request.send(api.getListMyFavorite())
264
+ },
265
+ getPageContent: async (context, event) => {
266
+ const { room, index } = context
267
+ if (room.categoryId === MY_FAVORITE) {
268
+ return await request.send(api.getListMyFavorite())
269
+ } else {
270
+ return await request.send(api.getListCategory({ categoryId: room.categoryId, index }))
271
+ }
272
+ },
273
+ updatePageContent: async (context, event) => {
274
+ const { room, content, index } = context
275
+ if (room.categoryId === MY_FAVORITE) {
276
+ // return await request.send(api.getListMyFavorite({ timestamp: content.timestamp }))
277
+ } else {
278
+ return await request.send(
279
+ api.getListCategoryByRange({
280
+ categoryId: room.categoryId,
281
+ end: index,
282
+ timestamp: content.timestamp,
283
+ })
284
+ )
285
+ }
286
+ },
287
+ connectGame: async (context, event) => {
288
+ const { lang, isMobile } = context
289
+ const { providerId, gameId, gameTypeId, mappingId } = context.game
290
+ const params = {
291
+ provider_id: providerId,
292
+ game_id: gameId,
293
+ gametype_id: gameTypeId,
294
+ mapping_id: mappingId,
295
+ is_demo: false,
296
+ is_mobile: isMobile,
297
+ lang,
298
+ return_url: window.location.href,
299
+ }
300
+ const { redirect_url } = await request.send(api.playGame(params))
301
+ return { gameURL: redirect_url }
302
+ },
303
+ disconnectGame: async (context, event) => {
304
+ const uid_site = event.data
305
+ const provider_id = context.game.providerId
306
+ return await request.send(api.logoutGame({ uid_site, provider_id }))
307
+ },
308
+ addFavorite: async (context, event) => {
309
+ try {
310
+ await request.send(api.addFavoriteGame(event.data.gameCode))
311
+ return event.data
312
+ } catch (error) {
313
+ throw event.data
314
+ }
315
+ },
316
+ removeFavorite: async (context, event) => {
317
+ try {
318
+ await request.send(api.removeFavoriteGame(event.data.gameCode))
319
+ return event.data
320
+ } catch (error) {
321
+ throw event.data
322
+ }
323
+ },
324
+ },
325
+ }
326
+ )
327
+ }
@@ -5,6 +5,7 @@ import welcome from './welcome.js'
5
5
  import common from './common.js'
6
6
  import error from './error.js'
7
7
  import game from './game.js'
8
+ import game_v2 from './game_v2.js'
8
9
  import game_favorite from './game_favorite.js'
9
10
  import game_search from './game_search.js'
10
11
  import game_hot_new from './game_hot_new.js'
@@ -15,6 +16,7 @@ import reward_fc from './reward_fc.js'
15
16
  import reward from './reward.js'
16
17
  import setting from './setting.js'
17
18
  import side from './side.js'
19
+ import side_v2 from './side_v2.js'
18
20
  import user_info from './user_info.js'
19
21
  import user_sign_up from './user_sign_up.js'
20
22
  import user_reset_password from './user_reset_password.js'
@@ -38,6 +40,7 @@ export default {
38
40
  deposit,
39
41
  error,
40
42
  game,
43
+ game_v2,
41
44
  game_favorite,
42
45
  game_search,
43
46
  game_hot_new,
@@ -48,6 +51,7 @@ export default {
48
51
  reward_fc,
49
52
  setting,
50
53
  side,
54
+ side_v2,
51
55
  user_info,
52
56
  user_sign_up,
53
57
  user_reset_password,
@@ -0,0 +1,190 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import game from '../api/game.js'
3
+
4
+ export default function (request) {
5
+ return createMachine(
6
+ {
7
+ id: 'side',
8
+ initial: 'loading',
9
+ context: {
10
+ menu: null,
11
+ menuItemStack: [],
12
+ previousMenuItemStack: [],
13
+ historyStack: [],
14
+ },
15
+ states: {
16
+ loading: {
17
+ invoke: {
18
+ src: 'getMenu',
19
+ onDone: {
20
+ target: 'idle',
21
+ actions: ['setMenu', 'setDefaultMenuItemStack'],
22
+ },
23
+ },
24
+ },
25
+ idle: {
26
+ initial: 'pending',
27
+ states: {
28
+ pending: {
29
+ after: {
30
+ 20000: [
31
+ {
32
+ target: 'updating',
33
+ cond: 'isInMenu'
34
+ },
35
+ {
36
+ target: 'pending',
37
+ },
38
+ ]
39
+ }
40
+ },
41
+ updating: {
42
+ invoke: {
43
+ src: 'fetchUpdateMenu',
44
+ onDone: {
45
+ target: 'pending',
46
+ actions: 'updateMenu',
47
+ },
48
+ },
49
+ },
50
+ },
51
+ on: {
52
+ SELECT: {
53
+ actions: 'setMenuItemStack',
54
+ },
55
+ CANCEL: {
56
+ actions: 'restoreMenuItemStack',
57
+ },
58
+ PUSH: {
59
+ actions: 'pushHistoryStack'
60
+ },
61
+ POP: {
62
+ actions: 'popHistoryStack'
63
+ }
64
+ },
65
+ },
66
+ },
67
+ },
68
+ {
69
+ actions: {
70
+ setMenu: assign((context, event) => {
71
+ const menu = event.data
72
+ addLevelProperty(menu.data, 0)
73
+ return { menu }
74
+ }),
75
+ setDefaultMenuItemStack: assign((context, event) => {
76
+ const menuItemStack = getDefaultItem(event.data.data[0])
77
+ return { menuItemStack }
78
+ }),
79
+ setMenuItemStack: assign((context, event) => {
80
+ const { menuItemStack } = context
81
+ const inputItem = event.data
82
+ const isTopItem = inputItem.level === 0
83
+ const hasSubMenu = inputItem.sub.length
84
+
85
+ if (isTopItem) {
86
+ if (hasSubMenu) {
87
+ const isSameBranch = menuItemStack[0].categoryId === inputItem.categoryId
88
+ if (isSameBranch) {
89
+ return {
90
+ previousMenuItemStack: menuItemStack,
91
+ }
92
+ } else {
93
+ return {
94
+ previousMenuItemStack: menuItemStack,
95
+ menuItemStack: [event.data],
96
+ }
97
+ }
98
+ } else {
99
+ return {
100
+ previousMenuItemStack: [],
101
+ menuItemStack: [event.data],
102
+ historyStack: []
103
+ }
104
+ }
105
+ } else {
106
+ const slicedStack = menuItemStack.slice(0, inputItem.level)
107
+ return {
108
+ menuItemStack: [...slicedStack, event.data],
109
+ historyStack: []
110
+ }
111
+ }
112
+ }),
113
+ restoreMenuItemStack: assign((context, event) => {
114
+ const { previousMenuItemStack } = context
115
+ return {
116
+ previousMenuItemStack: [],
117
+ menuItemStack: previousMenuItemStack,
118
+ }
119
+ }),
120
+ updateMenu: assign((context, event) => {
121
+ const menu = event.data
122
+ if (menu.data.length) {
123
+ addLevelProperty(menu.data, 0)
124
+ return { menu }
125
+ }
126
+ }),
127
+ pushHistoryStack: assign((context, event) => {
128
+ const { menuItemStack, historyStack } = context
129
+ return {
130
+ historyStack: historyStack.concat(menuItemStack.slice(-1)),
131
+ }
132
+ }),
133
+ popHistoryStack: assign((context, event) => {
134
+ const { historyStack } = context
135
+ return {
136
+ historyStack: historyStack.slice(0, -1),
137
+ }
138
+ }),
139
+ },
140
+ guards: {
141
+ isInMenu: ((context, event) => {
142
+ const {menuItemStack, menu} = context
143
+ const menuItem = menuItemStack.slice(-1)[0]
144
+ return isItemInMenu(menuItem, menu.data)
145
+ })
146
+ },
147
+ services: {
148
+ getMenu: async (context, event) => {
149
+ return await request.send(game.getListRoot())
150
+ },
151
+ fetchUpdateMenu: async (context, event) => {
152
+ return await request.send(game.getListRoot({ timestamp: context.menu.timestamp }))
153
+ },
154
+ },
155
+ }
156
+ )
157
+ }
158
+
159
+ function isItemInMenu(targetItem, menuList) {
160
+ return menuList.reduce((result, currentItem) => {
161
+ if (result) {
162
+ return true
163
+ } else {
164
+ if (currentItem.categoryId === targetItem.categoryId) {
165
+ return true
166
+ } else if (currentItem.sub.length) {
167
+ return isItemInMenu(targetItem, currentItem.sub)
168
+ } else {
169
+ return false
170
+ }
171
+ }
172
+ }, false)
173
+ }
174
+
175
+ function getDefaultItem(node) {
176
+ if (node.sub.length > 0) {
177
+ const stack = getDefaultItem(node.sub[0])
178
+ return [node, ...stack]
179
+ } else {
180
+ return [node]
181
+ }
182
+ }
183
+
184
+ function addLevelProperty(list, level) {
185
+ const nextLevel = level + 1
186
+ for (let i = 0; i < list.length; i++) {
187
+ list[i].level = level
188
+ addLevelProperty(list[i].sub, nextLevel)
189
+ }
190
+ }
@@ -0,0 +1 @@
1
+ export const MY_FAVORITE = 'MyFavorite'
package/src/util/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  import { getDeviceToken } from './deviceToken.js'
2
+ export * as constant from './constant.js'
2
3
 
3
4
  export default { getDeviceToken }