Skip to content

Commit f42c998

Browse files
committed
wip adding Alert support
1 parent d239100 commit f42c998

4 files changed

Lines changed: 59 additions & 48 deletions

File tree

packages/core-dojo/src/DojoAlertStore.ts

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ import {
99
MAX_DIMENSION,
1010
areBoundsEqual,
1111
type PixelawCore,
12+
type Coordinate,
1213
} from "@pixelaw/core"
1314
import mitt from "mitt"
1415
import type { DojoStuff } from "./DojoEngine.init.ts"
1516
import type { SchemaType } from "./generated/models.gen.ts"
1617
import type { EntityKeysClause } from "@dojoengine/torii-client"
1718
import { getQueryBounds } from "./utils/querybuilder.ts"
1819
import type { DojoEngine } from "./DojoEngine.ts"
20+
import { convertFullHexString } from "./utils/utils.ts"
1921

2022
type State = { [key: string]: Alert | undefined }
2123

24+
const QUERY_RADIUS = 20
25+
2226
export class DojoAlertStore implements AlertStore {
2327
public readonly eventEmitter = mitt<AlertStoreEvents>()
2428
private dojoStuff
@@ -51,19 +55,24 @@ export class DojoAlertStore implements AlertStore {
5155

5256
private async initialize() {
5357
try {
54-
const items = await queryTorii(this.toriiUrl, createSqlQuery(this.queryBounds), (rows: any[]) => {
55-
return rows.map((item) => {
56-
return {
57-
...item,
58-
calldata: JSON.parse(item.calldata),
59-
}
60-
})
61-
})
58+
const items = await queryTorii(
59+
this.toriiUrl,
60+
createSqlQueryByRadius(this.core.getCenter(), QUERY_RADIUS),
61+
(rows: any[]) => {
62+
return rows.map((item) => {
63+
// const item = JSON.parse(str.d)
64+
return {
65+
...item,
66+
message: convertFullHexString(item.message),
67+
timestamp: Number.parseInt(item.timestamp, 16),
68+
} as Alert
69+
})
70+
},
71+
)
6272
for (const item of items) {
6373
this.setAlert(item.id, item)
6474
this.eventEmitter.emit("added", item)
6575
}
66-
// console.log({ items })
6776
} catch (e) {
6877
console.error(e)
6978
}
@@ -73,28 +82,25 @@ export class DojoAlertStore implements AlertStore {
7382
if (this.isSubscribed) return
7483
try {
7584
const subscription = this.sdk.client.onEventMessageUpdated(
76-
[
77-
KeysClause(
78-
["pixelaw-QueueScheduled"],
79-
[undefined],
80-
"VariableLen",
81-
).build() as unknown as EntityKeysClause,
82-
],
83-
false,
85+
[KeysClause(["pixelaw-Alert"], [undefined], "VariableLen").build() as unknown as EntityKeysClause],
8486
(id, data) => {
8587
if (id === "0x0") return
8688
try {
87-
const item = data["pixelaw-QueueScheduled"]
88-
89-
const Alert: Alert = {
90-
calldata: item.calldata.value.map((val) => val.value),
91-
called_system: item.called_system.value,
92-
id: item.id.value,
93-
selector: item.selector.value,
94-
timestamp: item.timestamp.value,
89+
const item = data["pixelaw-Alert"]
90+
const alert: Alert = {
91+
caller: item.caller.value,
92+
player: item.player.value,
93+
position: {
94+
x: item.position.value.x.value,
95+
y: item.position.value.y.value,
96+
},
97+
message: convertFullHexString(item.message.value),
98+
timestamp: Number.parseInt(item.timestamp.value, 16),
9599
}
96-
this.setAlert(item.id.value, Alert)
97-
this.eventEmitter.emit("scheduled", Alert)
100+
// console.log("alert", alert)
101+
// TODO decide if we store the Alert or not
102+
// this.setAlert(item.id.value, alert)
103+
this.core.events.emit("alert", alert)
98104
} catch (e) {
99105
console.error(e)
100106
}
@@ -117,43 +123,42 @@ export class DojoAlertStore implements AlertStore {
117123
public setAlert(key: string, Alert: Alert): void {
118124
this.state[key] = Alert
119125
}
126+
120127
getAll(): Alert[] {
121128
return this.dojoStuff!.apps
122129
}
130+
123131
public setBounds(newBounds: Bounds): void {
124132
const newQueryBounds = getQueryBounds(newBounds)
125133

126134
if (!this.queryBounds || !areBoundsEqual(this.queryBounds, newQueryBounds)) {
127135
this.queryBounds = newQueryBounds
128136
}
129137
}
138+
139+
getLastForPosition(position: Position): Alert[] {
140+
return []
141+
}
130142
}
143+
export function createSqlQueryByRadius(center: Coordinate, radius: number) {
144+
let result = `SELECT
145+
caller, player, message, "position.x" , "position.y", timestamp
146+
FROM "pixelaw-Alert"
147+
WHERE (("position.x" - ${center[0]}) * ("position.x" - ${center[0]}) + ("position.y" - ${center[1]}) * ("position.y" - ${center[1]})) <= (${radius} * ${radius})
148+
`
131149

150+
result += ";"
151+
return result
152+
}
132153
export function createSqlQuery(bounds: Bounds) {
133154
const [[left, top], [right, bottom]] = bounds
134-
const xWraps = right - left < 0
135-
const yWraps = bottom - top < 0
155+
136156
let result = `SELECT
137-
json_array(A.caller, A.player, ltrim(substr(P.message, 4), '0'), (A.x << 16) | A.y, A.timestamp) as d
157+
json_array(A.caller, A.player, ltrim(substr(A.message, 4), '0'), (A.x << 16) | A.y, A.timestamp) as d
138158
FROM "pixelaw-Alert" as A
139-
WHERE( 1 = 0 )
159+
WHERE (A.x >= ${left} AND A.y >= ${top} AND A.x <= ${right} AND A.y <= ${bottom} )
140160
`
141-
const ZERO = 0
142-
143-
if (xWraps && yWraps) {
144-
result += ` OR(x >= ${left} AND y >= ${top} AND x <= ${MAX_DIMENSION} AND y <= ${MAX_DIMENSION} )`
145-
result += ` OR(x >= ${left} AND y >= ${ZERO} AND x <= ${MAX_DIMENSION} AND y <= ${bottom} )`
146-
result += ` OR(x >= ${ZERO} AND y >= ${top} AND x <= ${right} AND y <= ${MAX_DIMENSION} )`
147-
result += ` OR(x >= ${ZERO} AND y >= ${ZERO} AND x <= ${right} AND y <= ${bottom} )`
148-
} else if (xWraps) {
149-
result += ` OR(x >= ${left} AND y >= ${ZERO} AND x <= ${MAX_DIMENSION} AND y <= ${bottom} )`
150-
result += ` OR(x >= ${ZERO} AND y >= ${ZERO} AND x <= ${right} AND y <= ${bottom} )`
151-
} else if (yWraps) {
152-
result += ` OR(x >= ${ZERO} AND y >= ${top} AND x <= ${right} AND y <= ${MAX_DIMENSION} )`
153-
result += ` OR(x >= ${ZERO} AND y >= ${ZERO} AND x <= ${right} AND y <= ${bottom} )`
154-
} else {
155-
result += ` OR(x >= ${top} AND y >= ${top} AND x <= ${right} AND y <= ${bottom} )`
156-
}
161+
157162
result += ";"
158163
return result
159164
}

packages/core-dojo/src/DojoEngine.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import DojoSqlPixelStore from "./DojoSqlPixelStore.ts"
2121
import { DojoWallet } from "./DojoWallet.ts"
2222
import { type DojoConfig, ENGINE_ID } from "./types.ts"
2323
import { DojoExecutor } from "./DojoExecutor.ts"
24+
import { DojoAlertStore } from "./DojoAlertStore.ts"
2425

2526
export class DojoEngine implements Engine {
2627
id: Engines = ENGINE_ID
@@ -55,6 +56,8 @@ export class DojoEngine implements Engine {
5556
this.core.executor = new DojoExecutor(this.dojoSetup.provider, null)
5657

5758
this.core.queue = await DojoQueueStore.getInstance(this.config.toriiUrl, this.dojoSetup)
59+
60+
this.core.alerts = await DojoAlertStore.getInstance(this.core)
5861
} catch (error) {
5962
console.error("Dojo init error:", error)
6063
}

packages/core/src/PixelawCore.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
AlertStore,
23
AppStore,
34
BaseWallet,
45
Coordinate,
@@ -38,6 +39,7 @@ export class PixelawCore {
3839
events = mitt<PixelCoreEvents>()
3940
queue: QueueStore = null!
4041
executor: Executor | null = null!
42+
alerts: AlertStore | null = null!
4143

4244
private worldsRegistry: WorldsRegistry
4345
private app: string | null = null

packages/core/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export interface QueueStore {
9090
export interface AlertStore {
9191
eventEmitter: ReturnType<typeof mitt<AlertStoreEvents>>
9292
getAll: () => Alert[]
93-
getLastForPosition: (position: Position) => Alert[]
93+
// getLastForPosition: (position: Position) => Alert[]
9494
}
9595

9696
export interface TileStore {
@@ -168,6 +168,7 @@ export type PixelCoreEvents = {
168168
tileStoreUpdated: number
169169
appStoreUpdated: number
170170
error: SimplePixelError
171+
alert: Alert
171172
userScrolled: { bounds: Bounds }
172173
userZoomed: { bounds: Bounds }
173174
cacheUpdated: number

0 commit comments

Comments
 (0)