mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
for AJ
This commit is contained in:
parent
729d562c80
commit
bfd11b9115
@ -30,34 +30,51 @@
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
<h2>Gun Duel!</h2>
|
||||
<span>Old western cowboy style! Two players are needed, whoever can shoot the other first wins!</span>
|
||||
<span><b>Fastest gun in the west, <span name="fastest">nut'n</span> by <span name="slinger">nobody</span>.</b></span>
|
||||
<form id="p1" class="assign-player" onsubmit="return false;">
|
||||
Player 1: <input type="text" name="p1" placeholder="nickname">
|
||||
<input type="submit" value="Join!">
|
||||
Player 1: <input type="text" name="p1" class="player" placeholder="nickname">
|
||||
<input type="submit" class="take" value="Join!">
|
||||
</form>
|
||||
<form id="p2" class="assign-player" onsubmit="return false;">
|
||||
Player 2: <input type="text" name="p2" placeholder="nickname">
|
||||
<input type="submit" value="Join!">
|
||||
Player 2: <input type="text" name="p2" class="player" placeholder="nickname">
|
||||
<input type="submit" class="take" value="Join!">
|
||||
</form>
|
||||
<div id="duel">
|
||||
|
||||
</div>
|
||||
<script>
|
||||
(function(){
|
||||
var gun = Gun(['http://localhost:8888/' + 'gun'])
|
||||
var me = {},
|
||||
gun = Gun(['http://localhost:8888/' + 'gun'])
|
||||
.load('game/duel', function(game){
|
||||
console.log(game);
|
||||
$(document).on('submit', '.assign-player', function(e){
|
||||
e.preventDefault();
|
||||
console.log("Choosing player!");
|
||||
var nick = $(this).find('input').val();
|
||||
if(!nick){ return }
|
||||
gun.path(this.id).get(function(val){
|
||||
console.log(val, nick, 'oh?');
|
||||
if(val){ return }
|
||||
gun.path('p1').set(nick);
|
||||
gun.path(this.id).set(me.player = nick);
|
||||
});
|
||||
})
|
||||
Gun.on(game._[Gun.sym.id]).event(function(node){
|
||||
console.log("change!", node);
|
||||
});
|
||||
Gun.obj.map(game, me.set);
|
||||
});
|
||||
me.set = function(val, name){
|
||||
$("[name='" + name + "']").text(val).val(val);
|
||||
Gun.on("duel-" + name).emit(val, name);
|
||||
}
|
||||
me.plock = function(val, id){
|
||||
console.log("OH?", val, id);
|
||||
$("#" + id).find('.player').attr("readonly", val? true : false);
|
||||
$("#" + id).find('.take').val(val? "Taken!" : "Join!").attr("disabled", val? true : false);
|
||||
}
|
||||
Gun.on("duel-p1").event(me.plock);
|
||||
Gun.on("duel-p2").event(me.plock);
|
||||
}());
|
||||
</script>
|
||||
</body>
|
||||
|
66
gun.js
66
gun.js
@ -129,12 +129,14 @@
|
||||
Gun.chain.get = function(cb){
|
||||
var gun = this;
|
||||
gun._.events.on(gun._.events.trace += 1).event(function(node){
|
||||
console.log("BOOM got it", node);
|
||||
if(gun._.field){
|
||||
return cb((node||{})[gun._.field]);
|
||||
return cb((node||{})[gun._.field]); // copy data first?
|
||||
}
|
||||
cb(Gun.obj.copy(node));
|
||||
cb(Gun.obj.copy(node)); // we do here.
|
||||
});
|
||||
if(gun._.loaded){
|
||||
gun._.events.at -= 1; // IDK why we are doing this, just trying to get something to work.
|
||||
Gun.log("GET stack trace", gun._.events.trace, gun._.events.at, gun);
|
||||
gun._.events.on(gun._.events.at += 1).emit(this._.node);
|
||||
}
|
||||
@ -613,7 +615,7 @@
|
||||
}
|
||||
}());
|
||||
Gun.log = function(a, b, c, d, e, f){ //s, l){
|
||||
//console.log(a, b, c, d, e, f);
|
||||
console.log(a, b, c, d, e, f);
|
||||
//console.log.apply(console, arguments);
|
||||
}
|
||||
own.sym = Gun.sym = {
|
||||
@ -636,6 +638,13 @@
|
||||
|
||||
}
|
||||
window.tab = tab; //window.XMLHttpRequest = null; // for debugging purposes
|
||||
(function(){
|
||||
tab.store = {};
|
||||
var store = window.localStorage || {setItem: function(){}, removeItem: function(){}, getItem: function(){}};
|
||||
tab.store.set = function(key, val){console.log('setting', key); return store.setItem(key, Gun.text.ify(val)) }
|
||||
tab.store.get = function(key){ return Gun.obj.ify(store.getItem(key)) }
|
||||
tab.store.del = function(key){ return store.removeItem(key) }
|
||||
}());
|
||||
tab.load = tab.load || function(key, cb, opt){
|
||||
cb = cb || function(){};
|
||||
opt = opt || {};
|
||||
@ -662,35 +671,36 @@
|
||||
tab.set = tab.set || function(nodes, cb){
|
||||
cb = cb || function(){};
|
||||
// TODO: batch and throttle later.
|
||||
tab.store.set(respond.id = 'send/' + Gun.text.random(), nodes);
|
||||
//console.log("localStorage the DELTA", nodes);
|
||||
Gun.obj.map(gun.__.opt.peers, function(peer, url){
|
||||
tab.ajax(url, nodes, function respond(err, reply, id){
|
||||
if(reply && reply.body){
|
||||
if(reply.body.defer){
|
||||
tab.set.defer[reply.body.defer] = respond;
|
||||
}
|
||||
if(reply.body.refed || reply.body.reply){
|
||||
//console.log("-------post-reply-all--------->", reply, err);
|
||||
respond(null, {headers: reply.headers, body: reply});
|
||||
Gun.obj.map(reply.body.refed, function(r, id){
|
||||
var cb;
|
||||
if(cb = tab.set.defer[id]){
|
||||
cb(null, {headers: reply.headers, body: r}, id);
|
||||
}
|
||||
});
|
||||
// TODO: should be able to do some type of "checksum" that every request cleared, and if not, figure out what is wrong/wait for finish.
|
||||
return;
|
||||
}
|
||||
tab.sent(reply.body);
|
||||
}
|
||||
Gun.obj.del(tab.set.defer, id);
|
||||
}, {headers: {'Gun-Sub': tab.subscribe.sub || ''}});
|
||||
tab.ajax(url, nodes, respond, {headers: {'Gun-Sub': tab.subscribe.sub || ''}});
|
||||
});
|
||||
function respond(err, reply, id){
|
||||
if(reply && reply.body){
|
||||
if(reply.body.defer){
|
||||
tab.set.defer[reply.body.defer] = respond;
|
||||
}
|
||||
if(reply.body.refed || reply.body.reply){
|
||||
//console.log("-------post-reply-all--------->", reply, err);
|
||||
respond(null, {headers: reply.headers, body: reply});
|
||||
Gun.obj.map(reply.body.refed, function(r, id){
|
||||
var cb;
|
||||
if(cb = tab.set.defer[id]){
|
||||
cb(null, {headers: reply.headers, body: r}, id);
|
||||
}
|
||||
});
|
||||
// TODO: should be able to do some type of "checksum" that every request cleared, and if not, figure out what is wrong/wait for finish.
|
||||
return;
|
||||
}
|
||||
console.log('callback complete, now respond', respond.id);
|
||||
tab.store.del(respond.id);
|
||||
}
|
||||
Gun.obj.del(tab.set.defer, id);
|
||||
}
|
||||
}
|
||||
tab.set.defer = {};
|
||||
tab.sent = function(){
|
||||
// remove set from unsure queue.
|
||||
}
|
||||
tab.subscribe = function(id){ // TODO: BUG!!! ERROR! Unexpected end of input!!!! Fix!
|
||||
tab.subscribe = function(id){ // TODO: BUG!!! ERROR! Handle disconnection (onerror)!!!!
|
||||
tab.subscribe.to = tab.subscribe.to || {};
|
||||
if(id){
|
||||
tab.subscribe.to[id] = 1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user