forked from bubububaoshe/bubububaoshe.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtricks.js
More file actions
371 lines (369 loc) · 11.6 KB
/
tricks.js
File metadata and controls
371 lines (369 loc) · 11.6 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
class ObtainVector{
// note: player is the one who plays the current turn. it may be AI
// if player is AI, then oppo is the user
// user refer to user player of the client
constructor(){}
init(player, handChar, poolChar){
this.player = player;
this.preScore = player.score;
this.charScoreInc = 0;
this.comboCount = 0;
this.chars = [handChar, poolChar];
this.copyChars =[null, null];
this.swapChars = [null, null];
// chars indeed tabled by the current player
this.playerTableChars = [handChar, poolChar];
// bans of playerTableChars
this.banChars = [null, null];
this.allChars = [handChar, poolChar];
}
getNextTrick(type){
var so = this.getTrickSubjectObjectArrays(type);
var subjects = so[0], objects = so[1];
for(var i=0; i<subjects.length; i++){
var trick = subjects[i].getTrick(type);
if(trick != null && objects[i] == null){console.log("[计划释放技能]"+subjects[i].name+":"+trick.constructor.name);
return trick;
}
}
return null;
}
getTrickSubjectObjectArrays(type){
var subjects, objects;
switch(type){
case "CopyTrick":
subjects = this.chars;
objects = this.copyChars;
break;
case "SwapTrick":
subjects = this.chars;
objects = this.swapChars;
break;
case "UnnamedBanTrick":
subjects = this.playerTableChars;
objects = this.banChars;
break;
case "RevealTrick":
case "DealTrick":
subjects = this.allChars;
objects = null;
break;
}
return [subjects, objects];
}
performReveal(){
var chars = this.getTrickSubjectObjectArrays("RevealTrick")[0];
for(var i=0; i<chars.length; i++){
chars[i].performTricks("RevealTrick");
}
}
performDeal(){
var chars = this.getTrickSubjectObjectArrays("DealTrick")[0];
for(var i=0; i<chars.length; i++){
var nextdeal = chars[i].performTricks("DealTrick");
if(nextdeal != null)
return nextdeal;
}
return null;
}
trickSelector(type){
// only cards obtained by user may invoke a selection panel, AI would just select by algorithm
// show unfinished selector for tricks of <type>
// return true if selector is invoked, false otherwise.
var so = this.getTrickSubjectObjectArrays(type);
var subjects = so[0], objects = so[1];
for(var i=0; i<subjects.length; i++){
var trick = subjects[i].getTrick(type);
if(trick != null && objects[i] == null)
if(trick.selectTarget())
return true;
else{
trick.performTrick(null);
return this.trickSelector(type);
}
}
return false;
}
getHandChar(){return this.chars[0];}
getPoolChar(){return this.chars[1];}
getTrickPair(type, idx){
// preassumption: idx is valid
var so = this.getTrickSubjectObjectArrays(type);
var subjects = so[0], objects = so[1];
if(idx == 0)
if(objects[0] != null)
return [subjects[0], objects[0]];
else
return [subjects[1], objects[1]];
else
return [subjects[1], objects[1]];;
}
getLastTrickPair(type){
// returns the latest trick pair
var so = this.getTrickSubjectObjectArrays(type);
var subjects = so[0], objects = so[1];
for(var i=objects.length-1; i>=0; i--)
if(objects[i] != null)
return [subjects[i], objects[i]];
return null;
}
getTrickCount(type){
var objects = this.getTrickSubjectObjectArrays(type)[1];
return (objects[0]==null?0:1) (objects[1]==null?0:1);
}
setTrickTarget(type, target){
var so = this.getTrickSubjectObjectArrays(type);
var subjects = so[0], objects = so[1];
var trick = null;
for(var i=0; i<subjects.length; i++){
trick = subjects[i].getTrick(type);
if(trick != null && objects[i] == null){
if(type == "SwapTrick" && target != null){
var oppo = target.owner;
var preScore = oppo.score;
objects[i] = trick.performTrick(target);
this.playerTableChars[i] = objects[i];
this.allChars.unshift(objects[i]);
subjects[i].swapped = true;
objects[i].swapped = true;
oppo.addTableChar(subjects[i]);
messenger.animeScoreInc(oppo.id, preScore, oppo.score-preScore);
}
else
objects[i] = trick.performTrick(target);
return;
}
}
if(trick == null)
console.log("Invalid Trick Target: " + target.name + "[" + type +"]");
}
}
class Trick {
constructor(description) {
this.description = description;
this.disabled = false;
}
enabled(){return !this.disabled;}
performTrick(para) {
console.log(this.owner.name + ": " + this.description);
}
getDesc(){
return this.description;
}
recalculate(player){}
setOwner(char){this.owner = char;}
selectTarget(msg, controllerFunc){
//show target selection panel
//returns false if no valid target is found
if(this.hasValidTarget(model.player0)) {
oppoinfo.showSelectionPanel(this, msg, controllerFunc);
return true;
}
return false;
}
clone(){return null;}
isValidTarget(char){
return true;
}
hasValidTarget(oppo){
var chars = oppo.table.characters;
for(var i=0; i<chars.length; i++)
if(this.isValidTarget(chars[i]))
return true;
return false;
}
}
class SwapTrick extends Trick{
constructor(){
super("交换对方任意一张牌");
}
hasValidTarget(oppo){
if(oppo.table.getSize() > 0)
return true;
return false;
}
selectTarget(){return super.selectTarget("与之交换", controller.selectSwap);}
clone(){
return new SwapTrick();
}
performTrick(char){
if(char != null){console.log(this.owner.name + " <--> " + char.name);
var oppo = obtainVector.player.id==0?model.player1:model.player0;
oppo.removeTableChar(char);
char = char.getSpecial(obtainVector.player.specials);
}
this.disabled = true;
return char;
}
}
class RevealTrick extends Trick{
// reveal cardNumber of cards in opponent's hand
constructor(number){
var nos = ["零", "一", "两", "三", "四", "五", "六", "七", "八", "九", "十"];
super("随机翻开对手手牌"+nos[number]+"张");
this.cardNumber = number; //number of cards to reveal
}
performTrick() {console.log(this.owner.name+" 翻牌");
//if player performs reveal trick, opponent's hand cards are revealed
//if opponent performs reveal trick, player doesn't feel a thing
if(this.owner.owner == model.player1)
model.player0.hand.view.reveal(this.cardNumber);
}
clone(){return new RevealTrick(this.cardNumber);}
}
class DealTrick extends Trick{
/* definite = true:
the next deal is definitely one from <names> (if applicable)
definite = false;
the possibility that the next deal is from <names> is added by 1/2
*/
constructor(description, names, definite){
super(description);
this.names = names;
this.definite = definite;
}
performTrick() {
//returns a char from <names> with <probility>
var cans = [];
var repo = model.commonRepository.characters;
for(var i=0; i<this.names.length; i++)
for(var j=0; j<repo.length; j++)
if(this.names[i] == repo[j].name)
cans.push(repo[j]);
if(cans.length > 0 && (this.definite || getRandom(2)==1)){
console.log(this.owner.name+" 发牌");
return cans[getRandom(cans.length)];
}
console.log(this.owner.name+"放弃发牌");
return null;
}
clone(){return new DealTrick(this.description, this.names, this.definite);}
}
class ComboTrick extends Trick{
// add <bonus> to combo with <cname>
constructor(cname, bonus){
if(cname != null)
super(cname + "的分数增加" + bonus + "分");
else
super("自身能组成的组合每个增加" + bonus + "分");
this.comboName = cname;
this.bonus = bonus;
}
performTrick(combo) {
// add bonus to <combo> if applicable
// preassumption: this combo contains the trick owner
if(this.comboName == null || this.comboName == combo.getName()){console.log(this.owner.name+" 加持 "+combo.getName());
return this.bonus;}
return 0;
}
clone(){return new ComboTrick(this.comboName, this.bonus);}
}
class CharTrick extends Trick{
// add <bonus> to tabled char with <cname>
// note: only benefit chars on one's own side
constructor(cname, bonus){
super("己方" + cname + "的分数增加" + bonus + "分");
this.charName = cname;
this.bonus = bonus;
}
performTrick(player) {
/* check the players latest tabled char
if it's me, check all the tabled chars and add bonus if applicable
if it's not me, check the last tabled char and add bonus if applicable
*/
var chars = player.table.characters;
var last = chars[chars.length -1];
if(last == this.owner) {
for(var i=0; i<chars.length; i++)
if(chars[i].name == this.charName){console.log(this.owner.name+" 加持 "+this.charName);
player.score += this.bonus;
return;
}
}
else {
if(last.name == this.charName){console.log(this.owner.name+" 加持 "+this.charName);
player.score += this.bonus;}
}
}
recalculate(player){
var chars = player.table.characters;
for(var i=0; i<chars.length; i++)
if(chars[i].name == this.charName){
player.score += this.bonus;
return;
}
}
clone(){return new CharTrick(this.charName, this.bonus);}
}
class UnnamedBanTrick extends Trick{
constructor(group, names){
if(group == null)
super("禁用对方任意一张珍稀牌的技能");
else {
super("禁用对方任意一张" + group + "珍稀牌的技能");
this.group = group;
this.names = names;
}
}
isValidTarget(char){
if(this.names == null)
return char.getTrick("ComboTrick,CharTrick,NamedBanTrick")!=null;
else
return this.names.includes(char.name) && char.getTrick("ComboTrick,CharTrick,NamedBanTrick")!=null;
}
selectTarget(){return super.selectTarget("禁用其加分技能", controller.selectBan);}
performTrick(char){
if(char != null){
console.log(this.owner.name + " 禁用 " + char.name);
char.disabled = true;
char.owner.recalculate(true);
}
else
console.log(this.owner.name + " 无禁用对象");
this.disabled = true; //only works once
return char;
}
clone(){return new UnnamedBanTrick(this.group, this.names);}
}
class NamedBanTrick extends Trick{
constructor(name){
super("禁用对方"+name+"珍稀牌的技能");
this.targetName = name;
}
performTrick(char){
if(char.name == this.targetName && !char.disabled) {
console.log(this.owner.name +" 禁用 " + char.name);
char.disabled = true;
this.disabled = true; //disabled after it ban's the target
return true;
}
return false;
}
clone(){return new NamedBanTrick(this.targetName);}
}
class CopyTrick extends Trick{
constructor(){
super("复制对方任意一张珍稀牌的技能");
}
isValidTarget(char){
if(!char.isSpecial())
return false;
var noncopy = 0;
for(var i=0; i<char.tricks.length; i++)
if(char.tricks[i].constructor.name != "CopyTrick")
noncopy ++;
return noncopy > 0;
}
selectTarget(){return super.selectTarget("复制其所有技能", controller.selectCopy);}
performTrick(char){
if(char != null){
console.log(this.owner.name + " 复制 " + char.name);
var tricks = char.tricks;
for(var i=0; i<tricks.length; i++)
if(tricks[i].constructor.name != "CopyTrick")
this.owner.addTrick(tricks[i].clone());
}
this.disabled = true;
return char;
}
}