UNPKG

4.04 kBJavaScriptView Raw
1const GameChallenger = require('../ssb_ctrl/game_challenge');
2const GameSSBDao = require('../ssb_ctrl/game');
3const ChessIndex = require('ssb-chess-index');
4const GameDb = require('../db/game_db');
5const SocialCtrl = require('./social');
6const PlayerCtrl = require('./player');
7const MoveCtrl = require('./gameMove');
8const RecentActivityCtrl = require('./recentActivityCtrl');
9const PgnCtrl = require('./pgn');
10const MovesFinder = require('./validMovesFinder');
11const InviteCtrl = require('./inviteCtrl');
12const GameCtrl = require('./gameCtrl');
13const UserGamesUpdateWatcher = require('./userGameUpdatesWatcher');
14
15const settingsCtrl = require('./settings')();
16
17const BacklinkUtils = require('../utils/backlinks_obs');
18
19/**
20 * The main controller which can be used to access the functional area specific controllers.
21 *
22 * @param {*} sbot the scuttlebot instance
23 * @param {*} myIdent the user's public key
24 */
25module.exports = (sbot, myIdent) => {
26
27 const chessIndex = ChessIndex(sbot);
28
29 const backlinkUtils = BacklinkUtils();
30 const socialCtrl = SocialCtrl(sbot, myIdent, chessIndex);
31
32 const gameSSBDao = GameSSBDao(sbot, myIdent, backlinkUtils, socialCtrl);
33 const gameChallenger = GameChallenger(sbot, myIdent);
34 const gameDb = GameDb(chessIndex);
35 const moveCtrl = MoveCtrl(gameSSBDao, myIdent);
36 const pgnCtrl = PgnCtrl(gameSSBDao);
37
38 const playerCtrl = PlayerCtrl(sbot, gameDb, gameSSBDao);
39
40 const movesFinderCtrl = MovesFinder();
41
42 const userGamesUpdateWatcher = UserGamesUpdateWatcher(sbot, chessIndex);
43
44 const myGameUpdates = userGamesUpdateWatcher.latestGameMessageForPlayerObs(myIdent);
45
46 const gameCtrl = GameCtrl(myIdent, gameSSBDao, gameDb, userGamesUpdateWatcher, myGameUpdates);
47
48 const recentActivityCtrl = RecentActivityCtrl(
49 userGamesUpdateWatcher,
50 gameCtrl.getSituationObservable,
51 myIdent,
52 );
53
54 const inviteCtrl = InviteCtrl(myIdent, gameChallenger, gameDb, myGameUpdates);
55
56 function getMyIdent() {
57 return myIdent;
58 }
59
60 return {
61 /**
62 * Get the player's public key.
63 */
64 getMyIdent,
65
66 /**
67 * Game controller for fetching game states, and fetching games with properties like 'games where it's the player's move.'
68 *
69 * The games and game lists are returned as observables.
70 */
71 getGameCtrl: () => gameCtrl,
72
73 /**
74 * Invite controller. Manage incoming / outgoing game invitations.
75 */
76 getInviteCtrl: () => inviteCtrl,
77
78 /**
79 * Controller for performing moves on a game. Commits game moves to the user's feed.
80 */
81 getMoveCtrl: () => moveCtrl,
82
83 /**
84 * Controller for fetching information about users - such as their display name, who the player is following, etc.
85 */
86 getSocialCtrl: () => socialCtrl,
87
88 /**
89 * Controller for fetching information about players.
90 */
91 getPlayerCtrl: () => playerCtrl,
92
93 /**
94 * Controller for finding out about recent activity on games, such as games a player was in that ended recently.
95 * Useful for showing an aggregated list of activity such as a list of games that the user recently won / lost / drew.
96 *
97 * Differs from getUserGameWatcherCtrl
98 */
99 getRecentActivityCtrl: () => recentActivityCtrl,
100
101 /**
102 * Controller for calculating valid moves for a given game situation.
103 */
104 getMovesFinderCtrl: () => movesFinderCtrl,
105
106 /**
107 * Controller for exporting games as a PGN.
108 */
109 getPgnCtrl: () => pgnCtrl,
110
111 /**
112 * Controller for watching for updates in player's games. Useful for showing notifications when it's a
113 * player's turn to move, or watching for game updates to know when to update observables.
114 */
115 getUserGameWatcherCtrl: () => userGamesUpdateWatcher,
116
117 /**
118 * Get the sbot instance. An abstraction leak, but can be useful when using UI libraries that take the
119 * sbot instance as a parameter.
120 */
121 getSbot: () => sbot,
122
123 /**
124 * A controller for storing settings in local storage
125 */
126 getSettingsCtrl: () => settingsCtrl
127 };
128};