mirror of
https://github.com/solcloud/Counter-Strike.git
synced 2025-01-17 14:48:20 +01:00
Wip
This commit is contained in:
parent
040f916f4f
commit
d3143550c6
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/vendor/
|
/vendor/
|
||||||
|
/electron/node_modules/
|
||||||
|
22
electron/main.js
Normal file
22
electron/main.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
const {app, BrowserWindow, protocol} = require('electron')
|
||||||
|
const url = require('url')
|
||||||
|
|
||||||
|
app.whenReady().then(() => {
|
||||||
|
|
||||||
|
protocol.interceptFileProtocol('file', function (request, callback) { // todo migrate www/ files to relative links and remove this
|
||||||
|
const filePath = url.fileURLToPath('file://' + __dirname + '/../www' + request.url.slice('file://'.length))
|
||||||
|
callback(filePath)
|
||||||
|
})
|
||||||
|
|
||||||
|
const win = new BrowserWindow({
|
||||||
|
autoHideMenuBar: true,
|
||||||
|
width: 800,
|
||||||
|
height: 600,
|
||||||
|
webPreferences: { // todo do it properly with isolation
|
||||||
|
nodeIntegration: true,
|
||||||
|
contextIsolation: false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
win.loadFile('/index.html')
|
||||||
|
})
|
9
electron/package.json
Normal file
9
electron/package.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"main": "main.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "electron --no-sandbox ."
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"electron": "^21.1.1"
|
||||||
|
}
|
||||||
|
}
|
@ -121,9 +121,7 @@ class Server
|
|||||||
$nsLast = $nsCurrent + $sleepTimeNs;
|
$nsLast = $nsCurrent + $sleepTimeNs;
|
||||||
time_nanosleep(0, $sleepTimeNs);
|
time_nanosleep(0, $sleepTimeNs);
|
||||||
} else {
|
} else {
|
||||||
if ($this->serverLag === 0) {
|
$this->log('Server lag detected on tick ' . ($tickId - 1), LogLevel::WARNING);
|
||||||
$this->log('First Server tick lag detected on tick ' . ($tickId - 1), LogLevel::WARNING);
|
|
||||||
}
|
|
||||||
$this->serverLag++;
|
$this->serverLag++;
|
||||||
$nsLast = $nsCurrent;
|
$nsLast = $nsCurrent;
|
||||||
}
|
}
|
||||||
|
69
www/assets/js/UdpSocketConnector.js
Normal file
69
www/assets/js/UdpSocketConnector.js
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
const dgram = require('dgram');
|
||||||
|
|
||||||
|
export class UdpSocketConnector {
|
||||||
|
#game;
|
||||||
|
#socket;
|
||||||
|
sendIntervalId;
|
||||||
|
|
||||||
|
constructor(game) {
|
||||||
|
this.#game = game
|
||||||
|
}
|
||||||
|
|
||||||
|
close() {
|
||||||
|
this.#socket.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
connect(ip, port, loginCode) {
|
||||||
|
let logged = false;
|
||||||
|
|
||||||
|
const socket = dgram.createSocket('udp4');
|
||||||
|
this.#socket = socket
|
||||||
|
|
||||||
|
const connector = this
|
||||||
|
const game = this.#game
|
||||||
|
socket.on('close', function () {
|
||||||
|
clearInterval(connector.sendIntervalId)
|
||||||
|
console.log("UdpSocket closed")
|
||||||
|
});
|
||||||
|
socket.on('error', function (error) {
|
||||||
|
alert(`Cannot connect to '${ip}:${port}'`)
|
||||||
|
console.log("UdpSocket error: " + error.message)
|
||||||
|
});
|
||||||
|
socket.on('connect', function () {
|
||||||
|
console.log("UdpSocket connection established.")
|
||||||
|
if (!logged) {
|
||||||
|
console.log("Sending login code to server.")
|
||||||
|
socket.send("login " + loginCode)
|
||||||
|
logged = true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
socket.on('message', function (msg) {
|
||||||
|
let state
|
||||||
|
try {
|
||||||
|
state = JSON.parse(msg.toString())
|
||||||
|
} catch (err) {
|
||||||
|
game.end("Message parse error! " + err.message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
game.tick(state)
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.connect(port, ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
startLoop(control, tickMs) {
|
||||||
|
const game = this.#game
|
||||||
|
const socket = this.#socket
|
||||||
|
|
||||||
|
this.sendIntervalId = setInterval(function () {
|
||||||
|
if (!game.isPlaying() || !game.meIsAlive()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = control.getTickAction()
|
||||||
|
if (data !== '') {
|
||||||
|
socket.send(data)
|
||||||
|
}
|
||||||
|
}, tickMs)
|
||||||
|
}
|
||||||
|
}
|
@ -61,6 +61,6 @@ export class WebSocketConnector {
|
|||||||
|
|
||||||
|
|
||||||
socket.send('') // ping
|
socket.send('') // ping
|
||||||
}, tickMs - 1)
|
}, tickMs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ import {Game} from "./Game.js";
|
|||||||
import {HUD} from "./Hud.js";
|
import {HUD} from "./Hud.js";
|
||||||
import {Control} from "./Control.js";
|
import {Control} from "./Control.js";
|
||||||
import {World} from "./World.js";
|
import {World} from "./World.js";
|
||||||
import {WebSocketConnector} from "./WebSocketConnector.js";
|
|
||||||
import Stats from "./Stats.js";
|
import Stats from "./Stats.js";
|
||||||
|
|
||||||
let launchGame
|
let launchGame
|
||||||
@ -26,6 +25,7 @@ let launchGame
|
|||||||
throw new Error("Game already launched")
|
throw new Error("Game already launched")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let connector
|
||||||
initialized = true
|
initialized = true
|
||||||
const canvas = await world.init(setting.map, setting.world)
|
const canvas = await world.init(setting.map, setting.world)
|
||||||
hud.createHud(elementHud)
|
hud.createHud(elementHud)
|
||||||
@ -37,9 +37,8 @@ let launchGame
|
|||||||
control.requestLock()
|
control.requestLock()
|
||||||
}, {capture: true})
|
}, {capture: true})
|
||||||
canvasParent.appendChild(canvas)
|
canvasParent.appendChild(canvas)
|
||||||
canvasParent.appendChild(stats.dom);
|
canvasParent.appendChild(stats.dom)
|
||||||
|
|
||||||
let connector = new WebSocketConnector(game)
|
|
||||||
game.onEnd(function (msg) {
|
game.onEnd(function (msg) {
|
||||||
connector.close()
|
connector.close()
|
||||||
alert("Game ended: " + msg)
|
alert("Game ended: " + msg)
|
||||||
@ -48,7 +47,21 @@ let launchGame
|
|||||||
game.onReady(function (options) {
|
game.onReady(function (options) {
|
||||||
connector.startLoop(control, options.tickMs)
|
connector.startLoop(control, options.tickMs)
|
||||||
})
|
})
|
||||||
connector.connect(setting.url, setting.code)
|
|
||||||
|
const url = new URL(setting.url)
|
||||||
|
if (url.protocol === 'ws:') {
|
||||||
|
const ns = await import("./WebSocketConnector.js")
|
||||||
|
connector = new ns.WebSocketConnector(game)
|
||||||
|
connector.connect(setting.url, setting.code)
|
||||||
|
} else if (url.protocol === 'udp:') {
|
||||||
|
const ns = await import("./UdpSocketConnector.js")
|
||||||
|
connector = new ns.UdpSocketConnector(game)
|
||||||
|
let url = new URL(setting.url.replace('udp://', 'http://')) // URL do not parse udp parts well, so do http instead
|
||||||
|
connector.connect(url.hostname, url.port, setting.code)
|
||||||
|
} else {
|
||||||
|
alert('Unknown protocol given')
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
})()
|
})()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user