-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.js
More file actions
192 lines (151 loc) · 4.98 KB
/
Token.js
File metadata and controls
192 lines (151 loc) · 4.98 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Map with WeakRef-wrapped values and a FinalizationRegistry
// Created by Justin Bradform and used under MIT License
// Looking more and more like putting all of this in a proper yarn repo with webpacker may be useful...
class WeakValueMap extends Map {
constructor() {
super()
this.finalizer = new FinalizationRegistry(
key => super.delete(key)
)
}
get(key) {
const ref = super.get(key)
const value = ref && ref.deref()
// remove entry if reference is no longer valid
if (ref && !value) super.delete(key)
return value
}
#unregister(ref) {
const value = ref && ref.deref()
if (value) this.finalizer.unregister(value)
}
set(key, value) {
// unregister finalizer if an existing value
this.#unregister(super.get(key))
// register finalizer for the new value
this.finalizer.register(value, key, value)
// store a weak reference to the value
super.set(key, new WeakRef(value))
return this
}
delete(key) {
// unregister finalizer if an existing value
this.#unregister(super.get(key))
return super.delete(key)
}
clear() {
// unregister finalizer for existing values
for (const ref of super.values()) this.#unregister(ref)
return super.clear()
}
}
function getPlayerName()
{
return ;
}
class Token
{
static #wm = new WeakValueMap()
constructor(maptoolToken) {
this.maptoolToken = maptoolToken;
//MapTool.chat.broadcast(this.maptoolToken.toString());
//MapTool.chat.broadcast(this.maptoolToken.getId().toString());
this.barsProxy = new Proxy({}, {
get: this.#getBarProxy.bind(this),
set: this.#setBarProxy.bind(this)
});
this.statesProxy = new Proxy({}, {
get: this.#getStateProxy.bind(this),
set: this.#setStateProxy.bind(this)
});
}
static retrieveToken(maptoolToken) {
let token = Token.#wm.get(maptoolToken);
if(token === undefined)
{
token = new Token(maptoolToken);
Token.#wm.set(maptoolToken, token);
}
return token;
}
static getTokenById(id) {
//MapTool.chat.broadcast(MapTool.tokens.getTokenByID(id).toString());
return Token.retrieveToken(MapTool.tokens.getTokenByID(id));
}
static getTokenByName(tokenName) {
let id = MTScript.evalMacro(`[r: findToken("${tokenName.replace('"','')}")]`);
if(id === "") throw "Token not found";
return Token.getTokenById(id);
}
static get selectedTokens() {
return Array.from(MapTool.tokens.getSelectedTokens()).map(mtToken => retrieveToken(mtToken));
}
static get selectedToken() {
return retrieveToken(MapTool.tokens.getSelected());
}
get x(){
return this.maptoolToken.getX();
}
get y(){
return this.maptoolToken.getY();
}
isOwner(owner){
internalOwner = owner != undefined ? owner : MTScript.evalMacro("[r: getPlayerName()]");
return this.maptoolToken.isOwner(internalOwner);
}
set x(x){
return this.maptoolToken.setX(x);
}
set y(y){
return this.maptoolToken.setY(y);
}
set notes(notes){
return this.maptoolToken.setNotes(notes);
}
get notes(){
return this.maptoolToken.getNotes();
}
hasSight(){
return this.maptoolToken.hasSight();
}
set sight(sight){
return this.maptoolToken.setSight(sight);
}
get name(){
return this.maptoolToken.getName();
}
getProperty(property){
return "" + MTScript.evalMacro(`[r: getProperty("${property.replace('"','')}", "${this.id}")]`);
//return this.maptoolToken.getProperty(property);
}
setProperty(property, value){
//TODO: Switch this method to return a proxy object. Current syntax to use is myToken.setProperty("strength", 10) final syntax will be something akin to myToken.properties.strength = 10
return this.maptoolToken.setProperty(property, value);
}
set name(name){
return this.maptoolToken.setName(name);
}
get id(){
return this.maptoolToken.getId();
}
get bars(){
return this.barsProxy;
}
#getBarProxy(target, bar, receiver){
return "" + MTScript.evalMacro(`[r: getBar("${bar.replace('"','')}", "${this.id}")]`);
}
#setBarProxy(target, bar, value, receiver) {
MTScript.evalMacro(`[r: setBar("${bar.replace('"','')}", ${value}, "${this.id}")]`);
return;
}
get states(){
return this.statesProxy;
}
#getStateProxy(target, state, receiver){
return (("" + MTScript.evalMacro(`[r: getState("${state.replace('"','')}", "${this.id}")]`)) === "1");
}
#setStateProxy(target, state, value, receiver) {
MTScript.evalMacro(`[r: setState("${state.replace('"','')}", ${value ? 1 : 0}, "${this.id}")]`);
return;
}
}