diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-03-07 19:00:15 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-07 19:00:15 +0900 |
| commit | a227ef30e8bc8282605361b013d323bb3bbc4e7d (patch) | |
| tree | 8bd28f1d27a77e061e7b7f56f8794e483fe82f8f /src/api/models | |
| parent | v3999 (diff) | |
| parent | oops (diff) | |
| download | sharkey-a227ef30e8bc8282605361b013d323bb3bbc4e7d.tar.gz sharkey-a227ef30e8bc8282605361b013d323bb3bbc4e7d.tar.bz2 sharkey-a227ef30e8bc8282605361b013d323bb3bbc4e7d.zip | |
Merge pull request #1197 from syuilo/othello
Othello
Diffstat (limited to 'src/api/models')
| -rw-r--r-- | src/api/models/othello-game.ts | 53 | ||||
| -rw-r--r-- | src/api/models/othello-matching.ts | 42 |
2 files changed, 95 insertions, 0 deletions
diff --git a/src/api/models/othello-game.ts b/src/api/models/othello-game.ts new file mode 100644 index 0000000000..73a5c94b21 --- /dev/null +++ b/src/api/models/othello-game.ts @@ -0,0 +1,53 @@ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import db from '../../db/mongodb'; +import { IUser, pack as packUser } from './user'; + +const Game = db.get<IGame>('othello_games'); +export default Game; + +export interface IGame { + _id: mongo.ObjectID; + created_at: Date; + black_user_id: mongo.ObjectID; + white_user_id: mongo.ObjectID; + turn_user_id: mongo.ObjectID; + is_ended: boolean; + winner_id: mongo.ObjectID; + logs: any[]; +} + +/** + * Pack an othello game for API response + */ +export const pack = ( + game: any, + me?: string | mongo.ObjectID | IUser +) => new Promise<any>(async (resolve, reject) => { + + // Me + const meId: mongo.ObjectID = me + ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? me as mongo.ObjectID + : typeof me === 'string' + ? new mongo.ObjectID(me) + : (me as IUser)._id + : null; + + const _game = deepcopy(game); + + // Rename _id to id + _game.id = _game._id; + delete _game._id; + + // Populate user + _game.black_user = await packUser(_game.black_user_id, meId); + _game.white_user = await packUser(_game.white_user_id, meId); + if (_game.winner_id) { + _game.winner = await packUser(_game.winner_id, meId); + } else { + _game.winner = null; + } + + resolve(_game); +}); diff --git a/src/api/models/othello-matching.ts b/src/api/models/othello-matching.ts new file mode 100644 index 0000000000..89fcd6df6a --- /dev/null +++ b/src/api/models/othello-matching.ts @@ -0,0 +1,42 @@ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import db from '../../db/mongodb'; +import { IUser, pack as packUser } from './user'; + +const Matching = db.get<IMatching>('othello_matchings'); +export default Matching; + +export interface IMatching { + _id: mongo.ObjectID; + created_at: Date; + parent_id: mongo.ObjectID; + child_id: mongo.ObjectID; +} + +/** + * Pack an othello matching for API response + */ +export const pack = ( + matching: any, + me?: string | mongo.ObjectID | IUser +) => new Promise<any>(async (resolve, reject) => { + + // Me + const meId: mongo.ObjectID = me + ? mongo.ObjectID.prototype.isPrototypeOf(me) + ? me as mongo.ObjectID + : typeof me === 'string' + ? new mongo.ObjectID(me) + : (me as IUser)._id + : null; + + const _matching = deepcopy(matching); + + delete _matching._id; + + // Populate user + _matching.parent = await packUser(_matching.parent_id, meId); + _matching.child = await packUser(_matching.child_id, meId); + + resolve(_matching); +}); |