Skip to content

Commit

Permalink
Sync shared (#42)
Browse files Browse the repository at this point in the history
* sync shared cards

* Swap to barcodetype markers

* delete old patterns and add new pngs

* remove pattern detection leftovers

* add undefined check

* sync shared cards

* add undefined check

* Added markers.pdf and a link to it from lobby

* Added Uno-card assets

* Configured Uno-cards

* Added a better cardback

* Updated markers

* fixed Error from DB

* Updated Markers.pdf again

* sync shared cards

* add undefined check

* fornat

---------

Co-authored-by: heini208 <[email protected]>
Co-authored-by: Tom <[email protected]>
Co-authored-by: Frederick Behringer <[email protected]>
  • Loading branch information
4 people authored Mar 13, 2024
1 parent 734be56 commit ad83b0a
Show file tree
Hide file tree
Showing 181 changed files with 113 additions and 11,385 deletions.
6 changes: 2 additions & 4 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,5 @@ async function main() {
return ` > Started with port ${process.env.PORT}`;
}

main()
.then(console.log)
.catch(console.error);
// .finally(() => client.close());
main().then(console.log).catch(console.error);
// .finally(() => client.close());
15 changes: 11 additions & 4 deletions backend/src/cardSyncService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebSocket as Socket } from 'ws';
import { setInterval, clearInterval } from 'timers';
import { Card, UserCards } from './models/card';
import { AllCards, Card, UserCards } from './models/card';
import { Room } from './models/room';
import { User } from './models/user';

Expand Down Expand Up @@ -53,7 +53,8 @@ export class CardSyncService {
}

sendAllCards(): void {
this.room.sendMessageToUsers('allCards', this.userCards);
const allCards: AllCards = { sharedCard: this.sharedCard, userCards: this.userCards };
this.room.sendMessageToUsers('allCards', allCards);
}

stopSync(): void {
Expand All @@ -66,10 +67,15 @@ export class CardSyncService {
//Controlling the user cards
//------------------------------------------------
//

updateUserCards(user: User, cards: Card[]) {
console.debug('update userCards');
this.userCards[this.find(user)].cards = cards;
this.userCards[this.find(user)].cards = cards; //my view on the cards
let isSharedUpdated: boolean = user.updateCards(cards); //update in user
//update the shared card is needed
if (isSharedUpdated) {
this.sharedCard = user.getShared();
console.debug('New shared card', this.sharedCard);
}
console.debug(this.userCards);
}

Expand All @@ -90,4 +96,5 @@ export class CardSyncService {

//We have very few users. So an array is fine to use here.
private userCards: UserCards[] = [];
private sharedCard?: Card;
}
7 changes: 2 additions & 5 deletions backend/src/models/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ export type Card = {
*/
export type UserCards = { userId: string; cards: Card[] };

export function toUserCards(user: User, cards: Card[]): UserCards {
return { userId: user.id, cards: cards };
}

/**
* list of all cards currrently known to the session as
* regulary provided by the backend
*/
export type allCards = {
export type AllCards = {
sharedCard: Card | undefined;
userCards: UserCards[];
};
25 changes: 25 additions & 0 deletions backend/src/models/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WebSocket } from 'ws';
import { Card, UserCards, Zone } from './card';

export class User {
ws: WebSocket;
Expand All @@ -7,6 +8,8 @@ export class User {
isOwner: boolean;
timeout: NodeJS.Timeout | undefined;
markerMap: Map<string, any>;
private cards: Card[];
private sharedCard?: Card;

constructor(ws: WebSocket, id: string | undefined, name: string | undefined) {
this.ws = ws;
Expand All @@ -22,5 +25,27 @@ export class User {
}
this.isOwner = false;
this.markerMap = new Map();
this.cards = [];
}

getShared(): Card | undefined {
return this.sharedCard;
}

getCards(): UserCards {
return { userId: this.id, cards: this.cards };
}

updateCards(cards: Card[]): boolean {
const newShared: Card = cards.filter((card) => card.zone == Zone.shared)[0];
const newisNotNull = newShared;
const isDifferent = newShared?.cardFace != this.sharedCard?.cardFace;
const isSharedUpdated = newisNotNull && isDifferent;

if (isSharedUpdated) {
this.sharedCard = newShared;
}
this.cards = cards;
return isSharedUpdated;
}
}
195 changes: 0 additions & 195 deletions frontend-vue/public/markers/ZoneMarker1.patt

This file was deleted.

Loading

0 comments on commit ad83b0a

Please sign in to comment.