Skip to content

Commit e4df375

Browse files
committed
work on notifications and sql queries
1 parent 900dfd7 commit e4df375

11 files changed

Lines changed: 358 additions & 209 deletions

File tree

packages/core-dojo/src/DojoEngine.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +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"
24+
import { DojoNotificationStore } from "./DojoNotificationStore.ts"
2525

2626
export class DojoEngine implements Engine {
2727
id: Engines = ENGINE_ID
@@ -57,7 +57,7 @@ export class DojoEngine implements Engine {
5757

5858
this.core.queue = await DojoQueueStore.getInstance(this.config.toriiUrl, this.dojoSetup)
5959

60-
this.core.alerts = await DojoAlertStore.getInstance(this.core)
60+
this.core.notification = await DojoNotificationStore.getInstance(this.core)
6161
} catch (error) {
6262
console.error("Dojo init error:", error)
6363
}
@@ -75,7 +75,7 @@ export class DojoEngine implements Engine {
7575

7676
async executeQueueItem(item: QueueItem): Promise<boolean> {
7777
const dojoCall = {
78-
contractAddress: "0x5386eb3153d91bd740a4e17933f26056ce10faddf9a06f56a2c23c0c4794cb5", // TODO properly configure pixelaw_actions contract,
78+
contractAddress: "0x1f455be12a750b7228e5626e1efd6bcee4849f24e98bb098371d6e8f773f3", // TODO properly configure pixelaw_actions contract,
7979
entrypoint: "process_queue",
8080
calldata: [
8181
item.id,
Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { KeysClause, type SDK } from "@dojoengine/sdk"
22
import { queryTorii } from "@dojoengine/sdk/sql"
33
import {
4-
type AlertStore,
5-
type Alert,
4+
type NotificationStore,
5+
type Notification,
66
QueueStore,
7-
type AlertStoreEvents,
7+
type NotificationStoreEvents,
88
type Bounds,
99
MAX_DIMENSION,
1010
areBoundsEqual,
1111
type PixelawCore,
1212
type Coordinate,
13+
type Position,
1314
} from "@pixelaw/core"
1415
import mitt from "mitt"
1516
import type { DojoStuff } from "./DojoEngine.init.ts"
@@ -19,15 +20,15 @@ import { getQueryBounds } from "./utils/querybuilder.ts"
1920
import type { DojoEngine } from "./DojoEngine.ts"
2021
import { convertFullHexString } from "./utils/utils.ts"
2122

22-
type State = { [key: string]: Alert | undefined }
23+
type State = { [key: string]: Notification | undefined }
2324

2425
const QUERY_RADIUS = 20
2526

26-
export class DojoAlertStore implements AlertStore {
27-
public readonly eventEmitter = mitt<AlertStoreEvents>()
27+
export class DojoNotificationStore implements NotificationStore {
28+
public readonly eventEmitter = mitt<NotificationStoreEvents>()
2829
private dojoStuff
2930
private sdk: SDK<SchemaType>
30-
private static instance: DojoAlertStore
31+
private static instance: DojoNotificationStore
3132
private isSubscribed = false
3233
private cacheUpdated: number = Date.now()
3334
private state: State = {}
@@ -43,34 +44,40 @@ export class DojoAlertStore implements AlertStore {
4344
}
4445

4546
// Singleton factory
46-
public static async getInstance(core: PixelawCore): Promise<DojoAlertStore> {
47-
if (!DojoAlertStore.instance) {
48-
DojoAlertStore.instance = new DojoAlertStore(core)
47+
public static async getInstance(core: PixelawCore): Promise<DojoNotificationStore> {
48+
if (!DojoNotificationStore.instance) {
49+
DojoNotificationStore.instance = new DojoNotificationStore(core)
4950

50-
await DojoAlertStore.instance.subscribe()
51-
await DojoAlertStore.instance.initialize()
51+
await DojoNotificationStore.instance.subscribe()
52+
await DojoNotificationStore.instance.initialize()
5253
}
53-
return DojoAlertStore.instance
54+
return DojoNotificationStore.instance
5455
}
5556

5657
private async initialize() {
5758
try {
59+
// TODO Notifications should be filtered by wallet address
60+
// const wallet = this.core.wallet as DojoWallet
61+
5862
const items = await queryTorii(
5963
this.toriiUrl,
60-
createSqlQueryByRadius(this.core.getCenter(), QUERY_RADIUS),
64+
createSqlQueryByRadius(
65+
this.core.getCenter(),
66+
QUERY_RADIUS,
67+
this.core.lastNotification /*, wallet.getAccount()*/,
68+
),
6169
(rows: any[]) => {
6270
return rows.map((item) => {
6371
// const item = JSON.parse(str.d)
6472
return {
6573
...item,
66-
message: convertFullHexString(item.message),
67-
timestamp: Number.parseInt(item.timestamp, 16),
68-
} as Alert
74+
message: convertFullHexString(item.text),
75+
} as Notification
6976
})
7077
},
7178
)
7279
for (const item of items) {
73-
this.setAlert(item.id, item)
80+
// this.setNotification(item.id, item)
7481
this.eventEmitter.emit("added", item)
7582
}
7683
} catch (e) {
@@ -82,25 +89,33 @@ export class DojoAlertStore implements AlertStore {
8289
if (this.isSubscribed) return
8390
try {
8491
const subscription = this.sdk.client.onEventMessageUpdated(
85-
[KeysClause(["pixelaw-Alert"], [undefined], "VariableLen").build() as unknown as EntityKeysClause],
92+
[
93+
KeysClause(
94+
["pixelaw-Notification"],
95+
[undefined],
96+
"VariableLen",
97+
).build() as unknown as EntityKeysClause,
98+
],
8699
(id, data) => {
87100
if (id === "0x0") return
88101
try {
89-
const item = data["pixelaw-Alert"]
90-
const alert: Alert = {
91-
caller: item.caller.value,
92-
player: item.player.value,
102+
const item = data["pixelaw-Notification"]
103+
console.log(item)
104+
const notification: Notification = {
105+
from: item.from.value.option === "None" ? null : item.from.value.value.value,
106+
to: item.to.value.option === "None" ? null : item.to.value.value.value,
107+
color: item.color.value,
108+
app: item.app.value, // TODO
93109
position: {
94110
x: item.position.value.x.value,
95111
y: item.position.value.y.value,
96112
},
97-
message: convertFullHexString(item.message.value),
98-
timestamp: Number.parseInt(item.timestamp.value, 16),
113+
text: convertFullHexString(item.text.value),
99114
}
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)
115+
// console.log("notification", notification)
116+
// TODO decide if we store the Notification or not
117+
// this.setNotification(item.id.value, notification)
118+
this.core.events.emit("notification", notification)
104119
} catch (e) {
105120
console.error(e)
106121
}
@@ -120,11 +135,11 @@ export class DojoAlertStore implements AlertStore {
120135
}
121136
}
122137

123-
public setAlert(key: string, Alert: Alert): void {
124-
this.state[key] = Alert
138+
public setNotification(key: string, Notification: Notification): void {
139+
this.state[key] = Notification
125140
}
126141

127-
getAll(): Alert[] {
142+
getAll(): Notification[] {
128143
return this.dojoStuff!.apps
129144
}
130145

@@ -136,29 +151,33 @@ export class DojoAlertStore implements AlertStore {
136151
}
137152
}
138153

139-
getLastForPosition(position: Position): Alert[] {
154+
getLastForPosition(position: Position): Notification[] {
140155
return []
141156
}
142157
}
143-
export function createSqlQueryByRadius(center: Coordinate, radius: number) {
158+
export function createSqlQueryByRadius(center: Coordinate, radius: number, lastNotification: number, address: string) {
159+
console.log("add", address)
144160
let result = `SELECT
145-
caller, player, message, "position.x" , "position.y", timestamp
146-
FROM "pixelaw-Alert"
161+
"from", "to", "text", "position.x" , "position.y", "color"
162+
FROM "pixelaw-Notification"
147163
WHERE (("position.x" - ${center[0]}) * ("position.x" - ${center[0]}) + ("position.y" - ${center[1]}) * ("position.y" - ${center[1]})) <= (${radius} * ${radius})
148164
`
149165

150166
result += ";"
151167
return result
152168
}
169+
/*
170+
153171
export function createSqlQuery(bounds: Bounds) {
154172
const [[left, top], [right, bottom]] = bounds
155173
156174
let result = `SELECT
157175
json_array(A.caller, A.player, ltrim(substr(A.message, 4), '0'), (A.x << 16) | A.y, A.timestamp) as d
158-
FROM "pixelaw-Alert" as A
176+
FROM "pixelaw-Notification" as A
159177
WHERE (A.x >= ${left} AND A.y >= ${top} AND A.x <= ${right} AND A.y <= ${bottom} )
160178
`
161179
162180
result += ";"
163181
return result
164182
}
183+
*/

packages/core-dojo/src/DojoSqlPixelStore.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class DojoSqlPixelStore implements PixelStore {
6969
DojoSqlPixelStore.instance.worker.onmessage = DojoSqlPixelStore.instance.handleRefreshWorker.bind(
7070
DojoSqlPixelStore.instance,
7171
)
72-
console.log("subbing")
7372
DojoSqlPixelStore.instance.subscribe()
7473
}
7574
return DojoSqlPixelStore.instance
@@ -102,11 +101,11 @@ class DojoSqlPixelStore implements PixelStore {
102101
text: convertFullHexString(p.text.value),
103102
timestamp: p.timestamp.value,
104103
app,
105-
x: p.x.value,
106-
y: p.y.value,
104+
x: p.position.value.x.value,
105+
y: p.position.value.y.value,
107106
}
108107

109-
const key = `${p?.x.value}_${p?.y.value}`
108+
const key = `${pixel.x}_${pixel.y}`
110109
this.idLookupTable[id] = key
111110
this.setPixel(key, pixel)
112111
}
@@ -217,11 +216,11 @@ export function createSqlQuery(bounds: Bounds) {
217216
const [[left, top], [right, bottom]] = bounds
218217

219218
let result = `SELECT
220-
json_array(P.color, ltrim(substr(P.text, 32), '0'), ltrim(substr(P.action, 3), '0'), (P.x << 16) | P.y, ltrim(substr(A.name, 4), '0' )) as d
219+
json_array(P.color, ltrim(substr(P.text, 32), '0'), ltrim(substr(P.action, 3), '0'), (P."position.x" << 16) | P."position.y", ltrim(substr(A.name, 4), '0' )) as d
221220
FROM "pixelaw-Pixel" as P
222221
INNER JOIN "Pixelaw-App" as A
223222
ON P.app = A.system
224-
WHERE (P.x >= ${left} AND P.y >= ${top} AND P.x <= ${right} AND P.y <= ${bottom} )
223+
WHERE (P."position.x" >= ${left} AND P."position.y" >= ${top} AND P."position.x" <= ${right} AND P."position.y" <= ${bottom} )
225224
`
226225

227226
result += ";"

0 commit comments

Comments
 (0)