Skip to content

Commit

Permalink
Yd/battle update state (#244)
Browse files Browse the repository at this point in the history
Co-authored-by: Yauheni <[email protected]>
  • Loading branch information
YauheniDraichykau and Yauheni authored Feb 15, 2024
1 parent 416ad56 commit fefbbc9
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 8 deletions.
13 changes: 11 additions & 2 deletions frontend/apps/tamagotchi-battle/src/app/hooks/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getStateMetadata, ProgramMetadata, StateMetadata } from '@gear-js/api';
import { Buffer } from 'buffer';
import { useAlert, useReadFullState } from '@gear-js/react-hooks';
import { HexString } from '@polkadot/util/types';
import { AnyJson } from '@polkadot/types/types';

export function useProgramMetadata(source: string) {
const alert = useAlert();
Expand Down Expand Up @@ -47,7 +48,15 @@ export function useStateMetadata(source: string) {
return data;
}

export function useReadState<T>({ programId, meta }: { programId?: HexString; meta: string }) {
export function useReadState<T>({
programId,
meta,
payload,
}: {
programId?: HexString;
meta: string;
payload: AnyJson;
}) {
const metadata = useProgramMetadata(meta);
return useReadFullState<T>(programId, metadata, '0x');
return useReadFullState<T>(programId, metadata, payload);
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0002000000010000000001060000000000000000010a000000211e8c00085074616d61676f746368695f626174746c655f696f30426174746c65416374696f6e000118445374617274526567697374726174696f6e000000205265676973746572040118746d675f696404013054616d61676f746368694964000100204d616b654d6f766508011c706169725f69640c0118506169724964000120746d675f6d6f76651001104d6f76650002002c5374617274426174746c650003002041646441646d696e040004011c4163746f7249640004003c436865636b49664d6f76654d61646508011c706169725f69640c0118506169724964000118746d675f69641401504f7074696f6e3c54616d61676f7463686949643e000500000410106773746418636f6d6d6f6e287072696d6974697665731c4163746f724964000004000801205b75383b2033325d000008000003200000000c000c000005030010085074616d61676f746368695f626174746c655f696f104d6f76650001081841747461636b0000001c446566656e6365000100001404184f7074696f6e04045401040108104e6f6e6500000010536f6d65040004000001000018085074616d61676f746368695f626174746c655f696f2c426174746c654576656e7400012c4c526567697374726174696f6e537461727465640000002852656769737465726564040118746d675f696404013054616d61676f746368694964000100204d6f76654d61646500020040476f546f57616974696e6753746174650003002847616d6549734f7665720004002c496e666f557064617465640005001c4e657747616d6500060034426174746c65537461727465640007002c526f756e64526573756c7404001c01b8285061697249642c207531362c207531362c204f7074696f6e3c4d6f76653e2c204f7074696f6e3c4d6f76653e29000800204e6577526f756e640009002841646d696e4164646564000a00001c000004140c20202424002000000504002404184f7074696f6e04045401100108104e6f6e6500000010536f6d65040010000001000028085074616d61676f746368695f626174746c655f696f18426174746c65000028011861646d696e732c01305665633c4163746f7249643e00011c706c617965727330016442547265654d61703c4163746f7249642c20506c617965723e00012c706c61796572735f6964732c01305665633c4163746f7249643e00013c63757272656e745f706c61796572732c01305665633c4163746f7249643e00011473746174654c012c426174746c65537461746500013863757272656e745f77696e6e657204011c4163746f724964000114706169727350015842547265654d61703c5061697249642c20506169723e000140706c61796572735f746f5f70616972736c017842547265654d61703c4163746f7249642c205665633c5061697249643e3e00013c636f6d706c657465645f67616d65730c010875380001307265736572766174696f6e737c018042547265654d61703c4163746f7249642c205265736572766174696f6e49643e00002c000002040030042042547265654d617008044b0104045601340004004400000034085074616d61676f746368695f626174746c655f696f18506c6179657200002401146f776e657204011c4163746f7249640001106e616d65380118537472696e67000134646174655f6f665f62697274683c010c753634000118746d675f696404013054616d61676f74636869496400011c646566656e636520010c753136000114706f77657220010c7531360001186865616c746820010c753136000114636f6c6f72380118537472696e67000124766963746f7269657340010c75333200003800000502003c000005060040000005050044000002480048000004080434004c085074616d61676f746368695f626174746c655f696f2c426174746c65537461746500011030526567697374726174696f6e0000002047616d6549734f6e00010034576169744e657874526f756e640002002847616d6549734f7665720003000050042042547265654d617008044b010c045601540004006400000054085074616d61676f746368695f626174746c655f696f105061697200002001246f776e65725f6964732c01305665633c4163746f7249643e00011c746d675f6964732c01305665633c4163746f7249643e0001146d6f7665735801445665633c4f7074696f6e3c4d6f76653e3e000118726f756e64730c0108753800013067616d655f69735f6f7665725c0110626f6f6c00011877696e6e657204011c4163746f7249640001346d6f76655f646561646c696e653c010c7536340001186d73675f69646001244d657373616765496400005800000224005c00000500006010106773746418636f6d6d6f6e287072696d697469766573244d6573736167654964000004000801205b75383b2033325d000064000002680068000004080c54006c042042547265654d617008044b01040456017000040074000000700000020c0074000002780078000004080470007c042042547265654d617008044b010404560180000400840000008010106773746418636f6d6d6f6e287072696d697469766573345265736572766174696f6e4964000004000801205b75383b2033325d00008400000288008800000408048000
0002000100000000000105000000010900000000000000010f000000011000000099298800085074616d61676f746368695f626174746c655f696f18436f6e66696700002001246d61785f706f77657204010c7531360001246d696e5f706f77657204010c7531360001186865616c746804010c7531360001406d61785f7061727469636970616e747308010875380001486d61785f73746570735f696e5f726f756e64080108753800013474696d655f666f725f6d6f76650c010c7533320001386d696e5f6761735f616d6f756e7410010c753634000144626c6f636b5f6475726174696f6e5f6d7310010c75363400000400000504000800000503000c000005050010000005060014085074616d61676f746368695f626174746c655f696f30426174746c65416374696f6e000114445374617274526567697374726174696f6e000000205265676973746572040118746d675f696418013054616d61676f746368694964000100204d616b654d6f766508011c706169725f6964080118506169724964000120746d675f6d6f76652001104d6f76650002002c5374617274426174746c650003002041646441646d696e040018011c4163746f724964000400001810106773746418636f6d6d6f6e287072696d6974697665731c4163746f724964000004001c01205b75383b2033325d00001c00000320000000080020085074616d61676f746368695f626174746c655f696f104d6f76650001081841747461636b0000001c446566656e636500010000240418526573756c740804540128044501380108084f6b040028000000000c457272040038000001000028085074616d61676f746368695f626174746c655f696f2c426174746c655265706c790001344c526567697374726174696f6e537461727465640000002852656769737465726564040118746d675f696418013054616d61676f746368694964000100204d6f76654d61646500020040476f546f57616974696e6753746174650003002847616d6549734f7665720004002c496e666f557064617465640005001c4e657747616d6500060034426174746c65537461727465640007002c526f756e64526573756c7404002c01b8285061697249642c207531362c207531362c204f7074696f6e3c4d6f76653e2c204f7074696f6e3c4d6f76653e29000800204e6577526f756e640009002841646d696e4164646564000a0048426174746c6557617343616e63656c6c6564000b003047616d6546696e697368656404011c706c61796572733401305665633c4163746f7249643e000c00002c000004140804043030003004184f7074696f6e04045401200108104e6f6e6500000010536f6d65040020000001000034000002180038085074616d61676f746368695f626174746c655f696f2c426174746c654572726f720001302857726f6e675374617465000000404e6f74456e6f756768506c6179657273000100204e6f7441646d696e0002002847616d6549734f7665720003004050616972446f65734e6f74457869737400040048506c61796572446f65734e6f744578697374000500404e6f47616d6573466f72506c61796572000600344e6f74506c6179657247616d650007004c4d61784e756d6265725761735265616368656400080024546d67496e47616d650009002c4e6f74546d674f776e6572000a004454616d61676f7463686948617344696564000b00003c085074616d61676f746368695f626174746c655f696f2c426174746c65517565727900012824476574506c61796572040118746d675f696418011c4163746f7249640000001c506c617965727300010028506c61796572734964730002001453746174650003002047657450616972730004001c4765745061697204011c706169725f69640801185061697249640005001841646d696e730006003843757272656e74506c617965727300070038436f6d706c6574656447616d65730008001857696e6e65720009000040085074616d61676f746368695f626174746c655f696f40426174746c6551756572795265706c7900012818506c61796572040118706c617965724401384f7074696f6e3c506c617965723e0000001c506c617965727304011c706c617965727350016442547265654d61703c4163746f7249642c20506c617965723e00010028506c617965727349647304012c706c61796572735f6964733401305665633c4163746f7249643e00020014537461746504011473746174655c012c426174746c655374617465000300145061697273040114706169727360015842547265654d61703c5061697249642c20506169723e0004001050616972040110706169728401304f7074696f6e3c506169723e0005001841646d696e7304011861646d696e733401305665633c4163746f7249643e0006003843757272656e74506c617965727304013c63757272656e745f706c61796572733401305665633c4163746f7249643e00070038436f6d706c6574656447616d657304013c636f6d706c657465645f67616d657308010875380008001857696e6e657204011877696e6e657218011c4163746f724964000900004404184f7074696f6e04045401480108104e6f6e6500000010536f6d65040048000001000048085074616d61676f746368695f626174746c655f696f18506c6179657200002401146f776e657218011c4163746f7249640001106e616d654c0118537472696e67000134646174655f6f665f626972746810010c753634000118746d675f696418013054616d61676f74636869496400011c646566656e636504010c753136000114706f77657204010c7531360001186865616c746804010c753136000114636f6c6f724c0118537472696e67000124766963746f726965730c010c75333200004c000005020050042042547265654d617008044b0118045601480004005400000054000002580058000004081848005c085074616d61676f746368695f626174746c655f696f2c426174746c65537461746500011030526567697374726174696f6e0000002047616d6549734f6e00010034576169744e657874526f756e640002002847616d6549734f7665720003000060042042547265654d617008044b0108045601640004007c00000064085074616d61676f746368695f626174746c655f696f105061697200002401246f776e65725f6964733401305665633c4163746f7249643e00011c746d675f6964733401305665633c4163746f7249643e0001146d6f7665736801445665633c4f7074696f6e3c4d6f76653e3e000118726f756e6473080108753800013067616d655f69735f6f7665726c0110626f6f6c00011877696e6e657218011c4163746f7249640001306c6173745f7570646174656410010c75363400014c6d73675f6964735f696e5f776169746c69737470014c42547265655365743c4d65737361676549643e00015c616d6f756e745f6f665f736b69707065645f6d6f766573080108753800006800000230006c000005000070042042547265655365740404540174000400780000007410106773746418636f6d6d6f6e287072696d697469766573244d6573736167654964000004001c01205b75383b2033325d00007800000274007c000002800080000004080864008404184f7074696f6e04045401640108104e6f6e6500000010536f6d650400640000010000
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useEffect, useRef } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAccount, useApi, useSendMessage, useSendMessageHandler } from '@gear-js/react-hooks';
import { useProgramMetadata, useReadState } from 'app/hooks/api';
import { useAccount, useApi, useSendMessageHandler } from '@gear-js/react-hooks';
import { useProgramMetadata } from 'app/hooks/api';
import { useBattle } from '../context';
import { BATTLE_ADDRESS } from '../consts';
import meta from '../assets/meta/battle.meta.txt';
import type { HexString, UserMessageSent } from '@gear-js/api';
import type { UnsubscribePromise } from '@polkadot/api/types';
import type { BattleStatePlayer, BattleStateResponse } from '../types/battles';
import type { BattleStatePlayer } from '../types/battles';
import type { BattleCurrentStateVariants, RoundDamageType } from '../types/battles';
import { useFullState } from './use-full-state';

const programId = BATTLE_ADDRESS;

Expand All @@ -27,7 +28,9 @@ export function useInitBattleData() {
setPlayers,
setIsAdmin,
} = useBattle();
const { state } = useReadState<BattleStateResponse>({ programId, meta });

const state = useFullState(programId, meta);

const prevBattleState = useRef<BattleCurrentStateVariants | undefined>();
const metadata = useProgramMetadata(meta);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { useReadState } from 'app/hooks/api';
import { useMemo } from 'react';
import {
BattleAdminsResponse,
BattleCompletedGamesResponse,
BattleCurrentPlayersResponse,
BattleCurrentWinnerResponse,
BattleGameStateResponse,
BattlePairsResponse,
BattlePlayersIdsResponse,
BattlePlayersResponse,
} from '../types/battles';
import { HexString } from '@gear-js/api';

export const useFullState = (programId: HexString, meta: string) => {
const getPairsPayload = useMemo(() => ({ GetPairs: null }), []);
const adminsPayload = useMemo(() => ({ Admins: null }), []);
const playersPayload = useMemo(() => ({ Players: null }), []);
const playerIdsPayload = useMemo(() => ({ PlayersIds: null }), []);
const currentPlayersPayload = useMemo(() => ({ CurrentPlayers: null }), []);
const completedGamesPayload = useMemo(() => ({ CompletedGames: null }), []);
const winnerPayload = useMemo(() => ({ Winner: null }), []);
const statePayload = useMemo(() => ({ State: null }), []);

const { state: pairsState } = useReadState<BattlePairsResponse>({ programId, meta, payload: getPairsPayload });
const { state: adminsState } = useReadState<BattleAdminsResponse>({ programId, meta, payload: adminsPayload });
const { state: playersState } = useReadState<BattlePlayersResponse>({ programId, meta, payload: playersPayload });
const { state: playersIdsState } = useReadState<BattlePlayersIdsResponse>({
programId,
meta,
payload: playerIdsPayload,
});
const { state: currentPlayersState } = useReadState<BattleCurrentPlayersResponse>({
programId,
meta,
payload: currentPlayersPayload,
});
const { state: completedGamesState } = useReadState<BattleCompletedGamesResponse>({
programId,
meta,
payload: completedGamesPayload,
});
const { state: currentWinnerState } = useReadState<BattleCurrentWinnerResponse>({
programId,
meta,
payload: winnerPayload,
});
const { state: battleState } = useReadState<BattleGameStateResponse>({ programId, meta, payload: statePayload });

const state = useMemo(
() =>
!!pairsState &&
!!adminsState &&
!!currentPlayersState &&
!!playersState &&
!!completedGamesState &&
!!currentWinnerState &&
!!battleState &&
!!playersIdsState
? {
admins: adminsState.Admins.admins,
completedGames: completedGamesState.CompletedGames.completedGames,
currentWinner: currentWinnerState.Winner.winner,
players: playersState.Players.players,
playersIds: playersIdsState.PlayersIds.playersIds,
currentPlayers: currentPlayersState.CurrentPlayers.currentPlayers,
state: battleState.State.state,
pairs: pairsState.Pairs.pairs,
}
: undefined,
[
pairsState,
adminsState,
currentPlayersState,
playersState,
completedGamesState,
currentWinnerState,
battleState,
playersIdsState,
],
);

return state;
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,51 @@ export type BattleStateResponse = {
admins: HexString[];
completedGames: string;
currentWinner: HexString;
pairs: Record<string, BattleStatePair>;
players: Record<HexString, BattleStatePlayer>;
playersIds: HexString[];
currentPlayers: HexString[];
state: BattleCurrentStateVariants;
pairs: Record<string, BattleStatePair>;
};

export type BattlePairsResponse = {
Pairs: { pairs: Record<string, BattleStatePair> };
};

export type BattleCompletedGamesResponse = {
CompletedGames: { completedGames: string };
};

export type BattleCurrentWinnerResponse = {
Winner: { winner: HexString };
};

export type BattleAdminsResponse = {
Admins: {
admins: HexString[];
};
};

export type BattleGameStateResponse = {
State: {
state: BattleCurrentStateVariants;
};
};

export type BattlePlayersResponse = {
Players: {
players: Record<HexString, BattleStatePlayer>;
};
};

export type BattlePlayersIdsResponse = {
PlayersIds: {
playersIds: HexString[];
};
};

export type BattleCurrentPlayersResponse = {
CurrentPlayers: {
currentPlayers: HexString[];
};
};

0 comments on commit fefbbc9

Please sign in to comment.