aboutsummaryrefslogtreecommitdiff
path: root/webAO/packets/handlers/handleBN.ts
blob: 769a0dc55709ed71792d8dbb1057c464bb228ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { client } from "../../client";
import { AO_HOST } from "../../client/aoHost";
import { safeTags } from "../../encoding";
import { updateBackgroundPreview } from "../../dom/updateBackgroundPreview";
import { getIndexFromSelect } from "../../dom/getIndexFromSelect";
import { switchPanTilt } from "../../dom/switchPanTilt";
import transparentPng from "../../constants/transparentPng";
import fileExists from "../../utils/fileExists";

function setBackgroundImage(elementid: string, bgname: string, bgpart: string): boolean {

  let url;
  let success = false;
  for (const extension of client.background_extensions) {
    url = `${AO_HOST}background/${encodeURI(bgname.toLowerCase())}/${bgpart}${extension}`;

    const exists = fileExists(url);

    if (exists) {
      success = true;
      break;
    }
  }
  if (success)
    (<HTMLImageElement>document.getElementById(elementid)).src = url;
  else
    (<HTMLImageElement>document.getElementById(elementid)).src = transparentPng;
  return success;
}


/**
 * Handles a background change.
 * @param {Array} args packet arguments
 */

export const handleBN = (args: string[]) => {
  const bgFromArgs = safeTags(args[1]);
  client.viewport.setBackgroundName(bgFromArgs);
  const bgfolder = client.viewport.getBackgroundFolder();
  const bg_index = getIndexFromSelect(
    "bg_select",
    client.viewport.getBackgroundName(),
  );
  (<HTMLSelectElement>document.getElementById("bg_select")).selectedIndex =
    bg_index;
  updateBackgroundPreview();
  if (bg_index === 0) {
    (<HTMLInputElement>document.getElementById("bg_filename")).value =
      client.viewport.getBackgroundName();
  }


  setBackgroundImage("bg_preview",args[1],"defenseempty")

  setBackgroundImage("client_def_bench",args[1],"defensedesk")
  setBackgroundImage("client_wit_bench",args[1],"stand")
  setBackgroundImage("client_pro_bench",args[1],"prosecutiondesk")

  setBackgroundImage("client_court_def",args[1],"defenseempty")
  setBackgroundImage("client_court_wit",args[1],"witnessempty")
  setBackgroundImage("client_court_pro",args[1],"prosecutorempty")

  setBackgroundImage("client_court_deft",args[1],"transition_def")
  setBackgroundImage("client_court_prot",args[1],"transition_pro")
  
  if(setBackgroundImage("client_court",args[1],"court")) {
    (<HTMLInputElement>document.getElementById("client_pantilt")).checked =
        true;
      switchPanTilt();
  }

  if (client.charID === -1) {
    client.viewport.set_side({
      position: "jud",
      showSpeedLines: false,
      showDesk: true,
    });
  } else {
    client.viewport.set_side({
      position: client.chars[client.charID].side,
      showSpeedLines: false,
      showDesk: true,
    });
  }
};