<commit_before>// the url map for game-related actions.

var Jack = require("jack"), HashP = require("hashp").HashP;

var map = {};

var couch = require("./couch").CouchDB("http://localhost:5984/jic-jack-jo");

var emptyGame = {
    x : null,
    o : null,
    state : [["","",""],["","",""],["","","x"]],
    turn : "x",
    win : null
};

function sitDown (user) {
    // see if there is an empty seat.
    var seat = couch.GET("_design/jic-jack-jo/_view/open_seat").rows[0].value;
    var game;
    if (seat[0] === "open") {
        game = couch.GET(seat[1]);
        // make sure i'm not sitting there already...
        if (game) {
            if (game.x !== user && game.o !== user) {
                // sit down
                game[ seat[2] ] = user;
                if (couch.PUT(seat[1], game, true).status !== 201) return sitDown(user);
                return game;
            }
            // already sitting.
            return game;
        }
    }
    // either the seats are all full, or the GET failed.
    game = emptyGame;
    game.x = user;
    game._id = seat[1];
    if (!couch.PUT(seat[1], game, true)) return sitDown(user);
    return game;
}
map["/new"] = require("./allowed-methods").AllowedMethods(function (env) {
    var game = sitDown(env["jjj.user"]);
    return {
        status : 302,
        headers : {"Content-Type":"text/plain","Location":"http://"+env.HTTP_HOST+"/game/"+game._id},
        body : []
    };
}, ["POST", "PUT"]);

map["/move"] = require("./move").app;

map["/"] = function (env) {
    
    var id = env.PATH_INFO.replace(/^.*?(game-[0-9]+).*$/g, '$1');
    var game = couch.GET(id);
    if (!game) return {
        status : 302,
        headers : {Location:"http://"+env.HTTP_HOST+"/game/new"},
        body : []
    }
    game.you = env["jjj.user"];
    
    return {
        status :200,
        headers : {"Content-Type":"text/javascript"},
        body : [JSON.stringify(game)]
    };
};

exports.app = Jack.URLMap(map);<commit_msg>Temporarily allow GET and HEAD to /game/new.  And we need a content-type header, even if there's no body.<commit_after>// the url map for game-related actions.

var Jack = require("jack"), HashP = require("hashp").HashP;

var map = {};

var couch = require("./couch").CouchDB("http://localhost:5984/jic-jack-jo");

var emptyGame = {
    x : null,
    o : null,
    state : [["","",""],["","",""],["","","x"]],
    turn : "x",
    win : null
};

function sitDown (user) {
    // see if there is an empty seat.
    var seat = couch.GET("_design/jic-jack-jo/_view/open_seat").rows[0].value;
    var game;
    if (seat[0] === "open") {
        game = couch.GET(seat[1]);
        // make sure i'm not sitting there already...
        if (game) {
            if (game.x !== user && game.o !== user) {
                // sit down
                game[ seat[2] ] = user;
                if (couch.PUT(seat[1], game, true).status !== 201) return sitDown(user);
                return game;
            }
            // already sitting.
            return game;
        }
    }
    // either the seats are all full, or the GET failed.
    game = emptyGame;
    game.x = user;
    game._id = seat[1];
    if (!couch.PUT(seat[1], game, true)) return sitDown(user);
    return game;
}
map["/new"] = require("./allowed-methods").AllowedMethods(function (env) {
    var game = sitDown(env["jjj.user"]);
    return {
        status : 302,
        headers : {
            "Content-Type":"text/plain",
            "Location":"http://"+env.HTTP_HOST+"/game/"+game._id
        },
        body : []
    };
}, ["POST", "PUT", "HEAD", "GET"]);

map["/move"] = require("./move").app;

map["/"] = function (env) {
    var id = env.PATH_INFO.replace(/^.*?(game-[0-9]+).*$/g, '$1');
    var game = couch.GET(id);
    var url = "http://"+env.HTTP_HOST+"/game/new";
    if (!game) return {
        status : 302,
        headers : {
            Location:url,
            "Content-Type":"text/html"
        },
        body : ["<a href='"+url+"'>moved to this location</a>"]
    }
    game.you = env["jjj.user"];
    
    return {
        status :200,
        headers : {"Content-Type":"text/javascript"},
        body : [JSON.stringify(game)]
    };
};

exports.app = Jack.URLMap(map);
{'fraction_non_alphanumeric': 0.2019321394910462, 'fraction_numerical': 0.012252591894439209, 'mean_word_length': 2.071635311143271, 'pattern_counts': {'":': 7, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '854501', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1401, 'n_words': 328}