This commit is contained in:
Tobias Hopp 2024-04-02 01:41:51 +02:00
parent e63f351f63
commit 95d8d9a58f
20 changed files with 1006 additions and 577 deletions

5
.gitignore vendored
View File

@ -92,4 +92,7 @@ typings/
out/
# WebStorm
.idea/
.idea/
# Data Directory
data/

188
package-lock.json generated
View File

@ -14,10 +14,12 @@
"@fontsource/roboto": "^5.0.12",
"@mui/icons-material": "^5.15.14",
"@mui/material": "^5.15.14",
"@types/websocket": "^1.0.10",
"electron-squirrel-startup": "^1.0.0",
"node-wifi-scanner": "git+https://git.gaminggeneration.de/tobiash/node-wifi-scanner",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"websocket": "^1.0.34"
},
"devDependencies": {
"@electron-forge/cli": "^7.3.1",
@ -1953,7 +1955,6 @@
"version": "20.11.30",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.30.tgz",
"integrity": "sha512-dHM6ZxwlmuZaRmUPfv1p+KrdD1Dci04FbdEm/9wEMouFqxYoFl5aMkt0VMAUtYRQDyYvD41WJLukhq/ha3YuTw==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~5.26.4"
@ -2100,6 +2101,14 @@
"@types/node": "*"
}
},
"node_modules/@types/websocket": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/@types/websocket/-/websocket-1.0.10.tgz",
"integrity": "sha512-svjGZvPB7EzuYS94cI7a+qhwgGU1y89wUgjT6E2wVUfmAGIvRfT7obBvRtnhXCSsoMdlG4gBFGE7MfkIXZLoww==",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/ws": {
"version": "8.5.10",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz",
@ -3212,6 +3221,18 @@
"dev": true,
"license": "MIT"
},
"node_modules/bufferutil": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.8.tgz",
"integrity": "sha512-4T53u4PdgsXqKaIctwF8ifXlRTTmEPJ8iEPWFdGZvcf7sbwYo6FKFEX9eNNAnzFZ7EzJAQ3CJeOtCRA4rDp7Pw==",
"hasInstallScript": true,
"dependencies": {
"node-gyp-build": "^4.3.0"
},
"engines": {
"node": ">=6.14.2"
}
},
"node_modules/bytes": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
@ -3969,6 +3990,18 @@
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz",
"integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="
},
"node_modules/d": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/d/-/d-1.0.2.tgz",
"integrity": "sha512-MOqHvMWF9/9MX6nza0KgvFH4HpMU0EF5uUDXqX/BtxtU8NfB0QzRtJ8Oe/6SuS4kbhyzVJwjd97EA4PKrzJ8bw==",
"dependencies": {
"es5-ext": "^0.10.64",
"type": "^2.7.2"
},
"engines": {
"node": ">=0.12"
}
},
"node_modules/data-view-buffer": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/data-view-buffer/-/data-view-buffer-1.0.1.tgz",
@ -4873,6 +4906,21 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/es5-ext": {
"version": "0.10.64",
"resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.64.tgz",
"integrity": "sha512-p2snDhiLaXe6dahss1LddxqEm+SkuDvV8dnIQG0MWjyHpcMNfXKPE+/Cc0y+PhxJX3A4xGNeFCj5oc0BUh6deg==",
"hasInstallScript": true,
"dependencies": {
"es6-iterator": "^2.0.3",
"es6-symbol": "^3.1.3",
"esniff": "^2.0.1",
"next-tick": "^1.1.0"
},
"engines": {
"node": ">=0.10"
}
},
"node_modules/es6-error": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/es6-error/-/es6-error-4.1.1.tgz",
@ -4881,6 +4929,28 @@
"license": "MIT",
"optional": true
},
"node_modules/es6-iterator": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz",
"integrity": "sha512-zw4SRzoUkd+cl+ZoE15A9o1oQd920Bb0iOJMQkQhl3jNc03YqVjAhG7scf9C5KWRU/R13Orf588uCC6525o02g==",
"dependencies": {
"d": "1",
"es5-ext": "^0.10.35",
"es6-symbol": "^3.1.1"
}
},
"node_modules/es6-symbol": {
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.4.tgz",
"integrity": "sha512-U9bFFjX8tFiATgtkJ1zg25+KviIXpgRvRHS8sau3GfhVzThRQrOeksPeT0BWW2MNZs1OEWJ1DPXOQMn0KKRkvg==",
"dependencies": {
"d": "^1.0.2",
"ext": "^1.7.0"
},
"engines": {
"node": ">=0.12"
}
},
"node_modules/escalade": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.2.tgz",
@ -5158,6 +5228,20 @@
"node": ">=10.13.0"
}
},
"node_modules/esniff": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/esniff/-/esniff-2.0.1.tgz",
"integrity": "sha512-kTUIGKQ/mDPFoJ0oVfcmyJn4iBDRptjNVIzwIFR7tqWXdVI9xfA2RMwY/gbSpJG3lkdWNEjLap/NqVHZiJsdfg==",
"dependencies": {
"d": "^1.0.1",
"es5-ext": "^0.10.62",
"event-emitter": "^0.3.5",
"type": "^2.7.2"
},
"engines": {
"node": ">=0.10"
}
},
"node_modules/espree": {
"version": "9.6.1",
"resolved": "https://registry.npmjs.org/espree/-/espree-9.6.1.tgz",
@ -5232,6 +5316,15 @@
"node": ">= 0.6"
}
},
"node_modules/event-emitter": {
"version": "0.3.5",
"resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz",
"integrity": "sha512-D9rRn9y7kLPnJ+hMq7S/nhvoKwwvVJahBi2BPmx3bvbsEdK3W9ii8cBSGjP+72/LnM4n6fo3+dkCX5FeTQruXA==",
"dependencies": {
"d": "1",
"es5-ext": "~0.10.14"
}
},
"node_modules/eventemitter3": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz",
@ -5462,6 +5555,14 @@
"dev": true,
"license": "MIT"
},
"node_modules/ext": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/ext/-/ext-1.7.0.tgz",
"integrity": "sha512-6hxeJYaL110a9b5TEJSj0gojyHQAmA2ch5Os+ySCiA1QGdS697XWY1pzsrSjqA9LDEEgdB/KypIlR59RcLuHYw==",
"dependencies": {
"type": "^2.7.2"
}
},
"node_modules/extract-zip": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/extract-zip/-/extract-zip-2.0.1.tgz",
@ -7200,6 +7301,11 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/is-typedarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
"integrity": "sha512-cyA56iCMHAh5CdzjJIa4aohJyeO1YbwLi3Jc35MmRU6poroFjIGZzUzupGiRPOjgHg9TLu43xbpwXk523fMxKA=="
},
"node_modules/is-unicode-supported": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz",
@ -8180,6 +8286,11 @@
"dev": true,
"license": "MIT"
},
"node_modules/next-tick": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.1.0.tgz",
"integrity": "sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ=="
},
"node_modules/nice-try": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz",
@ -8285,6 +8396,16 @@
"node": "^12.13 || ^14.13 || >=16"
}
},
"node_modules/node-gyp-build": {
"version": "4.8.0",
"resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.0.tgz",
"integrity": "sha512-u6fs2AEUljNho3EYTJNBfImO5QTo/J/1Etd+NVdCj7qWKUSN/bSLkZwhDv7I+w/MSC6qJ4cknepkAYykDdK8og==",
"bin": {
"node-gyp-build": "bin.js",
"node-gyp-build-optional": "optional.js",
"node-gyp-build-test": "build-test.js"
}
},
"node_modules/node-loader": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/node-loader/-/node-loader-2.0.0.tgz",
@ -11069,6 +11190,11 @@
"dev": true,
"license": "0BSD"
},
"node_modules/type": {
"version": "2.7.2",
"resolved": "https://registry.npmjs.org/type/-/type-2.7.2.tgz",
"integrity": "sha512-dzlvlNlt6AXU7EBSfpAscydQ7gXB+pPGsPnfJnZpiNJBDj7IaJzQlBZYGdEi4R9HmPdBv2XmWJ6YUtoTa7lmCw=="
},
"node_modules/type-check": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",
@ -11186,6 +11312,14 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/typedarray-to-buffer": {
"version": "3.1.5",
"resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz",
"integrity": "sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==",
"dependencies": {
"is-typedarray": "^1.0.0"
}
},
"node_modules/typescript": {
"version": "4.5.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz",
@ -11220,7 +11354,6 @@
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==",
"dev": true,
"license": "MIT"
},
"node_modules/unique-filename": {
@ -11324,6 +11457,18 @@
"node": ">=8"
}
},
"node_modules/utf-8-validate": {
"version": "5.0.10",
"resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-5.0.10.tgz",
"integrity": "sha512-Z6czzLq4u8fPOyx7TU6X3dvUZVvoJmxSQ+IcrlmagKhilxlhZgxPK6C5Jqbkw1IDUmFTM+cz9QDnnLTwDz/2gQ==",
"hasInstallScript": true,
"dependencies": {
"node-gyp-build": "^4.3.0"
},
"engines": {
"node": ">=6.14.2"
}
},
"node_modules/util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
@ -11734,6 +11879,22 @@
"node": ">=10.13.0"
}
},
"node_modules/websocket": {
"version": "1.0.34",
"resolved": "https://registry.npmjs.org/websocket/-/websocket-1.0.34.tgz",
"integrity": "sha512-PRDso2sGwF6kM75QykIesBijKSVceR6jL2G8NGYyq2XrItNC2P5/qL5XeR056GhA+Ly7JMFvJb9I312mJfmqnQ==",
"dependencies": {
"bufferutil": "^4.0.1",
"debug": "^2.2.0",
"es5-ext": "^0.10.50",
"typedarray-to-buffer": "^3.1.5",
"utf-8-validate": "^5.0.2",
"yaeti": "^0.0.6"
},
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/websocket-driver": {
"version": "0.7.4",
"resolved": "https://registry.npmjs.org/websocket-driver/-/websocket-driver-0.7.4.tgz",
@ -11759,6 +11920,19 @@
"node": ">=0.8.0"
}
},
"node_modules/websocket/node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/websocket/node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
@ -11934,6 +12108,14 @@
"node": ">=10"
}
},
"node_modules/yaeti": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/yaeti/-/yaeti-0.0.6.tgz",
"integrity": "sha512-MvQa//+KcZCUkBTIC9blM+CU9J2GzuTytsOUwf2lidtvkx/6gnEp1QvJv34t9vdjhFmha/mUiNDbN0D0mJWdug==",
"engines": {
"node": ">=0.10.32"
}
},
"node_modules/yallist": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",

View File

@ -1,21 +1,36 @@
import {client as WebSocketClient, connection} from "websocket";
import {w3cwebsocket, IMessageEvent} from "websocket";
export default class CloudHandler {
private static websocket = new WebSocketClient();
private static connection: connection;
private static connection: w3cwebsocket;
static connect() {
return new Promise<void>((resolve, reject) => {
this.websocket.connect("ws://smartmonopoly.iif.li");
this.websocket.on("connectFailed", (err) => {
this.connection = new w3cwebsocket('wss://smartmonopoly.iif.li');
this.connection.onerror = (err: Error) => {
this.connection = null;
reject(err);
});
this.websocket.on("connect", (connection) => {
this.connection = connection;
};
this.connection.onopen = () => {
resolve();
});
};
this.connection.onclose = () => {
this.connection = null;
}
this.connection.onmessage = (e) => this.handleMessage(e);
});
}
private static handleMessage(e: IMessageEvent)
{
}
static isConnected(): boolean {
return !!this.connection;
}
}

58
src/ConfigHandler.ts Normal file
View File

@ -0,0 +1,58 @@
import path from "path";
import process from "process";
import * as fs from "fs";
import {Config, GameRules} from "./RawConstants";
export class ConfigHandler {
private static defaultConfig = {
rules: {
switchValues: {},
inputValues: {}
}
}
private static confPath = path.resolve(process.cwd(), "data", "config.json");
static generateDefault() {
fs.writeFileSync(this.confPath, JSON.stringify(this.defaultConfig, null, 1));
}
static get() {
if(!fs.existsSync(this.confPath))
this.generateDefault();
let confRaw = fs.readFileSync(this.confPath).toString('utf8');
try {
JSON.parse(confRaw);
}catch(e)
{
this.generateDefault();
}
console.log("Config retrieved!")
return JSON.parse(confRaw) as Config;
}
static set(conf: Config) {
if(!fs.existsSync(this.confPath))
this.generateDefault();
let confRaw = fs.readFileSync(this.confPath).toString('utf8');
try {
JSON.parse(confRaw);
}catch(e)
{
this.generateDefault();
}
let current = JSON.parse(confRaw) as Config;
current = {...current, ...conf};
console.log("Config saved!")
fs.writeFileSync(this.confPath, JSON.stringify(current, null, 1));
}
}

View File

@ -1,34 +0,0 @@
export type IPCChannel =
'WIFI_STATUS'
| 'WIFI_SCAN'
| 'WIFI_LIST'
| 'WIFI_CONNECT'
| 'FUNCTION_TEST'
| 'CLOUD_STATUS'
| 'CLOUD_CONNECT'
export interface IPCAnswer {
status: boolean,
data?: any
}
export interface IPCRequest {
data?: any,
}
export interface WiFiNetwork {
ssid: string,
isSecured: boolean,
isKnown?: boolean,
psk?: string,
rssi?: number,
}
export interface FunctionTest {
hasSudo: boolean,
hasWPASupplicant: boolean,
hasInternet: boolean
}

View File

@ -1,6 +1,6 @@
import { ipcMain } from 'electron';
import IpcMainInvokeEvent = Electron.IpcMainInvokeEvent;
import {IPCAnswer, IPCChannel, IPCRequest} from "./IPCConstants";
import {IPCAnswer, IPCChannel, IPCRequest} from "./RawConstants";
export const IPCHandler = (

View File

@ -1,4 +1,4 @@
import {WiFiNetwork} from "./IPCConstants";
import {WiFiNetwork} from "./RawConstants";
import {spawn, exec} from 'node:child_process';
import * as dns from "dns";
import * as process from "process";

78
src/RawConstants.ts Normal file
View File

@ -0,0 +1,78 @@
export type IPCChannel =
'WIFI_SCAN'
| 'WIFI_CONNECT'
| 'FUNCTION_TEST'
| 'CLOUD_STATUS'
| 'CLOUD_CONNECT'
| 'SETTINGS'
| 'PREPARING'
export enum IPCListenChannels {
'KEYPAD_INPUT'
}
export interface IPCAnswer {
status: boolean,
data?: any
}
export interface IPCRequest {
data?: any,
}
export interface WiFiNetwork {
ssid: string,
isSecured: boolean,
isKnown?: boolean,
psk?: string,
rssi?: number,
}
export interface FunctionTest {
hasSudo: boolean,
hasWPASupplicant: boolean,
hasInternet: boolean
}
export type GameSwitch = {
name: GameSwitchNames,
label: string,
depends?: GameSwitchNames,
value: boolean
}
export type GameInput = {
name: GameInputNames,
type: "number" | "string",
label: string,
depends?: GameSwitchNames,
value: string
}
export type GameSwitchNames =
"GET_BONUS_PASSING_START"
| "DOUBLE_BONUS_ON_GO"
| "PUT_PAID_PENALTIES_IN_MIDDLE"
| "RECEIVE_PENALTIES_AT_FREE_PARKING"
| "PUT_10_IN_MIDDLE_AFTER_EACH_GO"
| "STREET_MUST_BE_AUCTIONED_IF_NOT_PURCHASED"
| "TWO_HOTELS_PER_STREET_ALLOWED"
| "MORTGAGE_REDUCES_RENT"
| "GOODS_GO_TO_OTHER_PLAYERS_UPON_BANKRUPTCY"
| "STREETS_CAN_BE_SOLD_BACK_TO_THE_BANK"
| "PRISON_CAN_BE_PURCHASED_RENT_GOES_TO_PLAYER"
| "KEEP_MONEY_MORE_SECRET";
export type GameInputNames =
"PASSING_GO_CASH"
| "STARTING_CASH"
| "PRISON_RELEASE_FEE";
export interface GameRules {
switchValues: { [key in GameSwitchNames]?: boolean },
inputValues: { [key in GameInputNames]?: string }
}
export interface Config {
rules?: GameRules
}

View File

@ -1,7 +1,8 @@
import {IPCHandler} from "./IPCHandler";
import {FunctionTest, WiFiNetwork} from "./IPCConstants";
import {FunctionTest, GameRules} from "./RawConstants";
import OSHandler from "./OSHandler";
import CloudHandler from "./CloudHandler";
import {ConfigHandler} from "./ConfigHandler";
const wifiScan = require("node-wifi-scanner");
@ -40,7 +41,7 @@ export default class SmartMonopoly {
}
});
IPCHandler("WIFI_SCAN", async (e, request, args) => {
IPCHandler("WIFI_SCAN", async (e, request) => {
try {
let networks = await OSHandler.scanWifis();
return {status: true, data: networks};
@ -49,13 +50,39 @@ export default class SmartMonopoly {
}
});
IPCHandler("CLOUD_CONNECT", async (e, request, args) => {
IPCHandler("CLOUD_CONNECT", async (e, request) => {
try {
await CloudHandler.connect();
return {status: true}
} catch (e) {
return {status: false, data: e};
}
});
IPCHandler("SETTINGS", async(e, request) => {
if(request.data && request.data["rules"])
{
let rules = request.data.rules as GameRules;
ConfigHandler.set({rules: rules});
}
return {status: true, data: ConfigHandler.get()};
});
IPCHandler("PREPARING", async(e, request) => {
const useCloud = !!request.data.useCloud;
console.log("Preparing - useCloud: " + useCloud);
if(useCloud && !CloudHandler.isConnected())
try {
await CloudHandler.connect();
}catch(e)
{
return {status: false, data: 'Cloud connection issue: ' + e};
}
//todo when using cloud, get session etc.
return {status: true};
})
}
}

View File

@ -2,7 +2,7 @@
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
import {ipcRenderer, contextBridge} from 'electron';
import {IPCAnswer, IPCChannel, IPCRequest} from "./IPCConstants";
import {IPCAnswer, IPCChannel, IPCRequest} from "./RawConstants";
import IpcRendererEvent = Electron.IpcRendererEvent;
contextBridge.exposeInMainWorld('api', {

View File

@ -2,7 +2,6 @@ import {createRoot} from 'react-dom/client';
import React, {Component} from "react";
import Startup from "./Startup";
import WiFi from "./WiFi";
import Cloud from "./Cloud";
import Game from "./Game";
import GameSetup from "./GameSetup";
import GameEnd from "./GameEnd";
@ -10,58 +9,8 @@ import Setup from "./Setup";
const root = createRoot(document.getElementById("root"));
//window.app = window.MyNamespace || {};
interface HeadingSwitcherState {
counter: number;
}
class HeadingSwitcher extends Component<{}, HeadingSwitcherState> {
private interval: NodeJS.Timeout;
constructor(props: {}) {
super(props);
this.state = {counter: 0};
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), 500);
}
componentWillUnmount() {
clearInterval(this.interval);
}
tick() {
if (this.state.counter > 5)
this.setState({counter: 1});
else
this.setState({counter: this.state.counter + 1});
}
render() {
switch (this.state.counter) {
case 1:
return <h1>H1</h1>;
case 2:
return <h2>H2</h2>;
case 3:
return <h3>H3</h3>;
case 4:
return <h4>H4</h4>;
case 5:
return <h5>H5</h5>;
case 6:
return <h6>H6</h6>;
}
}
}
enum PAGE {
export enum PAGE {
STARTUP,
SETUP,
WIFI,
@ -83,7 +32,7 @@ export class App extends Component<{}, AppState> {
constructor(props: {}) {
super(props);
this.state = {
currentPage: PAGE.SETUP,
currentPage: PAGE.GAME_SETUP,
showWiFi: false,
}
}
@ -102,6 +51,13 @@ export class App extends Component<{}, AppState> {
}));
}
public setPage = (page: PAGE) => {
this.setState((prevState) => ({
...prevState,
currentPage: page
}));
}
render() {
@ -109,7 +65,7 @@ export class App extends Component<{}, AppState> {
<div className="app">
{this.state.showWiFi ? <WiFi/> : null}
{this.state.currentPage == PAGE.STARTUP ? <Startup ref={(ref) => {
{this.state.currentPage == PAGE.STARTUP || this.state.currentPage == PAGE.SETUP ? <Startup visible={this.state.currentPage == PAGE.STARTUP} ref={(ref) => {
App.PAGES.set(PAGE.STARTUP, ref);
}}/> : null}
@ -117,14 +73,6 @@ export class App extends Component<{}, AppState> {
App.PAGES.set(PAGE.SETUP, ref);
}}/> : null}
{this.state.currentPage == PAGE.WIFI ? <WiFi ref={(ref) => {
App.PAGES.set(PAGE.WIFI, ref);
}}/> : null}
{this.state.currentPage == PAGE.CLOUD ? <Cloud ref={(ref) => {
App.PAGES.set(PAGE.CLOUD, ref);
}}/> : null}
{this.state.currentPage == PAGE.GAME_SETUP ? <GameSetup ref={(ref) => {
App.PAGES.set(PAGE.GAME_SETUP, ref);
}}/> : null}

View File

@ -1,25 +0,0 @@
import {Component} from "react";
interface CloudState {
}
export default class Cloud extends Component<{}, CloudState> {
constructor(props: {}) {
super(props);
}
componentDidMount() {
}
componentWillUnmount() {
}
render() {
return <p>Test</p>
}
}

47
src/web/GameHandler.ts Normal file
View File

@ -0,0 +1,47 @@
import {PAGE} from "./App";
export enum GAME_STATE {
NOT_STARTED,
PREPARING,
PLAYING,
END
}
export default class GameHandler {
static GAMESTATE: GAME_STATE = GAME_STATE.NOT_STARTED;
static setGameState(state: GAME_STATE)
{
switch (state)
{
case GAME_STATE.NOT_STARTED:
window.app.setPage(PAGE.STARTUP);
break;
case GAME_STATE.PREPARING :
window.app.setPage(PAGE.GAME_SETUP);
break;
}
this.GAMESTATE = state;
}
static async requestPreparing(useCloud: boolean)
{
let response = await window.api.request("PREPARING", {
data:
{
useCloud: useCloud,
}
});
if(!response.status)
{
this.setGameState(GAME_STATE.NOT_STARTED);
throw new Error(response.data);
}
this.setGameState(GAME_STATE.PREPARING);
return true;
}
}

25
src/web/IPCListener.ts Normal file
View File

@ -0,0 +1,25 @@
import {IPCAnswer, IPCListenChannels} from "../RawConstants";
type IPCListen = {
channel: IPCListenChannels,
fn: (message: IPCAnswer, ...args: any) => void,
}
export default class IPCListener {
private static attachments: Map<number, IPCListen> = new Map<number, IPCListen>();
public static attach()
{
for(let c of Object.keys(IPCListenChannels))
{
window.api.listen((c as unknown as IPCListenChannels), (event, message: IPCAnswer, ...args: any) => {
for(let [a, listener] of this.attachments)
{
listener.fn(message, ...args);
}
});
}
}
}

View File

@ -1,117 +1,137 @@
import React, {Component} from "react";
import {FormControl, FormControlLabel, FormGroup, FormLabel, Grid, Switch} from "@mui/material";
type GameSwitch = {
name: GameSwitchNames,
label: string,
depends?: GameSwitchNames,
value: boolean
}
type GameSwitchNames =
"GET_BONUS_PASSING_START"
| "DOUBLE_BONUS_ON_GO"
| "PUT_PAID_PENALTIES_IN_MIDDLE"
| "RECEIVE_PENALTIES_AT_FREE_PARKING"
| "PUT_10_IN_MIDDLE_AFTER_EACH_GO"
| "STREET_MUST_BE_AUCTIONED_IF_NOT_PURCHASED"
| "TWO_HOTELS_PER_STREET_ALLOWED"
| "NO_MORTGAGE_ON_STREETS_WITH_BUILDINGS"
| "MORTGAGE_REDUCES_RENT"
| "GOODS_GO_TO_OTHER_PLAYERS_UPON_BANKRUPTCY"
| "STREETS_CAN_BE_SOLD_BACK_TO_THE_BANK"
| "PRISON_CAN_BE_PURCHASED_RENT_GOES_TO_PLAYER";
type GameInputs = {
"PASSING_GO_CASH": string,
"STARTING_CASH": string,
"PRISON_RELEASE_FEE": string
}
import {
Button,
FormControl,
FormControlLabel,
FormGroup,
FormLabel,
Grid, Snackbar,
Switch,
TextField,
Typography
} from "@mui/material";
import RestartAltIcon from '@mui/icons-material/RestartAlt';
import CloseIcon from '@mui/icons-material/Close';
import SaveIcon from '@mui/icons-material/Save';
import {PAGE} from "./App";
import {GameInput, GameSwitch, GameRules, GameSwitchNames, GameInputNames, Config} from "../RawConstants";
interface InitialSetupState {
open: boolean,
switchValues: GameSwitch[],
inputValues: GameInputs
inputValues: GameInput[],
savedMsg: boolean
}
const defaultConf: InitialSetupState = {
open: true,
savedMsg: false,
switchValues: [
{
name: "GET_BONUS_PASSING_START",
label: "Über Los Bonus einziehen",
value: true
},
{
name: "DOUBLE_BONUS_ON_GO",
label: "Wenn auf Los, doppelten Bonus einziehen",
depends: "GET_BONUS_PASSING_START",
value: true
},
{
name: "PUT_PAID_PENALTIES_IN_MIDDLE",
label: "Gezahlte Strafen in die 'Mitte' legen",
value: true
},
{
name: "RECEIVE_PENALTIES_AT_FREE_PARKING",
label: "Bei 'Frei Parken' Strafen aus der 'Mitte' einziehen",
depends: "PUT_PAID_PENALTIES_IN_MIDDLE",
value: true
},
{
name: "PUT_10_IN_MIDDLE_AFTER_EACH_GO",
label: "Nach jedem Los 10 in die Mitte",
depends: "PUT_PAID_PENALTIES_IN_MIDDLE",
value: false
},
{
name: "STREET_MUST_BE_AUCTIONED_IF_NOT_PURCHASED",
label: "Straße muss versteigert werden bei Nichtkauf",
value: true
},
{
name: "TWO_HOTELS_PER_STREET_ALLOWED",
label: "Zwei Hotels pro Straße erlaubt",
value: false
},
{
name: "MORTGAGE_REDUCES_RENT",
label: "Hypothek verringert Miete",
value: false
},
{
name: "GOODS_GO_TO_OTHER_PLAYERS_UPON_BANKRUPTCY",
label: "Güter gehen an anderen Spieler über bei Bankrott",
value: true
},
{
name: "STREETS_CAN_BE_SOLD_BACK_TO_THE_BANK",
label: "Straßen können zurück an die Bank verkauft werden",
value: true
},
{
name: "PRISON_CAN_BE_PURCHASED_RENT_GOES_TO_PLAYER",
label: "Gefängnis kann erworben werden, Miete geht an den Spieler",
value: false
},
{
name: "KEEP_MONEY_MORE_SECRET",
label: "Geld von anderen Spielern ungenauer anzeigen",
value: false
}
],
inputValues: [
{
name: "STARTING_CASH",
type: "number",
label: "Start Guthaben",
value: "1500"
},
{
name: "PASSING_GO_CASH",
type: "number",
label: "Lohn über Los",
value: "200"
},
{
name: "PRISON_RELEASE_FEE",
type: "number",
label: "Gefängnis Gebühren",
value: "50"
}
]
}
export default class Setup extends Component<{}, InitialSetupState> {
constructor(props: {}) {
super(props);
this.state = {
open: true,
switchValues: [
{
name: "GET_BONUS_PASSING_START",
label: "Über Los Bonus einziehen",
value: true
},
{
name: "DOUBLE_BONUS_ON_GO",
label: "Wenn auf Los, doppelten Bonus einziehen",
depends: "GET_BONUS_PASSING_START",
value: true
},
{
name: "PUT_PAID_PENALTIES_IN_MIDDLE",
label: "Gezahlte Strafen in die 'Mitte' legen",
value: true
},
{
name: "RECEIVE_PENALTIES_AT_FREE_PARKING",
label: "Bei 'Frei Parken' erhaltene Strafen",
depends: "PUT_PAID_PENALTIES_IN_MIDDLE",
value: true
},
{
name: "PUT_10_IN_MIDDLE_AFTER_EACH_GO",
label: "Nach jedem Los 10 in die Mitte",
depends: "PUT_PAID_PENALTIES_IN_MIDDLE",
value: false
},
{
name: "STREET_MUST_BE_AUCTIONED_IF_NOT_PURCHASED",
label: "Straße muss versteigert werden bei Nichtkauf",
value: true
},
{
name: "TWO_HOTELS_PER_STREET_ALLOWED",
label: "Zwei Hotels pro Straße erlaubt",
value: false
},
{
name: "NO_MORTGAGE_ON_STREETS_WITH_BUILDINGS",
label: "Keine Hypothek auf Straßen mit Bauten",
value: true
},
{
name: "MORTGAGE_REDUCES_RENT",
label: "Hypothek verringert Miete",
value: false
},
{
name: "GOODS_GO_TO_OTHER_PLAYERS_UPON_BANKRUPTCY",
label: "Güter gehen an anderen Spieler über bei Bankrott",
value: true
},
{
name: "STREETS_CAN_BE_SOLD_BACK_TO_THE_BANK",
label: "Straßen können zurück an die Bank verkauft werden",
value: true
},
{
name: "PRISON_CAN_BE_PURCHASED_RENT_GOES_TO_PLAYER",
label: "Gefängnis kann erworben werden, Miete geht an den Spieler",
value: false
}
],
inputValues: {
"PASSING_GO_CASH": "200",
"STARTING_CASH": "1500",
"PRISON_RELEASE_FEE": "50"
}
...defaultConf,
}
}
reset = () => {
this.setState(prevState => ({
...prevState,
...defaultConf
}))
//window.api.request("")
}
style = {
position: 'absolute',
top: '50%',
@ -125,7 +145,7 @@ export default class Setup extends Component<{}, InitialSetupState> {
};
onSwitch = (event: React.ChangeEvent<HTMLInputElement>) => {
const { name, checked } = event.target;
const {name, checked} = event.target;
this.setState((prevState) => ({
...prevState,
@ -141,8 +161,94 @@ export default class Setup extends Component<{}, InitialSetupState> {
}));
}
componentDidMount() {
onInput = (event: React.ChangeEvent<HTMLInputElement>) => {
const {name, value} = event.target;
this.setState((prevState) => ({
...prevState,
inputValues: prevState.inputValues.map((switchObj) => {
if (switchObj.name === name) {
return {
...switchObj,
value: value
};
}
return switchObj;
})
}));
}
getSettings = () => {
return new Promise<void>((resolve, reject) => {
window.api.request("SETTINGS", {}).then(answer => {
try {
let data = answer.data as Config;
for (let [sw, state] of Object.entries(data.rules.switchValues)) {
this.setState((prevState) => ({
...prevState,
switchValues: prevState.switchValues.map((obj) => {
if (obj.name === sw) {
return {
...obj,
value: state
};
}
return obj;
})
}));
}
for (let [sw, state] of Object.entries(data.rules.inputValues)) {
this.setState((prevState) => ({
...prevState,
inputValues: prevState.inputValues.map((obj) => {
if (obj.name === sw) {
return {
...obj,
value: state
};
}
return obj;
})
}));
}
resolve();
} catch(e)
{
reject(e);
}
})
})
}
saveSettings = (silent = false) => {
let gameSettings: any = {
switchValues: {},
inputValues: {}
};
for (let ele of this.state.switchValues) {
gameSettings.switchValues[ele.name] = ele.value;
if(!!ele.depends && ele.value)
gameSettings.switchValues[ele.name] = this.checkDependsValue(ele.depends);
}
for (let ele of this.state.inputValues) {
gameSettings.inputValues[ele.name] = ele.value;
}
window.api.request("SETTINGS", {data: {rules: gameSettings as GameRules}}).then();
if (!silent)
this.setState(prevState => ({
...prevState,
savedMsg: true
}));
}
componentDidMount() {
this.getSettings().then();
}
componentWillUnmount() {
@ -153,27 +259,41 @@ export default class Setup extends Component<{}, InitialSetupState> {
window.app.toggleWiFiSettings(false);
}
checkDependsValue = (depends: string) => {
for(let x of this.state.switchValues)
{
if(x.name == depends)
checkDependsValue = (depends: GameSwitchNames) => {
for (let x of this.state.switchValues) {
if (x.name == depends)
return x.value;
}
}
render() {
return <div className="setup">
<Grid container spacing={2}>
<Snackbar
open={this.state.savedMsg}
autoHideDuration={6000}
onClose={() => {
this.setState(prevState => ({
...prevState,
savedMsg: false
}))
}}
message={"Einstellungen gespeichert!"}
/>
<Typography variant="h2" sx={{mb: 2}}>Einstellungen</Typography>
<Grid container spacing={3}>
<Grid item xs={6}>
<FormControl component="fieldset" variant="standard">
<FormLabel component="legend">Regeln</FormLabel>
<FormLabel component="legend">Schalter-Regeln</FormLabel>
<FormGroup>
{Object.values(this.state.switchValues).map(value => {
return (
<FormControlLabel sx={{mb: 1}}
control={
<Switch checked={(value.value && !value.depends) || (value.value && !!value.depends && this.checkDependsValue(value.depends))} disabled={!!value.depends && !this.checkDependsValue(value.depends)} onChange={this.onSwitch}
name={value.name}/>
<Switch
checked={(value.value && !value.depends) || (value.value && !!value.depends && this.checkDependsValue(value.depends))}
disabled={!!value.depends && !this.checkDependsValue(value.depends)}
onChange={this.onSwitch}
name={value.name}/>
} label={value.label}
/>)
})}
@ -182,14 +302,37 @@ export default class Setup extends Component<{}, InitialSetupState> {
</Grid>
<Grid item xs={6}>
<FormControl component="fieldset" variant="standard">
<FormLabel component="legend">Standardwerte</FormLabel>
<FormLabel component="legend">Werte-Regeln</FormLabel>
<FormGroup>
{Object.values(this.state.inputValues).map(value => {
return (
<FormControlLabel sx={{mb: 2}}
control={
<TextField type={value.type} variant="standard"
value={value.value}
disabled={!!value.depends && !this.checkDependsValue(value.depends)}
onChange={this.onInput}
name={value.name}/>
} label={value.label}
/>)
})}
</FormGroup>
</FormControl>
</Grid>
<Grid item xs={3.5}></Grid>
<Grid item xs={"auto"} alignItems="center">
<Button variant="contained" color="secondary" onClick={() => {
this.getSettings().then(r => window.app.setPage(PAGE.STARTUP));
}}>Abbrechen <CloseIcon/></Button>
<Button sx={{ml: 2}} variant="outlined" color="error"
onClick={() => this.reset()}>Zurücksetzen <RestartAltIcon/></Button>
<Button sx={{ml: 2}} variant="contained" color="success" onClick={() => {
this.saveSettings(); window.app.setPage(PAGE.STARTUP);
}}>Speichern <SaveIcon/></Button>
</Grid>
</Grid>
</div>
}

View File

@ -2,41 +2,53 @@ import React, {Component} from "react";
import {
Backdrop,
Box,
CircularProgress,
Fade,
Modal,
Stack,
Typography,
Button,
Chip,
CircularProgress,
Dialog,
DialogTitle, DialogContent, DialogContentText, DialogActions, Chip, FormControl, Snackbar
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
Fade,
FormControl,
Modal,
Snackbar,
Stack,
Typography, Zoom
} from "@mui/material";
import {FunctionTest} from "../IPCConstants";
import {FunctionTest} from "../RawConstants";
import SettingsIcon from '@mui/icons-material/Settings';
import PlayCircleIcon from '@mui/icons-material/PlayCircle';
import CloudOffIcon from '@mui/icons-material/CloudOff';
import CloudIcon from '@mui/icons-material/Cloud';
import {PAGE} from "./App";
import GameHandler from "./GameHandler";
interface StartupProps {
visible: boolean
}
interface StartupState {
statusTxt: string,
openCloudConnectModal: boolean,
showStartBtn: boolean,
isConnected: boolean,
cloudErrorMsg: string,
isCloudConnected: boolean,
snackErrorMsg: string,
isConnectionIssue: boolean,
startCounter: number,
}
export default class Startup extends Component<{}, StartupState> {
constructor(props: {}) {
export default class Startup extends Component<StartupProps, StartupState> {
constructor(props: StartupProps) {
super(props);
this.state = {
statusTxt: "Smart-Monopoly wird gestartet...",
openCloudConnectModal: false,
showStartBtn: false,
isConnected: false,
isCloudConnected: false,
isConnectionIssue: false,
cloudErrorMsg: "",
snackErrorMsg: "",
startCounter: 10,
};
}
@ -62,7 +74,9 @@ export default class Startup extends Component<{}, StartupState> {
let response = await window.api.request("CLOUD_CONNECT", {});
this.setState((prevState) => ({
...prevState,
cloudErrorMsg: response.data.toString()
snackErrorMsg: response.data.toString(),
isCloudConnected: response.status,
isConnectionIssue: !response.status,
}));
console.log(response)
return response.status;
@ -73,6 +87,27 @@ export default class Startup extends Component<{}, StartupState> {
startupBtnClick = () => {
// Startup handle
this.setState(prevState => ({
...prevState,
showStartBtn: false,
statusTxt: "Kapitalismus an die Macht!"
}));
setTimeout(async () => {
try {
let resp = await GameHandler.requestPreparing(this.state.isCloudConnected);
} catch(e)
{
this.setState(prevState => ({
...prevState,
showStartBtn: true,
snackErrorMsg: "Start fehlgeschlagen! \n" + e.toString()
}));
await this.cloudDecision(false);
}
}, 1500)
}
style = {
@ -88,13 +123,12 @@ export default class Startup extends Component<{}, StartupState> {
};
async cloudDecision(decision: boolean) {
this.setState((prevState) => ({
...prevState,
openCloudConnectModal: false,
isConnectionIssue: false,
isConnected: false,
isCloudConnected: false,
startCounter: 30
}));
clearInterval(this.counterInterval);
@ -124,20 +158,19 @@ export default class Startup extends Component<{}, StartupState> {
...prevState,
statusTxt: "Bereit zum Spielen?",
showStartBtn: true,
isConnectionIssue: !connected,
isConnected: connected
})
)
if(connected)
if (connected)
this.counterInterval = setInterval(() => {
if(this.state.startCounter == 0) {
if (!this.props.visible) return;
if (this.state.startCounter == 0) {
clearInterval(this.counterInterval);
this.startupBtnClick();
return;
}
this.setState((prevState) => ({
...prevState,
startCounter: prevState.startCounter-1
startCounter: prevState.startCounter - 1
}));
}, 1000);
@ -151,29 +184,35 @@ export default class Startup extends Component<{}, StartupState> {
startCounter: 30
}));
this.counterInterval = setInterval(() => {
this.setState((prevState) => ({
...prevState,
startCounter: prevState.startCounter-1
}));
if(this.state.startCounter == 0) {
if (!this.props.visible) return;
if (this.state.startCounter == 0) {
clearInterval(this.counterInterval);
this.startupBtnClick();
return;
}
this.setState((prevState) => ({
...prevState,
startCounter: prevState.startCounter - 1
}));
}, 1000);
}
}
render() {
return <div className="startup">
return <div className={`startup ${this.props.visible ? '' : 'hidden'}`}>
<Snackbar
open={this.state.cloudErrorMsg != ""}
open={this.state.snackErrorMsg != ""}
autoHideDuration={8000}
onClose={() => {this.setState(prevState => ({
...prevState,
cloudErrorMsg: ""
}))}}
message={this.state.cloudErrorMsg}
onClose={() => {
this.setState(prevState => ({
...prevState,
snackErrorMsg: ""
}))
}}
message={this.state.snackErrorMsg}
/>
<Dialog
open={this.state.isConnectionIssue}
@ -226,7 +265,8 @@ export default class Startup extends Component<{}, StartupState> {
<i>Dafür wird eine WiFi-Verbindung hergestellt</i>
<br/>
<br/>
<Button variant="contained" sx={{mr: 1}} onClick={() => this.cloudDecision(true)}>Ja</Button>
<Button variant="contained" sx={{mr: 1}}
onClick={() => this.cloudDecision(true)}>Ja</Button>
<Button variant="contained" onClick={() => this.cloudDecision(false)}
color="error">Nein</Button>
</Typography>
@ -243,16 +283,24 @@ export default class Startup extends Component<{}, StartupState> {
<Box alignItems="center" sx={{width: '100%'}}>
<FormControl sx={{mr: 2, minWidth: '30%'}}>
{this.state.showStartBtn && <Button color="secondary" variant="contained">Setup <SettingsIcon/></Button>}
<Zoom in={this.state.showStartBtn}><Button color="secondary"
onClick={() => window.app.setPage(PAGE.SETUP)}
variant="contained">Einstellungen <SettingsIcon/></Button></Zoom>
</FormControl>
<FormControl sx={{minWidth: '30%'}}>
{this.state.showStartBtn && <Button color="success" variant="contained">Start <Chip sx={{ml: 1}} size="small" label={this.state.startCounter} /> <PlayCircleIcon/></Button>}
<Zoom in={this.state.showStartBtn}><Button color="success"
onClick={() => this.startupBtnClick()}
variant="contained">Start <Chip sx={{ml: 1}}
size="small"
label={this.state.startCounter}/>
<PlayCircleIcon/></Button></Zoom>
</FormControl>
</Box>
<Chip sx={{mt: 3}} color={this.state.isConnected ? "success" : "default"} onClick={() => this.setState(prevState => ({
<Chip sx={{mt: 3}} color={this.state.isCloudConnected ? "success" : "default"}
disabled={!this.state.showStartBtn} onClick={() => this.setState(prevState => ({
...prevState,
openCloudConnectModal: true
}))} label={this.state.isConnected ? <CloudIcon/> : <CloudOffIcon />}/>
}))} label={this.state.isCloudConnected ? <CloudIcon/> : <CloudOffIcon/>}/>
</Stack>
</div>

View File

@ -17,7 +17,7 @@ import {
import WifiPasswordIcon from "@mui/icons-material/WifiPassword";
import WifiIcon from "@mui/icons-material/Wifi";
import {IPCAnswer, WiFiNetwork} from "../IPCConstants";
import {IPCAnswer, WiFiNetwork} from "../RawConstants";
interface WiFiState {

View File

@ -7,3 +7,6 @@
text-align: center;
}
.hidden {
display: none;
}

View File

@ -1,13 +1,13 @@
import {IPCAnswer, IPCChannel, IPCRequest} from "../IPCConstants";
import {IPCAnswer, IPCChannel, IPCListenChannels, IPCRequest} from "../RawConstants";
import IpcRendererEvent = Electron.IpcRendererEvent;
import {App} from "./App"
import {App} from "./App";
export {}
declare global {
interface Window {
"api": {
request: (channel: IPCChannel, request: IPCRequest, ...args: any) => Promise<IPCAnswer>;
listen: (channel: IPCChannel, func: (event: IpcRendererEvent, message: IPCAnswer, ...args: any) => void) => void;
listen: (channel: IPCListenChannels, func: (event: IpcRendererEvent, message: IPCAnswer, ...args: any) => void) => void;
}
app: App;
}

477
yarn.lock

File diff suppressed because it is too large Load Diff