Angular web app for entering Techblox commands
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

51 lines
1.8KB

  1. import { Component, ElementRef, ViewChild } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { FormControl } from '@angular/forms';
  4. @Component({
  5. selector: 'app-root',
  6. templateUrl: './app.component.html',
  7. styleUrls: ['./app.component.scss']
  8. })
  9. export class AppComponent {
  10. title = 'TBConsoleClient';
  11. logMessages = '';
  12. commandControl: FormControl = new FormControl('');
  13. commands: { command: string, line: string }[] = [];
  14. displayedCommands: { command: string, line: string }[] = [];
  15. @ViewChild('logMessagesContainer') logMessagesView: ElementRef;
  16. constructor(private http: HttpClient) {
  17. this.getCommandList();
  18. this.commandControl.valueChanges.subscribe(commandText => {
  19. this.displayedCommands = this.commands.filter(cmd => cmd.command.toLowerCase().startsWith(commandText.toLowerCase()));
  20. })
  21. }
  22. async sendCommand() {
  23. try {
  24. const command = this.commandControl.value;
  25. this.commandControl.setValue('');
  26. const res = await this.http.post('http://localhost:8019/command', command, {responseType: 'text'}).toPromise();
  27. this.logMessages += res + "\n";
  28. } catch (e) {
  29. if (e.status == 0)
  30. this.logMessages += "Failed to contact mod! Make sure it is running and listening.\n";
  31. else
  32. this.logMessages += e.message + "\n";
  33. }
  34. await this.wait(50);
  35. this.logMessagesView.nativeElement.scrollTop = this.logMessagesView.nativeElement.scrollHeight;
  36. }
  37. async getCommandList() {
  38. const res = await this.http.post('http://localhost:8019/commands', '', {responseType: 'text'}).toPromise();
  39. this.commands = res.split('\n').map(cmd => ({command: cmd.split(' - ')[0], line: cmd}));
  40. }
  41. wait(ms) {
  42. return new Promise(resolve => setTimeout(resolve, ms));
  43. }
  44. }