Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6326b5fef6 | |||
| 4f2c149f5a | |||
| 48d620e878 | |||
| 92644d9008 | |||
| 2598e5864e | |||
| 8d2df73484 | |||
| 44575a8a08 | |||
| c82d784866 |
@@ -1,5 +1,5 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||||
import { BehaviorSubject, Observable } from 'rxjs';
|
import { BehaviorSubject, Observable } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
|
|
||||||
@@ -23,7 +23,10 @@ export class AccountService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
login(username, password) {
|
login(username, password) {
|
||||||
return this.httpClient.post<User>(environment.apiUrl + '/fake_login', { username, password })
|
const body = new HttpParams()
|
||||||
|
.set('username', username)
|
||||||
|
.set('password', password);
|
||||||
|
return this.httpClient.post<User>(environment.apiUrl + '/login', body)
|
||||||
.pipe((map(user => {
|
.pipe((map(user => {
|
||||||
localStorage.setItem('user', JSON.stringify(user));
|
localStorage.setItem('user', JSON.stringify(user));
|
||||||
this.userSubject.next(user);
|
this.userSubject.next(user);
|
||||||
@@ -32,7 +35,11 @@ export class AccountService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
register(user) {
|
register(user) {
|
||||||
return this.httpClient.post<User>(environment.apiUrl + '/fake_registration', user);
|
const body = new HttpParams()
|
||||||
|
.set('username', user.username)
|
||||||
|
.set('password', user.password)
|
||||||
|
.set('email', user.email);
|
||||||
|
return this.httpClient.post(environment.apiUrl + '/signup', body);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/app/account/jwt.interceptor.spec.ts
Normal file
16
src/app/account/jwt.interceptor.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { JwtInterceptor } from './jwt.interceptor';
|
||||||
|
|
||||||
|
describe('JwtInterceptor', () => {
|
||||||
|
beforeEach(() => TestBed.configureTestingModule({
|
||||||
|
providers: [
|
||||||
|
JwtInterceptor
|
||||||
|
]
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
const interceptor: JwtInterceptor = TestBed.inject(JwtInterceptor);
|
||||||
|
expect(interceptor).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
31
src/app/account/jwt.interceptor.ts
Normal file
31
src/app/account/jwt.interceptor.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import {
|
||||||
|
HttpRequest,
|
||||||
|
HttpHandler,
|
||||||
|
HttpEvent,
|
||||||
|
HttpInterceptor
|
||||||
|
} from '@angular/common/http';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
import { environment } from '../../environments/environment';
|
||||||
|
import { AccountService } from './account.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class JwtInterceptor implements HttpInterceptor {
|
||||||
|
|
||||||
|
constructor(private accountService: AccountService) {}
|
||||||
|
|
||||||
|
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||||
|
const user = this.accountService.userValue;
|
||||||
|
const isLoggedIn = user && user.token;
|
||||||
|
const isApiUrl = request.url.startsWith(environment.apiUrl);
|
||||||
|
if (isLoggedIn && isApiUrl) {
|
||||||
|
request = request.clone({
|
||||||
|
setHeaders: {
|
||||||
|
Authorization: `Bearer ${user.token}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return next.handle(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
<span *ngIf="loading" class="spinner-border spinner-border-sm mr-1"></span>
|
<span *ngIf="loading" class="spinner-border spinner-border-sm mr-1"></span>
|
||||||
Login
|
Login
|
||||||
</button>
|
</button>
|
||||||
<a mat-button routerLink="/register">Sign up</a>
|
<a mat-button routerLink="/signup">Sign up</a>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</mat-card-content>
|
</mat-card-content>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
import { FormControl, FormGroup } from '@angular/forms';
|
import { FormBuilder, Validators} from '@angular/forms';
|
||||||
import { AccountService } from '../account.service';
|
import { AccountService } from '../account.service';
|
||||||
import { ActivatedRoute, Router } from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
import { first } from 'rxjs/operators';
|
import { first } from 'rxjs/operators';
|
||||||
@@ -10,16 +10,16 @@ import { first } from 'rxjs/operators';
|
|||||||
styleUrls: ['./login.component.css']
|
styleUrls: ['./login.component.css']
|
||||||
})
|
})
|
||||||
export class LoginComponent implements OnInit {
|
export class LoginComponent implements OnInit {
|
||||||
form: FormGroup = new FormGroup({
|
form = this.formBuilder.group({
|
||||||
username: new FormControl(''),
|
username: ['', Validators.required],
|
||||||
password: new FormControl(''),
|
password: ['', [Validators.required, Validators.minLength(15)]],
|
||||||
});
|
});
|
||||||
loading = false;
|
loading = false;
|
||||||
returnUrl = this.activatedRoute.snapshot.queryParams['returnUrl'] || '/';
|
returnUrl = this.activatedRoute.snapshot.queryParams['returnUrl'] || '/';
|
||||||
|
|
||||||
onLogin() {
|
onLogin() {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.accountService.login(this.form.controls['username'], this.form.controls['password'])
|
this.accountService.login(this.form.get('username').value, this.form.get('password').value)
|
||||||
.pipe(first())
|
.pipe(first())
|
||||||
.subscribe(data => {
|
.subscribe(data => {
|
||||||
this.router.navigate([this.returnUrl]);
|
this.router.navigate([this.returnUrl]);
|
||||||
@@ -33,6 +33,7 @@ export class LoginComponent implements OnInit {
|
|||||||
|
|
||||||
constructor(private accountService: AccountService,
|
constructor(private accountService: AccountService,
|
||||||
private activatedRoute: ActivatedRoute,
|
private activatedRoute: ActivatedRoute,
|
||||||
|
private formBuilder: FormBuilder,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
import { RegisterComponent } from './register.component';
|
import { SignupComponent } from './signup.component';
|
||||||
|
|
||||||
describe('RegisterComponent', () => {
|
describe('RegisterComponent', () => {
|
||||||
let component: RegisterComponent;
|
let component: SignupComponent;
|
||||||
let fixture: ComponentFixture<RegisterComponent>;
|
let fixture: ComponentFixture<SignupComponent>;
|
||||||
|
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
declarations: [ RegisterComponent ]
|
declarations: [ SignupComponent ]
|
||||||
})
|
})
|
||||||
.compileComponents();
|
.compileComponents();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fixture = TestBed.createComponent(RegisterComponent);
|
fixture = TestBed.createComponent(SignupComponent);
|
||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
});
|
});
|
||||||
@@ -8,10 +8,10 @@ import { PasswordErrorStateMatcher } from './password-error-state-matcher';
|
|||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-register',
|
selector: 'app-register',
|
||||||
templateUrl: './register.component.html',
|
templateUrl: './signup.component.html',
|
||||||
styleUrls: ['./register.component.css']
|
styleUrls: ['./signup.component.css']
|
||||||
})
|
})
|
||||||
export class RegisterComponent implements OnInit {
|
export class SignupComponent implements OnInit {
|
||||||
form = this.formBuilder.group({
|
form = this.formBuilder.group({
|
||||||
username: ['', Validators.required],
|
username: ['', Validators.required],
|
||||||
email: ['', [Validators.required, Validators.email]],
|
email: ['', [Validators.required, Validators.email]],
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
export class User {
|
export class User {
|
||||||
id: number;
|
id: number;
|
||||||
|
character: string;
|
||||||
username: string;
|
username: string;
|
||||||
email: string;
|
email: string;
|
||||||
token: string;
|
token: string;
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { Routes, RouterModule } from '@angular/router';
|
import { Routes, RouterModule } from '@angular/router';
|
||||||
|
|
||||||
import { AppComponent } from './app.component';
|
import { AppComponent } from './app.component';
|
||||||
import { AuthGuard } from './account/auth.guard';
|
import { AuthGuard } from './account/auth.guard';
|
||||||
import { LoginComponent } from './account/login/login.component';
|
import { LoginComponent } from './account/login/login.component';
|
||||||
import { RegisterComponent } from './account/register/register.component';
|
import { SignupComponent } from './account/signup/signup.component';
|
||||||
|
|
||||||
|
|
||||||
|
const gameModule = () => import('./game/game.module').then(x => x.GameModule);
|
||||||
|
|
||||||
const routes: Routes = [
|
const routes: Routes = [
|
||||||
{ path: '', component: AppComponent, canActivate: [AuthGuard] },
|
{ path: '', component: AppComponent, canActivate: [AuthGuard] },
|
||||||
|
// { path: '', redirectTo: '/game', pathMatch: 'prefix', canActivate: [AuthGuard] },
|
||||||
{ path: 'login', component: LoginComponent },
|
{ path: 'login', component: LoginComponent },
|
||||||
{ path: 'register', component: RegisterComponent },
|
{ path: 'signup', component: SignupComponent },
|
||||||
|
{ path: 'game', loadChildren: gameModule, canActivate: [AuthGuard] },
|
||||||
{ path: '**', redirectTo: '' },
|
{ path: '**', redirectTo: '' },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { HttpClientModule } from '@angular/common/http';
|
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
|
||||||
import { FlexLayoutModule } from '@angular/flex-layout';
|
import { FlexLayoutModule } from '@angular/flex-layout';
|
||||||
import { ReactiveFormsModule } from '@angular/forms';
|
import { ReactiveFormsModule } from '@angular/forms';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
@@ -12,23 +12,16 @@ import { MatInputModule } from '@angular/material/input';
|
|||||||
|
|
||||||
import { AppRoutingModule } from './app-routing.module';
|
import { AppRoutingModule } from './app-routing.module';
|
||||||
import { AppComponent } from './app.component';
|
import { AppComponent } from './app.component';
|
||||||
import { ChatComponent } from './chat/chat.component';
|
|
||||||
import { EntryComponent } from './chat/entry/entry.component';
|
|
||||||
import { InputComponent } from './chat/input/input.component';
|
|
||||||
import { LoginComponent } from './account/login/login.component';
|
import { LoginComponent } from './account/login/login.component';
|
||||||
import { TestComponent } from './test/test.component';
|
import { SignupComponent } from './account/signup/signup.component';
|
||||||
import { RegisterComponent } from './account/register/register.component';
|
import { GameModule } from './game/game.module';
|
||||||
import { fakeBackendProvider } from './utils/fake-backend';
|
import { JwtInterceptor } from './account/jwt.interceptor';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
AppComponent,
|
AppComponent,
|
||||||
ChatComponent,
|
|
||||||
EntryComponent,
|
|
||||||
InputComponent,
|
|
||||||
LoginComponent,
|
LoginComponent,
|
||||||
TestComponent,
|
SignupComponent,
|
||||||
RegisterComponent
|
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
HttpClientModule,
|
HttpClientModule,
|
||||||
@@ -41,9 +34,13 @@ import { fakeBackendProvider } from './utils/fake-backend';
|
|||||||
MatInputModule,
|
MatInputModule,
|
||||||
ReactiveFormsModule,
|
ReactiveFormsModule,
|
||||||
AppRoutingModule,
|
AppRoutingModule,
|
||||||
|
GameModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
fakeBackendProvider,
|
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
|
||||||
|
// fakeBackendProvider,
|
||||||
|
],
|
||||||
|
exports: [
|
||||||
],
|
],
|
||||||
bootstrap: [AppComponent]
|
bootstrap: [AppComponent]
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#chat-log {
|
#chat-log {
|
||||||
height: calc(99% - 34px);
|
height: calc(99% - 34px);
|
||||||
overflow-y: scroll;
|
overflow-y: scroll;
|
||||||
|
background-color: goldenrod;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
<div id="chat-log">
|
<div id="chat-log">
|
||||||
<app-entry *ngFor="let entry of entries"
|
<div *ngFor="let entry of entries">
|
||||||
[entry]=entry>
|
<div [ngSwitch]="entry.type">
|
||||||
</app-entry>
|
<app-entry *ngSwitchCase="'messages'" [entry]="entry"></app-entry>
|
||||||
|
<app-system-entry *ngSwitchCase="'system'" [entry]="entry"></app-system-entry>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<app-input></app-input>
|
<app-input></app-input>
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
import { Entry } from './entry/entry';
|
|
||||||
import { Message } from './message';
|
import { Message } from './message';
|
||||||
import {SocketService} from '../socket/socket.service';
|
import { SystemMessage } from './entry/entry';
|
||||||
|
import { Messages } from './entry/messages/messages';
|
||||||
|
import { SocketService } from '../socket/socket.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-chat',
|
selector: 'app-chat',
|
||||||
@@ -10,28 +12,39 @@ import {SocketService} from '../socket/socket.service';
|
|||||||
})
|
})
|
||||||
export class ChatComponent implements OnInit {
|
export class ChatComponent implements OnInit {
|
||||||
|
|
||||||
entries = new Array<Entry>();
|
entries = new Array<Messages|SystemMessage>();
|
||||||
|
|
||||||
public addMessage(message: Message): void {
|
public addMessage(message: Message): void {
|
||||||
if ((this.entries.length > 0)
|
if ((this.entries.length > 0)
|
||||||
&& (this.entries[this.entries.length - 1].character == message.sender)) {
|
&& (this.entries[this.entries.length - 1].type === 'messages')) {
|
||||||
this.entries[this.entries.length - 1].messages.push(message.message);
|
let entry = this.entries[this.entries.length - 1] as Messages;
|
||||||
|
if (entry.character == message.character) {
|
||||||
|
entry.messages.push(message);
|
||||||
|
} else {
|
||||||
|
this.entries.push(new Messages(message));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.entries.push(new Entry(message.sender, 'Aangular User', message.message));
|
this.entries.push(new Messages(message));
|
||||||
}
|
}
|
||||||
window.setTimeout(ChatComponent.scrollToBottom, 5);
|
window.setTimeout(ChatComponent.scrollToBottom, 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addSystemMessage(message: SystemMessage): void {
|
||||||
|
this.entries.push(message);
|
||||||
|
}
|
||||||
|
|
||||||
static scrollToBottom() {
|
static scrollToBottom() {
|
||||||
const chatLog = document.getElementById('chat-log');
|
const chatLog = document.getElementById('chat-log');
|
||||||
chatLog.scrollTop = chatLog.scrollHeight;
|
chatLog.scrollTop = chatLog.scrollHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private socketService: SocketService) {
|
constructor(private socketService: SocketService) {
|
||||||
socketService.onTestMessage().subscribe((message: Message) => {
|
socketService.onPublicMessage().subscribe((message: Message) => {
|
||||||
console.log(message);
|
|
||||||
this.addMessage(message);
|
this.addMessage(message);
|
||||||
});
|
});
|
||||||
|
socketService.onSystemMessage().subscribe((message: SystemMessage) => {
|
||||||
|
this.addSystemMessage(message);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
|||||||
29
src/app/chat/chat.module.ts
Normal file
29
src/app/chat/chat.module.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { NgModule } from '@angular/core';
|
||||||
|
import { CommonModule } from '@angular/common';
|
||||||
|
|
||||||
|
import { MatCardModule } from '@angular/material/card';
|
||||||
|
import { MatInputModule } from '@angular/material/input';
|
||||||
|
|
||||||
|
import { ChatComponent } from './chat.component';
|
||||||
|
import { SystemMessageComponent } from './entry/system-message/system-message.component';
|
||||||
|
import { MessagesComponent } from './entry/messages/messages.component';
|
||||||
|
import { InputComponent } from './input/input.component';
|
||||||
|
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
declarations: [
|
||||||
|
ChatComponent,
|
||||||
|
InputComponent,
|
||||||
|
MessagesComponent,
|
||||||
|
SystemMessageComponent
|
||||||
|
],
|
||||||
|
exports: [
|
||||||
|
ChatComponent,
|
||||||
|
],
|
||||||
|
imports: [
|
||||||
|
CommonModule,
|
||||||
|
MatCardModule,
|
||||||
|
MatInputModule
|
||||||
|
]
|
||||||
|
})
|
||||||
|
export class ChatModule { }
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Component, Input, OnInit } from '@angular/core';
|
|
||||||
import { Entry } from './entry';
|
|
||||||
|
|
||||||
@Component({
|
|
||||||
selector: 'app-entry',
|
|
||||||
templateUrl: './entry.component.html',
|
|
||||||
styleUrls: ['./entry.component.css']
|
|
||||||
})
|
|
||||||
export class EntryComponent implements OnInit {
|
|
||||||
|
|
||||||
@Input() entry: Entry;
|
|
||||||
|
|
||||||
constructor() { }
|
|
||||||
|
|
||||||
ngOnInit(): void {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Entry } from './entry';
|
import { Messages } from './entry';
|
||||||
|
|
||||||
describe('Entry', () => {
|
describe('Entry', () => {
|
||||||
it('should create an instance', () => {
|
it('should create an instance', () => {
|
||||||
expect(new Entry()).toBeTruthy();
|
expect(new Messages()).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,25 @@
|
|||||||
export class Entry {
|
export abstract class Entry {
|
||||||
|
public timestamp;
|
||||||
|
|
||||||
public messages: Array<string> = new Array<string>();
|
protected constructor() {
|
||||||
|
this.timestamp = new Date();
|
||||||
constructor(public character: string,
|
|
||||||
public user: string,
|
|
||||||
message: string) {
|
|
||||||
this.messages.push(message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class SystemMessage extends Entry {
|
||||||
|
|
||||||
|
constructor(public message: string,
|
||||||
|
public severity: SeverityEnum) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public get type(): string {
|
||||||
|
return 'system'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum SeverityEnum {
|
||||||
|
info = 'info',
|
||||||
|
warning = 'warning',
|
||||||
|
error = 'error',
|
||||||
|
}
|
||||||
|
|||||||
6
src/app/chat/entry/messages/messages.component.css
Normal file
6
src/app/chat/entry/messages/messages.component.css
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
.eye {
|
||||||
|
margin-right: 4px;
|
||||||
|
padding-left: 2px;
|
||||||
|
padding-right: 2px;
|
||||||
|
border: solid 1px;
|
||||||
|
}
|
||||||
@@ -3,13 +3,14 @@
|
|||||||
<div mat-card-avatar class="avatar"></div>
|
<div mat-card-avatar class="avatar"></div>
|
||||||
<mat-card-title class="character">
|
<mat-card-title class="character">
|
||||||
{{entry.character}}
|
{{entry.character}}
|
||||||
<div class="timestamp">23:31</div>
|
<div class="timestamp">{{entry.timestamp | date:'HH:mm' }}</div>
|
||||||
</mat-card-title>
|
</mat-card-title>
|
||||||
<mat-card-subtitle class="user">played by {{entry.user}}</mat-card-subtitle>
|
<mat-card-subtitle class="user">{{entry.user}}</mat-card-subtitle>
|
||||||
</mat-card-header>
|
</mat-card-header>
|
||||||
<mat-card-content>
|
<mat-card-content>
|
||||||
<div class="messages" *ngFor="let message of entry.messages">
|
<div class="messages" *ngFor="let message of entry.messages">
|
||||||
{{message}}
|
<div>{{message.message}}</div>
|
||||||
|
<span *ngFor="let eye of message.eyes" class="eye">{{eye}}</span><span *ngIf="message.result">→ {{message.result}}</span>
|
||||||
</div>
|
</div>
|
||||||
</mat-card-content>
|
</mat-card-content>
|
||||||
</mat-card>
|
</mat-card>
|
||||||
@@ -1,20 +1,20 @@
|
|||||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
import { EntryComponent } from './entry.component';
|
import { MessagesComponent } from './messages.component';
|
||||||
|
|
||||||
describe('EntryComponent', () => {
|
describe('EntryComponent', () => {
|
||||||
let component: EntryComponent;
|
let component: MessagesComponent;
|
||||||
let fixture: ComponentFixture<EntryComponent>;
|
let fixture: ComponentFixture<MessagesComponent>;
|
||||||
|
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
declarations: [ EntryComponent ]
|
declarations: [ MessagesComponent ]
|
||||||
})
|
})
|
||||||
.compileComponents();
|
.compileComponents();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fixture = TestBed.createComponent(EntryComponent);
|
fixture = TestBed.createComponent(MessagesComponent);
|
||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
});
|
});
|
||||||
18
src/app/chat/entry/messages/messages.component.ts
Normal file
18
src/app/chat/entry/messages/messages.component.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { Component, Input, OnInit } from '@angular/core';
|
||||||
|
import { Messages } from './messages';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-entry',
|
||||||
|
templateUrl: './messages.component.html',
|
||||||
|
styleUrls: ['../entry.component.css', './messages.component.css']
|
||||||
|
})
|
||||||
|
export class MessagesComponent implements OnInit {
|
||||||
|
|
||||||
|
@Input() entry: Messages;
|
||||||
|
|
||||||
|
constructor() { }
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
7
src/app/chat/entry/messages/messages.spec.ts
Normal file
7
src/app/chat/entry/messages/messages.spec.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { Messages } from './messages';
|
||||||
|
|
||||||
|
describe('Messages', () => {
|
||||||
|
it('should create an instance', () => {
|
||||||
|
expect(new Messages()).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
23
src/app/chat/entry/messages/messages.ts
Normal file
23
src/app/chat/entry/messages/messages.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { Entry } from '../entry';
|
||||||
|
import { Message } from '../../message';
|
||||||
|
|
||||||
|
export class Messages extends Entry {
|
||||||
|
public messages: Array<Message> = new Array<Message>();
|
||||||
|
|
||||||
|
constructor(message: Message) {
|
||||||
|
super();
|
||||||
|
this.messages.push(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public get character(): string {
|
||||||
|
return this.messages[0].character;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get type(): string {
|
||||||
|
return 'messages'
|
||||||
|
}
|
||||||
|
|
||||||
|
public get user(): string {
|
||||||
|
return this.messages[0].user;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
.system-avatar {
|
||||||
|
background-size: cover;
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<mat-card>
|
||||||
|
<mat-card-header class="header">
|
||||||
|
<div mat-card-avatar class="system-avatar">
|
||||||
|
<img [alt]="entry.severity" src="../../../../assets/build_circle-24px.svg">
|
||||||
|
</div>
|
||||||
|
<mat-card-title class="character">
|
||||||
|
{{entry.severity | titlecase}}
|
||||||
|
<div class="timestamp">{{entry.timestamp | date:'HH:mm' }}</div>
|
||||||
|
</mat-card-title>
|
||||||
|
<mat-card-subtitle class="user">System</mat-card-subtitle>
|
||||||
|
</mat-card-header>
|
||||||
|
<mat-card-content>
|
||||||
|
{{entry.message}}
|
||||||
|
</mat-card-content>
|
||||||
|
</mat-card>
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { SystemMessageComponent } from './system-message.component';
|
||||||
|
|
||||||
|
describe('SystemEntryComponent', () => {
|
||||||
|
let component: SystemMessageComponent;
|
||||||
|
let fixture: ComponentFixture<SystemMessageComponent>;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [ SystemMessageComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(SystemMessageComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { Component, Input, OnInit } from '@angular/core';
|
||||||
|
import { SystemMessage } from '../entry';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-system-entry',
|
||||||
|
templateUrl: './system-message.component.html',
|
||||||
|
styleUrls: ['../entry.component.css']
|
||||||
|
})
|
||||||
|
export class SystemMessageComponent implements OnInit {
|
||||||
|
|
||||||
|
@Input() entry: SystemMessage;
|
||||||
|
|
||||||
|
constructor() { }
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
import {SocketService} from '../../socket/socket.service';
|
|
||||||
import {Message} from '../message';
|
import { Message } from '../message';
|
||||||
import {Events} from '../../socket/events-enum';
|
import { Events } from '../../socket/events-enum';
|
||||||
|
import { SocketService } from '../../socket/socket.service';
|
||||||
|
import { AccountService } from '../../account/account.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-input',
|
selector: 'app-input',
|
||||||
@@ -11,11 +13,15 @@ import {Events} from '../../socket/events-enum';
|
|||||||
export class InputComponent implements OnInit {
|
export class InputComponent implements OnInit {
|
||||||
|
|
||||||
onEnter(value: string): void {
|
onEnter(value: string): void {
|
||||||
const message = new Message('Aangular Frontend', value);
|
if (value.length > 0) {
|
||||||
this.socketService.send(Events.publicMessage, message);
|
const user = this.accountService.userValue;
|
||||||
|
const message = new Message(user.character, user.username, value);
|
||||||
|
this.socketService.send(Events.publicMessage, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private socketService: SocketService) { }
|
constructor(private accountService: AccountService,
|
||||||
|
private socketService: SocketService) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
export class Message {
|
export class Message {
|
||||||
|
|
||||||
constructor(public sender: string,
|
public eyes?: Array<number>;
|
||||||
|
|
||||||
|
public result?: number;
|
||||||
|
|
||||||
|
constructor(public character: string,
|
||||||
|
public user: string,
|
||||||
public message: string) {
|
public message: string) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/app/game/game-routing.module.ts
Normal file
18
src/app/game/game-routing.module.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { NgModule } from '@angular/core';
|
||||||
|
import { Routes, RouterModule } from '@angular/router';
|
||||||
|
import { GameComponent } from './game.component';
|
||||||
|
import { TestComponent } from './test/test.component';
|
||||||
|
|
||||||
|
const routes: Routes = [
|
||||||
|
{ path: '', component: GameComponent,
|
||||||
|
children: [
|
||||||
|
{ path: '', redirectTo: 'test', pathMatch: 'prefix' },
|
||||||
|
{ path: 'test', component: TestComponent },
|
||||||
|
]}
|
||||||
|
];
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
imports: [RouterModule.forChild(routes)],
|
||||||
|
exports: [RouterModule]
|
||||||
|
})
|
||||||
|
export class GameRoutingModule { }
|
||||||
7
src/app/game/game.component.css
Normal file
7
src/app/game/game.component.css
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#game-items {
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
#navbar {
|
||||||
|
background-color: #333333;
|
||||||
|
}
|
||||||
11
src/app/game/game.component.html
Normal file
11
src/app/game/game.component.html
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<div fxLayout="row" id="game-items">
|
||||||
|
<div fxFlex="48px" id="navbar">
|
||||||
|
<app-navbar></app-navbar>
|
||||||
|
</div>
|
||||||
|
<div fxFlex>
|
||||||
|
<router-outlet></router-outlet>
|
||||||
|
</div>
|
||||||
|
<div fxFlex="20%">
|
||||||
|
<app-chat></app-chat>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
25
src/app/game/game.component.spec.ts
Normal file
25
src/app/game/game.component.spec.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { GameComponent } from './game.component';
|
||||||
|
|
||||||
|
describe('GameComponent', () => {
|
||||||
|
let component: GameComponent;
|
||||||
|
let fixture: ComponentFixture<GameComponent>;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [ GameComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(GameComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
15
src/app/game/game.component.ts
Normal file
15
src/app/game/game.component.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-game',
|
||||||
|
templateUrl: './game.component.html',
|
||||||
|
styleUrls: ['./game.component.css']
|
||||||
|
})
|
||||||
|
export class GameComponent implements OnInit {
|
||||||
|
|
||||||
|
constructor() { }
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
31
src/app/game/game.module.ts
Normal file
31
src/app/game/game.module.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { NgModule } from '@angular/core';
|
||||||
|
import { CommonModule } from '@angular/common';
|
||||||
|
import { FlexModule } from '@angular/flex-layout';
|
||||||
|
|
||||||
|
import { MatCardModule } from '@angular/material/card';
|
||||||
|
|
||||||
|
import { GameRoutingModule } from './game-routing.module';
|
||||||
|
import { GameComponent } from './game.component';
|
||||||
|
import { NavbarComponent } from './navbar/navbar.component';
|
||||||
|
import { TestComponent } from './test/test.component';
|
||||||
|
import { ChatModule } from '../chat/chat.module';
|
||||||
|
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
declarations: [
|
||||||
|
GameComponent,
|
||||||
|
NavbarComponent,
|
||||||
|
TestComponent,
|
||||||
|
],
|
||||||
|
exports: [
|
||||||
|
NavbarComponent
|
||||||
|
],
|
||||||
|
imports: [
|
||||||
|
CommonModule,
|
||||||
|
GameRoutingModule,
|
||||||
|
FlexModule,
|
||||||
|
MatCardModule,
|
||||||
|
ChatModule,
|
||||||
|
]
|
||||||
|
})
|
||||||
|
export class GameModule { }
|
||||||
8
src/app/game/navbar/navbar.component.css
Normal file
8
src/app/game/navbar/navbar.component.css
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
.nav-img {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-item {
|
||||||
|
margin: 2px 4px;
|
||||||
|
}
|
||||||
11
src/app/game/navbar/navbar.component.html
Normal file
11
src/app/game/navbar/navbar.component.html
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<div fxLayout="column" fxLayoutAlign=" center">
|
||||||
|
<a routerLink="test">
|
||||||
|
<img class="nav-img" src="assets/build_circle-24px.svg" alt="TODO">
|
||||||
|
</a>
|
||||||
|
<a routerLink="test">
|
||||||
|
<img class="nav-img" src="assets/build_circle-24px.svg" alt="TODO">
|
||||||
|
</a>
|
||||||
|
<a routerLink="test">
|
||||||
|
<img class="nav-img" src="assets/build_circle-24px.svg" alt="TODO">
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
25
src/app/game/navbar/navbar.component.spec.ts
Normal file
25
src/app/game/navbar/navbar.component.spec.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { NavbarComponent } from './navbar.component';
|
||||||
|
|
||||||
|
describe('NavbarComponent', () => {
|
||||||
|
let component: NavbarComponent;
|
||||||
|
let fixture: ComponentFixture<NavbarComponent>;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [ NavbarComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(NavbarComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
15
src/app/game/navbar/navbar.component.ts
Normal file
15
src/app/game/navbar/navbar.component.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-navbar',
|
||||||
|
templateUrl: './navbar.component.html',
|
||||||
|
styleUrls: ['./navbar.component.css']
|
||||||
|
})
|
||||||
|
export class NavbarComponent implements OnInit {
|
||||||
|
|
||||||
|
constructor() { }
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import {SocketService} from '../socket/socket.service';
|
import {SocketService} from '../../socket/socket.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-test',
|
selector: 'app-test',
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
export enum Events {
|
export enum Events {
|
||||||
publicMessage = 'public message'
|
publicMessage = 'public message',
|
||||||
|
systemMessage = 'system message',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import { Observable } from 'rxjs';
|
|||||||
import * as socketIo from 'socket.io-client';
|
import * as socketIo from 'socket.io-client';
|
||||||
|
|
||||||
import { Events } from './events-enum';
|
import { Events } from './events-enum';
|
||||||
|
import { SystemMessage } from '../chat/entry/entry';
|
||||||
|
import { Message } from '../chat/message';
|
||||||
|
|
||||||
const SERVER_URL = 'http://localhost:5005'
|
const SERVER_URL = 'http://localhost:5005'
|
||||||
|
|
||||||
@@ -26,12 +28,21 @@ export class SocketService implements OnInit {
|
|||||||
this.socket.emit(event, message);
|
this.socket.emit(event, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public onTestMessage(): Observable<any> {
|
public onPublicMessage(): Observable<Message> {
|
||||||
return new Observable<any>(observer => {
|
return new Observable<Message>(observer => {
|
||||||
this.socket.on(Events.publicMessage, (data) => observer.next(data));
|
this.socket.on(Events.publicMessage, (data) => observer.next(data));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public onSystemMessage(): Observable<SystemMessage> {
|
||||||
|
return new Observable<SystemMessage>(observer => {
|
||||||
|
this.socket.on(Events.systemMessage, (data: SystemMessage) => {
|
||||||
|
data = Object.assign(SystemMessage, data);
|
||||||
|
observer.next(new SystemMessage(data.message, data.severity));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.initSocket();
|
this.initSocket();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export class FakeBackend implements HttpInterceptor {
|
|||||||
return ok({
|
return ok({
|
||||||
id: user.id,
|
id: user.id,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
|
character: user.character,
|
||||||
token: 'fake-jwt-token',
|
token: 'fake-jwt-token',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -50,6 +51,7 @@ export class FakeBackend implements HttpInterceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
user.id = users.length ? Math.max(...users.map(x => x.id)) + 1 : 1;
|
user.id = users.length ? Math.max(...users.map(x => x.id)) + 1 : 1;
|
||||||
|
user.character = 'placeholder';
|
||||||
users.push(user);
|
users.push(user);
|
||||||
localStorage.setItem('users', JSON.stringify(users));
|
localStorage.setItem('users', JSON.stringify(users));
|
||||||
console.log('Register user: ' + user);
|
console.log('Register user: ' + user);
|
||||||
|
|||||||
1
src/assets/build_circle-24px.svg
Normal file
1
src/assets/build_circle-24px.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24" viewBox="0 0 24 24" width="24"><g><rect fill="none" height="24" width="24"/><rect fill="none" height="24" width="24"/></g><g><g><path d="M12,2C6.48,2,2,6.48,2,12c0,5.52,4.48,10,10,10s10-4.48,10-10 C22,6.48,17.52,2,12,2z M16.54,15.85l-0.69,0.69c-0.39,0.39-1.02,0.39-1.41,0l-3.05-3.05c-1.22,0.43-2.64,0.17-3.62-0.81 c-1.11-1.11-1.3-2.79-0.59-4.1l2.35,2.35l1.41-1.41L8.58,7.17c1.32-0.71,2.99-0.52,4.1,0.59c0.98,0.98,1.24,2.4,0.81,3.62 l3.05,3.05C16.93,14.82,16.93,15.46,16.54,15.85z" fill-rule="evenodd"/></g></g></svg>
|
||||||
|
After Width: | Height: | Size: 602 B |
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
export const environment = {
|
export const environment = {
|
||||||
production: false,
|
production: false,
|
||||||
apiUrl: 'http://localhost:4200',
|
apiUrl: 'http://localhost:5005',
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user