summaryrefslogtreecommitdiff
path: root/frontend/src/models
diff options
context:
space:
mode:
authorVictor Chabbert <chabbertvi@eisti.eu>2017-05-28 20:42:21 +0200
committerVictor Chabbert <chabbertvi@eisti.eu>2017-05-28 20:42:21 +0200
commit0bf423172ffb2e4030b521b0985d133cb5c61dd9 (patch)
tree0c24821770274a414f80b3092d5be54902c042d9 /frontend/src/models
parentFix proxy not working since CRA upgrade (diff)
downloadseven-wonders-0bf423172ffb2e4030b521b0985d133cb5c61dd9.tar.gz
seven-wonders-0bf423172ffb2e4030b521b0985d133cb5c61dd9.tar.bz2
seven-wonders-0bf423172ffb2e4030b521b0985d133cb5c61dd9.zip
Move to immutable with Records
Diffstat (limited to 'frontend/src/models')
-rw-r--r--frontend/src/models/errors.js34
-rw-r--r--frontend/src/models/games.js41
-rw-r--r--frontend/src/models/players.js26
3 files changed, 101 insertions, 0 deletions
diff --git a/frontend/src/models/errors.js b/frontend/src/models/errors.js
new file mode 100644
index 00000000..c00954cd
--- /dev/null
+++ b/frontend/src/models/errors.js
@@ -0,0 +1,34 @@
+import { Record, List } from 'immutable';
+
+const ErrorsRecord = Record({
+ nextId: 0,
+ history: new List(),
+});
+
+export default class ErrorsState extends ErrorsRecord {
+ addError(error) {
+ const errorObject = new Error({ id: this.nextId, error: new ErrorBag(error) });
+ return this.set('history', this.history.push(errorObject)).set('nextId', this.nextId + 1);
+ }
+}
+
+const ErrorRecord = Record({
+ id: -1,
+ timestamp: new Date(),
+ error: new ErrorsRecord(),
+});
+
+export class Error extends ErrorRecord {}
+
+const ErrorBagRecord = Record({
+ type: '',
+ position: 'bottom-left',
+ options: {
+ icon: 'error',
+ removeOnHover: true,
+ showCloseButton: true,
+ },
+ title: 'Unknown Error',
+});
+
+export class ErrorBag extends ErrorBagRecord {}
diff --git a/frontend/src/models/games.js b/frontend/src/models/games.js
new file mode 100644
index 00000000..95bf8015
--- /dev/null
+++ b/frontend/src/models/games.js
@@ -0,0 +1,41 @@
+import { Record, Map, List } from 'immutable';
+
+const SettingsRecord = Record({
+ initialGold: 3,
+ lostPointsPerDefeat: 1,
+ timeLimitInSeconds: 45,
+ randomSeedForTests: -1,
+ discardedCardGold: 3,
+ defaultTradingCost: 2,
+ wonPointsPerVictoryPerAge: {
+ '1': 1,
+ '2': 3,
+ '3': 5,
+ },
+ wonderSidePickMethod: 'EACH_RANDOM',
+ pointsPer3Gold: 1,
+});
+export class Settings extends SettingsRecord {}
+
+const GameRecord = Record({
+ id: -1,
+ name: null,
+ players: new List(),
+ settings: new Settings(),
+ state: 'LOBBY',
+});
+export class Game extends GameRecord {}
+
+const GamesRecord = Record({
+ all: new Map(),
+ current: '',
+});
+export default class GamesState extends GamesRecord {
+ addGame(g) {
+ const game = new Game(g);
+ return this.mergeDeepIn(['all', game.id], game);
+ }
+ addGames(games) {
+ return this.mergeIn(['all'], games.map(game => new Game(game)));
+ }
+}
diff --git a/frontend/src/models/players.js b/frontend/src/models/players.js
new file mode 100644
index 00000000..3df32c57
--- /dev/null
+++ b/frontend/src/models/players.js
@@ -0,0 +1,26 @@
+import { Record, Map } from 'immutable';
+
+const PlayerRecord = Record({
+ username: null,
+ displayName: null,
+ index: 0,
+ ready: false,
+});
+export class Player extends PlayerRecord {}
+
+const PlayersRecord = Record({
+ all: new Map(),
+ current: '',
+});
+export default class PlayerState extends PlayersRecord {
+ addPlayer(p) {
+ const player = new Player(p);
+ const playerMap = new Map({ [player.username]: player });
+ return this.addPlayers(playerMap).set('current', player.username);
+ }
+
+ addPlayers(p) {
+ const players = new Map(p);
+ return this.mergeIn(['all'], players.map(player => new Player(player)));
+ }
+}
bgstack15