Skip to content

Commit

Permalink
feat: made seperate components / fixed routing etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSteppacher committed Aug 2, 2023
1 parent 2c02421 commit f65f00b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 43 deletions.
5 changes: 2 additions & 3 deletions src/app/misc/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SygotchiErstellenComponent } from "../sygotchi-erstellen/sygotchi-erste
import { TeamSComponent } from "../team-s/team-s.component";
import { DsgvoPageComponent } from "../dsgvo-page/dsgvo-page.component";
import { ImpressumComponent } from "../impressum/impressum.component";
import { ShowSygotchiComponent } from "../show-sygotchi/show-sygotchi.component";
import {SleepSceneComponent} from "../sleep-scene/sleep-scene.component";

export const APP_ROUTES: Routes = [
{path: '', redirectTo: 'auth', pathMatch: 'full'},
Expand All @@ -14,7 +14,6 @@ export const APP_ROUTES: Routes = [
{path: 'team-site', component: TeamSComponent},
{path: 'dsgvo', component: DsgvoPageComponent},
{path: 'impressum', component: ImpressumComponent },
{path: 'sygotchi', component: ShowSygotchiComponent, canActivate: [AuthGuard]},
{path: 'sleep', component: SleepSceneComponent, canActivate: [AuthGuard]},
{path: '**', redirectTo: 'auth' }

]
6 changes: 3 additions & 3 deletions src/app/services/actions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export class ActionsService {
}

sleep(): Observable<SyGotchi> {
return this.http.post<SyGotchi>(`${this.apiUrl}/tamagotchi`, null)
}
return this.http.post<SyGotchi>(`${this.apiUrl}/tamagotchi/sleep`, null)
}

wakeUp(): Observable<SyGotchi> {
return this.http.post<SyGotchi>(`${this.apiUrl}/tamagotchi`, null)
return this.http.post<SyGotchi>(`${this.apiUrl}/tamagotchi/wakeUp`, null)
}

}
8 changes: 0 additions & 8 deletions src/app/show-sygotchi/show-sygotchi.component.html
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
<canvas id="canvas"></canvas>
<button (click)="onSleep()" class="btn btn-primary">
<div *ngIf="isAsleep;else other_content">
<span>Aufwachen</span>
</div>
<ng-template #other_content>
<span>Schlafen</span>
</ng-template>
</button>
33 changes: 7 additions & 26 deletions src/app/show-sygotchi/show-sygotchi.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Project } from "paper";
import { WebsocketService } from '../services/websocket.service';
import { selectSygotchi } from '../store/sygotchi.selectors';
import { setSygotchi } from '../store/sygotchi.actions';
import {Store} from "@ngrx/store";

@Component({
selector: 'app-show-sygotchi',
Expand All @@ -18,10 +19,9 @@ export class ShowSygotchiComponent implements OnInit {
canvas: any
shapePath: any
shape: any
isAsleep: boolean = false

constructor(private actionsService: ActionsService, private store: Store, private wsService: WebsocketService) { }

ngOnInit(): void {
window['paper'] = paper;
new Project('canvas');
Expand All @@ -36,18 +36,17 @@ export class ShowSygotchiComponent implements OnInit {
if (this.sygotchi === null) {
this.actionsService.getSygotchi().subscribe(result => {
this.sygotchi = result
this.store.dispach(setSygotchi({ sygotchi: result as SyGotchi }))
this.store.dispatch(setSygotchi({ sygotchi: result as SyGotchi }))
this.buildSygotchi()
})
} else {
this.wsService.initializeWebSocketConnection(this.sygotchi.id)
this.buildSygotchi()
}


this.buildSygotchi()
this.getSleepStatus()
})
})
}


buildSygotchi() {
const shapeType = this.sygotchi.shape;
Expand Down Expand Up @@ -125,22 +124,4 @@ export class ShowSygotchiComponent implements OnInit {
})
this.canvas.addChild(this.shapePath);
}

onSleep() {
if (this.isAsleep) {
this.isAsleep = false
this.actonsService.sleep().subscribe(sygotchi => {
this.sygotchi = sygotchi
})
} else {
this.isAsleep = true
this.actonsService.wakeUp().subscribe(sygotchi => {
this.sygotchi = sygotchi
})
}
}

getSleepStatus() {

}
}
10 changes: 9 additions & 1 deletion src/app/sleep-scene/sleep-scene.component.html
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<p>sleep-scene works!</p>
<app-show-sygotchi></app-show-sygotchi>
<button (click)="onSleep()" class="btn btn-primary">
<div *ngIf="isAsleep;else goSleep">
<span>Aufwachen</span>
</div>
<ng-template #goSleep>
<span>Schlafen</span>
</ng-template>
</button>
66 changes: 64 additions & 2 deletions src/app/sleep-scene/sleep-scene.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,72 @@
import { Component } from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {ActionsService} from "../services/actions.service";
import {Store} from "@ngrx/store";
import {SyGotchi} from "../entities/syGotchi";
import {setSygotchi} from "../store/sygotchi.actions";
import {selectSygotchi} from "../store/sygotchi.selectors";

@Component({
selector: 'app-sleep-scene',
templateUrl: './sleep-scene.component.html',
styleUrls: ['./sleep-scene.component.scss']
})
export class SleepSceneComponent {
export class SleepSceneComponent implements OnInit {
isAsleep: boolean = false
message = {text: '', error: false}
sygotchi: SyGotchi
showMessage: boolean = false

constructor(private actionsService: ActionsService, private store: Store) {}

ngOnInit() {
this.store.select(selectSygotchi)
.subscribe(
syGotchi => {
this.isAsleep = syGotchi.sleeping
this.sygotchi = syGotchi
}
)
}

onSleep() {
if(!this.isAsleep) {
this.actionsService.sleep()
.subscribe(
result => {
this.message.text = 'SyGotchi schläft nun.'
this.messageHandler()

this.store.dispatch(setSygotchi({sygotchi: result as SyGotchi}))
},
() => {
this.message.error = true
this.message.text = "SyGotchi ist nicht müde genug."
this.messageHandler()
}
)
} else {
this.actionsService.wakeUp()
.subscribe(
result => {
this.message.text = 'SyGotchi ist wieder aufgewacht.'
this.messageHandler()

this.store.dispatch(setSygotchi({sygotchi: result as SyGotchi}))
},
() => {
this.message.error = true
this.message.text = "SyGotchi kann nicht aufwachen."
this.messageHandler()
}
)
}
}

messageHandler() {
this.showMessage = true

setTimeout(() => {
this.showMessage = false
}, 7000)
}
}

0 comments on commit f65f00b

Please sign in to comment.