2019-11-02 00:06:25 +01:00
|
|
|
const render_widgets_tictactoe_widget = require('../templates/widgets/tictactoe_widget.hbs');
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const tictactoe_data_holder = function () {
|
|
|
|
const self = {};
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const me = people.my_current_user_id();
|
2020-02-12 07:51:29 +01:00
|
|
|
const square_values = new Map();
|
2019-11-02 00:06:25 +01:00
|
|
|
let num_filled = 0;
|
|
|
|
let waiting = false;
|
|
|
|
let game_over = false;
|
2018-02-23 15:45:25 +01:00
|
|
|
|
|
|
|
function is_game_over() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const lines = [
|
2018-02-23 15:45:25 +01:00
|
|
|
[1, 2, 3],
|
|
|
|
[4, 5, 6],
|
|
|
|
[7, 8, 9],
|
|
|
|
[1, 4, 7],
|
|
|
|
[2, 5, 8],
|
|
|
|
[3, 6, 9],
|
|
|
|
[1, 5, 9],
|
|
|
|
[7, 5, 3],
|
|
|
|
];
|
|
|
|
|
|
|
|
function line_won(line) {
|
2020-02-12 07:51:29 +01:00
|
|
|
const token = square_values.get(line[0]);
|
2018-02-23 15:45:25 +01:00
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-02-12 07:51:29 +01:00
|
|
|
square_values.get(line[1]) === token &&
|
|
|
|
square_values.get(line[2]) === token);
|
2018-02-23 15:45:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const board = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
2018-02-23 15:45:25 +01:00
|
|
|
function filled(i) {
|
2020-02-12 07:51:29 +01:00
|
|
|
return square_values.get(i);
|
2018-02-23 15:45:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 05:11:31 +01:00
|
|
|
return lines.some(line_won) || board.every(filled);
|
2018-02-23 15:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.get_widget_data = function () {
|
|
|
|
function square(i) {
|
|
|
|
return {
|
2020-02-12 07:51:29 +01:00
|
|
|
val: square_values.get(i),
|
2018-02-23 15:45:25 +01:00
|
|
|
idx: i,
|
2020-02-12 07:51:29 +01:00
|
|
|
disabled: waiting || square_values.get(i) || game_over,
|
2018-02-23 15:45:25 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const squares = [
|
2018-02-23 15:45:25 +01:00
|
|
|
[square(1), square(2), square(3)],
|
|
|
|
[square(4), square(5), square(6)],
|
|
|
|
[square(7), square(8), square(9)],
|
|
|
|
];
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const token = num_filled % 2 === 0 ? 'X' : 'O';
|
|
|
|
let move_status = token + "'s turn";
|
2018-02-23 15:45:25 +01:00
|
|
|
|
|
|
|
if (game_over) {
|
|
|
|
move_status = "Game over!";
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_data = {
|
2018-02-23 15:45:25 +01:00
|
|
|
squares: squares,
|
|
|
|
move_status: move_status,
|
|
|
|
};
|
|
|
|
|
|
|
|
return widget_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.handle = {
|
|
|
|
square_click: {
|
|
|
|
outbound: function (idx) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2018-02-23 15:45:25 +01:00
|
|
|
type: 'square_click',
|
|
|
|
idx: idx,
|
|
|
|
num_filled: num_filled,
|
|
|
|
};
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
|
|
|
|
inbound: function (sender_id, data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const idx = data.idx;
|
2018-02-23 15:45:25 +01:00
|
|
|
|
|
|
|
if (data.num_filled !== num_filled) {
|
|
|
|
blueslip.info('out of sync', data.num_filled);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const token = num_filled % 2 === 0 ? 'X' : 'O';
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2020-02-12 07:51:29 +01:00
|
|
|
if (square_values.has(idx)) {
|
2018-02-23 15:45:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-06 18:19:09 +02:00
|
|
|
waiting = sender_id === me;
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2020-02-12 07:51:29 +01:00
|
|
|
square_values.set(idx, token);
|
2018-02-23 15:45:25 +01:00
|
|
|
num_filled += 1;
|
|
|
|
|
|
|
|
game_over = is_game_over();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
self.handle_event = function (sender_id, data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const type = data.type;
|
2018-02-23 15:45:25 +01:00
|
|
|
if (self.handle[type]) {
|
|
|
|
self.handle[type].inbound(sender_id, data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.activate = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = opts.elem;
|
|
|
|
const callback = opts.callback;
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const tictactoe_data = tictactoe_data_holder();
|
2018-02-23 15:45:25 +01:00
|
|
|
|
|
|
|
function render() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_data = tictactoe_data.get_widget_data();
|
|
|
|
const html = render_widgets_tictactoe_widget(widget_data);
|
2018-02-23 15:45:25 +01:00
|
|
|
elem.html(html);
|
|
|
|
|
|
|
|
elem.find("button.tictactoe-square").on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
2020-02-22 01:26:48 +01:00
|
|
|
const str_idx = $(e.target).attr('data-idx');
|
|
|
|
const idx = parseInt(str_idx, 10);
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = tictactoe_data.handle.square_click.outbound(idx);
|
2018-02-23 15:45:25 +01:00
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-26 09:30:57 +02:00
|
|
|
elem.handle_events = function (events) {
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const event of events) {
|
2018-02-23 15:45:25 +01:00
|
|
|
tictactoe_data.handle_event(event.sender_id, event.data);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 15:45:25 +01:00
|
|
|
render();
|
|
|
|
};
|
|
|
|
|
|
|
|
render();
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.tictactoe_widget = exports;
|