Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

Commit

Permalink
Fixed a bug in time calculation.
Browse files Browse the repository at this point in the history
Slightly fixed the shadow on the window.
Added a silly agent for testing.
Updated README.
  • Loading branch information
yahiaetman committed Dec 1, 2019
1 parent c21d5dd commit c082fbc
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 31 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ You can download a binary version for Windows from [Releases](https://github.com
### To run the server or an example:

- To run the server, run: `yarn start`.
- To play go-game only (offline), run: `yarn console-go`.
- To run the console client, run: `yarn console-client [name] [address=ws://localhost:8080]`.
- To run the console client, run: `yarn console:client [name] [address=ws://localhost:8080]`.

### Additional Options

- To the server on the console, run: `yarn console-server`.
- To run the server on the console, run: `yarn console:server`.
- To run a silly greedy agent, run: `yarn console:silly-agent`.
- To play go-game only (offline), run: `yarn console:go`.
- To package the server for distribution, run: `yarn dist`.
- To run tests, run: `yarn test`.
- To build the main and rendering threads in watch mode, run: `yarn watch:main` and `yarn watch:renderer` each in a separate terminal.

## Configuration

Expand Down
Binary file modified docs/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "go-server",
"version": "0.0.1",
"version": "0.0.2",
"description": "Go Server is a 2-Player Websockets Go Judging Server.",
"main": "public/main/main.js",
"repository": "https://github.com/yahiaetman/Go-Server.git",
Expand All @@ -24,9 +24,10 @@
"watch:main": "parcel watch src/main/main.ts -d public/main --target electron --no-hmr",
"watch:renderer": "parcel watch src/renderer/index.html -d public/renderer --public-url ./ --target electron --no-hmr",
"production": "parcel build src/main/main.ts -d public/main --public-url ./ --target electron --no-source-maps --no-minify && parcel build src/renderer/index.html -d public/renderer --public-url ./ --target electron --no-source-maps --no-minify",
"console-go": "ts-node ./src/examples/console-go.ts",
"console-server": "ts-node ./src/examples/console-server.ts",
"console-client": "ts-node ./src/examples/console-client.ts"
"console:go": "ts-node ./src/examples/console-go.ts",
"console:server": "ts-node ./src/examples/console-server.ts",
"console:client": "ts-node ./src/examples/console-client.ts",
"console:silly-agent": "cross-env NODE_OPTIONS=--experimental-worker ts-node ./src/examples/console-silly-agent.ts"
},
"jest": {
"preset": "ts-jest"
Expand All @@ -50,6 +51,8 @@
{
"filter": [
"!.vscode${/*}",
"!.cache${/*}",
"!.github${/*}",
"!src${/*}",
"!typings${/*}",
"!tests${/*}",
Expand Down Expand Up @@ -101,6 +104,7 @@
"@typescript-eslint/eslint-plugin": "^2.1.0",
"@typescript-eslint/parser": "^2.1.0",
"@vue/component-compiler-utils": "^3.0.0",
"cross-env": "^6.0.3",
"electron": "^6.0.7",
"electron-builder": "^21.2.0",
"eslint": "^6.3.0",
Expand Down
29 changes: 26 additions & 3 deletions src/common/Go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ export interface EndGameInfo {
export default class GoGame {
private configuration!: GameConfiguration;
private history!: HistoryEntry[];
private idleDeltaTime!: number;

constructor (configuration?: GameConfiguration){
this.Configuration = configuration || GoGame.DefaultConfiguration;
}

public set Configuration(configuration: GameConfiguration) {
this.configuration = configuration;
this.idleDeltaTime = configuration.idleDeltaTime;
this.history = [{
type: 'start',
state: this.configuration.initialState,
Expand All @@ -57,7 +59,7 @@ export default class GoGame {
public get Configuration(): GameConfiguration {
let config = _.cloneDeep(this.configuration);
config.moveLog = [];
config.idleDeltaTime = 0;
config.idleDeltaTime = this.idleDeltaTime;
let lastRemainingTime: number = 0;
for(let entry of this.history){
if(entry.type === "start"){
Expand Down Expand Up @@ -109,7 +111,9 @@ export default class GoGame {
}

public get CurrentState(): GameState {
return this.history[this.history.length-1].state;
let state = _.cloneDeep(this.history[this.history.length-1].state);
state.players[state.turn].remainingTime -= this.idleDeltaTime;
return state;
}

public get BoardSize(): number {
Expand Down Expand Up @@ -158,6 +162,7 @@ export default class GoGame {
public timeout() {
const nextState = _.cloneDeep(this.CurrentState);
nextState.players[nextState.turn].remainingTime = 0;
this.idleDeltaTime = 0;
this.history.push({
type: 'end',
state: nextState,
Expand All @@ -169,11 +174,16 @@ export default class GoGame {
});
}

public pause(idleDeltaTime: number){
this.idleDeltaTime += idleDeltaTime;
}

public apply(move: Move, deltaTime: number): {valid: boolean, state: GameState, message?: string} {
let state = this.CurrentState;
let nextState: GameState = _.cloneDeep(state);
nextState.players[nextState.turn].remainingTime -= deltaTime;
if(move.type === 'resign'){
this.idleDeltaTime = 0;
this.history.push({
type: 'move',
state: nextState,
Expand All @@ -190,6 +200,7 @@ export default class GoGame {
});
return {valid: true, state: nextState};
} else if(move.type === 'pass'){
this.idleDeltaTime = 0;
nextState.turn = ColorUtility.FlipColor(nextState.turn);
nextState.players[nextState.turn].prisoners++;
this.history.push({
Expand Down Expand Up @@ -249,13 +260,25 @@ export default class GoGame {
type: 'move',
state: nextState,
move: move
})
});
this.idleDeltaTime = 0;
return {valid: true, state: nextState};
} else {
return {valid: false, state: this.CurrentState, message: "Invalid type"};
}
}

public undo() {
let lastEntry = this.history[this.history.length - 1];
if(lastEntry.type == 'end'){
this.history.pop();
lastEntry = this.history[this.history.length - 1];
}
if(lastEntry.type != 'start'){
this.history.pop();
}
}

private Analyze(board: Board): {board: number[][], clusters: {color: Color, count: number, neighbors: {[index: string]:number}}[]}{
const boardSize: number = this.BoardSize;
let idCount: number = 0;
Expand Down
27 changes: 17 additions & 10 deletions src/common/game-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class GameManager {
this.configLayers.configFile = this.readConfig(GameManager.GameConfigPath);
}
if(this.state !== ManagerState.PLAYING && this.volatile) {
this.game.Configuration = this.Configuration;
this.game.Configuration = this.StartingConfiguration;
this.options?.reload?.();
}
});
Expand All @@ -63,12 +63,12 @@ export default class GameManager {
this.configLayers.checkpointFile = this.readConfig(GameManager.CheckpointPath);
}
if(this.state !== ManagerState.PLAYING && this.volatile) {
this.game.Configuration = this.Configuration;
this.game.Configuration = this.StartingConfiguration;
this.options?.reload?.();
}
});

this.game = new GoGame(this.Configuration);
this.game = new GoGame(this.StartingConfiguration);
this.state = ManagerState.READY;
this.volatile = true;
process.nextTick(()=>{this.options?.reload?.()});
Expand All @@ -94,7 +94,6 @@ export default class GameManager {
private saveCheckpoint() {
this.logger?.info?.(`Saving a checkpoint...`);
let config = this.game.Configuration;
config.idleDeltaTime = this.game.CurrentState.players[this.game.CurrentState.turn].remainingTime - this.timer.lap();
fs.writeFileSync(GameManager.CheckpointPath, JSON.stringify(config, null, 4));
}

Expand All @@ -106,7 +105,7 @@ export default class GameManager {
const discardedPath = path.join(path.dirname(GameManager.CheckpointPath), `discarded-${dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss')}.json`);
fs.renameSync(GameManager.CheckpointPath, discardedPath);
} else {
this.game.Configuration = this.Configuration;
this.game.Configuration = this.StartingConfiguration;
}
this.options.reload?.();
}
Expand All @@ -121,9 +120,17 @@ export default class GameManager {
}
}

public get Configuration() : GameConfiguration {
public get StartingConfiguration(): GameConfiguration {
if(this.configLayers.checkpointFile !== null) return this.configLayers.checkpointFile;
else if(this.configLayers.configFile !== null) return this.configLayers.configFile;
else return GoGame.DefaultConfiguration;
}

public get CurrentConfiguration() : GameConfiguration {
if(this.state == ManagerState.PLAYING){
return this.game.Configuration;
let config = this.game.Configuration;
config.idleDeltaTime = this.game.CurrentState.players[this.game.CurrentState.turn].remainingTime - this.timer.lap();
return config;
} else {
if(this.configLayers.checkpointFile !== null) return this.configLayers.checkpointFile;
else if(this.configLayers.configFile !== null) return this.configLayers.configFile;
Expand All @@ -148,7 +155,7 @@ export default class GameManager {
public start() {
if(this.state == ManagerState.PLAYING) return;

let config = this.Configuration;
let config = this.StartingConfiguration;
this.game.Configuration = config;
this.timer.start(this.game.CurrentState.players[this.game.CurrentState.turn].remainingTime - config.idleDeltaTime);
this.state = ManagerState.PLAYING;
Expand All @@ -158,6 +165,8 @@ export default class GameManager {
public stop() {
this.state = ManagerState.READY;
this.timer.pause();
let state = this.game.CurrentState
this.game.pause(state.players[state.turn].remainingTime - this.timer.lap());
this.saveCheckpoint();
}

Expand Down Expand Up @@ -202,8 +211,6 @@ export default class GameManager {
let state = _.cloneDeep(this.game.CurrentState);
if(this.state == ManagerState.PLAYING)
state.players[state.turn].remainingTime = this.timer.lap();
else
state.players[state.turn].remainingTime -= this.Configuration.idleDeltaTime;
return state;
}

Expand Down
31 changes: 26 additions & 5 deletions src/examples/console-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ rl.on("line", (input)=>{
let move: Move;
let args = input.toLowerCase().trim().split(/\s+/, 2);
switch(args[0] ?? ''){
case 'pass':
case 'p': case 'pass':
move = {type: 'pass'};
break;
case 'resign':
case 'r': case 'resign':
move = {type: 'resign'};
break;
case 'place':
case 'place':{
if(args.length < 2){
console.error("Please enter a point");
rl.prompt();
Expand All @@ -86,10 +86,31 @@ rl.on("line", (input)=>{
}
move = {type: 'place', point: point};
break;
default:
console.error("Invalid Command");
}
case 'h': case 'help':{
console.log("Commands:");
console.log(" - pass");
console.log(" - resign");
console.log(" - place <point>");
console.log(" - help");
console.log(" - exit");
rl.prompt();
return;
}
case 'exit': case 'quit':{
console.log('Goodbye!'); // Remember to say goodbye to the user before quitting :D
process.exit(0);
}
default:{
let point = PointUtility.Parse(args[1]);
if(point == null){
console.error("Invalid Command");
rl.prompt();
return;
}
move = {type: 'place', point: point};
break;
}
}
processEvent({ type: "INPUT", move: move });
}).on("close", ()=>{
Expand Down
6 changes: 6 additions & 0 deletions src/examples/console-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ rl.on('line', (input)=>{
console.log(" - start");
console.log(" - stop");
console.log(" - clear");
console.log(" - help");
console.log(" - exit");
rl.prompt();
break;
}
case 'exit': case 'quit':{
console.log('Goodbye!'); // Remember to say goodbye to the user before quitting :D
process.exit(0);
}
default:{
console.log(`Unknown command ${commands[0]}`);
rl.prompt();
Expand Down
Loading

0 comments on commit c082fbc

Please sign in to comment.