@@ -9,16 +9,20 @@ import {
99 MAX_DIMENSION ,
1010 areBoundsEqual ,
1111 type PixelawCore ,
12+ type Coordinate ,
1213} from "@pixelaw/core"
1314import mitt from "mitt"
1415import type { DojoStuff } from "./DojoEngine.init.ts"
1516import type { SchemaType } from "./generated/models.gen.ts"
1617import type { EntityKeysClause } from "@dojoengine/torii-client"
1718import { getQueryBounds } from "./utils/querybuilder.ts"
1819import type { DojoEngine } from "./DojoEngine.ts"
20+ import { convertFullHexString } from "./utils/utils.ts"
1921
2022type State = { [ key : string ] : Alert | undefined }
2123
24+ const QUERY_RADIUS = 20
25+
2226export 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+ }
132153export 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}
0 commit comments