-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
239 additions
and
103 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
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
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
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
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 |
---|---|---|
@@ -1,22 +1,121 @@ | ||
import { Account, AccountInterface } from "starknet"; | ||
import { Account, RpcProvider } from "starknet"; | ||
import { IWorld } from "../generated/typescript/contracts.gen"; | ||
import { ClientComponents } from "../createClientComponents"; | ||
import { getComponentValueStrict } from "@dojoengine/recs"; | ||
import { getEntityIdFromKeys } from "@dojoengine/utils"; | ||
import { poseidonHashMany } from "micro-starknet"; | ||
import { logDebug } from "../../ui/lib/utils"; | ||
import { uuid } from "@latticexyz/utils"; | ||
|
||
export const opBuyHero = async ( | ||
{ client }: { client: IWorld }, | ||
{ Player, Altar, PlayerInvPiece }: ClientComponents, | ||
{ Player, Altar, Piece, PlayerInvPiece, PlayerProfile }: ClientComponents, | ||
rpcProvider: RpcProvider, | ||
account: Account, | ||
altarSlot: number, | ||
invSlot: number | ||
) => { | ||
const playerEntity = getEntityIdFromKeys([BigInt(account.address)]); | ||
const playerProfile = getComponentValueStrict(PlayerProfile, playerEntity); | ||
const player = getComponentValueStrict(Player, playerEntity); | ||
const altar = getComponentValueStrict(Altar, playerEntity); | ||
const playerInvEntity = getEntityIdFromKeys([ | ||
BigInt(account.address), | ||
BigInt(invSlot), | ||
]); | ||
|
||
// check | ||
if (player.coin <= 0 || player.inventoryCount >= 6) { | ||
alert("cannot buy"); | ||
return; | ||
} | ||
|
||
playerProfile.pieceCounter += 1; | ||
|
||
const pieceGid = Number( | ||
poseidonHashMany([ | ||
BigInt(account.address), | ||
BigInt(playerProfile.pieceCounter), | ||
]) & BigInt(0xffffffff) | ||
); | ||
const pieceEntity = getEntityIdFromKeys([BigInt(pieceGid)]); | ||
|
||
const creatureId = Number(Object.entries(altar)[altarSlot][1]); | ||
|
||
player.coin -= 1; | ||
player.inventoryCount += 1; | ||
|
||
logDebug(`generate gid ${pieceGid}`); | ||
|
||
const pieceOverUuid = uuid(); | ||
Piece.addOverride(pieceOverUuid, { | ||
entity: pieceEntity, | ||
value: { | ||
gid: pieceGid, | ||
owner: BigInt(account.address), | ||
idx: 0, | ||
slot: invSlot, | ||
level: 1, | ||
creature_index: creatureId, | ||
x: 0, | ||
y: 0, | ||
}, | ||
}); | ||
|
||
// altar override | ||
const altarOverride = uuid(); | ||
const altarArray = Object.entries(altar); | ||
altarArray[altarSlot][1] = 0; | ||
Altar.addOverride(altarOverride, { | ||
entity: playerEntity, | ||
value: altarArray.reduce((accumulator, [key, value]) => { | ||
accumulator[key] = value; | ||
return accumulator; | ||
}, {}), | ||
}); | ||
|
||
// player inv piece override | ||
const playerInvOverride = uuid(); | ||
PlayerInvPiece.addOverride(playerInvOverride, { | ||
entity: playerInvEntity, | ||
value: { | ||
owner: BigInt(account.address), | ||
slot: invSlot, | ||
gid: pieceGid, | ||
}, | ||
}); | ||
|
||
// player override | ||
const playerOverride = uuid(); | ||
Player.addOverride(playerOverride, { | ||
entity: playerEntity, | ||
value: player, | ||
}); | ||
|
||
// player profile override | ||
const playerProfileOverride = uuid(); | ||
PlayerProfile.addOverride(playerProfileOverride, { | ||
entity: playerEntity, | ||
value: playerProfile, | ||
}); | ||
|
||
try { | ||
return await client.home.buyHero({ | ||
const tx = await client.home.buyHero({ | ||
account, | ||
altarSlot, | ||
invSlot, | ||
}); | ||
await rpcProvider.waitForTransaction(tx.transaction_hash, { | ||
retryInterval: 1000, | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
throw e; | ||
} finally { | ||
Piece.removeOverride(pieceOverUuid); | ||
Altar.removeOverride(altarOverride); | ||
PlayerInvPiece.removeOverride(playerInvOverride); | ||
Player.removeOverride(playerOverride); | ||
PlayerProfile.removeOverride(playerProfileOverride); | ||
} | ||
}; |
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,75 @@ | ||
import { Account, RpcProvider } from "starknet"; | ||
import { IWorld } from "../generated/typescript/contracts.gen"; | ||
import { ClientComponents } from "../createClientComponents"; | ||
import { getComponentValueStrict } from "@dojoengine/recs"; | ||
import { getEntityIdFromKeys } from "@dojoengine/utils"; | ||
import { uuid } from "@latticexyz/utils"; | ||
|
||
export const opSellHero = async ( | ||
{ client }: { client: IWorld }, | ||
{ Player, Piece, PlayerInvPiece }: ClientComponents, | ||
rpcProvider: RpcProvider, | ||
account: Account, | ||
gid: number | ||
) => { | ||
const playerEntity = getEntityIdFromKeys([BigInt(account.address)]); | ||
const player = getComponentValueStrict(Player, playerEntity); | ||
const pieceEntity = getEntityIdFromKeys([BigInt(gid)]); | ||
const piece = getComponentValueStrict(Piece, pieceEntity); | ||
const playerInvEntity = getEntityIdFromKeys([ | ||
BigInt(account.address), | ||
BigInt(piece.slot), | ||
]); | ||
const playerInvPiece = getComponentValueStrict( | ||
PlayerInvPiece, | ||
playerInvEntity | ||
); | ||
|
||
// check | ||
player.coin += 1; | ||
player.inventoryCount -= 1; | ||
|
||
const pieceOverUuid = uuid(); | ||
Piece.addOverride(pieceOverUuid, { | ||
entity: pieceEntity, | ||
value: { | ||
...piece, | ||
owner: 0n, | ||
slot: 0, | ||
}, | ||
}); | ||
|
||
// player inv piece override | ||
const playerInvOverride = uuid(); | ||
PlayerInvPiece.addOverride(playerInvOverride, { | ||
entity: playerInvEntity, | ||
value: { | ||
...playerInvPiece, | ||
gid: 0, | ||
}, | ||
}); | ||
|
||
// player override | ||
const playerOverride = uuid(); | ||
Player.addOverride(playerOverride, { | ||
entity: playerEntity, | ||
value: player, | ||
}); | ||
|
||
try { | ||
const tx = await client.home.sellHero({ | ||
account, | ||
gid, | ||
}); | ||
await rpcProvider.waitForTransaction(tx.transaction_hash, { | ||
retryInterval: 1000, | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
throw e; | ||
} finally { | ||
Piece.removeOverride(pieceOverUuid); | ||
PlayerInvPiece.removeOverride(playerInvOverride); | ||
Player.removeOverride(playerOverride); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.