Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mergeconflict #23

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions backend/prisma/migrations/20231114131240_update/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Warnings:

- You are about to drop the `ChatMemberships` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `Chats` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `Messages` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `Roles` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `Scores` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropForeignKey
ALTER TABLE "ChatMemberships" DROP CONSTRAINT "ChatMemberships_chat_id_fkey";

-- DropForeignKey
ALTER TABLE "ChatMemberships" DROP CONSTRAINT "ChatMemberships_role_id_fkey";

-- DropForeignKey
ALTER TABLE "ChatMemberships" DROP CONSTRAINT "ChatMemberships_user_id_fkey";

-- DropForeignKey
ALTER TABLE "Messages" DROP CONSTRAINT "Messages_chat_id_fkey";

-- DropForeignKey
ALTER TABLE "Messages" DROP CONSTRAINT "Messages_user_id_fkey";

-- DropForeignKey
ALTER TABLE "Roles" DROP CONSTRAINT "Roles_parent_role_id_fkey";

-- DropForeignKey
ALTER TABLE "Scores" DROP CONSTRAINT "Scores_game_id_fkey";

-- DropForeignKey
ALTER TABLE "Scores" DROP CONSTRAINT "Scores_user_id_fkey";

-- AlterTable
ALTER TABLE "Games" ADD COLUMN "left_user_score" INTEGER,
ADD COLUMN "right_user_score" INTEGER;

-- DropTable
DROP TABLE "ChatMemberships";

-- DropTable
DROP TABLE "Chats";

-- DropTable
DROP TABLE "Messages";

-- DropTable
DROP TABLE "Roles";

-- DropTable
DROP TABLE "Scores";

-- AddForeignKey
ALTER TABLE "Games" ADD CONSTRAINT "Games_left_user_id_fkey" FOREIGN KEY ("left_user_id") REFERENCES "Users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Games" ADD CONSTRAINT "Games_right_user_id_fkey" FOREIGN KEY ("right_user_id") REFERENCES "Users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions backend/prisma/migrations/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "Games" ALTER COLUMN "left_user_score" DROP NOT NULL,
ALTER COLUMN "right_user_score" DROP NOT NULL;
125 changes: 65 additions & 60 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,76 +19,81 @@ model Users {
nick String?

//Opposite Relations
messages Messages[]
memberships ChatMemberships[]
scores Scores[]
// messages Messages[]
// memberships ChatMemberships[]
// scores Scores[]
gamesL Games[] @relation("LeftUser")
gamesR Games[] @relation("RightUser")
}

model Chats {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

//Opposite Relations
messages Messages[]
memberships ChatMemberships[]
}

model Messages {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
chat_id Int
user_id Int
message String

//Relations
chat Chats @relation(fields: [chat_id], references: [id])
user Users @relation(fields: [user_id], references: [id])
}

model ChatMemberships {
id Int @id @default(autoincrement())
chat_id Int
user_id Int
role_id Int

//Relations
role Roles @relation(fields: [role_id], references: [id])
chat Chats @relation(fields: [chat_id], references: [id])
user Users @relation(fields: [user_id], references: [id])
}

model Roles {
id Int @id @default(autoincrement())
name String @unique
description String?
parent_role_id Int?

//Relations
parentRole Roles? @relation("ChildRoles", fields: [parent_role_id], references: [id])

//Opposite Relations
childRoles Roles[] @relation("ChildRoles")
memberships ChatMemberships[]
}
// model Chats {
// id Int @id @default(autoincrement())
// createdAt DateTime @default(now())
// updatedAt DateTime @updatedAt

// //Opposite Relations
// messages Messages[]
// memberships ChatMemberships[]
// }

// model Messages {
// id Int @id @default(autoincrement())
// createdAt DateTime @default(now())
// chat_id Int
// user_id Int
// message String

// //Relations
// chat Chats @relation(fields: [chat_id], references: [id])
// user Users @relation(fields: [user_id], references: [id])
// }

// model ChatMemberships {
// id Int @id @default(autoincrement())
// chat_id Int
// user_id Int
// role_id Int

// //Relations
// role Roles @relation(fields: [role_id], references: [id])
// chat Chats @relation(fields: [chat_id], references: [id])
// user Users @relation(fields: [user_id], references: [id])
// }

// model Roles {
// id Int @id @default(autoincrement())
// name String @unique
// description String?
// parent_role_id Int?

// //Relations
// parentRole Roles? @relation("ChildRoles", fields: [parent_role_id], references: [id])

// //Opposite Relations
// childRoles Roles[] @relation("ChildRoles")
// memberships ChatMemberships[]
// }

model Games {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
left_user_id Int
right_user_id Int
left_user_score Int?
right_user_score Int?

//Opposite Relations
scores Scores[]
// scores Scores[]
l_user Users @relation("LeftUser", fields: [left_user_id], references: [id])
r_user Users @relation("RightUser", fields: [right_user_id], references: [id])
}

model Scores {
id Int @id @default(autoincrement())
game_id Int
user_id Int
score Int
// model Scores {
// id Int @id @default(autoincrement())
// game_id Int
// user_id Int
// score Int

//Relations
game Games @relation(fields: [game_id], references: [id])
user Users @relation(fields: [user_id], references: [id])
}
// //Relations
// game Games @relation(fields: [game_id], references: [id])
// }
70 changes: 56 additions & 14 deletions backend/src/game/GameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Injectable } from '@nestjs/common';
import { User } from 'src/user/User';
import { sharedEventEmitter } from './game.events';
import { GameService } from './game.service';
import { Games, PrismaClient } from '@prisma/client';

@Injectable()
export class GameState {
gameId: string;
prisma: PrismaClient;
GameData: Games;
intervalId: NodeJS.Timeout | null;

user1: User;
Expand Down Expand Up @@ -37,7 +39,8 @@ export class GameState {
speedFactor: number;

constructor() {
this.gameId = this.generateID();
this.prisma = new PrismaClient();
this.GameData = null;
this.intervalId = null;

this.user1 = null;
Expand Down Expand Up @@ -68,6 +71,8 @@ export class GameState {
this.gameInit();
}



gameInit() {
const startAngle = this.randomAngle();
this.ballX = this.fieldWidth / 2;
Expand Down Expand Up @@ -101,34 +106,35 @@ export class GameState {
}

/* Generates a random ID string. */
generateID(): string {
const timestamp = Date.now();
const randomValue = Math.floor(Math.random() * 1000);
//Not used anymore
// generateID(): string {
// const timestamp = Date.now();
// const randomValue = Math.floor(Math.random() * 1000);

const id = `${timestamp}-${randomValue}`;
return id;
}
// const id = `${timestamp}-${randomValue}`;
// return id;
// }

movePaddleUp(player: User) {
if (player === this.user1) {
if (player == this.user1) {
if (this.leftPosition > 0) {
this.leftPosition -= 10;
}
}
else if (player === this.user2) {
else if (player == this.user2) {
if (this.rightPosition > 10) {
this.rightPosition -= 10;
}
}
}

movePaddleDown(player: User) {
if (player === this.user1) {
if (player == this.user1) {
if (this.leftPosition + this.paddlesHeight < this.fieldHeight) {
this.leftPosition += 10;
}
}
else if (player === this.user2) {
else if (player == this.user2) {
if (this.rightPosition + this.paddlesHeight < this.fieldHeight) {
this.rightPosition += 10;
}
Expand Down Expand Up @@ -205,8 +211,9 @@ export class GameState {
return angle;
}

playerVictory () {
async playerVictory () {
if (this.scorePlayer1 == this.winningScore || this.scorePlayer2 == this.winningScore) {
await this.updateGameScore();
clearInterval(this.intervalId);
this.intervalId = null
this.gameInit();
Expand All @@ -220,4 +227,39 @@ export class GameState {
isRunning(): boolean {
return this.intervalId !== null;
}
}


async initializeGame(leftId: number, rightId: number) {
// Assuming you're using Prisma to interact with a database
this.GameData = await this.prisma.games.create({
data: {
// left_user_id: leftId,
left_user_id: 1,
// right_user_id: rightId,
right_user_id: 2,
left_user_score: 0,
right_user_score: 0,
createdAt: new Date(),
},
});

// You can handle the result or perform other actions based on the Prisma query result
console.log('New game created:', this.GameData);
}

async updateGameScore() {
try {
const updatedGame = await this.prisma.games.update({
where: { id: this.GameData.id }, // Specify the condition for the row to be updated (in this case, based on the game's ID)
data: {
left_user_score: this.scorePlayer1,
right_user_score: this.scorePlayer2,
// Other fields you want to update
},
});
console.log('Updated game:', updatedGame);
} catch (error) {
console.error('Error updating game:', error);
}
}
}
Loading