Skip to content

Instantly share code, notes, and snippets.

View objectiveSee's full-sized avatar

Danny Ricciotti objectiveSee

  • Starship Studios
View GitHub Profile
@objectiveSee
objectiveSee / gist:6488719
Last active December 22, 2015 14:58
Optimal card shuffling and dealing algorithm for a multi-player game server.
THE PROBLEM:
A game server deals hands of cards to each player in a multi-player game.
The ideal user experience is for players to be able to play multiple
games (with random opponents) and not be dealt repeated cards until
they have seen most of the cards in the deck.
CONDITIONS:
1. Each card has a unique id.
@objectiveSee
objectiveSee / gist:6246542
Created August 16, 2013 01:42
How can I save the value of foo from changing code in this function only?
if(request.args.game_id === undefined) {
var foo = undefined;
Game.transactById(request.args.game_id, function(err, game) {
foo = game.participantDiscards(request.getUserId(), request.args.card_id_array);
}, function(err, game) {
User.transactAndSave(userid, function(user) {
// Q chain here
}, function(err, user) {
});
@objectiveSee
objectiveSee / gist:6023351
Created July 17, 2013 18:52
This is the output of running 'pod install'. i Dont recognize any of the pods in this list.
Analyzing dependencies
[!] Pod::Executable pull
Updating a1d1b07..de42d82
error: Your local changes to the following files would be overwritten by merge:
AFKissXMLRequestOperation/0.0.1/AFKissXMLRequestOperation.podspec
AdMob/6.4.2/AdMob.podspec
@objectiveSee
objectiveSee / gist:5968782
Created July 10, 2013 18:24
Right now drawAnswers() gets called before makeDeckIfNeeded() promise is fulfilled. How do I fix that? Does it involve bind()?
return Q.fcall(game.makeDeckIfNeeded.bind(game))
.then(game.deck.drawAnswers(participant, cardsToGet))
.then(function(newCards) {
// add newcards to hand
var newHand = _.union(h,newCards);
participant.hand = newHand;
return Q.fcall(function() { return game.update(); });
});
/**
* NOTE: game.participants contains 3 objects.
GOAL: I would like each promise created in lines 31-43 to be executed in serial.
I do not want any line of code in drawAnswers() to be called before the previous promise is fulfilled.
BUG: The log in drawAnswers() shows me that drawAnswers() is being called 3 times before the
1st promise from drawAnswers() is fulfilled.
shitFinder.findGamesPastDay()
.then(shitFinder.printShit())
.done(function (result) {
console.log('DONE! res=', result); // logs the result from findGamesPastDay instead of foo
process.exit(0)
}, function (error) {
process.exit(1);
});
printShit: function(result) {
// bootstrap!
require('../lib/globals.js');
(function() {
var HttpServer = require('../lib/http_server.js');
var SocketServer = require('../lib/socket_server.js');
var h = new HttpServer();
var s = new SocketServer();
double delayInSeconds = 0.3;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSLog(@"!!!!!!! Go time !!!!!!");
EAForcedUpdateViewController *f = [[EAForcedUpdateViewController alloc] initWithNibName:nil bundle:nil];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = f;
// [self.window resignKeyWindow];
// [self.window setHidden:YES];
/**
* question: how can I rewrite lines 8 & 9 to not use an intermediate function?
*
*
*/
Q.fcall(this.findGamesNearby.bind(this,user,location))
.then(function(games_nearby) {
return matchmaking.makeGameIfNeeded(games_nearby, user, location);
});