Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into vt-tamagotchi-battl…
Browse files Browse the repository at this point in the history
…e-new
  • Loading branch information
vraja-nayaka committed Oct 31, 2024
2 parents 0b9fb58 + a32f55f commit 8355dad
Show file tree
Hide file tree
Showing 73 changed files with 2,374 additions and 719 deletions.
2 changes: 2 additions & 0 deletions frontend/apps/galactic-express/src/app/hooks/index.ts
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';
8 changes: 8 additions & 0 deletions frontend/apps/galactic-express/src/app/hooks/use-pending.ts
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 frontend/apps/galactic-express/src/app/hooks/use-sign-and-send.ts
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 };
};
1 change: 1 addition & 0 deletions frontend/apps/galactic-express/src/app/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './sails';
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';
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,
});
}
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,
});
}
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 };
}
};

5 changes: 5 additions & 0 deletions frontend/apps/galactic-express/src/app/utils/sails/index.ts
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';
Loading

0 comments on commit 8355dad

Please sign in to comment.