Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { DateTime, Duration } from 'luxon'
import {
UserChallenge,
ChallengeType,
EcogestureType,
ChallengeState,
BadgeState,
TypeChallenge,
} from 'services/dataChallengeContracts'
import { FluidType } from 'enum/fluid.enum'
import { CHALLENGETYPE_DOCTYPE, ECOGESTURE_DOCTYPE } from 'doctypes'
export class UserChallengeEntity {
_id?: string
startingDate: string
endingDate: string
state: ChallengeState
maxEnergy: number
currentEnergy: number
badge: BadgeState
relationships: any
constructor(
startingDate: string,
endingDate: string,
state: ChallengeState,
maxEnergy: number,
currentEnergy: number,
badge: BadgeState,
relationships: any,
_id?: string
) {
this.startingDate = startingDate
this.endingDate = endingDate
this.state = state
this.maxEnergy = maxEnergy
this.currentEnergy = currentEnergy
this.badge = badge
this._id = _id
this.relationships = relationships
}
}
export class ChallengeTypeEntity {
_id: string
type: TypeChallenge
title: string
description: string
level: number
duration: Duration
fluidTypes: FluidType[]
relationships: any
constructor(
_id: string,
type: TypeChallenge,
title: string,
description: string,
level: number,
duration: Duration,
fluidTypes: FluidType[],
relationships: any
) {
this._id = _id
this.type = type
this.title = title
this.description = description
this.level = level
this.duration = duration
this.fluidTypes = fluidTypes
this.relationships = relationships
}
}
export default class ChallengeDataMapper {
public mapFromUserChallenge(
userChallenge: UserChallenge
): UserChallengeEntity {
const mappedEcogestures = userChallenge.selectedEcogestures.map(
ecogesture => ({
_id: ecogesture.id,
_type: ECOGESTURE_DOCTYPE,
})
)
const mappedUserChallenge: UserChallengeEntity = {
startingDate: userChallenge.startingDate.toISO(),
endingDate: userChallenge.endingDate.toISO(),
state: userChallenge.state,
maxEnergy: userChallenge.maxEnergy,
currentEnergy: userChallenge.currentEnergy,
relationships: {
selectedEcogestures: {
data: mappedEcogestures,
},
challengeType: {
data: {
_id: userChallenge.challengeType
? userChallenge.challengeType.id
: '',
_type: CHALLENGETYPE_DOCTYPE,
},
},
},
}
return mappedUserChallenge
}
public mapToUserChallenge(
userChallengeEntity: UserChallengeEntity,
userChallengeEntityRelationships?: any | null
): UserChallenge {
let selectedEcogestures: EcogestureType[] = []
let challengeType: ChallengeType | null = null
if (userChallengeEntityRelationships) {
const idOfEcogestures = userChallengeEntity.relationships.selectedEcogestures.data.map(
eg => eg._id
)
const challengeTypes = userChallengeEntityRelationships.filter(
challengeType =>
challengeType._type === CHALLENGETYPE_DOCTYPE &&
challengeType._id ===
userChallengeEntity.relationships.challengeType.data._id
)
const EcogesturesRelationships = userChallengeEntityRelationships.filter(
eg => idOfEcogestures.includes(eg._id)
)
selectedEcogestures = EcogesturesRelationships.filter(
relationship => relationship._type === ECOGESTURE_DOCTYPE
)
challengeType = challengeTypes ? challengeTypes[0] : null
}
const mappedUserChallenge: UserChallenge = new UserChallenge(
DateTime.fromISO(userChallengeEntity.startingDate).startOf('day'),
DateTime.fromISO(userChallengeEntity.endingDate).startOf('day'),
userChallengeEntity.state,
selectedEcogestures,
challengeType,
userChallengeEntity.maxEnergy,
userChallengeEntity.currentEnergy,
userChallengeEntity.badge,
userChallengeEntity._id
)
return mappedUserChallenge
}
public mapToChallengeType(
challengeEntity: ChallengeTypeEntity,
challengeTypeEntityRelationships?: any | null,
unlockedEcogestures?: string[],
fluidTypes?: FluidType[]
): ChallengeType {
const completeAvailableEcogestures: any[] = []
if (challengeTypeEntityRelationships) {
for (const ecogestureType of challengeEntity.relationships
.availableEcogestures.data) {
const unfilteredEcogestures = challengeTypeEntityRelationships.filter(
relationship =>
relationship._type === ECOGESTURE_DOCTYPE &&
relationship._id === ecogestureType._id
)
if (unfilteredEcogestures && unfilteredEcogestures.length === 1)
completeAvailableEcogestures.push(unfilteredEcogestures[0])
}
}
// let filteredAvailableEcogestures = completeAvailableEcogestures.filter(
// eg =>
// fluidTypes &&
// (fluidTypes.includes(eg.fluidTypes[0]) ||
// fluidTypes.includes(eg.fluidTypes[1]) ||
// fluidTypes.includes(eg.fluidTypes[2]))
// )
let filteredAvailableEcogestures = completeAvailableEcogestures
const fluidFilteredEcogestures = [...new Set(filteredAvailableEcogestures)]
fluidFilteredEcogestures.forEach(eg => {
if (unlockedEcogestures && !unlockedEcogestures.includes(eg._id)) {
filteredAvailableEcogestures.push(eg)
}
})
filteredAvailableEcogestures = [...new Set(filteredAvailableEcogestures)]
if (filteredAvailableEcogestures.length < 2) {
filteredAvailableEcogestures = completeAvailableEcogestures
}
const randomEcogesture =
filteredAvailableEcogestures[
Math.floor(Math.random() * filteredAvailableEcogestures.length)
]
const randomEcogestures = filteredAvailableEcogestures.filter(
eg => eg.pack === randomEcogesture.pack && eg.id !== randomEcogesture.id
)
randomEcogestures.push(randomEcogesture)
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const mappedChallengeType = new ChallengeType(
challengeEntity._id,
challengeEntity.type,
challengeEntity.title,
challengeEntity.description,
challengeEntity.level,
challengeEntity.duration,
challengeEntity.fluidTypes,
randomEcogestures
)
return mappedChallengeType
}
public mapToChallengeTypes(
challengeEntities: ChallengeTypeEntity[],
challengeTypeEntityRelationships?: any | null,
unlockedEcogestures?: any,
fluidTypes?: FluidType[]
): ChallengeType[] {
return challengeEntities.map(challengeEntity =>
this.mapToChallengeType(
challengeEntity,
challengeTypeEntityRelationships,
unlockedEcogestures,
fluidTypes
)
)
}
}