11import { KeysClause , type SDK } from "@dojoengine/sdk"
22import { queryTorii } from "@dojoengine/sdk/sql"
33import {
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"
1415import mitt from "mitt"
1516import type { DojoStuff } from "./DojoEngine.init.ts"
@@ -19,15 +20,15 @@ import { getQueryBounds } from "./utils/querybuilder.ts"
1920import type { DojoEngine } from "./DojoEngine.ts"
2021import { convertFullHexString } from "./utils/utils.ts"
2122
22- type State = { [ key : string ] : Alert | undefined }
23+ type State = { [ key : string ] : Notification | undefined }
2324
2425const 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+
153171export 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+ */
0 commit comments