2020-07-15 01:29:15 +02:00
|
|
|
const render_widgets_tictactoe_widget = require("../templates/widgets/tictactoe_widget.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
class TicTacToeData {
|
|
|
|
me = people.my_current_user_id();
|
|
|
|
square_values = new Map();
|
|
|
|
num_filled = 0;
|
|
|
|
waiting = false;
|
|
|
|
game_over = false;
|
|
|
|
|
|
|
|
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],
|
|
|
|
];
|
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
const line_won = (line) => {
|
|
|
|
const token = this.square_values.get(line[0]);
|
2018-02-23 15:45:25 +01:00
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
return (
|
|
|
|
this.square_values.get(line[1]) === token &&
|
|
|
|
this.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];
|
2020-07-23 03:40:49 +02:00
|
|
|
const filled = (i) => this.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
|
|
|
}
|
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
get_widget_data() {
|
|
|
|
const square = (i) => ({
|
|
|
|
val: this.square_values.get(i),
|
|
|
|
idx: i,
|
|
|
|
disabled: this.waiting || this.square_values.get(i) || this.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)],
|
|
|
|
];
|
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
const token = this.num_filled % 2 === 0 ? "X" : "O";
|
2019-11-02 00:06:25 +01:00
|
|
|
let move_status = token + "'s turn";
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
if (this.game_over) {
|
2018-02-23 15:45:25 +01:00
|
|
|
move_status = "Game over!";
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget_data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
squares,
|
|
|
|
move_status,
|
2018-02-23 15:45:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return widget_data;
|
2020-07-23 03:40:49 +02:00
|
|
|
}
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
handle = {
|
2018-02-23 15:45:25 +01:00
|
|
|
square_click: {
|
2020-07-23 03:40:49 +02:00
|
|
|
outbound: (idx) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "square_click",
|
2020-07-20 22:18:43 +02:00
|
|
|
idx,
|
2020-07-23 03:40:49 +02:00
|
|
|
num_filled: this.num_filled,
|
2018-02-23 15:45:25 +01:00
|
|
|
};
|
|
|
|
return event;
|
|
|
|
},
|
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
inbound: (sender_id, data) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const idx = data.idx;
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
if (data.num_filled !== this.num_filled) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.info("out of sync", data.num_filled);
|
2018-02-23 15:45:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
const token = this.num_filled % 2 === 0 ? "X" : "O";
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
if (this.square_values.has(idx)) {
|
2018-02-23 15:45:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
this.waiting = sender_id === this.me;
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
this.square_values.set(idx, token);
|
|
|
|
this.num_filled += 1;
|
2018-02-23 15:45:25 +01:00
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
this.game_over = this.is_game_over();
|
2018-02-23 15:45:25 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
handle_event(sender_id, data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const type = data.type;
|
2020-07-23 03:40:49 +02:00
|
|
|
if (this.handle[type]) {
|
|
|
|
this.handle[type].inbound(sender_id, data);
|
2018-02-23 15:45:25 +01:00
|
|
|
}
|
2020-07-23 03:40:49 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 15:45:25 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-07-23 03:40:49 +02:00
|
|
|
const tictactoe_data = new TicTacToeData();
|
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);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.find("button.tictactoe-square").on("click", (e) => {
|
2018-02-23 15:45:25 +01:00
|
|
|
e.stopPropagation();
|
2020-07-15 01:29:15 +02:00
|
|
|
const str_idx = $(e.target).attr("data-idx");
|
2020-02-22 01:26:48 +01:00
|
|
|
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;
|