-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathRoom.jsx
282 lines (248 loc) · 9.94 KB
/
Room.jsx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import React, { Component } from 'react'
import { connect } from 'react-redux'
import reducers from './reducers.jsx'
import { Redirect } from 'react-router-dom'
import { Beforeunload } from 'react-beforeunload'
import ReactMarkdown from 'react-markdown'
import Modal from 'react-modal';
import ModalFactory from './ModalFactory.jsx'
import JitsiVideo from './JitsiVideo.jsx'
import ArtRoom from './ArtRoom.jsx'
import IFrameRoom from './IFrameRoom.jsx'
import ImageMapRoom from './ImageMapRoom.jsx'
import Door from './Door.jsx'
import Adventure from './Adventure.jsx'
import Navigation from './Navigation.jsx'
import { PokeNotification } from './Poke.jsx'
import { WebSocketApi } from './WebAPI.jsx'
import LocalStorage from './LocalStorage.jsx'
import Config from './Config.jsx'
import { ChatStreamRoom } from './ChatStreamRoom.jsx'
class Room extends Component {
/*
* A room has the following:
* 1. Name of room
* 2. Optional description
* 3. Content (video, art, text, etc)
* 4. Navigation component used to move through rooms
* 5. Optional artifact, i.e. something the user finds or unlocks, like a map
* Add new rooms to rooms.json, where individual rooms are defined. Add new TYPES of rooms
* by creating a new component for that room and updating the getRoomType() mapping.
*/
constructor(props) {
super(props)
this.useLocalSessions = Config.useLocalSessions || false
// These are the room types for which we show the map button
this.roomTypesWithMap = {
JITSI: true,
IFRAME: true,
CHATSTREAM: true,
CONTENT: true,
IMAGEMAP: true
}
// These are the room types which require doors
this.roomTypesWithDoors = {
JITSI: true,
IFRAME: true,
CHATSTREAM: true,
IMAGEMAP: true
}
this.state = {
room: this.props.currentRoom.room,
entered: false,
users: []
}
this.socketApi = new WebSocketApi()
this.socketApi.startPinging(this.props.user.id)
this.socketApi.on('user-event', this.props.updateUsers.bind(this))
this.socketApi.on(`poke-${this.props.user.id}`, this.handlePoke.bind(this))
this.computePokeUnlockedState()
}
getRoomData() {
return this.props.rooms[this.state.room]
}
getRoomContent() {
/*
* There are currently 3 different types of rooms:
* 1. Regular Jitsi room that just has video
* 2. Art room which has a small video panel and an image
* 3. Text-based adventure rooms where you have to make a decision
* 4. Image map based room to display an organized & navigable set of content
* 5. Special purpose rooms that exist at a different route
*/
const roomData = this.getRoomData()
const jitsiData = {
displayName: this.props.user.username,
avatar: this.props.user.avatar,
roomName: this.state.room,
muteRoom: roomData.muteRoom,
hideVideo: roomData.hideVideo,
disableSharing: roomData.type === 'IFRAME'
}
const combo = { ...roomData, ...jitsiData };
return {
ART: <ArtRoom jitsiData={jitsiData} art={roomData.art}></ArtRoom>,
JITSI: <JitsiVideo jitsiData={jitsiData}></JitsiVideo>,
IFRAME: <IFrameRoom jitsiData={jitsiData} iframeOptions={roomData.iframeOptions}></IFrameRoom>,
CHATSTREAM: <ChatStreamRoom {...combo}></ChatStreamRoom>,
ADVENTURE: <Adventure options={roomData} onClick={this.onAdventureClick.bind(this)}></Adventure>,
IMAGEMAP: <ImageMapRoom imageMapOptions={roomData.imageMapOptions}></ImageMapRoom>
}[roomData.type]
}
getRoomDescription() {
if (this.props.rooms[this.state.room].description) {
return (
<div className="room-content">
<ReactMarkdown
source={this.props.rooms[this.state.room].description}
allowedTypes={['text', 'break', 'emphasis', 'strong', 'delete', 'link']}
unwrapDisallowed={true}>
</ReactMarkdown>
</div>
)
}
}
setBackground() {
const roomData = this.getRoomData()
if (roomData.backgroundImage) {
document.querySelector('.room').style.backgroundImage = `url(${roomData.backgroundImage})`
} else {
this.clearBackground()
}
}
clearBackground() {
if (document.querySelector('.room')) {
document.querySelector('.room').style.backgroundImage = ""
}
}
onAdventureClick(room) {
this.setState({ room, entered: true })
this.props.updateCurrentRoom({ room, entered: true })
this.socketApi.enterRoom(this.props.user, room)
}
updateRoom(room) {
// Leave current room
this.clearBackground()
this.socketApi.leaveRoom(this.props.user, this.state.room)
// Go to new room, but don't open the door for rooms that have doors
const entered = !this.roomTypesWithDoors[this.props.rooms[room].type]
this.setState({ room, entered })
this.props.updateCurrentRoom({ room, entered })
// reset door anim
const door = document.getElementById('door')
if (door) {
door.style.webkitAnimation = "none";
setTimeout(() => { door.style.webkitAnimation = "" })
}
}
onSwitchRoom(room) {
if (window.api) {
window.api.executeCommand('hangup')
window.api = null
setTimeout(this.updateRoom.bind(this, room), 500)
} else {
this.updateRoom(room)
document.activeElement.blur()
}
}
onEnterRoom() {
// Open the door
this.setBackground()
this.setState({ entered: true })
this.props.updateCurrentRoom({
room: this.state.room,
entered: true
})
this.socketApi.enterRoom(this.props.user, this.state.room)
}
handleBeforeUnload() {
// Update server if user closes tab or refreshes
this.socketApi.leaveRoom(this.props.user, this.state.room)
}
handleModalChange(modal) {
this.setState({ modal })
}
computeMapState(room) {
const mapAlreadyUnlocked = LocalStorage.get("MAP_UNLOCKED")
const mapUnlockThreshold = parseInt(Config.mapUnlockThreshold)
const visitedRooms = Object.values(this.props.visited).filter(x => x).length
const isMapUnlocked =
Config.debug ||
mapAlreadyUnlocked ||
(_.has(this.roomTypesWithMap, room.type) && (visitedRooms >= mapUnlockThreshold))
const showMapTooltip = !mapAlreadyUnlocked && isMapUnlocked
LocalStorage.set("MAP_UNLOCKED", isMapUnlocked)
return {
mapVisible: isMapUnlocked && !Config.mapDisabled,
showMapTooltip: showMapTooltip
}
}
computePokeUnlockedState() {
if (Config.poke) {
if (this.useLocalSessions && LocalStorage.get('POKE_UNLOCKED')) {
this.props.unlockPoking()
} else {
const timeout = parseFloat(Config.poke.unlockAfterMinutes) * 60 * 1000
setTimeout(() => {
this.props.unlockPoking()
LocalStorage.set('POKE_UNLOCKED', true)
}, timeout)
}
}
}
handlePoke(pokeData) {
this.setState({ pokeData })
setTimeout(() => {
this.setState({ pokeData: null })
}, 5000)
}
render() {
if (Object.keys(this.props.rooms).length === 0) {
// room config not loaded yet
return null
}
this.useLocalSessions && LocalStorage.touch("USER") // keep session alive
const room = this.props.rooms[this.state.room]
if (room.type === 'REDIRECT') {
this.socketApi.enterRoom(this.props.user, this.state.room)
return <Redirect to={room.route} />
}
const userList = this.props.users[this.state.room] || []
const content = this.state.entered ?
this.getRoomContent() :
<Door room={room} users={userList} tintColor={room.doorTint} onClick={this.onEnterRoom.bind(this)}></Door>
const roomClass = this.state.entered ? "room entered" : "room"
const mapState = this.computeMapState(room)
// Allows modal to have access to react components and state
Modal.setAppElement('.app')
return (
<div className={roomClass}>
<div className="room-header">
<h2 className="room-header">{room.name}</h2>
</div>
{content}
{this.getRoomDescription()}
<Navigation
directions={room.directions}
onClick={this.onSwitchRoom.bind(this)}
showMapButton={mapState.mapVisible}
showMapTooltip={mapState.showMapTooltip}
hideSettings={room.type === 'ADVENTURE'}
handleOpenModal={this.handleModalChange.bind(this)}></Navigation>
<Beforeunload onBeforeunload={this.handleBeforeUnload.bind(this)} />
<Modal
isOpen={!!this.state.modal}
onRequestClose={() => this.handleModalChange(null)}
className={`${this.state.modal}-modal`}>
<ModalFactory room={this} modal={this.state.modal} />
</Modal>
{!!this.state.pokeData && <PokeNotification {...this.state.pokeData} />}
</div>
)
}
}
export default connect(state => state, {
updateUsers: reducers.updateUsersActionCreator,
updateCurrentRoom: reducers.updateCurrentRoomActionCreator,
unlockPoking: reducers.unlockPokingActionCreator
})(Room)