Skip to content
This repository was archived by the owner on Jan 12, 2023. It is now read-only.

Commit 6b393de

Browse files
committed
style: warnings fixed
1 parent 4c229ea commit 6b393de

File tree

10 files changed

+67
-66
lines changed

10 files changed

+67
-66
lines changed

ui/src/core/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const throwReturn = <T>(message: string): T => {
3333
export const shuffle = <T>(arr: T[]) => {
3434
const swap = (i: number, j: number) => {
3535
[arr[i], arr[j]] = [arr[j], arr[i]];
36-
}
36+
};
3737

3838
for (let i = arr.length - 1; i > 0; i--) {
3939
const j = Math.floor(Math.random() * (i + 1));

ui/src/logic/game/game.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Engine, World, Bodies, Vector, Composite, Body, Events } from "matter-js";
1+
import { Engine, World, Bodies, Vector, Body, Events } from "matter-js";
22
import { throwReturn } from "../../core/common";
33
import { GameAI } from "../gameAi/GameAi";
44
import { createSensorExecutor, SensorExecutor } from "./sensors";
@@ -41,15 +41,15 @@ export type GameInput = {
4141

4242

4343
export enum EGameStateObjectType {
44-
player, bullet, wall
44+
player, bullet, wall,
4545
}
4646

4747
export type GameStateObject = {
4848
/** engine body */
4949
body: Body,
5050
type: EGameStateObjectType,
5151
health: number,
52-
}
52+
};
5353

5454
export type GameStatePlayer = GameStateObject & {
5555
type: EGameStateObjectType.player,
@@ -176,11 +176,11 @@ export class Game {
176176
let vec = Vector.rotate(Vector.create(1, 0), body.angle);
177177
vec = Vector.mult(vec, 5);
178178
Body.setVelocity(body, vec);
179-
player.health -= .0005
179+
player.health -= .0005;
180180
}
181181
if (x.rotate !== 0) {
182182
Body.setAngularVelocity(body, x.rotate * .3);
183-
player.health -= Math.abs(x.rotate) * .005
183+
player.health -= Math.abs(x.rotate) * .005;
184184
}
185185
if (x.use) {
186186
this.use(player);
@@ -211,14 +211,14 @@ export class Game {
211211
} else if (alivePlayers.length === 0) {
212212
this.gameState.winner = -2;
213213
}
214-
};
214+
}
215215

216216
private processBullets() {
217217
const { gameState: { bullets } } = this;
218218
// todo: based on current step delta
219-
bullets.forEach(x => { x.health -= .01 });
219+
bullets.forEach(x => { x.health -= .01; });
220220

221-
bullets.map(x => {
221+
bullets.forEach(x => {
222222
const { body, health } = x;
223223
const { speed, velocity } = body;
224224

@@ -233,12 +233,14 @@ export class Game {
233233
const bulletSize = 5;
234234
const itemCooldown = 2000;
235235

236-
// todo: check ammo
236+
// todo: check ammo
237237
const { body: { position, angle }, item } = player;
238238
const { gameState: { bullets }, settings: { game: { playerSize } } } = this;
239239

240-
if (item.cooldown !== 0)
240+
if (item.cooldown !== 0){
241+
player.health -= .2;
241242
return;
243+
}
242244
item.cooldown = itemCooldown;
243245

244246
const distance = playerSize + bulletSize + 1;
@@ -257,9 +259,7 @@ export class Game {
257259
type: EGameStateObjectType.bullet,
258260
health: 1,
259261
body,
260-
})
261-
262-
player.health -= .1;
262+
});
263263
}
264264

265265
// todo: move all sensor related things into GameAI folder
@@ -294,17 +294,17 @@ export class Game {
294294
player.health -= damage;
295295
bullet.health -= damage;
296296
} else if (objects[1].type === EGameStateObjectType.bullet) {
297-
objects.forEach(x => x.health /= 2)
297+
objects.forEach(x => x.health /= 2);
298298
}
299299
}
300-
}
300+
};
301301

302302
pairs.forEach(processColisionPair);
303303
}
304304

305305
constructor(userSettings: Partial<GameSettings> = {}) {
306306
const settings = this.settings = mergeSettings(Game.SETTINGS_DEFAULT, userSettings);
307-
// create engine (as any for ignoring deprecated warning
307+
// create engine (as any for ignoring deprecated warning
308308
// - no other function to create engine known)
309309
this.engine = (Engine as any).create();
310310
const world = this.world;
@@ -345,7 +345,7 @@ export class Game {
345345
timeRemaining: settings.game.maxGameLength,
346346
};
347347

348-
Events.on(this.engine, "collisionStart", this.onCollision.bind(this))
348+
Events.on(this.engine, "collisionStart", this.onCollision.bind(this));
349349

350350
this.sensorExecutor = createSensorExecutor(this);
351351
}

ui/src/logic/game/sensors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Engine, Vector, Composite, Bodies, Body } from "matter-js";
1+
import { Vector, Body } from "matter-js";
22
import { raycast } from "../../utils/raycast";
33
import { Game } from "./game";
44

ui/src/logic/gameAi/GameAi.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IAProcessGenerationFunction, IASelectionFunctionType } from "../ai/ga/g
33
import { GeneticAlgorithmNeuralNet } from "../ai/gann/gann";
44
import { NeuralNet } from "../ai/nn/nn";
55
import { IANNActivationFunction } from "../ai/nn/nnActivationFunctions";
6-
import { Game, GameSettings } from "../game/game";
6+
import { GameSettings } from "../game/game";
77
import { GameAiEval } from "./GameAiEval";
88

99

@@ -27,7 +27,7 @@ export type GameAIInitParams = {
2727
/** sensor angles from center to both side (in radians) */
2828
sensors: number[]
2929
}
30-
}
30+
};
3131

3232
/**
3333
* class for training game bots
@@ -47,8 +47,8 @@ export class GameAI {
4747
selection: {
4848
type: IASelectionFunctionType.percent,
4949
value: 10,
50-
}
51-
}
50+
},
51+
},
5252
},
5353
nnInit: {
5454
hiddenLayers: [8, 8, 8],
@@ -57,7 +57,7 @@ export class GameAI {
5757
aiParams: {
5858
sensors: [Math.PI * 1 / 4, Math.PI * 1 / 8, Math.PI * 1 / 32],
5959
},
60-
}
60+
};
6161

6262
private onGameEnd: (() => void) | undefined = undefined;
6363

@@ -80,15 +80,15 @@ export class GameAI {
8080
queue.pop()!,
8181
queue.pop()
8282
// create fake player if second not present so it's dna score will not be affected
83-
?? { dna: dnas[randInt(dnas.length)], games: 0, wins: 0 }
83+
?? { dna: dnas[randInt(dnas.length)], games: 0, wins: 0 },
8484
];
8585
players.forEach(p => { p.games++; });
8686

8787
const evaler = new GameAiEval(players.map(x => x.dna), this.gameSettings);
8888
evaler.run();
8989
const winner = evaler.game.gameState.winner;
9090
if (winner >= 0) {
91-
players.forEach(x => x.wins--)
91+
players.forEach(x => x.wins--);
9292
players[winner].wins += 20;
9393
} else {
9494
players.forEach(x => x.wins -= 0.5);
@@ -125,7 +125,7 @@ export class GameAI {
125125
inputs,
126126
hiddens: ai.nnInit.hiddenLayers,
127127
outputs,
128-
}
128+
},
129129
},
130130
_environment: { _environmentBatch },
131131
});

ui/src/logic/gameAi/GameAiEval.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ const numFromType = (type: EGameStateObjectType | "none" | "unknown") => {
3333
console.warn("unknown type of sensor");
3434
return -1;
3535
}
36-
}
36+
};
3737

3838
/** distance of two vecotrs */
39-
const dist = (vec1: Vector, vec2: Vector) => Vector.magnitude(Vector.add(vec1, Vector.neg(vec2)))
39+
const dist = (vec1: Vector, vec2: Vector) => Vector.magnitude(Vector.add(vec1, Vector.neg(vec2)));
4040

4141
/**
4242
* class for evaling game with bots
@@ -61,7 +61,7 @@ export class GameAiEval {
6161
const outputs = GameAiEval.NN_OUTPUTS;
6262
const inputs = GameAiEval.NN_INPUTS_GET({ sensors });
6363
return NeuralNet.create({
64-
layerScheme: { hiddens, inputs, outputs }, afunction
64+
layerScheme: { hiddens, inputs, outputs }, afunction,
6565
});
6666
}
6767

@@ -86,13 +86,13 @@ export class GameAiEval {
8686
use: numbers[1] >= .5,
8787
walk: numbers[2] >= .5,
8888
switch: numbers[3],
89-
}
89+
};
9090
}
9191

9292
/** Calculates all turns until all done */
9393
public run() {
9494
while (!this.done) {
95-
this.next()
95+
this.next();
9696
}
9797
}
9898

ui/src/logic/gameAi/GameAiLiveTrain.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { GameAiEval } from "./GameAiEval";
77

88
export type GameAiLiveTrainConst = {
99
hiddens: number[],
10-
}
10+
};
1111

1212
export type Bot = {
1313
health: number,
@@ -28,7 +28,7 @@ export class GameAiLiveTrain {
2828
/** how many hp will player get after game when survives */
2929
healthGrowAfterGame: .2,
3030
sensors: [Math.PI * 1 / 4, Math.PI * 1 / 8, Math.PI * 1 / 32],
31-
}
31+
};
3232

3333
public bots: Bot[];
3434

@@ -45,7 +45,7 @@ export class GameAiLiveTrain {
4545

4646
/** calculate next ai steps. increment last of all bots -> select two bots and play with them -> create new bots from maxed up bots */
4747
public next() {
48-
const { healthGrowAfterGame, healthMultiplier, maxPop, sensors } = this.params;
48+
const { healthGrowAfterGame, healthMultiplier, maxPop } = this.params;
4949
const { bots } = this;
5050
bots.sort((a, b) => - a.games + b.games + (- a.wins + b.wins) * 1_000);
5151

@@ -94,10 +94,10 @@ export class GameAiLiveTrain {
9494

9595
bot.bonus += exceedingHealth * bonusMultiplier;
9696
}
97-
})
97+
});
9898

9999
if (resHealth.some(x => x <= 0))
100-
resHealth.forEach((x, i) => { if (x > 0) selected[i].wins++; })
100+
resHealth.forEach((x, i) => { if (x > 0) selected[i].wins++; });
101101

102102
selected.forEach(x => { x.lastGame = 0; x.games++; });
103103

@@ -126,17 +126,17 @@ export class GameAiLiveTrain {
126126

127127
console.log("new bot created");
128128
bot.bonus--;
129-
bots.push({ bonus: 0, games: 0, wins: 0, health: this.params.healthMultiplier, lastGame: 0, nn: bot.nn.mutate(.01) })
129+
bots.push({ bonus: 0, games: 0, wins: 0, health: this.params.healthMultiplier, lastGame: 0, nn: bot.nn.mutate(.01) });
130130
}
131131
}
132132

133133

134134
constructor(constructorParams: GameAiLiveTrainConst, paramsOveride: Partial<GameAiLiveTrain["params"]>) {
135135
const params = this.params = { ...this.params, ...paramsOveride };
136136
const { hiddens } = constructorParams;
137-
const { healthGrowAfterGame, healthMultiplier, maxPop, sensors } = params;
137+
const { sensors } = params;
138138

139-
this.bots = range(params.maxPop).map(x => ({
139+
this.bots = range(params.maxPop).map(() => ({
140140
bonus: 0,
141141
health: params.healthMultiplier,
142142
games: 0,

ui/src/views/MainPage.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { Card, Row, Col } from "antd";
22
import React, { useState } from "react";
3-
import { Analyze } from "./Analyze";
43
import { PlayPage } from "./Play";
5-
import { Settings, TSettingState } from "./Settings";
6-
import { History } from "./History";
74
import { RunAI } from "./RunAI";
8-
import { GameAI } from "../logic/gameAi/GameAi";
9-
import { GameAiEval } from "../logic/gameAi/GameAiEval";
105
import { NeuralNet } from "../logic/ai/nn/nn";
6+
// import { GameAI } from "../logic/gameAi/GameAi";
7+
// import { Analyze } from "./Analyze";
8+
// import { Settings, TSettingState } from "./Settings";
9+
// import { History } from "./History";
10+
// import { GameAiEval } from "../logic/gameAi/GameAiEval";
1111

12-
const { defaultInitParams } = GameAI;
12+
// const { defaultInitParams } = GameAI;
1313

1414
type TMainPageProps = {
1515

1616
};
1717

1818
export const MainPage: React.FC<TMainPageProps> = () => {
19-
const [aiSettings, setAiSettings] = useState<TSettingState>(defaultInitParams as any);
19+
// const [aiSettings, setAiSettings] = useState<TSettingState>(defaultInitParams as any);
2020
const [snapshot, setSnapshot] = useState<NeuralNet[] | undefined>(undefined);
2121

2222
return <>

ui/src/views/Play.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,32 @@ export const PlayPage: React.FC<TRunnerProps> = ({ capture, snapshot }) => {
2323
const ref = useRef<HTMLDivElement>(undefined as any);
2424
const [renderer, setRenderer] = useState<Render | undefined>(undefined);
2525
const setRunning = runningState[1];
26+
const running = runningState[0];
2627

2728
const canStart = !(runningState[0] || !snapshot);
2829

2930
const start = () => {
3031
if (!canStart) return;
3132
setRunning(true);
3233
setEvaler(new GameAiEval(snapshot!, Game.SETTINGS_DEFAULT));
33-
}
34+
};
3435

3536
const stop = () => {
3637
setRunning(false);
37-
}
38+
};
39+
40+
3841

3942
useEffect(() => {
40-
if (runningState[0]) return;
43+
if (running) return;
4144
if (renderer)
42-
Render.stop(renderer)
45+
Render.stop(renderer);
4346
setRenderer(undefined);
4447
if (ref.current) ref.current.innerHTML = "";
45-
}, [renderer, runningState[0]])
48+
}, [renderer, running]);
4649

4750
useEffect(() => {
48-
if (!ref.current || !evaler || !runningState[0]) return;
51+
if (!ref.current || !evaler || !running) return;
4952
// if (!ref.current) return;
5053
const element = ref.current;
5154

@@ -115,7 +118,7 @@ export const PlayPage: React.FC<TRunnerProps> = ({ capture, snapshot }) => {
115118
const rafLoop = () => {
116119
step();
117120
if (!game.isGameOver) {
118-
// if ([runningState[0]])
121+
if (running)
119122
requestAnimationFrame(rafLoop);
120123
} else {
121124

@@ -126,7 +129,7 @@ export const PlayPage: React.FC<TRunnerProps> = ({ capture, snapshot }) => {
126129
requestAnimationFrame(rafLoop);
127130

128131
Render.run(render);
129-
}, [evaler])
132+
}, [evaler, running]);
130133

131134
const button = runningState[0]
132135
? <Button onClick={stop}>Stop</Button>

0 commit comments

Comments
 (0)