-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
160 lines (142 loc) · 4.53 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*************** MONGODB DATABASE ***************/
import { MongoClient, ObjectId } from 'mongodb';
import assert from 'assert';
// Connection URL
const uri = process.env.MONGODB_URI || 'mongodb://localhost:27017';
// Database name
const dbName = 'bughaus';
let db;
// TODO: Perhaps privatize this
export default db;
export function connectClient() {
// Connect to a running mongodb deployment
return MongoClient.connect(uri).then((client) => {
console.log("** Connected successfully to mongo server **");
db = client.db(dbName);
return db;
// TODO: when do you do this?
// client.close();
}).catch((error) => {
console.error(error);
});
}
const INITIAL_FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
const INITIAL_PIECES = [
{ key: 'a1', square: 'a1', piece: 'r', color: 'w' },
{ key: 'b1', square: 'b1', piece: 'n', color: 'w' },
{ key: 'c1', square: 'c1', piece: 'b', color: 'w' },
{ key: 'd1', square: 'd1', piece: 'q', color: 'w' },
{ key: 'e1', square: 'e1', piece: 'k', color: 'w' },
{ key: 'f1', square: 'f1', piece: 'b', color: 'w' },
{ key: 'g1', square: 'g1', piece: 'n', color: 'w' },
{ key: 'h1', square: 'h1', piece: 'r', color: 'w' },
{ key: 'a2', square: 'a2', piece: 'p', color: 'w' },
{ key: 'b2', square: 'b2', piece: 'p', color: 'w' },
{ key: 'c2', square: 'c2', piece: 'p', color: 'w' },
{ key: 'd2', square: 'd2', piece: 'p', color: 'w' },
{ key: 'e2', square: 'e2', piece: 'p', color: 'w' },
{ key: 'f2', square: 'f2', piece: 'p', color: 'w' },
{ key: 'g2', square: 'g2', piece: 'p', color: 'w' },
{ key: 'h2', square: 'h2', piece: 'p', color: 'w' },
{ key: 'a7', square: 'a7', piece: 'p', color: 'b' },
{ key: 'b7', square: 'b7', piece: 'p', color: 'b' },
{ key: 'c7', square: 'c7', piece: 'p', color: 'b' },
{ key: 'd7', square: 'd7', piece: 'p', color: 'b' },
{ key: 'e7', square: 'e7', piece: 'p', color: 'b' },
{ key: 'f7', square: 'f7', piece: 'p', color: 'b' },
{ key: 'g7', square: 'g7', piece: 'p', color: 'b' },
{ key: 'h7', square: 'h7', piece: 'p', color: 'b' },
{ key: 'a8', square: 'a8', piece: 'r', color: 'b' },
{ key: 'b8', square: 'b8', piece: 'n', color: 'b' },
{ key: 'c8', square: 'c8', piece: 'b', color: 'b' },
{ key: 'd8', square: 'd8', piece: 'q', color: 'b' },
{ key: 'e8', square: 'e8', piece: 'k', color: 'b' },
{ key: 'f8', square: 'f8', piece: 'b', color: 'b' },
{ key: 'g8', square: 'g8', piece: 'n', color: 'b' },
{ key: 'h8', square: 'h8', piece: 'r', color: 'b' }
];
const INITIAL_GAME = {
// Store all the positions for game in one big array b/c there's only one position that
// you're looking at. Storing positions in 2 board arrays will get confusing to reconcile
// what the actual latest position you're looking at is from the latest move
gameHistory: [
{
boardNum: 0,
fen: INITIAL_FEN,
board: INITIAL_PIECES,
wCaptured: '',
bCaptured: '',
wDropped: [],
bDropped: [],
moveIndex: -1
},
{
boardNum: 1,
fen: INITIAL_FEN,
board: INITIAL_PIECES,
wCaptured: '',
bCaptured: '',
wDropped: [],
bDropped: [],
moveIndex: -1
}
],
currentGames: [
{
wPlayer: null,
bPlayer: null,
moves: []
},
{
wPlayer: null,
bPlayer: null,
moves: []
}
]
};
export function createGame(gameId) {
const collection = db.collection('games');
// Returns a promise if no callback is called
return collection.insertOne({ _id: gameId, ...INITIAL_GAME });
}
export function getGame(gameId) {
const collection = db.collection('games');
return collection.findOne({ '_id': gameId });
}
export function updateGame(gameId, data) {
const collection = db.collection('games');
return collection.updateOne({ '_id': gameId }, { '$set': data });
}
export function updatePlayer(gameId, { boardNum, color, username }) {
const collection = db.collection('games');
return collection.updateOne(
{ '_id': gameId },
{ '$set':
{
[`currentGames.${boardNum}.${color}Player`]: username
}
}
);
}
export function addMove(gameId, boardNum, move) {
const collection = db.collection('games');
return collection.updateOne(
{ '_id': gameId },
{ '$push':
{
[`currentGames.${boardNum}.moves`]: move
}
}
);
}
export function addPosition(gameId, position) {
const collection = db.collection('games');
return collection.updateOne(
{ '_id': gameId },
{ '$push':
{
gameHistory: position
}
}
);
}