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 { HttpClient } from '@angular/common/http';
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
@@ -23,7 +23,10 @@ export class AccountService {
|
||||
}
|
||||
|
||||
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 => {
|
||||
localStorage.setItem('user', JSON.stringify(user));
|
||||
this.userSubject.next(user);
|
||||
@@ -32,7 +35,11 @@ export class AccountService {
|
||||
}
|
||||
|
||||
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>
|
||||
Login
|
||||
</button>
|
||||
<a mat-button routerLink="/register">Sign up</a>
|
||||
<a mat-button routerLink="/signup">Sign up</a>
|
||||
</div>
|
||||
</form>
|
||||
</mat-card-content>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { FormControl, FormGroup } from '@angular/forms';
|
||||
import { FormBuilder, Validators} from '@angular/forms';
|
||||
import { AccountService } from '../account.service';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { first } from 'rxjs/operators';
|
||||
@@ -10,16 +10,16 @@ import { first } from 'rxjs/operators';
|
||||
styleUrls: ['./login.component.css']
|
||||
})
|
||||
export class LoginComponent implements OnInit {
|
||||
form: FormGroup = new FormGroup({
|
||||
username: new FormControl(''),
|
||||
password: new FormControl(''),
|
||||
form = this.formBuilder.group({
|
||||
username: ['', Validators.required],
|
||||
password: ['', [Validators.required, Validators.minLength(15)]],
|
||||
});
|
||||
loading = false;
|
||||
returnUrl = this.activatedRoute.snapshot.queryParams['returnUrl'] || '/';
|
||||
|
||||
onLogin() {
|
||||
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())
|
||||
.subscribe(data => {
|
||||
this.router.navigate([this.returnUrl]);
|
||||
@@ -33,6 +33,7 @@ export class LoginComponent implements OnInit {
|
||||
|
||||
constructor(private accountService: AccountService,
|
||||
private activatedRoute: ActivatedRoute,
|
||||
private formBuilder: FormBuilder,
|
||||
private router: Router,
|
||||
) { }
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { RegisterComponent } from './register.component';
|
||||
import { SignupComponent } from './signup.component';
|
||||
|
||||
describe('RegisterComponent', () => {
|
||||
let component: RegisterComponent;
|
||||
let fixture: ComponentFixture<RegisterComponent>;
|
||||
let component: SignupComponent;
|
||||
let fixture: ComponentFixture<SignupComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ RegisterComponent ]
|
||||
declarations: [ SignupComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(RegisterComponent);
|
||||
fixture = TestBed.createComponent(SignupComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
@@ -8,10 +8,10 @@ import { PasswordErrorStateMatcher } from './password-error-state-matcher';
|
||||
|
||||
@Component({
|
||||
selector: 'app-register',
|
||||
templateUrl: './register.component.html',
|
||||
styleUrls: ['./register.component.css']
|
||||
templateUrl: './signup.component.html',
|
||||
styleUrls: ['./signup.component.css']
|
||||
})
|
||||
export class RegisterComponent implements OnInit {
|
||||
export class SignupComponent implements OnInit {
|
||||
form = this.formBuilder.group({
|
||||
username: ['', Validators.required],
|
||||
email: ['', [Validators.required, Validators.email]],
|
||||
@@ -1,5 +1,6 @@
|
||||
export class User {
|
||||
id: number;
|
||||
character: string;
|
||||
username: string;
|
||||
email: string;
|
||||
token: string;
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { Routes, RouterModule } from '@angular/router';
|
||||
|
||||
import { AppComponent } from './app.component';
|
||||
import { AuthGuard } from './account/auth.guard';
|
||||
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 = [
|
||||
{ path: '', component: AppComponent, canActivate: [AuthGuard] },
|
||||
// { path: '', redirectTo: '/game', pathMatch: 'prefix', canActivate: [AuthGuard] },
|
||||
{ path: 'login', component: LoginComponent },
|
||||
{ path: 'register', component: RegisterComponent },
|
||||
{ path: 'signup', component: SignupComponent },
|
||||
{ path: 'game', loadChildren: gameModule, canActivate: [AuthGuard] },
|
||||
{ path: '**', redirectTo: '' },
|
||||
];
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 { ReactiveFormsModule } from '@angular/forms';
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
@@ -12,23 +12,16 @@ import { MatInputModule } from '@angular/material/input';
|
||||
|
||||
import { AppRoutingModule } from './app-routing.module';
|
||||
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 { TestComponent } from './test/test.component';
|
||||
import { RegisterComponent } from './account/register/register.component';
|
||||
import { fakeBackendProvider } from './utils/fake-backend';
|
||||
import { SignupComponent } from './account/signup/signup.component';
|
||||
import { GameModule } from './game/game.module';
|
||||
import { JwtInterceptor } from './account/jwt.interceptor';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AppComponent,
|
||||
ChatComponent,
|
||||
EntryComponent,
|
||||
InputComponent,
|
||||
LoginComponent,
|
||||
TestComponent,
|
||||
RegisterComponent
|
||||
SignupComponent,
|
||||
],
|
||||
imports: [
|
||||
HttpClientModule,
|
||||
@@ -41,9 +34,13 @@ import { fakeBackendProvider } from './utils/fake-backend';
|
||||
MatInputModule,
|
||||
ReactiveFormsModule,
|
||||
AppRoutingModule,
|
||||
GameModule,
|
||||
],
|
||||
providers: [
|
||||
fakeBackendProvider,
|
||||
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
|
||||
// fakeBackendProvider,
|
||||
],
|
||||
exports: [
|
||||
],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#chat-log {
|
||||
height: calc(99% - 34px);
|
||||
overflow-y: scroll;
|
||||
background-color: goldenrod;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
<div id="chat-log">
|
||||
<app-entry *ngFor="let entry of entries"
|
||||
[entry]=entry>
|
||||
</app-entry>
|
||||
<div *ngFor="let entry of entries">
|
||||
<div [ngSwitch]="entry.type">
|
||||
<app-entry *ngSwitchCase="'messages'" [entry]="entry"></app-entry>
|
||||
<app-system-entry *ngSwitchCase="'system'" [entry]="entry"></app-system-entry>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<app-input></app-input>
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { Entry } from './entry/entry';
|
||||
|
||||
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({
|
||||
selector: 'app-chat',
|
||||
@@ -10,28 +12,39 @@ import {SocketService} from '../socket/socket.service';
|
||||
})
|
||||
export class ChatComponent implements OnInit {
|
||||
|
||||
entries = new Array<Entry>();
|
||||
entries = new Array<Messages|SystemMessage>();
|
||||
|
||||
public addMessage(message: Message): void {
|
||||
if ((this.entries.length > 0)
|
||||
&& (this.entries[this.entries.length - 1].character == message.sender)) {
|
||||
this.entries[this.entries.length - 1].messages.push(message.message);
|
||||
&& (this.entries[this.entries.length - 1].type === 'messages')) {
|
||||
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 {
|
||||
this.entries.push(new Entry(message.sender, 'Aangular User', message.message));
|
||||
this.entries.push(new Messages(message));
|
||||
}
|
||||
window.setTimeout(ChatComponent.scrollToBottom, 5);
|
||||
}
|
||||
|
||||
public addSystemMessage(message: SystemMessage): void {
|
||||
this.entries.push(message);
|
||||
}
|
||||
|
||||
static scrollToBottom() {
|
||||
const chatLog = document.getElementById('chat-log');
|
||||
chatLog.scrollTop = chatLog.scrollHeight;
|
||||
}
|
||||
|
||||
constructor(private socketService: SocketService) {
|
||||
socketService.onTestMessage().subscribe((message: Message) => {
|
||||
console.log(message);
|
||||
socketService.onPublicMessage().subscribe((message: Message) => {
|
||||
this.addMessage(message);
|
||||
});
|
||||
socketService.onSystemMessage().subscribe((message: SystemMessage) => {
|
||||
this.addSystemMessage(message);
|
||||
})
|
||||
}
|
||||
|
||||
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', () => {
|
||||
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>();
|
||||
|
||||
constructor(public character: string,
|
||||
public user: string,
|
||||
message: string) {
|
||||
this.messages.push(message);
|
||||
protected constructor() {
|
||||
this.timestamp = new Date();
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
<mat-card-title class="character">
|
||||
{{entry.character}}
|
||||
<div class="timestamp">23:31</div>
|
||||
<div class="timestamp">{{entry.timestamp | date:'HH:mm' }}</div>
|
||||
</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-content>
|
||||
<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>
|
||||
</mat-card-content>
|
||||
</mat-card>
|
||||
@@ -1,20 +1,20 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { EntryComponent } from './entry.component';
|
||||
import { MessagesComponent } from './messages.component';
|
||||
|
||||
describe('EntryComponent', () => {
|
||||
let component: EntryComponent;
|
||||
let fixture: ComponentFixture<EntryComponent>;
|
||||
let component: MessagesComponent;
|
||||
let fixture: ComponentFixture<MessagesComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ EntryComponent ]
|
||||
declarations: [ MessagesComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(EntryComponent);
|
||||
fixture = TestBed.createComponent(MessagesComponent);
|
||||
component = fixture.componentInstance;
|
||||
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 {SocketService} from '../../socket/socket.service';
|
||||
import {Message} from '../message';
|
||||
import {Events} from '../../socket/events-enum';
|
||||
|
||||
import { Message } from '../message';
|
||||
import { Events } from '../../socket/events-enum';
|
||||
import { SocketService } from '../../socket/socket.service';
|
||||
import { AccountService } from '../../account/account.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-input',
|
||||
@@ -11,11 +13,15 @@ import {Events} from '../../socket/events-enum';
|
||||
export class InputComponent implements OnInit {
|
||||
|
||||
onEnter(value: string): void {
|
||||
const message = new Message('Aangular Frontend', value);
|
||||
this.socketService.send(Events.publicMessage, message);
|
||||
if (value.length > 0) {
|
||||
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 {
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
export class Message {
|
||||
|
||||
constructor(public sender: string,
|
||||
public eyes?: Array<number>;
|
||||
|
||||
public result?: number;
|
||||
|
||||
constructor(public character: string,
|
||||
public user: 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 { HttpClient } from '@angular/common/http';
|
||||
import {SocketService} from '../socket/socket.service';
|
||||
import {SocketService} from '../../socket/socket.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-test',
|
||||
@@ -1,3 +1,4 @@
|
||||
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 { Events } from './events-enum';
|
||||
import { SystemMessage } from '../chat/entry/entry';
|
||||
import { Message } from '../chat/message';
|
||||
|
||||
const SERVER_URL = 'http://localhost:5005'
|
||||
|
||||
@@ -26,12 +28,21 @@ export class SocketService implements OnInit {
|
||||
this.socket.emit(event, message);
|
||||
}
|
||||
|
||||
public onTestMessage(): Observable<any> {
|
||||
return new Observable<any>(observer => {
|
||||
public onPublicMessage(): Observable<Message> {
|
||||
return new Observable<Message>(observer => {
|
||||
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() {
|
||||
this.initSocket();
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ export class FakeBackend implements HttpInterceptor {
|
||||
return ok({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
character: user.character,
|
||||
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.character = 'placeholder';
|
||||
users.push(user);
|
||||
localStorage.setItem('users', JSON.stringify(users));
|
||||
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 = {
|
||||
production: false,
|
||||
apiUrl: 'http://localhost:4200',
|
||||
apiUrl: 'http://localhost:5005',
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user