Skip to content

Instantly share code, notes, and snippets.

@halfmanhalfdonut
Last active December 16, 2015 13:39
Show Gist options
  • Save halfmanhalfdonut/5443712 to your computer and use it in GitHub Desktop.
Save halfmanhalfdonut/5443712 to your computer and use it in GitHub Desktop.
Super Street Fighter II stat extractor (2p)
fs = require 'fs'
class SF2
constructor: (file) ->
# Skeleton json response
@stats = {
"session": +new Date
"p1": {
"fighters": [],
"total": {}
},
"p2": {
"fighters": [],
"total": {}
}
}
# open the file synchronously. I know, totally anti-node, but we actually need to wait for this.
@fd = fs.openSync(file, 'r')
total = @fighters.length
totalMinOne = total - 1
# All but the last one, which is 'totals'
for i in [0...totalMinOne] by 1
@stats.p1.fighters.push(@getRecordWithName(@fighters[i], @p1[i]))
@stats.p2.fighters.push(@getRecordWithName(@fighters[i], @p2[i]))
# Ok, add in the totals
@stats.p1.total = @getRecord(@p1[totalMinOne])
@stats.p2.total = @getRecord(@p2[totalMinOne])
# That's all we need, close it
fs.closeSync(@fd)
# You can also access this from sf2.stats, but it's kind of ugly. Let's give a method for it!
getStats: ->
return @stats
# We want the record for this location set as well as the fighter's name
getRecordWithName: (name, initLoc) ->
record = @getRecord(initLoc)
record["name"] = name
return record
# Grab the record for a given location set
getRecord: (initLoc) ->
return {
"wins" : @getFullPart(initLoc)
"losses" : @getFullPart(initLoc + 6)
"ties": @getFullPart(initLoc + 12)
}
# Grab a given stat, eg wins
getFullPart: (loc) ->
return parseInt(@getPart(loc) + @getPart(loc + 2), 10)
# Get a single digit from a stat, ie if they have 10 wins, the first part is 1, second part is 0
getPart: (loc) ->
buffer = new Buffer(1)
fs.readSync(@fd, buffer, 0, 1, loc)
return buffer.toString()
# Fighter names
fighters: [
"ryu",
"e. honda",
"blanka",
"guile",
"ken",
"chun li",
"zangief",
"dhalsim",
"cammy",
"t. hawk",
"fei long",
"dee jay",
"balrog",
"vega",
"sagat",
"m. bison",
"total"
]
# Player 1 locations
# Each number indicates the decimal location within the file per fighter
# Given the record of 13 Wins, 4 Losses, and 5 Ties, with wins at location X...
# The start location is the first digit of their WINS column (1)
# The second digit of WINS is X + 2 from that location. (3)
# The first digit of LOSSES is X + 6 ( ) <-- space indicates nothingness, or zero!
# The second digit of LOSSES is X + 8 (4)
# The first digit of TIES is X + 12 ( )
# The second digit of TIES is X + 14 (5)
# These arrays map to the fighters array, so check that for order
p1: [
171543,
171607,
171671,
171735,
171799,
171863,
171927,
171991,
172055,
172119,
172183,
172247,
172311,
172375,
172439,
172503,
172631
]
# player 2 locations, same deal as p1
p2: [
171581,
171645,
171709,
171773,
171837,
171901,
171965,
172029,
172093,
172157,
172221,
172285,
172349,
172413,
172477,
172541,
172669
]
# Export this so we can use it elsewhere!
module.exports = SF2
var SF2, fs;
fs = require('fs');
SF2 = (function() {
function SF2(file) {
var i, total, totalMinOne, _i;
this.stats = {
"session": +(new Date),
"p1": {
"fighters": [],
"total": {}
},
"p2": {
"fighters": [],
"total": {}
}
};
this.fd = fs.openSync(file, 'r');
total = this.fighters.length;
totalMinOne = total - 1;
for (i = _i = 0; _i < totalMinOne; i = _i += 1) {
this.stats.p1.fighters.push(this.getRecordWithName(this.fighters[i], this.p1[i]));
this.stats.p2.fighters.push(this.getRecordWithName(this.fighters[i], this.p2[i]));
}
this.stats.p1.total = this.getRecord(this.p1[totalMinOne]);
this.stats.p2.total = this.getRecord(this.p2[totalMinOne]);
fs.closeSync(this.fd);
}
SF2.prototype.getStats = function() {
return this.stats;
};
SF2.prototype.getRecordWithName = function(name, initLoc) {
var record;
record = this.getRecord(initLoc);
record["name"] = name;
return record;
};
SF2.prototype.getRecord = function(initLoc) {
return {
"wins": this.getFullPart(initLoc),
"losses": this.getFullPart(initLoc + 6),
"ties": this.getFullPart(initLoc + 12)
};
};
SF2.prototype.getFullPart = function(loc) {
return parseInt(this.getPart(loc) + this.getPart(loc + 2), 10);
};
SF2.prototype.getPart = function(loc) {
var buffer;
buffer = new Buffer(1);
fs.readSync(this.fd, buffer, 0, 1, loc);
return buffer.toString();
};
SF2.prototype.fighters = ["ryu", "e. honda", "blanka", "guile", "ken", "chun li", "zangief", "dhalsim", "cammy", "t. hawk", "fei long", "dee jay", "balrog", "vega", "sagat", "m. bison", "total"];
SF2.prototype.p1 = [171543, 171607, 171671, 171735, 171799, 171863, 171927, 171991, 172055, 172119, 172183, 172247, 172311, 172375, 172439, 172503, 172631];
SF2.prototype.p2 = [171581, 171645, 171709, 171773, 171837, 171901, 171965, 172029, 172093, 172157, 172221, 172285, 172349, 172413, 172477, 172541, 172669];
return SF2;
})();
module.exports = SF2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment