@@ -10,6 +10,16 @@ export type Log = {
10
10
text : string
11
11
} ;
12
12
13
+ export type Transaction = {
14
+ cost : number ,
15
+ message : Array < number >
16
+ }
17
+
18
+ export type Block = {
19
+ messages : Array < Transaction > ,
20
+ round : number
21
+ }
22
+
13
23
// Return a timestamp representing the _current time in ms, not necessarily from
14
24
// any particular epoch.
15
25
const timeMS : ( ) => number = typeof window !== 'undefined' && window . performance && window . performance . now ?
@@ -57,6 +67,11 @@ export default class Match {
57
67
*/
58
68
readonly logs : Array < Array < Log > > ;
59
69
70
+ /**
71
+ * The blockchain, an array of blocks per round.
72
+ */
73
+ readonly blockchain : Array < Block > ;
74
+
60
75
/**
61
76
* The current game world.
62
77
* DO NOT CACHE this reference between calls to seek() and compute(), it may
@@ -118,6 +133,7 @@ export default class Match {
118
133
this . snapshots . push ( this . _current . copy ( ) ) ;
119
134
this . deltas = new Array ( 1 ) ;
120
135
this . logs = new Array ( 1 ) ;
136
+ this . blockchain = new Array ( 1 ) ;
121
137
this . maxTurn = header . maxRounds ( ) ;
122
138
this . _lastTurn = null ;
123
139
this . _seekTo = 0 ;
@@ -136,6 +152,93 @@ export default class Match {
136
152
if ( delta . logs ( ) ) {
137
153
this . parseLogs ( delta . roundID ( ) , < string > delta . logs ( flatbuffers . Encoding . UTF16_STRING ) ) ;
138
154
}
155
+ this . parseBlockchain ( delta ) ;
156
+ }
157
+
158
+ /**
159
+ * parses blockchain broadcasts
160
+ */
161
+ parseBlockchain ( delta : schema . Round ) {
162
+ let blockMessages = new Array < Transaction > ( ) ;
163
+
164
+ // lol the schema format for this is real weird
165
+ // THIS IS THE HACKIEST SOLUTION MANKIND HAS EVER SEEN
166
+ // another option is actually changing the schema, but we can't remove parts of it
167
+ // so then we would need to add a new thing
168
+ // which would (1) break old replays and (2) have twice as big new replays
169
+ // sorry this is probably the best solution
170
+ // DO NOT COPY THIS CODE FOR FUTURE USE. MODIFY!!!
171
+
172
+ let j = 0 ;
173
+ let messageStringLen = delta . broadcastedMessagesLength ( ) ;
174
+ for ( let i = 0 ; i < delta . broadcastedMessagesCostsLength ( ) ; i ++ ) {
175
+ let messageCost = delta . broadcastedMessagesCosts ( i ) ;
176
+ // console.log(messageCost);
177
+ // let x = delta.broadcastedMessages(j, flatbuffers.Encoding.UTF16_STRING);
178
+ // var offset = delta.bb!.__offset(delta.bb_pos, 42);
179
+ // var h = delta.bb!.__vector(delta.bb_pos + offset)
180
+ // console.log(h);
181
+ // var k = delta.bb!.readInt32(h);
182
+ // console.log(k);
183
+ // console.log(delta.bb!.readInt32(h+4));
184
+ // console.log(delta.bb!.readInt32(h+8));
185
+ // console.log(delta.bb!.readInt32(h+12));
186
+ // console.log(delta.bb!.readInt32(h+16));
187
+ let thisTransaction = "" ;
188
+ let offset = delta . bb ! . __offset ( delta . bb_pos , 42 ) ;
189
+ let startPointer = delta . bb ! . __vector ( delta . bb_pos + offset ) ;
190
+ if ( offset ) {
191
+ // this is ascii
192
+ let x = String . fromCharCode ( delta . bb ! . readInt32 ( startPointer + 4 * j ) ) ;
193
+ while ( x !== ' ' ) {
194
+ // console.log(x);
195
+ // this will be
196
+ thisTransaction += x ;
197
+ j += 1 ;
198
+ if ( j >= messageStringLen ) {
199
+ console . log ( "BLOCKCHAIN ERROR SHOULD NEVER HAPPEN" ) ;
200
+ break ;
201
+ }
202
+ x = String . fromCharCode ( delta . bb ! . readInt32 ( startPointer + 4 * j ) ) ;
203
+ }
204
+ j += 1 ;
205
+ // now split this transaction on _
206
+ let splitMessage = thisTransaction . split ( "_" ) ;
207
+ let messageArr = new Array < number > ( ) ;
208
+ for ( let j = 0 ; j < splitMessage . length ; j ++ ) {
209
+ messageArr . push ( parseInt ( splitMessage [ j ] ) ) ;
210
+ }
211
+ blockMessages . push ( {
212
+ cost : messageCost ,
213
+ message : messageArr
214
+ } ) ;
215
+ } else {
216
+ console . log ( "couldn't display blockchain" ) ;
217
+ }
218
+ }
219
+
220
+ // console.log(blockMessages);
221
+
222
+ // for (let i = 0; i < delta.broadcastedMessagesCostsLength(); i++) {
223
+ // let messageString = delta.broadcastedMessages(i);
224
+ // let messageCost = delta.broadcastedMessagesCosts(i);
225
+ // console.log(messageString);
226
+ // // string is formatted as int_int_int
227
+ // let splitMessage = messageString.split("_");
228
+ // let messageArr = new Array<number>();
229
+ // for (let j = 0; j < splitMessage.length; j++) {
230
+ // messageArr.push(parseInt(splitMessage[j]));
231
+ // }
232
+ // blockMessages.push({
233
+ // cost: messageCost,
234
+ // message: messageArr
235
+ // });
236
+ // }
237
+
238
+ this . blockchain . push ( {
239
+ messages : blockMessages ,
240
+ round : delta . roundID ( )
241
+ } ) ;
139
242
}
140
243
141
244
/**
0 commit comments