Skip to content

Instantly share code, notes, and snippets.

View JeffML's full-sized avatar
🏠
Barely working

Jeff Lowery JeffML

🏠
Barely working
View GitHub Profile
@JeffML
JeffML / dataSource.js
Last active August 21, 2017 04:16
MyLittleDomain
const myLittleTypes = [{
id: 1,
description: 'This is good',
}, {
id: 2,
description: 'This is better',
}, {
id: 3,
description: 'This is the best!',
}];
@JeffML
JeffML / author.js
Created August 21, 2017 01:25
Refactoring authorposts schema
export default `
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post] # the list of Posts by this author
}`
@JeffML
JeffML / authorpostResolvers.js
Created August 21, 2017 00:40
authorposts schema
import {
authors,
posts
} from './dataSource';
const rootResolvers = {
Query: {
posts: () => posts,
author: (_, {
id
@JeffML
JeffML / rootSchema.js
Last active August 21, 2017 00:29
Reorg: root schema
import {
makeExecutableSchema
} from 'graphql-tools';
import {
schema as authorpostsSchema,
resolvers as authorpostsResolvers
} from './authorposts';
@JeffML
JeffML / SquareControl.js
Created July 16, 2017 23:33
service to determine chess square controlled by one side
module.exports = function squareControl() {
const getSquaresControlledBy = (board, pieceColor) => {
var pieces = board.boardPieces[pieceColor];
const promises = pieces.map(p =>
new Promise(resolve => {
this.act({
role: "movement",
cmd: "legalMoves",
piece: p,
@JeffML
JeffML / legalMovesWithKing.js
Last active July 16, 2017 23:29
king moves helper method
module.exports = function (boardAndPiece, candidateMoves, reply) {
const opposingColor = boardAndPiece.piece.color === 'W' ? 'black' : 'white';
//temporarily remove the K to avoid cycles
boardAndPiece.board.removePiece(boardAndPiece.piece);
function canCastle(king, rook, intervening, opposing) {
// console.log("canCastle", arguments)
const opposingControlled = [...opposing.controlled]
@JeffML
JeffML / LegalMoves2.js
Created July 16, 2017 23:18
Second legalMoves service
this.add('role:movement,cmd:legalMoves', function (msg, reply) {
this.prior(msg, function (err, result) {
if (msg.board) {
const result2 = legalMovesWithBoard(msg, result);
if (msg.piece.piece === 'K') {
legalMovesWithKing.call(this, msg, result2, reply)
} else {
reply(err, result2);
}
} else {
@JeffML
JeffML / legalMovesUsingMoveVectors.js
Created July 16, 2017 22:41
chess micro service example
const legalMovesWithBoard = require("./helpers/legalMovesWithBoard")
//...
this.add('role:movement,cmd:legalMoves', function (msg, reply) {
this.prior(msg, function (err, result) {
if (msg.board) {
const result2 = legalMovesWithBoard(msg, result);
//...
@JeffML
JeffML / knightMoves.js
Created July 16, 2017 22:33
method used to determine knight moves on a chess board
function knightChecks(boardAndPiece, candidateMoves) {
const newMoves = [];
for (const m of candidateMoves.moves) {
const p = boardAndPiece.board.pieceAt(m)
if (!p) {
newMoves.push(m)
} else if (p.color !== boardAndPiece.piece.color) {
m.hasCaptured = p;
newMoves.push(m)
@JeffML
JeffML / legalMovesWithBoard.js
Last active July 16, 2017 21:59
Legal moves based on moveVectors
module.exports = function (boardAndPiece, candidateMoves) {
if (!boardAndPiece.board) return candidateMoves;
const rangeChecks = {
B: vectorChecks,
R: vectorChecks,
K: vectorChecks,
Q: vectorChecks,
P: pawnChecks,
N: knightChecks