Skip to content
This repository was archived by the owner on Sep 15, 2020. It is now read-only.

Commit 8abf2e0

Browse files
committed
chore(code): cleanup
Reformatted the code to be easier to read.
1 parent 5ad5d59 commit 8abf2e0

37 files changed

+710
-641
lines changed

.idea/codeStyles/Project.xml

+18-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api/Api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ export class Api {
99
private static viewer: Viewer
1010

1111
static getGameManager(): GameManager {
12-
if(!this.gameManager) {
12+
if (!this.gameManager) {
1313
this.gameManager = new GameManager()
1414
}
1515
return this.gameManager
1616
}
1717

1818

1919
static getViewer() {
20-
if(!this.viewer) {
20+
if (!this.viewer) {
2121
this.viewer = new Viewer()
2222
}
2323
return this.viewer

src/api/Logger.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import * as fs from 'fs'
1+
import * as fs from 'fs'
22
import * as path from 'path'
3-
import { log } from '../helpers/Utils'
3+
import { log } from '../helpers/Utils'
44

55
export class Logger {
66
private static logger: Logger
77

88
public static getLogger() {
9-
if(!this.logger) {
9+
if (!this.logger) {
1010
let logPath = process.env.SGC_LOG_PATH
11-
if(!fs.existsSync(logPath))
11+
if (!fs.existsSync(logPath)) {
1212
fs.mkdirSync(logPath)
13+
}
1314
const d = new Date()
1415
let logFile = path.join(logPath, `software-challenge-gui-${d.getFullYear()}.${d.getUTCMonth() + 1}.${d.getUTCDate()}.log`)
1516
log('logging to ' + logFile)
@@ -25,7 +26,7 @@ export class Logger {
2526
private static StringToColor = (str: string) => {
2627
const colours = 32
2728
let clr = 0
28-
for(let i = 0; i < str.length; i++) {
29+
for (let i = 0; i < str.length; i++) {
2930
clr += str.charCodeAt(i)
3031
clr %= colours
3132
}
@@ -36,18 +37,18 @@ export class Logger {
3637
this.logToHTML = logToHTML
3738
this.logFile = logFile
3839
this.logToConsole = logToConsole
39-
if(logToHTML) {
40+
if (logToHTML) {
4041
this.logFile = this.logFile + '.html'
4142
}
4243
try {
43-
if(!fs.existsSync(this.logFile)) {
44+
if (!fs.existsSync(this.logFile)) {
4445
this.createLogFile()
4546
} else {
46-
if(!fs.existsSync(this.logFile)) {
47+
if (!fs.existsSync(this.logFile)) {
4748
fs.writeFileSync(this.logFile, '')
4849
}
4950
}
50-
} catch(err) {
51+
} catch (err) {
5152
console.log('Could not create log file, falling back to console logging.')
5253
this.logToHTML = false
5354
this.logFile = null
@@ -56,11 +57,11 @@ export class Logger {
5657
}
5758

5859
private __log(timestamp: string, actor: string, focus: string, message: string) {
59-
if(this.logToConsole) {
60+
if (this.logToConsole) {
6061
console.log(`[${timestamp}] ${actor}:${focus}:\n${message}\n\n`)
6162
}
62-
if(this.logFile) {
63-
if(this.logToHTML) {
63+
if (this.logFile) {
64+
if (this.logToHTML) {
6465
fs.appendFile(this.logFile, `
6566
<div class="message" actor="${actor.replace(/\W/g, '')}" focus="${focus.replace(/\W/g, '')}">
6667
<div class="header" style="color: ${Logger.StringToColor(actor)};">
@@ -70,9 +71,9 @@ export class Logger {
7071
</div>
7172
<pre class="message-content">${(message + '').replace(/\</g, '&lt;').replace(/\>/g, '&gt;').trim()}</pre>
7273
</div>
73-
`, (err) => { if(err) { console.error(err) } })
74+
`, (err) => { if (err) { console.error(err) } })
7475
} else {
75-
fs.appendFile(this.logFile, message, (err) => { if(err) { console.error(err) } })
76+
fs.appendFile(this.logFile, message, (err) => { if (err) { console.error(err) } })
7677
}
7778
}
7879
}
@@ -96,15 +97,15 @@ export class Logger {
9697
}
9798

9899
public focus(actor: string, focus: string) {
99-
return {log: (message) => this.log(actor, focus, message)}
100+
return { log: (message) => this.log(actor, focus, message) }
100101
}
101102

102103
public getLogFilePath(): string {
103104
return this.logFile
104105
}
105106

106107
public getCompleteLog(): string {
107-
if(this.logFile) {
108+
if (this.logFile) {
108109
return fs.readFileSync(this.logFile).toString()
109110
} else {
110111
return ''

src/api/asynchronous/AsyncApi.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Server } from './Server'
2-
import { GameState, Move } from '../rules/CurrentGame'
1+
import { Server } from './Server'
2+
import { GameState, Move } from '../rules/CurrentGame'
33
import { AsyncGameManager } from './AsyncGameManager'
4-
import * as portfinder from 'portfinder'
4+
import * as portfinder from 'portfinder'
55

66
export class AsyncApi {
77
private static server: Promise<Server>
@@ -11,22 +11,22 @@ export class AsyncApi {
1111
private static nextKey: number = 0
1212

1313
public static getServer(): Promise<Server> {
14-
if(AsyncApi.server == null) {
15-
AsyncApi.server = portfinder.getPortPromise({port: 13050})
14+
if (AsyncApi.server == null) {
15+
AsyncApi.server = portfinder.getPortPromise({ port: 13050 })
1616
.then(port => new Server(port, true))
1717
}
1818
return AsyncApi.server
1919
}
2020

2121
public static getAsyncGameManager(port: number): AsyncGameManager {
22-
if(!this.asyncGameManager) {
22+
if (!this.asyncGameManager) {
2323
this.asyncGameManager = new AsyncGameManager(port)
2424
}
2525
return this.asyncGameManager
2626
}
2727

2828
public static hasMoveRequest(gameId: number): boolean {
29-
if(this.moveRequests.has(gameId)) {
29+
if (this.moveRequests.has(gameId)) {
3030
return this.moveRequests.has(gameId) && this.moveRequests.get(gameId).size > 0
3131
} else {
3232
return false
@@ -41,7 +41,7 @@ export class AsyncApi {
4141
console.log(`redeemMoveRequest for game ${gameId}, id ${id}, map: `, this.moveRequests.get(gameId))
4242
console.log('Move:', move)
4343
let request = this.moveRequests.get(gameId).get(id)
44-
if(!request) {
44+
if (!request) {
4545
console.log(`found no request for id ${id}, map was`, this.moveRequests.get(gameId))
4646
} else {
4747
request.callback(move)//Handle things on the client side
@@ -51,12 +51,12 @@ export class AsyncApi {
5151

5252
public static lodgeActionRequest(gameId: number, state: GameState, callback: (m: Move) => void) {
5353
console.log('new move request for game ' + gameId)
54-
if(!this.moveRequests.has(gameId)) {
54+
if (!this.moveRequests.has(gameId)) {
5555
this.moveRequests.set(gameId, new Map<number, MoveRequest>())
5656
}
5757

5858
this.moveRequests.get(gameId).set(this.nextKey, {
59-
state: state,
59+
state: state,
6060
callback: callback,
6161
})
6262
this.nextKey++

src/api/asynchronous/AsyncGameManager.ts

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { GameCreationOptions, GameType } from '../rules/GameCreationOptions'
2-
import { Game } from '../rules/Game'
3-
import { LiveGame } from './LiveGame'
4-
import { Replay } from './Replay'
5-
import { AsyncApi } from './AsyncApi'
6-
import { GameStatus } from '../rules/GameStatus'
7-
import { TransferableMoveRequest } from '../rules/TransferableMoveRequest'
8-
import { Logger } from '../Logger'
9-
import { GameState, GameResult, Piece } from '../rules/CurrentGame'
10-
import * as express from 'express'
11-
import * as bodyParser from 'body-parser'
12-
import { log, stringify } from '../../helpers/Utils'
13-
import { GameServerInfo } from '../synchronous/GameManagerWorkerInterface'
2+
import { Game } from '../rules/Game'
3+
import { LiveGame } from './LiveGame'
4+
import { Replay } from './Replay'
5+
import { AsyncApi } from './AsyncApi'
6+
import { GameStatus } from '../rules/GameStatus'
7+
import { TransferableMoveRequest } from '../rules/TransferableMoveRequest'
8+
import { Logger } from '../Logger'
9+
import { GameState, GameResult, Piece } from '../rules/CurrentGame'
10+
import * as express from 'express'
11+
import * as bodyParser from 'body-parser'
12+
import { log, stringify } from '../../helpers/Utils'
13+
import { GameServerInfo } from '../synchronous/GameManagerWorkerInterface'
1414

1515

1616
export class AsyncGameManager {
@@ -37,7 +37,7 @@ export class AsyncGameManager {
3737
this.closer.close()
3838
res.send('server stopped')
3939
})
40-
} catch(e) {
40+
} catch (e) {
4141
// TODO extract exception handling (DRY)
4242
res.status(500).send(e)
4343
}
@@ -46,7 +46,7 @@ export class AsyncGameManager {
4646
this.server.get('/list-games', (req, res) => {
4747
try {
4848
res.json(Array.from(this.games.keys()))
49-
} catch(e) {
49+
} catch (e) {
5050
res.status(500).send(e)
5151
}
5252
})
@@ -63,7 +63,7 @@ export class AsyncGameManager {
6363
log('Error while waiting for game to ready: ' + stringify(e))
6464
res.status(500).send(e)
6565
})
66-
} catch(e) {
66+
} catch (e) {
6767
res.status(500).send(e.toString())
6868
}
6969
})
@@ -72,10 +72,10 @@ export class AsyncGameManager {
7272
try {
7373
let options = req.body
7474
let g: Game = this.games.get(options.gameId)
75-
if(g instanceof LiveGame) {
75+
if (g instanceof LiveGame) {
7676
g.saveReplay(options.path)
7777
}
78-
} catch(e) {
78+
} catch (e) {
7979
res.status(500).send(e.toString())
8080
}
8181
})
@@ -84,25 +84,25 @@ export class AsyncGameManager {
8484
try {
8585
let gameId = parseInt(req.query.id)
8686
let game: Game = this.games.get(gameId)
87-
if(game instanceof LiveGame) {
87+
if (game instanceof LiveGame) {
8888
game.cancel()
8989
}
9090
this.games.delete(gameId)
91-
} catch(e) {
91+
} catch (e) {
9292
res.status(500).send(e.toString())
9393
}
9494
})
9595

9696
this.server.get('/status', (req, res) => {
9797
try {
9898
let gameId = parseInt(req.query.id)
99-
if(this.games.has(gameId)) {//check if this game even exists
99+
if (this.games.has(gameId)) {//check if this game even exists
100100
let game: any = this.games.get(gameId)//Fetch game, prepare answer
101101
res.send(this.report_status(game, gameId))
102102
} else {
103103
res.status(404).send('No game with id ' + gameId)
104104
}
105-
} catch(e) {
105+
} catch (e) {
106106
res.status(500).send(e)
107107
}
108108
})
@@ -111,14 +111,14 @@ export class AsyncGameManager {
111111
try {
112112
let gameId = parseInt(req.query.id)
113113
let turn = parseInt(req.query.turn)
114-
if(this.games.has(gameId)) {
114+
if (this.games.has(gameId)) {
115115
this.games.get(gameId).getState(turn).then(gs => {
116116
res.send(gs)
117117
})
118118
} else {
119119
res.status(404).send('No game with id ' + gameId)
120120
}
121-
} catch(e) {
121+
} catch (e) {
122122
res.status(500).send(e)
123123
}
124124
})
@@ -127,13 +127,13 @@ export class AsyncGameManager {
127127
try {
128128
let gameId = parseInt(req.query.id)
129129
let moveId = parseInt(req.query.moveId)
130-
if(this.games.has(gameId)) {
130+
if (this.games.has(gameId)) {
131131
AsyncApi.redeemMoveRequest(gameId, moveId, req.body)
132132
res.send(`Sent move with id ${moveId} for game ${gameId}`)
133133
} else {
134134
res.status(404).send('No game with id ' + gameId)
135135
}
136-
} catch(e) {
136+
} catch (e) {
137137
res.status(500).send(e)
138138
}
139139
})
@@ -143,8 +143,8 @@ export class AsyncGameManager {
143143
.then(server => {
144144
res.send({
145145
status: server.getStatus(),
146-
port: server.getPort(),
147-
error: server.stderr.join(),
146+
port: server.getPort(),
147+
error: server.stderr.join(),
148148
} as GameServerInfo)
149149
})
150150
.catch(e => res.status(500).send(e))
@@ -157,18 +157,18 @@ export class AsyncGameManager {
157157

158158
resp.numberOfStates = game.getStateCount()
159159

160-
if(game.isReplay) {//Game is a replay, all states should be loaded, report so
160+
if (game.isReplay) {//Game is a replay, all states should be loaded, report so
161161
resp.gameStatus = 'REPLAY'
162162
resp.gameResult = game.getResult()
163-
} else if(game instanceof LiveGame) { //Game is a live game and might or might not be finished, let's find out
163+
} else if (game instanceof LiveGame) { //Game is a live game and might or might not be finished, let's find out
164164
let lg: LiveGame = game
165-
if(lg.isLive()) {
166-
if(AsyncApi.hasMoveRequest(gameId)) {//If there's an action request currently lodged with the API
165+
if (lg.isLive()) {
166+
if (AsyncApi.hasMoveRequest(gameId)) {//If there's an action request currently lodged with the API
167167
resp.gameStatus = 'REQUIRES INPUT'
168168
let [id, mr] = AsyncApi.getMoveRequest(gameId)//Get the request and assemble the response. We can request this ActionRequest many times, but only redeem it once
169169
resp.moveRequest = {
170170
state: mr.state,
171-
id: id,
171+
id: id,
172172
}
173173
} else { //Game is live, but doesn't require input
174174
resp.gameStatus = 'RUNNING'

0 commit comments

Comments
 (0)