Skip to content

Commit

Permalink
chore(multiplayer): remove room occupancy limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Apr 28, 2024
1 parent 408b877 commit 23ad3f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 59 deletions.
2 changes: 0 additions & 2 deletions api-etc/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ export const ACCEPTED_ORIGINS = new Set([
'https://www.farmhand.life',
'https://v6p9d9t4.ssl.hwcdn.net', // itch.io's CDN that the game is served from
])

export const MAX_ROOM_SIZE = 25
4 changes: 0 additions & 4 deletions src/common/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export const MAX_ROOM_NAME_LENGTH = 25

export const HEARTBEAT_INTERVAL_PERIOD = 10 * 1000 // 10 seconds

export const SERVER_ERRORS = {
ROOM_FULL: 'ROOM_FULL',
}
61 changes: 8 additions & 53 deletions src/components/Farmhand/Farmhand.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,13 @@ import {
Z_INDEX,
STANDARD_VIEW_LIST,
} from '../../constants'
import {
HEARTBEAT_INTERVAL_PERIOD,
SERVER_ERRORS,
} from '../../common/constants'
import { HEARTBEAT_INTERVAL_PERIOD } from '../../common/constants'
import {
CONNECTED_TO_ROOM,
LOAN_INCREASED,
POSITIONS_POSTED_NOTIFICATION,
RECIPE_LEARNED,
RECIPES_LEARNED,
ROOM_FULL_NOTIFICATION,
} from '../../templates'
import {
CONNECTING_TO_SERVER,
Expand Down Expand Up @@ -638,7 +634,7 @@ export default class Farmhand extends FarmhandReducers {
await this.initializeNewGame()
}

this.syncToRoom().catch(errorCode => this.handleRoomSyncError(errorCode))
this.syncToRoom()

this.setState({ hasBooted: true })
}
Expand Down Expand Up @@ -683,7 +679,7 @@ export default class Farmhand extends FarmhandReducers {
}

if (isOnline !== prevState.isOnline || room !== prevState.room) {
this.syncToRoom().catch(errorCode => this.handleRoomSyncError(errorCode))
this.syncToRoom()

if (!isOnline && typeof heartbeatTimeoutId === 'number') {
clearTimeout(heartbeatTimeoutId)
Expand Down Expand Up @@ -914,18 +910,10 @@ export default class Farmhand extends FarmhandReducers {

this.state.peerRoom?.leave()

const { errorCode, valueAdjustments } = await getData(
endpoints.getMarketData,
{
farmId: this.state.id,
room: room,
}
)

if (errorCode) {
// Bail out and move control to this try's catch
throw new Error(errorCode)
}
const { valueAdjustments } = await getData(endpoints.getMarketData, {
farmId: this.state.id,
room: room,
})

this.scheduleHeartbeat()

Expand Down Expand Up @@ -957,12 +945,7 @@ export default class Farmhand extends FarmhandReducers {
// fails. Possibility: Regenerate valueAdjustments and notify the user
// they are offline.

if (SERVER_ERRORS[message]) {
// Bubble up the errorCode to be handled by game logic
throw message
}

this.showNotification(SERVER_ERROR, 'error')
this.showNotification(`Server error: ${message}`, 'error')

console.error(e)
}
Expand All @@ -973,34 +956,6 @@ export default class Farmhand extends FarmhandReducers {
})
}

handleRoomSyncError(errorCode) {
const { room } = this.state

switch (errorCode) {
case SERVER_ERRORS.ROOM_FULL:
const roomNameChunks = room.split('-')
const roomNumber = parseInt(roomNameChunks.slice(-1)[0]) // May be NaN
const nextRoomNumber = isNaN(roomNumber) ? 2 : roomNumber + 1
const roomBaseName = roomNameChunks
.slice(0, isNaN(roomNumber) ? undefined : -1)
.join('-')
const nextRoom = `${roomBaseName}-${nextRoomNumber}`

this.showNotification(
ROOM_FULL_NOTIFICATION`${room}${nextRoom}`,
'warning'
)

this.setState(() => ({
redirect: `/online/${encodeURIComponent(nextRoom)}`,
}))

break

default:
}
}

scheduleHeartbeat() {
const { heartbeatTimeoutId } = this.state
clearTimeout(heartbeatTimeoutId ?? -1)
Expand Down

0 comments on commit 23ad3f2

Please sign in to comment.