Skip to content

Commit

Permalink
feat(multiplayer): [wip] go offline when server cannot be reached
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Apr 28, 2024
1 parent 23ad3f2 commit c99454d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/components/Farmhand/Farmhand.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,15 +939,20 @@ export default class Farmhand extends FarmhandReducers {

this.showNotification(CONNECTED_TO_ROOM`${room}`, 'success')
} catch (e) {
const message = e instanceof Error ? e.message : 'Unexpected error'

// TODO: Add some reasonable fallback behavior in case the server request
// fails. Possibility: Regenerate valueAdjustments and notify the user
// they are offline.

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

console.error(e)

this.setState(() => {
return {
redirect: '/',
cowIdOfferedForTrade: '',
}
})
}

this.setState({
Expand Down Expand Up @@ -1064,11 +1069,17 @@ export default class Farmhand extends FarmhandReducers {
nextDayState.priceSurges
)
} catch (e) {
// NOTE: This will get reached when there's an issue posting data to the server.
serverMessages.push({
message: SERVER_ERROR,
severity: 'error',
})

this.setState({
redirect: '/',
cowIdOfferedForTrade: '',
})

console.error(e)
}

Expand Down
2 changes: 1 addition & 1 deletion src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const INVALID_DATA_PROVIDED = 'Invalid Farmhand data provided.'
export const UPDATE_AVAILABLE =
"A game update is available! Click this message to reload and see what's new."
export const SERVER_ERROR =
"There was an issue communicating with the server. You can keep playing offline, and you'll be reconnected as soon as things improve."
'There was an issue connecting to the server. Please try again in a moment.'
export const CONNECTING_TO_SERVER = 'Connecting...'
export const DISCONNECTING_FROM_SERVER = 'Disconnecting...'
export const DISCONNECTED_FROM_SERVER = 'You are now playing offline.'
Expand Down

0 comments on commit c99454d

Please sign in to comment.