-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #497 from gear-foundation/vt-galactic-express-add-…
…sails Galactic Express: add sails
- Loading branch information
Showing
44 changed files
with
1,100 additions
and
404 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { usePending } from './use-pending'; | ||
export * from './use-sign-and-send'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useAtom } from 'jotai'; | ||
import { IS_LOADING } from 'atoms'; | ||
|
||
export function usePending() { | ||
const [pending, setPending] = useAtom(IS_LOADING); | ||
|
||
return { pending, setPending }; | ||
} |
44 changes: 44 additions & 0 deletions
44
frontend/apps/galactic-express/src/app/hooks/use-sign-and-send.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { usePending } from './'; | ||
import { useCheckBalance } from '@dapps-frontend/hooks'; | ||
import { useAlert } from '@gear-js/react-hooks'; | ||
import { GenericTransactionReturn, TransactionReturn } from '@gear-js/react-hooks/dist/esm/hooks/sails/types'; | ||
|
||
export type Options = { | ||
onSuccess?: () => void; | ||
onError?: (error?: Error) => void; | ||
}; | ||
|
||
export const useSignAndSend = () => { | ||
const { checkBalance } = useCheckBalance(); | ||
const { setPending } = usePending(); | ||
const alert = useAlert(); | ||
|
||
const signAndSend = async ( | ||
transaction: TransactionReturn<() => GenericTransactionReturn<null>>, | ||
options?: Options, | ||
) => { | ||
const { onSuccess, onError } = options || {}; | ||
const calculatedGas = Number(transaction.extrinsic.args[2].toString()); | ||
checkBalance( | ||
calculatedGas, | ||
async () => { | ||
try { | ||
const { response } = await transaction.signAndSend(); | ||
await response(); | ||
onSuccess?.(); | ||
setPending(false); | ||
} catch (e) { | ||
onError?.(e as Error); | ||
setPending(false); | ||
console.error(e); | ||
if (typeof e === 'string') { | ||
alert.error(e); | ||
} | ||
} | ||
}, | ||
onError, | ||
); | ||
}; | ||
|
||
return { signAndSend }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './sails'; |
2 changes: 2 additions & 0 deletions
2
frontend/apps/galactic-express/src/app/utils/sails/events/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { useEventGameCanceledSubscription } from './use-event-game-canceled-subscription'; | ||
export { useEventPlayerDeletedSubscription } from './use-event-player-deleted-subscription'; |
22 changes: 22 additions & 0 deletions
22
.../apps/galactic-express/src/app/utils/sails/events/use-event-game-canceled-subscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useProgramEvent } from '@gear-js/react-hooks'; | ||
import { useProgram } from 'app/utils'; | ||
import { REGISTRATION_STATUS } from 'atoms'; | ||
import { useSetAtom } from 'jotai'; | ||
|
||
export function useEventGameCanceledSubscription(isUserAdmin: boolean) { | ||
const program = useProgram(); | ||
const setRegistrationStatus = useSetAtom(REGISTRATION_STATUS); | ||
|
||
const onData = () => { | ||
if (!isUserAdmin) { | ||
setRegistrationStatus('GameCanceled'); | ||
} | ||
}; | ||
|
||
useProgramEvent({ | ||
program, | ||
serviceName: 'galacticExpress', | ||
functionName: 'subscribeToGameCanceledEvent', | ||
onData, | ||
}); | ||
} |
24 changes: 24 additions & 0 deletions
24
...apps/galactic-express/src/app/utils/sails/events/use-event-player-deleted-subscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { HexString } from '@gear-js/api'; | ||
import { useProgramEvent, useAccount } from '@gear-js/react-hooks'; | ||
import { useProgram } from 'app/utils'; | ||
import { REGISTRATION_STATUS } from 'atoms'; | ||
import { useSetAtom } from 'jotai'; | ||
|
||
export function useEventPlayerDeletedSubscription() { | ||
const program = useProgram(); | ||
const { account } = useAccount(); | ||
const setRegistrationStatus = useSetAtom(REGISTRATION_STATUS); | ||
|
||
const onData = ({ player_id }: { player_id: HexString }) => { | ||
if (account?.decodedAddress === player_id) { | ||
setRegistrationStatus('PlayerRemoved'); | ||
} | ||
}; | ||
|
||
useProgramEvent({ | ||
program, | ||
serviceName: 'galacticExpress', | ||
functionName: 'subscribeToPlayerDeletedEvent', | ||
onData, | ||
}); | ||
} |
90 changes: 90 additions & 0 deletions
90
frontend/apps/galactic-express/src/app/utils/sails/galactic-express.idl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
type Participant = struct { | ||
id: actor_id, | ||
name: str, | ||
fuel_amount: u8, | ||
payload_amount: u8, | ||
}; | ||
|
||
type State = struct { | ||
games: vec struct { actor_id, GameState }, | ||
player_to_game_id: vec struct { actor_id, actor_id }, | ||
dns_info: opt struct { actor_id, str }, | ||
admin: actor_id, | ||
}; | ||
|
||
type GameState = struct { | ||
admin: actor_id, | ||
admin_name: str, | ||
altitude: u16, | ||
weather: Weather, | ||
reward: u128, | ||
stage: StageState, | ||
bid: u128, | ||
}; | ||
|
||
type Weather = enum { | ||
Clear, | ||
Cloudy, | ||
Rainy, | ||
Stormy, | ||
Thunder, | ||
Tornado, | ||
}; | ||
|
||
type StageState = enum { | ||
Registration: vec struct { actor_id, Participant }, | ||
Results: Results, | ||
}; | ||
|
||
type Results = struct { | ||
turns: vec vec struct { actor_id, Turn }, | ||
rankings: vec struct { actor_id, u128 }, | ||
participants: vec struct { actor_id, Participant }, | ||
}; | ||
|
||
type Turn = enum { | ||
Alive: struct { fuel_left: u8, payload_amount: u8 }, | ||
Destroyed: HaltReason, | ||
}; | ||
|
||
type HaltReason = enum { | ||
PayloadOverload, | ||
FuelOverload, | ||
SeparationFailure, | ||
AsteroidCollision, | ||
FuelShortage, | ||
EngineFailure, | ||
}; | ||
|
||
constructor { | ||
New : (dns_id_and_name: opt struct { actor_id, str }); | ||
}; | ||
|
||
service GalacticExpress { | ||
CancelGame : () -> null; | ||
CancelRegister : () -> null; | ||
ChangeAdmin : (new_admin: actor_id) -> null; | ||
CreateNewSession : (name: str) -> null; | ||
DeletePlayer : (player_id: actor_id) -> null; | ||
Kill : (inheritor: actor_id) -> null; | ||
LeaveGame : () -> null; | ||
Register : (creator: actor_id, participant: Participant) -> null; | ||
StartGame : (fuel_amount: u8, payload_amount: u8) -> null; | ||
query Admin : () -> actor_id; | ||
query All : () -> State; | ||
query DnsInfo : () -> opt struct { actor_id, str }; | ||
query GetGame : (player_id: actor_id) -> opt GameState; | ||
|
||
events { | ||
GameFinished: Results; | ||
NewSessionCreated: struct { altitude: u16, weather: Weather, reward: u128, bid: u128 }; | ||
Registered: struct { actor_id, Participant }; | ||
RegistrationCanceled; | ||
PlayerDeleted: struct { player_id: actor_id }; | ||
GameCanceled; | ||
GameLeft; | ||
AdminChanged: struct { new_admin: actor_id }; | ||
Killed: struct { inheritor: actor_id }; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './sails'; | ||
export * from './lib'; | ||
export * from './events'; | ||
export * from './queries'; | ||
export * from './messages'; |
Oops, something went wrong.