Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Position Validation #31

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/contract/cbor.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const CBOR = `5905a70100003232323232323223232323232323232322322533300e32323232323232325333016301430173754016264a66602e602a60306ea80044cc00800cdd7180e180c9baa00116301b301c301c3018375402a264a66602e602460306ea80204c8c8c94ccc068c060c06cdd500089919299980e180d180e9baa001132533301d3015301e37540022646464a666040603c60426ea80044c94ccc084c8c8c8c8c8c8c8c8c8c8c94ccc0b14ccc0b0cdc424000605a6ea8c0c402c5288a99981619981619baf0070024a0944528899981619baf0090044a09445280991929998171816000899299981798168008a511533302f302a00114a22940c0bcdd50010a9998171814800899299981798138008a511533302f302a00114a22940c0bcdd5001099299981798138008a511533302f302d00114a22940c0bcdd500118171baa3032302f375400e6062605c6ea8004c0c0c0c4008c0bc004c0bc008c0b4004c0b4c0a4dd500518159816001181500098150011814000981400098119baa02013300c00d00114a06eb8c094c088dd50008b1812181298109baa01e30150013022301f37540022c604260446044603c6ea8c084c078dd50008b19802004119baf3004301e3754002004603e60386ea8c07cc080c070dd5180f980e1baa00116323300300923375e6006603a6ea8004008c078c06cdd50051180f00091191980080080191299980f0008a60103d87a800013232533301d300500213374a90001981080125eb804cc010010004c088008c08000458c06cc060dd500591191980080080191299980e0008a50132533301a3371e6eb8c07c008010528899801801800980f8009bac3019301a301a301a301a301a301a00237586030002603060300046eb0c058004c048dd5180a801180a180a80098081baa00114984d958c94ccc034c02c00454ccc040c03cdd50010a4c2c2a66601a601000226464a666024602a004264932999807980698081baa00113232323232323232533301a301d002132498cc0400048c94ccc064c05c00454ccc070c06cdd50010a4c2c2a66603260280022a66603860366ea80085261616301937540022c6eb0c06c004c06c008dd6980c800980c8011bad30170013017002375a602a00260226ea80045858c04c004c03cdd50010b18069baa00130010092533300a3008300b37540022646464646464646464646464a666032603800426464646464931980980291bad0013301200623016001533301830163019375400e26464646464646464a666046604c004264646464931810002180f802980e803192999810980f8008a99981218119baa00814985854ccc084c07000454ccc090c08cdd50040a4c2c2a66604260320022a66604860466ea802052616163021375400e2c604800260480046044002604400460400026040004603c00260346ea801c58c044020c04002458dd6180d000980d0011bac3018001301800230160013016002301400130140023012001301200232533300f300e0011533300c3007300d00114a22a6660186014601a00229405858dd5180800098061baa00116370e90021119198008008019129998070008a4c26466006006602400460066020002464a666010600c00226464a66601a60200042930b1bae300e001300a37540042a666010600600226464a66601a60200042930b1bae300e001300a37540042c60106ea8004dc3a40044a66600a6006600c6ea80044c8c8c8c94ccc030c03c0084c92653330093007300a37540062646464646464646464646464a66603060360042930b1bad30190013019002375a602e002602e0046eb4c054004c054008dd6980980098098011bad30110013011002375a601e00260166ea800c5858dd698068009806801180580098039baa00116253330043002300537540022646464646464a66601a60200042930b1bad300e001300e002375a601800260180046eb4c028004c018dd50008b1b87480015cd2ab9d5573caae7d5d02ba157441`;
export const CBOR = `59135e0100003323232323232323232323232232232323232323232323223232253330143232323232323232533301c3014301e3754016264a66603a602a603e6ea80044cc00800cdd7181198101baa001153301e4913c65787065637420566572696669636174696f6e4b657943726564656e7469616c287061796d656e745f63726564656e7469616c29203d2061646d696e0016302230233023301f375402a264a66603a6024603e6ea80204c8c8c8c94ccc084c064c08cdd5000899192999811980d98129baa00113253330243370e900218131baa0011323232323253330293021302b3754002264a666054646464646464646464646464646464646464646464646464646464a66608aa66608a66e21200030473754609601e294454ccc114ccc114cdd78058022504a229444ccc114cdd78068032504a229404cc88c8c94cc12924010a6d61705f6368616e676500153304a30183330490034c0103d87a80004c0103d87980001533304900314a226464646464a66609c608c60a06ea80044c94cc140c078dd40008a99828180f1ba833704014094266e20004cdc10050251bad3054305137540022a6609e92014965787065637420536f6d6528646966666572656e636529203d206d6174682e737172742864785f73717561726564202b2064795f73717561726564202b20647a5f7371756172656429001632533304e3371000290000a60103d87a80001533304e33712002900109818998299ba80014bd7009818998299ba8323330010010023370666e000092002480108894ccc144cdc48010008801099980180180099b833370000266e0c01000520044bd7019b80337006603466e04dd698298011bad305300448010cc068cdc09bad3053001375a60a600690021980d19b81375a60a660a80026eb4c14cc15000d20043053001304e375460a2609c6ea800cc144004c130dd5182798261baa002304e304f304b375400a609a609c60946ea802cc0400a4ccc114cdd7804000a504a2609460966096609600460920026092004608e002608e004608a002608a60826ea8064c10cc110c110c110008c108004c108008c100004c100008c0f8004c0f8004c0e4dd5017980080091299981d0008a4000266e01200233002002303d001300100122253330353371000290000a40002a66606a605a002290010a9919981b181719b86002480104ccc010010c00400ccdc18012400826002666008008600200666e0cc0200092004370400446e64cc008005221003001001222533333303a00213232323232323300c0020013371491010128000025333036337100069007099b80483c80400c54ccc0d8cdc4001a410004266e00cdc0241002800690068a9981ba4929576861742061726520796f7520646f696e673f204e6f2049206d65616e2c20736572696f75736c792e001653330390011337149101035b5d2900004133714911035b5f2000375c6076607866601000266074980102415d003303a375266e2922010129000044bd70111981e26103422c20003303c375266601001000466e28dd7180c0009bae30100014bd701bac3037002375a606a0026466ec0dd4181a8009ba730360013754004264a66606e002266e292201027b7d00002133714911037b5f2000375c6072607464646600200200644a6660740022006266446607a98103422c20003303d3752666012012607400466e292201023a2000333009009303b002337146eb8c064004dd71808800a5eb80c0f0004cc008008c0f4004cc0e13010342207d0033038375200497ae03756004264a66606e002266e29221025b5d00002133714911035b5f2000375c6072607466600c00266070980102415d0033038375200497ae0223303a4c0103422c20003303a375266600c00c00466e28dd7180b0009bae300e0014bd701bac002133006375a0040022646466e2922102682700001323330010013006371a00466e292201012700003222533303633710004900008008991919199803003180580299b8b33700004a66607266e2000920141481805206e3371666e000054ccc0e4cdc4000a4028290300a40dc00866e18009202033706002901019b8e004002375c0046e0120012223233001001004225333036001100413300330380013300200230390012232330010010032253330303028001133714910101300000315333030337100029000099b8a489012d003300200233702900000089980299b8400148050cdc599b803370a002900a240c00066002002444a66605a66e2400920001001133300300333708004900a19b8b3370066e140092014481800044cc03c0400045281bae302f302c37540022a6605492014665787065637420566572696669636174696f6e4b657943726564656e7469616c287061796d656e745f63726564656e7469616c29203d206f6c645f646174756d2e6f776e657200163001302b37540424605c605e00266030002046a66666605a00220022a6604c0442c2a6604c0442c2a6604c0442c2a6604c0442c6054604e6ea800454cc0952412765787065637420496e6c696e65446174756d286461746129203d206f75747075745f646174756d00163029302a302a302637546052604c6ea800454cc09124016a65787065637420536f6d65287363726970745f6f757470757429203d0a202020202020202020206c6973742e66696e64286f7574707574732c20666e286f757470757429207b206f75747075742e61646472657373203d3d207363726970745f61646472657373207d2900163300400923375e6008604c6ea8004008c09cc090dd51813981418121baa3027302437540022a660449214365787065637420536f6d65287363726970745f696e70757429203d207472616e73616374696f6e2e66696e645f696e70757428696e707574732c206f75745f726566290016323300300a23375e6006604a6ea8004008c098c08cdd5005918130009119198008008019129998130008a60103d87a8000132325333024300500213007330290024bd70099802002000981500118140009ba54800054cc0792411f657870656374205370656e64286f75745f72656629203d20707572706f7365001637586044603e6ea802c88c8cc00400400c894ccc08c004528099299981019b8f375c604c00400829444cc00c00c004c098004dd618101810981098109810981098108011bac301f001301f301f0023758603a00260326ea8c070008c06cc070004c05cdd50008a4c2a6602a9211856616c696461746f722072657475726e65642066616c73650013656325333013300b00115333017301637540042930a9980a0088b0a9998099804000899299980c0008a9980a8090b099299980c980e00109924c66010002464a66602e601e60326ea80044c94ccc07000454cc064058584c8c94ccc07800454cc06c060584c8c94ccc08000454cc074068584c8c94ccc08800454cc07c070584c94ccc08cc0980084c92633012001232325333022301a00115333026302537540042930a998118100b0a999811180b8008a99981318129baa002149854cc08c0805854cc08c08058c08cdd5000a99999981400088008a9981080f0b0a9981080f0b0a9981080f0b0a9981080f0b0a9981000e8b1929999998138008a9981000e8b0a9981000e8b09bac001153302001d16153302001d1630240013024002325333333025001153301e01b16153301e01b16153301e01b161375a0022a6603c0362c6044002604400464a6666660460022a660380322c2a660380322c2a660380322c26eb400454cc07006458c080004c080008c94cccccc08400454cc06805c5854cc06805c5854cc06805c584dd68008a9980d00b8b180f000980d1baa001153301801516533333301e001100115330170141615330170141615330170141615330170141615330160131632533333301d001153301601316153301601316137580022a6602c0262c2a6602c0262c6034002602c6ea800854cc05004458c050dd5000a99999980c80088008a998090078b0a998090078b0a998090078b0a998090078b19800804007112999808180418091baa002132533301500115330120021613232533301700115330140041613232533301900115330160061613232533301b00115330180081613232533301d001153301a00a1613232533301f001153301c00c16132325333021001153301e00e161325333022302500213232323232324994ccc08cc06cc094dd500309929998140008a9981280a8b0991929998150008a9981380b8b0991929998160008a9981480c8b099299981698180010a4c2a660540342c64a6666660620022a660540342c2a660540342c2a660540342c26eb400454cc0a806858c0b8004c0b8008c94cccccc0bc00454cc0a00605854cc0a00605854cc0a0060584dd68008a9981400c0b181600098160011929999998168008a9981300b0b0a9981300b0b0a9981300b0b09bad001153302601616302a0013026375400c2a660480282c6602a00e4a6666660560022a660480282c2a660480282c2a660480282c26eb400454cc09005058cc0500208cc0614cccccc0a8004400454cc08c04c5854cc08c04c5854cc08c04c5854cc08c04c5804d4ccc080c060c088dd500489929998128008a998110090b0991929998138008a9981200a0b0991929998148008a9981300b0b0991929998158008a9981400c0b0992999816181780109919191924c660440080386604200a0366603e00c03464a66605260420022a66605a60586ea8020526153302a01a1615333029301e0011533302d302c37540102930a9981500d0b0a99981499b874801000454ccc0b4c0b0dd50040a4c2a660540342c2a660540342c60546ea801c54cc0a406458c94cccccc0c0004400454cc0a40645854cc0a40645854cc0a40645854cc0a406458c0b4004c0b4008c94cccccc0b8004400454cc09c05c5854cc09c05c5854cc09c05c5854cc09c05c58c0ac004c0ac008c94cccccc0b0004400454cc0940545854cc0940545854cc0940545854cc09405458c0a4004c0a4008c94cccccc0a8004400454cc08c04c5854cc08c04c5854cc08c04c5854cc08c04c58c09c004c08cdd50048a998108088b19809805008198090058078a9980f8078b19299999981300088008a9980f8078b0a9980f8078b0a9980f8078b0a9980f8078b181180098118011929999998120008a9980e8068b0a9980e8068b09bac001153301d00d16153301d00d1630210013021002325333333022001153301b00b16153301b00b16137580022a660360162c2a660360162c603e002603e00464a66666604000220022a660320122c2a660320122c2a660320122c2a660320122c603a002603a00464a66666603c00220022a6602e00e2c2a6602e00e2c2a6602e00e2c2a6602e00e2c6036002603600464a66666603800220022a6602a00a2c2a6602a00a2c2a6602a00a2c2a6602a00a2c6032002603200464a666666034002264a66602e602c0022a6660266010602a002294454ccc04cc02cc0540045280a9980a0020b0a9980a0020b1baa0011533013003161533013003161533013003161533013003163017001301337540042a660220022c44646600200200644a66602a00229309919801801980c8011801980b800911929998079803800899299980a0008a998088018b099299980a980c0010a4c2a660240082c64a6666660320022a660240082c2a660240082c2a660240082c2a660240082c26eb8004c058004c048dd50018a9998079802000899299980a0008a998088018b099299980a980c0010a4c2a660240082c64a6666660320022a660240082c2a660240082c2a660240082c2a660240082c26eb8004c058004c048dd50018a998080010b18081baa002370e9001112999806180218071baa0021325333011001153300e00216132325333013001153301000416132533301430170021324994ccc040c020c048dd5001899299980a8008a998090030b09919299980b8008a9980a0040b09919299980c8008a9980b0050b099299980d180e8010a4c2a6602e0162c64a66666603c0022a6602e0162c2a6602e0162c2a6602e0162c26eb400454cc05c02c58c06c004c06c008c94cccccc07000454cc0540245854cc0540245854cc054024584dd68008a9980a8048b180c800980c80119299999980d0008a998098038b0a998098038b0a998098038b09bad0011533013007163017001301337540062a6602200a2c2a6602200a2c64a6666660300022a6602200a2c2a6602200a2c2a6602200a2c26eb400454cc04401458c054004c054008c94cccccc058004400454cc03c00c5854cc03c00c5854cc03c00c5854cc03c00c58c04c004c03cdd50010a998068008b112999805980198069baa0021325333010001153300d00216132325333012001153300f0041613232533301400115330110061613253330153018002149854cc04801c58c94cccccc06400454cc04801c5854cc04801c5854cc04801c584dd68008a998090038b180b000980b00119299999980b8008a998080028b0a998080028b0a998080028b09bad00115330100051630140013014002325333333015001153300e00316153300e00316153300e003161375a0022a6601c0062c6024002601c6ea800854cc03000458dc3a4000a66666601e00220022a6601000c2c2a6601000c2c2a6601000c2c2a6601000c2c6eb4005240121657870656374206e65775f646174756d3a2047616d6544617461203d20646174610049011272656465656d65723a2052656465656d6572004901136f6c645f646174756d3a2047616d6544617461005734ae7155ceaab9e5573eae815d0aba25748981051a00da33600001`;
47 changes: 31 additions & 16 deletions src/contract/datum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface GameData {
player: Player;
monsters: MabObject[];
leveltime: number[];
level: LevelId;
}
export interface Player {
playerState: PlayerState;
Expand All @@ -27,12 +28,9 @@ export interface MabObject {
}

export interface Position {
momentumX: number;
momentumY: number;
momentumZ: number;
angle: number;
x: number;
y: number;
z: number;
floorZ: number;
}

export enum PlayerState {
Expand All @@ -41,20 +39,26 @@ export enum PlayerState {
REBORN,
}

export const initialGameData = (ownerKey: string, adminKey: string) => ({
export interface LevelId {
map: number;
skill: number;
episode: number;
}

export const initialGameData = (
ownerKey: string,
adminKey: string,
): GameData => ({
isOver: false,
owner: ownerKey,
admin: adminKey,
player: {
playerState: PlayerState.LIVE,
mapObject: {
position: {
momentumX: 0,
momentumY: 0,
momentumZ: 0,
angle: 0,
x: 0,
y: 0,
z: 0,
floorZ: 0,
},
health: 100,
},
Expand All @@ -71,6 +75,11 @@ export const initialGameData = (ownerKey: string, adminKey: string) => ({
},
monsters: [],
leveltime: [],
level: {
map: -1,
skill: -1,
episode: -1,
},
});

export const buildDatum = (state: GameData): string => {
Expand All @@ -82,6 +91,7 @@ export const buildDatum = (state: GameData): string => {
encodePlayer(state.player),
state.monsters.map((monster) => encodeMapObject(monster)),
state.leveltime.map((time) => BigInt(time)),
encodeLevelId(state.level),
]),
);
};
Expand Down Expand Up @@ -128,12 +138,9 @@ const encodeMapObject = (mapObject: MabObject) => {

const encodePosition = (position: Position) => {
return new Constr(0, [
BigInt(position.momentumX),
BigInt(position.momentumY),
BigInt(position.momentumZ),
BigInt(position.angle),
BigInt(position.x),
BigInt(position.y),
BigInt(position.z),
BigInt(position.floorZ),
]);
};

Expand All @@ -148,6 +155,14 @@ const encodePlayerState = (state: PlayerState): Data => {
}
};

const encodeLevelId = (level: LevelId): Data => {
return new Constr(0, [
BigInt(level.map),
BigInt(level.skill),
BigInt(level.episode),
]);
};

const encodeBoolean = (b: boolean) => {
return b ? new Constr(1, []) : new Constr(0, []);
};
Expand Down
106 changes: 62 additions & 44 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { CBOR } from "./contract/cbor";
import {
GameData,
LevelId,
Player,
PlayerStats,
buildDatum,
Expand Down Expand Up @@ -81,6 +82,7 @@ export async function fetchNewGame(region: string) {
};
// TODO: protocol from host
const protocol = gameServerUrl.startsWith("https") ? "https" : "http";

hydra = new Hydra(`${protocol}://${node}`, 100);
await hydra.populateUTxO();
hydra.onTxSeen = (_txId, tx) => {
Expand All @@ -93,16 +95,18 @@ export async function fetchNewGame(region: string) {
if (!redeemer) {
return;
}
const cmd = decodeRedeemer(toHex(redeemer));
cmdQueue.push(cmd);
// append some representation of the tx into the UI
appendTx(cmd);
if (cmdQueue.length > 1000) {
console.warn(
"Command queue grows big, should cleanup",
cmdQueue.length,
);
}
const cmds = decodeRedeemer(toHex(redeemer));
cmds.forEach((cmd) => {
cmdQueue.push(cmd);
// append some representation of the tx into the UI
appendTx(cmd);
if (cmdQueue.length > 1000) {
console.warn(
"Command queue grows big, should cleanup",
cmdQueue.length,
);
}
});
};
hydra.onTxConfirmed = () => {
const now = performance.now();
Expand Down Expand Up @@ -160,7 +164,8 @@ export async function hydraSend(
cmd: Cmd,
player: Player,
gameState: GameState,
leveltime?: number,
leveltime: number,
level: LevelId,
) {
if (!gameData || !hydra) throw new Error("Game data not initialized");

Expand All @@ -182,6 +187,7 @@ export async function hydraSend(

gameData.leveltime[0] = leveltime;
}
gameData.level = level;
// TODO: the latestUTxO should be fetched from the script address, filtering by admin in datum.
if (latestUTxO == null) {
const utxos = await hydra.getUtxos(scriptAddress);
Expand Down Expand Up @@ -261,20 +267,28 @@ const buildCollateralInput = (txHash: string, txIx: number) => {
const encodeRedeemer = (cmd: Cmd): string => {
return Data.to(
new Constr(1, [
new Constr(0, [
BigInt(cmd.forwardMove),
BigInt(cmd.sideMove),
BigInt(0),
[],
]),
[
yHSJ marked this conversation as resolved.
Show resolved Hide resolved
new Constr(0, [
BigInt(cmd.forwardMove),
BigInt(cmd.sideMove),
BigInt(0),
[],
]),
],
]),
);
};

const decodeRedeemer = (redeemer: string): Cmd => {
const d = (Data.from(redeemer) as Constr<Data>).fields[0] as Constr<Data>;

return { forwardMove: Number(d.fields[0]), sideMove: Number(d.fields[1]) };
const decodeRedeemer = (redeemer: string): Cmd[] => {
console.log(redeemer);
const cmds = (Data.from(redeemer) as Constr<Data>).fields[0] as Array<
Constr<Data>
>;
// TODO: return an array
return cmds.map((cmd) => ({
forwardMove: Number(cmd.fields[0]),
sideMove: Number(cmd.fields[1]),
}));
};

const buildTx = async (
Expand All @@ -297,41 +311,45 @@ const buildTx = async (
scriptRef: inputUtxo.scriptRef,
};

const tx = await lucid
.newTx()
.collectFrom([lucidUtxo], redeemer)
.payToContract(scriptAddress, { inline: datum }, { lovelace: BigInt(0) })
.readFrom([
{
txHash: scriptRef!.split("#")[0],
outputIndex: Number(scriptRef!.split("#")[1]),
address: scriptAddress,
assets: { lovelace: BigInt(0) },
scriptRef: {
type: "PlutusV2",
script: CBOR,
},
const tx = lucid.newTx();
tx.collectFrom([lucidUtxo], redeemer);

tx.payToContract(scriptAddress, { inline: datum }, { lovelace: BigInt(0) });
tx.readFrom([
{
txHash: scriptRef!.split("#")[0],
outputIndex: Number(scriptRef!.split("#")[1]),
address: scriptAddress,
assets: { lovelace: BigInt(0) },
scriptRef: {
type: "PlutusV2",
script: CBOR,
},
])
.addSigner(address)
.complete();

},
]);
tx.addSigner(address);
let complete;
try {
complete = await tx.complete();
} catch (e) {
throw e;
}
const collateral = buildCollateralInput(
collateralUtxo.txHash,
collateralUtxo.outputIndex,
);
const txBody = tx.txComplete.body();
const witnessSet = tx.txComplete.witness_set();
const auxData = tx.txComplete.auxiliary_data();
const txBody = complete.txComplete.body();
const witnessSet = complete.txComplete.witness_set();
const auxData = complete.txComplete.auxiliary_data();

txBody.set_collateral(collateral);
const collateralTx = C.Transaction.new(txBody, witnessSet, auxData);
txBody.free();
witnessSet.free();
auxData?.free();
collateral.free();
tx.txComplete = collateralTx;
const signedTx = await tx.sign().complete();
complete.txComplete = collateralTx;
const signedTx = await complete.sign().complete();
const body = signedTx.txSigned.body();
const outputs = body.outputs();
body.free();
Expand Down