-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmudata.php
309 lines (245 loc) · 7.12 KB
/
mudata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
//error_reporting(E_NONE);
//ini_set('display_errors', 0);
if( empty( session_id() ) )
session_start( );
$file = "mudata.json";
// undefined index
// Array of 7 or 8 fields
$indata = json_decode( $_REQUEST["json"], true );
$loc = 0;
$messages = [];
if ( !empty( $indata ) )
{
$loc = $indata[ 0 ];
$messages = array_slice( $indata, 7 );
foreach ( $messages as &$value )
{
$value = 'a person says: "' . $value .'"';
}
unset($value);
$indata = array_slice( $indata, 0, 7 );
}
else
{
$indata = [ 0,0,0,0,0,0,0 ];
}
$outdata = json_decode( file_get_contents( $file ), true );
if ( !$outdata )
$outdata = [];
$changes = false;
$oldLoc = 0;
$outMessages = [];
// Copy over old information
if( empty( $outdata[ session_id() ] ) )
{
$outdata[ session_id() ] = [
"mudata" => $indata,
"messages" => [],
"timestamp" => time()
];
$changes = true;
}
else
{
// Copy over old location
$oldata = $outdata[ session_id() ];
$oldLoc = $oldata[ "mudata" ][ 0 ];
$outMessages = $oldata[ "messages" ];
$outdata[ session_id() ][ "messages" ] = [];
// Update data, timestamp
$outdata[ session_id() ][ "mudata" ] = $indata;
$outdata[ session_id() ][ "timestamp" ] = time();
// Timestamp changes!
//if ( $loc !== $oldLoc || count( $messages ) || count( $outMessages ) )
$changes = true;
}
// TODO: "status messages" if moved
// Copy over outgoing messages
//$outMessages = array_slice( $oldata[ "mudata" ], 7 );
if ( $loc !== $oldLoc )
{
// We moved, clear "chat" messages
$outMessages = [];
$amounts = [ 0,0,0,0,0,0,0 ];
forEach ( $outdata as $sid => $data )
{
if ( session_id() === $sid )
continue;
for ( $n = 0; $n < 7; $n++ )
if ( $data["mudata"][0] === $indata[$n] )
$amounts[ $n ]++;
}
for ( $n = 0; $n < 7; $n++ )
{
$dir = director( $n, $amounts[$n] );
if ( $dir )
$outMessages[] = $dir;
}
}
// if current user moves or typed a message: store as location infos
$mudata = [];
forEach ( $outdata as $sid => $data )
{
if ( session_id() === $sid )
continue;
if ( time() - $data[ "timestamp" ] > 120 )
{
unset( $outdata[ $sid ] );
continue;
}
// Fill array with messages)
if ( $loc !== $oldLoc )
{
$message = "";
if ( $data[ "mudata" ][ 0 ] === $loc && $oldLoc )
{
// Appeared
$message = "a person just appeared " . directive( $oldLoc, $data[ "mudata" ] );
}
else if ( $data[ "mudata" ][ 0 ] === $oldLoc && $loc )
{
// Disappeared
$message = "a person just " . direction( $loc, $data[ "mudata" ] );
}
if ( $message )
$outdata[ $sid ][ "messages" ][] = $message;
}
// Copy chat message to user on same location
if ( $loc === $data[ "mudata" ][ 0 ] && count( $messages ) )
{
$outdata[ $sid ][ "messages" ] = array_merge( $data[ "messages" ], $messages );
$changes = true;
}
}
// read post data as json
// read 'db'
// store (optional) incoming user message for all users on curloc
// if oldloc === currentloc
// read/remove incoming messages from db
// else
// store custom message for all users on oldloc
// remove stale id's (2 minutes idle)
// write to db/file
// return json array of combined data minus the current session id one
/*
// Invalid argument supplied
forEach( $delta as $deltask )
{
$found = false;
forEach( $mudata as $key => $value )
{
if ( $deltask["id"] == $value["id"] )
{
if ( $mudata[$key]["done"] != $deltask["done"] )
{
$mudata[$key]["done"] = $deltask["done"];
$changes = true;
}
// TODO: barcode array merge + check
$mudata[$key]["barcodes"] = array_unique( array_merge( $mudata[$key]["barcodes"], $deltask["barcodes"] ), SORT_REGULAR );
if ( $mudata[$key]["label"] != $deltask["label"] )
{
if ( $deltask["label"] == "" )
unset( $mudata[ $key ] );
else
$mudata[$key]["label"] = $deltask["label"];
$changes = true;
}
$found = true;
}
}
if ( !$found )
{
$mudata = array_merge( $delta, $mudata );
$changes = true;
}
}
*/
if ( $changes )
{
$outdata = json_encode( $outdata, JSON_PRETTY_PRINT );
$ret = file_put_contents( $file, $outdata );
}
// Align indexes
$outMessages = array_values( $outMessages );
$outMessages = json_encode( $outMessages, JSON_PRETTY_PRINT );
echo $outMessages;
function directive( $_location, $_locations )
{
// a person just appeared (from {direcion})
// [the north,the south,the east,the west,above,below]
if ( $_location === $_locations[ 1 ] )
return "from the north";
if ( $_location === $_locations[ 2 ] )
return "from the south";
if ( $_location === $_locations[ 3 ] )
return "from the east";
if ( $_location === $_locations[ 4 ] )
return "from the west";
if ( $_location === $_locations[ 5 ] )
return "from above";
if ( $_location === $_locations[ 6 ] )
return "from below";
return "";
}
function direction( $_location, $_locations )
{
// a person just disappeared
// a person just went {direction}
// [north,south,east,west,up,down]
if ( $_location === $_locations[ 1 ] )
return "went north";
if ( $_location === $_locations[ 2 ] )
return "went south";
if ( $_location === $_locations[ 3 ] )
return "went east";
if ( $_location === $_locations[ 4 ] )
return "went west";
if ( $_location === $_locations[ 5 ] )
return "went up";
if ( $_location === $_locations[ 6 ] )
return "went down";
return "disappeared";
}
function director( $_location, $_amount )
{
if ( !$_amount )
return "";
$message = "there ";
if ( $_location > 4 )
{
$message .= "seems to be someone ".($_location===5?"above":"below");
}
else
{
if ( $_amount === 1 )
$message .= "is a person";
else
$message .= "are ".$amounts[ 0 ]." persons";
if ( !$_location )
$message .= " standing next to";
else if ( $_location === 1 )
$message .= " north of";
else if ( $_location === 2 )
$message .= " south of";
else if ( $_location === 3 )
$message .= " east of";
else if ( $_location === 4 )
$message .= " west of";
}
$message .= " you";
/*
there {is a person|are n persons|seems to be someone} (standing) {next to|{north|south|east|west} of|above|below} you
there {is a person|are n persons} standing {direction} you
next to
[north,south,east,west] of
there seems to be someone {direction} you
above
below
*/
return $message;
}
?>