Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse common invite #8

Open
wants to merge 3 commits into
base: ipfs-tag
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Redux/MainRedux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ const actions = {
refreshChatRequest: createAction('REFRESH_CHAT_REQUEST'),
tagged: createAction('TAGGED', resolve => {
return (tagger: string) => resolve({ tagger })
})
}),
// Takes a heartbeat from game players in the form of a common invite to use
newSharedInvite: createAction('NEW_SHARED_INVITE', resolve => {
return (id: string, key: string, date: number) => resolve({ id, key, date })
}),
}

export type MainActions = ActionType<typeof actions>
Expand Down Expand Up @@ -130,6 +134,11 @@ export interface MainState {
gameInvite?: IExternalInvite
gameLog: Tags[]
chat: IText[]
sharedInvite?: {
id: string,
key: string,
date: number
}
}

const initialState: MainState = {
Expand All @@ -143,6 +152,12 @@ const initialState: MainState = {

export function reducer(state = initialState, action: MainActions) {
switch (action.type) {
case getType(actions.newSharedInvite): {
if (state.sharedInvite && state.sharedInvite.date && state.sharedInvite.date < action.payload.date) {
return state
}
return { ...state, sharedInvite: action.payload}
}
case getType(actions.leaveGame): {
return { ...state, gameInfo: {started: false, duration: (24 * 3600)}}
}
Expand Down Expand Up @@ -253,6 +268,7 @@ export const MainSelectors = {
contacts: (state: RootState) => state.main.gameInfo.contacts,
currentIt: (state: RootState) => state.main.gameInfo.currentIt,
duration: (state: RootState) => state.main.gameInfo.duration,
chat: (state: RootState) => state.main.chat
chat: (state: RootState) => state.main.chat,
sharedInvite: (state: RootState) => state.main.sharedInvite
}
export default actions
8 changes: 8 additions & 0 deletions src/Sagas/SubSagas/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ export function* checkGameStatus(thread: Thread) {
const json = Buffer.from(data).toString()
const gameEvent = JSON.parse(json)

// Whenever a user creates a new invite, they share it with all players so it be reused for some period
if (gameEvent.event === 'invite' && gameEvent.id && gameEvent.key) {
console.log('GOT INVITE')
yield put(MainActions.newSharedInvite(gameEvent.id, gameEvent.key, file.date.seconds as number))
continue
}

// Deal with the very first start event
if (started === false && actor === initiator && gameEvent.event === 'start') {
started = true
Expand Down Expand Up @@ -184,6 +191,7 @@ export function* checkGameStatus(thread: Thread) {
if (started) {
yield put(MainActions.startGameSuccess(startTime))
}

yield put(MainActions.setCurrentIt(tagged))

yield call(refreshGameContacts, thread)
Expand Down
33 changes: 30 additions & 3 deletions src/Sagas/SubSagas/Invites.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { takeLatest, put, call, select } from 'redux-saga/effects'
import { delay } from 'redux-saga'
import { ActionType } from 'typesafe-actions'
import { Buffer } from 'buffer'
import MainActions, {MainSelectors} from '../../Redux/MainRedux'
import Textile from '@textile/react-native-sdk'
import Textile, { IExternalInvite } from '@textile/react-native-sdk'
import { Alert } from 'react-native'
import { collectThreads } from '../MainSagas'

Expand Down Expand Up @@ -65,11 +66,37 @@ import { collectThreads } from '../MainSagas'
// }
// }

function * transmitHeartbeat(invite: IExternalInvite, date: number) {
try {
yield put(MainActions.newSharedInvite(invite.id, invite.key, date))
const gameThread = yield select(MainSelectors.gameThread)
if (gameThread) {
const payload = JSON.stringify({ "event": "invite", "id": invite.id, "key": invite.key})
const input = Buffer.from(payload).toString('base64')
yield call(Textile.files.addData, input, gameThread.id)
}
} catch (err) {
console.log(err)
}
}

export function* generateNewInvite(action: ActionType<typeof MainActions.generateNewInvite>) {
const gameThread = yield select(MainSelectors.gameThread)
if (gameThread) {
const invite = yield call([Textile.invites, 'addExternal'], gameThread.id)
yield put(MainActions.generateNewInviteSuccess(invite))
// check if we have a shared invite that is less than 30m old
const sharedInvite = yield select(MainSelectors.sharedInvite)
const date = (new Date().getTime()) / 1000
if (sharedInvite && sharedInvite.date && date - sharedInvite.date < 1800 && date - sharedInvite.date > 0) {
// if we do have a valid shared invite, use it instead of creating a new one
const invite: IExternalInvite = {id: sharedInvite.id, key: sharedInvite.key, inviter: ''}
yield put(MainActions.generateNewInviteSuccess(invite))
yield call(transmitHeartbeat, invite, date)
} else {
// no valid shared invite, so create a novel external invite and share it with the rest of the players
const invite = yield call([Textile.invites, 'addExternal'], gameThread.id)
yield put(MainActions.generateNewInviteSuccess(invite))
yield call(transmitHeartbeat, invite, date)
}
}
}

Expand Down