On Github mimming / how-to-mmoize-anything
without starting a server
Seb Lee-Delisle's MMO Asteroids blog post
175,000 registered developers
// Write
Firebase ref =
new Firebase("https://dinosaurs.firebaseio.com/");
ref.child("note").setValue("Dinosaurs are awesome");
Dinosaur brontosaurus = new Dinosaur();
ref.child("really-existed").push().setValue(brontosaurus);
// Read
ref.child("note").addValueEventListener(
new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
Log.v(TAG, snapshot.getValue());
}
public void onCancelled(FirebaseError firebaseError) {
Log.e(TAG, "Uhoh! " + firebaseError);
}});
// Replace writes
// This
public void addBubble(float x, float y) {
bubbles.push(new Bubble(x,y));
}
// Becomes
public void addBubble(float x, float y) {
firebaseRef.push().setValue(new Bubble(x, y));
}
// Add listeners to model constructor
// Listen to changes in bubbles from everywhere
firebaseRef.addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot snap, String s) {
// Add to local copy
bubbles.put(
snap.getKey(),
snap.getValue(Bubble.class));
}
...);
// Find chatty data
Class Bubble {
long radius;
...
}
// Calculate locally where you can
Class Bubble {
long born;
public long getRadius() {
long deltaMs =
System.currentTimeMillis() - this.born;
return deltaMs * RADIUS_CHANGE_PER_MS + INITIAL_RADIUS;
}
}
// Listen for clock skew
Firebase skewRef = new Firebase(
"https://dino.firebaseio.com/.info/serverTimeOffset");
skewRef.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
clockSkew = dataSnapshot.getValue(Long.class);
}
});
// Use it elsewhere
public long getRadius() {
long deltaMs = currentTime - this.born + clockSkew;
return deltaMs * RADIUS_CHANGE_PER_MS + INITIAL_RADIUS;
}
// Add an on disconnect handler playerDataRef.onDisconnect().removeValue()