use common game logic on server/client

This commit is contained in:
Zutatensuppe 2020-11-17 22:34:15 +01:00
parent 6cf4f71c86
commit 656d6a567c
5 changed files with 448 additions and 413 deletions

View file

@ -15,10 +15,10 @@ function send(message) {
let clientSeq
let events
function connect(gameId, playerId) {
function connect(gameId, clientId) {
clientSeq = 0
events = {}
conn = new WsClient(WS_ADDRESS, playerId + '|' + gameId)
conn = new WsClient(WS_ADDRESS, clientId + '|' + gameId)
return new Promise(r => {
conn.connect()
send([Protocol.EV_CLIENT_INIT])
@ -29,6 +29,11 @@ function connect(gameId, playerId) {
const game = msg[1]
r(game)
} else if (msgType === Protocol.EV_SERVER_EVENT) {
const msgClientId = msg[1]
const msgClientSeq = msg[2]
if (msgClientId === clientId && events[msgClientSeq]) {
delete events[msgClientSeq]
}
changesCallback(msg)
}
})

6
game/Game.js Normal file
View file

@ -0,0 +1,6 @@
import GameCommon from './../common/GameCommon.js'
export default {
createGame: GameCommon.setGame,
handleInput: GameCommon.handleInput,
}

View file

@ -6,6 +6,7 @@ import Debug from './Debug.js'
import Communication from './Communication.js'
import Util from './../common/Util.js'
import PuzzleGraphics from './PuzzleGraphics.js'
import Game from './Game.js'
if (typeof GAME_ID === 'undefined') throw '[ GAME_ID not set ]'
if (typeof WS_ADDRESS === 'undefined') throw '[ WS_ADDRESS not set ]'
@ -95,12 +96,13 @@ export default class EventAdapter {
async function main() {
let gameId = GAME_ID
let me = initme()
let CLIENT_ID = initme()
let cursorGrab = await Graphics.loadImageToBitmap('/grab.png')
let cursorHand = await Graphics.loadImageToBitmap('/hand.png')
const game = await Communication.connect(gameId, me)
const game = await Communication.connect(gameId, CLIENT_ID)
Game.createGame(GAME_ID, game);
const bitmaps = await PuzzleGraphics.loadPuzzleBitmaps(game.puzzle)
const puzzle = game.puzzle
@ -110,7 +112,7 @@ async function main() {
const changePlayer = (change) => {
for (let k of Object.keys(change)) {
players[me][k] = change[k]
players[CLIENT_ID][k] = change[k]
}
}
@ -137,7 +139,7 @@ async function main() {
for(let [changeType, changeData] of evChanges) {
switch (changeType) {
case 'player': {
if (changeData.id !== me) {
if (changeData.id !== CLIENT_ID) {
players[changeData.id] = changeData
rerender = true
}
@ -166,14 +168,16 @@ async function main() {
let _last_mouse_down = null
const onUpdate = () => {
for (let evt of evts.consumeAll()) {
// LOCAL ONLY CHANGES
// -------------------------------------------------------------
const type = evt[0]
const pos = {x: evt[1], y: evt[2]}
if (type === 'move') {
rerender = true
changePlayer(pos)
if (_last_mouse_down && !getFirstOwnedTile(puzzle, me)) {
if (_last_mouse_down && !getFirstOwnedTile(puzzle, CLIENT_ID)) {
// move the cam
const mouse = viewport.worldToViewport(pos)
const diffX = Math.round(mouse.x - _last_mouse_down.x)
@ -197,6 +201,10 @@ async function main() {
changePlayer(pos)
}
}
// LOCAL + SERVER CHANGES
// -------------------------------------------------------------
Game.handleInput(GAME_ID, CLIENT_ID, evt)
Communication.sendClientEvent(evt)
}
}