Skip to content

Commit

Permalink
migrate to ethers
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jun 3, 2021
1 parent 53497ef commit 6764a3e
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 68 deletions.
44 changes: 37 additions & 7 deletions ecs/Bicho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,41 @@ interface UpdateEvent {
rotation?: ReadOnlyVector3;
}

export enum BichoType {
Avestruz,
Águia,
Burro,
Borboleta,
Cachorro,
Cabra,
Carneiro,
Camelo,
Cobra,
Coelho,
Cavalo,
Elefante,
Galo,
Gato,
Jacaré,
Leão,
Macaco,
Porco,
Pavão,
Peru,
Touro,
Tigre,
Urso,
Veado,
Vaca,
COUNT,
}

export default class Bicho extends NPC {
static contract: any;

address: string;

id: number;
type: BichoType;

bus: MessageBus;

Expand All @@ -30,17 +59,18 @@ export default class Bicho extends NPC {

constructor(
address: string,
id: number,
type: BichoType,
bus: MessageBus,
transformArgs: TransformConstructorArgs,
remote = false,
) {
super(transformArgs, placeholder, () => {});
super(transformArgs, placeholder, () => {},
{ onlyClickTrigger: true, onlyETrigger: true, walkingAnim: 'Walk' });
this.address = address;
this.id = id;
this.type = type;
this.bus = bus;
this.remote = remote;
this.eventId = `${this.address}[${this.id}]`;
this.eventId = `${this.address}[${this.type}]`;
this.bus.on(this.eventId, (value) => this.update(value));
}

Expand All @@ -49,11 +79,11 @@ export default class Bicho extends NPC {
}

update({ position, rotation }: UpdateEvent) {
if (!this.remote) this.bus.emit('bicho', { address: this.address, id: this.id });
if (!this.remote) this.bus.emit('bicho', { address: this.address, type: this.type });
const transform = this.getComponent(Transform);
if (rotation) transform.rotation.setEuler(0, rotation.y, 0);
if (position) {
const offset = this.id % 2 ? 1 : -1;
const offset = this.type % 2 ? 1 : -1;
transform.position.set(position.x, position.y, position.z + offset);
}
}
Expand Down
27 changes: 14 additions & 13 deletions ecs/SceneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
ISystem,
MessageBus,
} from 'decentraland-ecs';
import Bicho from './Bicho';
import getUserBichos from './utils/getUserBichos';
import Bicho, { BichoType } from './Bicho';
import getUserAddress from './web3/getUserAddress';
import getUserBichos from './web3/getUserBichos';

declare const dcl: DecentralandInterface;

Expand All @@ -15,13 +16,13 @@ interface BaseEvent {
}

interface BichoEvent extends BaseEvent {
id: number;
type: BichoType;
}

export default class SceneManager implements ISystem {
bus = new MessageBus();

bichos = {} as Record<string, Record<number, Bicho>>;
bichos: { [address: string]: { [T in BichoType]?: Bicho } } = {};

engine: IEngine;

Expand All @@ -35,12 +36,12 @@ export default class SceneManager implements ISystem {
dcl.onEvent((event) => this.onDCLEvent(event));

this.bus.on('request', () => this.onRequest());
this.bus.on('bicho', ({ address, id }: BichoEvent) => this.onBicho(address, id));
this.bus.on('bicho', ({ address, type }: BichoEvent) => this.onBicho(address, type));
this.bus.on('deactivate', ({ address }: BaseEvent) => this.onDeactivate(address));
this.bus.emit('request', null);
getUserBichos().then(({ address, bichos }) => {
Promise.all([getUserAddress(), getUserBichos()]).then(([address, bichos]) => {
this.address = address;
bichos.forEach((id) => this.bus.emit('bicho', { address, id } as BichoEvent));
bichos?.forEach((type) => this.bus.emit('bicho', { address, type } as BichoEvent));
});
}

Expand All @@ -53,18 +54,18 @@ export default class SceneManager implements ISystem {
Object.values(this.bichos[this.address]).forEach((bicho) => bicho.onDCLEvent(event));
}

onBicho(address: string, id: number) {
onBicho(address: string, type: BichoType) {
this.bichos[address] ??= {};
if (this.bichos[address][id]) return;
const bicho = new Bicho(address, id, this.bus, {}, address !== this.address);
if (this.bichos[address][type]) return;
const bicho = new Bicho(address, type, this.bus, {}, address !== this.address);
this.engine.addEntity(bicho);
this.bichos[address][id] = bicho;
this.bichos[address][type] = bicho;
}

onRequest() {
if (!this.address || !this.bichos[this.address]) return;
Object.keys(this.bichos[this.address]).forEach((id) => this.bus.emit('bicho',
{ address: this.address, id: Number(id) } as BichoEvent));
Object.keys(this.bichos[this.address]).forEach((type) => this.bus.emit('bicho',
{ address: this.address, type: Number(type) } as BichoEvent));
}

onDeactivate(address: string) {
Expand Down
12 changes: 0 additions & 12 deletions ecs/utils/getRequestManager.ts

This file was deleted.

11 changes: 0 additions & 11 deletions ecs/utils/getUserAddress.ts

This file was deleted.

25 changes: 0 additions & 25 deletions ecs/utils/getUserBichos.ts

This file was deleted.

18 changes: 18 additions & 0 deletions ecs/web3/getBichoContract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Contract } from '@ethersproject/contracts';
import { Bicho as BichoContract } from '../../abi/types/Bicho';
import BichoABI from '../../abi/Bicho.json';
import getProvider from './getProvider';

declare const BICHO_ADDRESS: string;

let contract: Promise<BichoContract>;

const getContract = async () => {
const provider = await getProvider();
return new Contract(BICHO_ADDRESS, BichoABI, provider) as BichoContract;
};

export default async () => {
contract ??= getContract();
return contract;
};
23 changes: 23 additions & 0 deletions ecs/web3/getProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DecentralandInterface } from 'decentraland-ecs';
import { Web3Provider, TransactionResponse } from '@ethersproject/providers';

declare const dcl: DecentralandInterface;

class DCLProvider extends Web3Provider {
// eslint-disable-next-line class-methods-use-this
async getTransaction(hash: string | Promise<string>) {
return { hash: await hash } as TransactionResponse;
}
}

let provider: Promise<DCLProvider>;

const getProvider = async () => {
const { rpcHandle } = await dcl.loadModule('web3-provider');
return new DCLProvider(await dcl.callRpc(rpcHandle, 'getProvider', []));
};

export default async () => {
provider ??= getProvider();
return provider;
};
15 changes: 15 additions & 0 deletions ecs/web3/getUserAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DecentralandInterface } from 'decentraland-ecs';

declare const dcl: DecentralandInterface;

let address: Promise<string>;

const getAddress = async () => {
const { rpcHandle } = await dcl.loadModule('Identity');
return await dcl.callRpc(rpcHandle, 'getUserPublicKey', []) as string;
};

export default async () => {
address ??= getAddress();
return address;
};
16 changes: 16 additions & 0 deletions ecs/web3/getUserBichos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BichoType } from '../Bicho';
import getBichoContract from './getBichoContract';
import getUserAddress from './getUserAddress';

export default async () => {
const [address, contract] = await Promise.all([getUserAddress(), getBichoContract()]);
if (!address) return null;
const balances = await contract.balanceOfBatch(
Array(25).fill(address),
Array(25).fill(null).map((_, i) => i),
);
return balances
.map((balance, id) => ({ id, balance }))
.filter(({ balance }) => !balance.isZero())
.map(({ id }) => id % BichoType.COUNT as BichoType);
};

0 comments on commit 6764a3e

Please sign in to comment.