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

Solucion #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 18 additions & 15 deletions src/app/core/services/game-state.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {Injectable} from '@angular/core';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/of';

import {Observable} from 'rxjs/Observable';
import {HttpClient} from '@angular/common/http';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

import {environment} from '../../../environments/environment';
import {SessionService} from './uthorization/session.service';
import {Credentials} from '../model/credentials';
import { environment } from '../../../environments/environment';
import { SessionService } from './uthorization/session.service';
import { Credentials } from '../model/credentials';
import Player from '../model/player';
import {PlayerService} from './player.service';
import { PlayerService } from './player.service';


@Injectable()
Expand All @@ -26,17 +26,17 @@ export class GameStateService {
eventSource.addEventListener('gameState', (e) => {
console.log('gameState', e);
const game = JSON.parse(e.data);
if (this.getPlayerDto(this.sessionService.getCredentials().email, game.players)){
if (this.getPlayerDto(this.sessionService.getCredentials().email, game.players)) {
this.eventObserver.next(game);
} else {
this.playerService.getPlayerState().subscribe((playerGame) => {
if (playerGame.players) {
this.eventObserver.next(playerGame);
return {playerGame};
return { playerGame };
} else {
return playerService.getPlayer().subscribe(player => {
this.eventObserver.next({...playerGame, players: [player]});
return {...playerGame, players: [player]};
this.eventObserver.next({ ...playerGame, players: [player] });
return { ...playerGame, players: [player] };
});
}
});
Expand All @@ -46,11 +46,11 @@ export class GameStateService {
this.playerService.getPlayerState().subscribe((playerGame) => {
if (playerGame.players) {
this.eventObserver.next(playerGame);
return {playerGame};
return { playerGame };
} else {
return playerService.getPlayer().subscribe(player => {
this.eventObserver.next({...playerGame, players: [player]});
return {...playerGame, players: [player]};
this.eventObserver.next({ ...playerGame, players: [player] });
return { ...playerGame, players: [player] };
});
}
});
Expand Down Expand Up @@ -95,4 +95,7 @@ export class GameStateService {
}
}
}



}
42 changes: 34 additions & 8 deletions src/app/core/services/player.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Injectable} from '@angular/core';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import {HttpClient} from '@angular/common/http';
import {environment} from '../../../environments/environment';
import {SessionService} from './uthorization/session.service';
import {Credentials} from '../model/credentials';
import {Observable} from "rxjs/Observable";
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import { SessionService } from './uthorization/session.service';
import { Credentials } from '../model/credentials';
import { Observable } from "rxjs/Observable";


@Injectable()
Expand All @@ -16,12 +16,38 @@ export class PlayerService {
this.credentials = sessionService.getCredentials();
}

getPlayerState(): Observable<any>{
getPlayerState(): Observable<any> {
return this.http.get(`${environment.api}/api/player/state`);
}

getPlayer(){
getPlayer() {
return this.http.get(`${environment.api}/api/player/`);
}

bet(token: string, bet: number) {
this.http.post(`${environment.api}/api/player/bet`, {
'token': token,
'bet': {
value : bet
}
}).subscribe((item)=>{
});
}

stand(token: string) {
this.http.post(`${environment.api}/api/player/stand`, {
token: token
}).subscribe((item)=>{
console.log(item);
});
}

hit(token: string) {
this.http.post(`${environment.api}/api/player/hit`, {
token: token
}).subscribe((item)=>{
console.log(item);
});
}

}
16 changes: 12 additions & 4 deletions src/app/states/game/game.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ <h1>Let's Play!</h1>
<div *ngIf="player.cards as cards">
<img *ngFor="let card of cards" [src]="card" alt="">
</div>

<label for="betValue">Entry the bet</label>
<input #betValue name="betvalue" type="text" value="">

<div class="actions">
<button type="button">Bet!</button>
<button type="button">Hit</button>
<button type="button">Stand</button>


<button type="button" (click)="bet(betValue.value)">Bet!</button>
<button type="button" (click)="hit()">Hit</button>
<button type="button" (click)="stand()">Stand</button>


</div>
</div>
</div>
</section>
</section>
33 changes: 25 additions & 8 deletions src/app/states/game/game.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {Component, OnInit} from '@angular/core';
import {GameStateService} from '../../core/services/game-state.service';
import {Observable} from 'rxjs/Observable';
import { Component, OnInit } from '@angular/core';
import { GameStateService } from '../../core/services/game-state.service';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

//Services
import { SessionService } from '../../core/services/uthorization/session.service';

import {PlayerService} from '../../core/services/player.service';


import { PlayerService } from '../../core/services/player.service';
import Player from '../../core/model/player';


@Component({
selector: 'app-game',
templateUrl: './game.component.html',
Expand All @@ -24,7 +29,10 @@ export class GameComponent implements OnInit {
DEALER_TURN: 'Dealer\'s turn'
};

constructor(private gameStateService: GameStateService, private playerService: PlayerService) {
constructor(
private gameStateService: GameStateService,
private playerService: PlayerService,
private sessionService: SessionService) {
}

ngOnInit() {
Expand All @@ -34,16 +42,25 @@ export class GameComponent implements OnInit {
});
}

bet() {
bet(betText: any) {
let betValue: number = parseInt(betText == "" ? "0" : betText);
if (betValue == 0) {
betValue = 100;
}
console.log(betValue);
let token:string = this.sessionService.getSessionToken();

this.playerService.bet(token, betValue);
}

hit() {

let token:string = this.sessionService.getSessionToken();
this.playerService.hit(token);
}

stand() {

let token:string = this.sessionService.getSessionToken();
this.playerService.stand(token);
}

}