UNPKG

4.87 kBPlain TextView Raw
1/// <reference path="gameplayerbase.ts" />
2/// <reference path="helper.ts" />
3
4
5
6class GameTableBase<TGamePlayer extends GamePlayerBase> {
7
8 public ActivePlayer: TGamePlayer;
9
10 public Players: TGamePlayer[];
11
12 public Status = TableStatus.New;
13
14
15 constructor(private GamePlayerClass, public Channel = '', public Mode = 0, public MaxPlayersCount = 2, public IsVIPTable = false) {
16 this.Players = [];
17 }
18
19
20 public join(user, ipaddress, channel, mode) {
21
22 var player = this.Players.filter(p => p.UserID == user.UserID)[0];
23 if (!player)
24 player = new this.GamePlayerClass(user.UserID, ipaddress, user.IsVIP, true);
25
26 player.IsOnline = true;
27
28 switch (this.Status) {
29 // ახალი მაგიდაა და მოთამაშეები ივსება ჯერ
30 case TableStatus.New:
31 {
32 if (!this.Players.contains(player)) {
33 this.Players.push(player);
34 this.playersChanged();
35 }
36
37 // თუ 2 კაცი შეიკრიბა თამაში დაიწყოს
38 if (this.Players.length == this.MaxPlayersCount) {
39 this.start();
40 }
41 }
42 break;
43
44 // დაწყებული იყო თამაში და აგრძელებს თამაშს
45 case TableStatus.Started:
46 case TableStatus.StartedWaiting:
47 {
48 if (!this.Players.contains(player)) return;
49
50 this.Status = TableStatus.Started;
51 this.playersChanged();
52 }
53 break;
54
55 // სხვა ყველა შემთხვევებში ვუშვებთ უკან, ახალი მოთამაშე ვერ შემოვა
56 default:
57 return;
58 }
59 }
60
61 public leave(userid: number) {
62
63 var player = this.Players.filter(p=> p.UserID == userid)[0];
64 if (player == null) return;
65
66 player.IsOnline = false;
67
68
69 switch (this.Status) {
70 // თუ ჯერ არ დაწყებულა თამაში, იშლება მოთამაშე სიიდან ეგრევე
71 case TableStatus.New:
72 {
73 this.Players.remove(player);
74 this.playersChanged();
75 }
76 break;
77
78 // თუ დაწყებული იყო თამაში, მაგიდის სტატუსი იცვლება და მაგიდაზე დარჩენილ
79 // მოთამაშეს ეგზავნება მაგიდის განახლებული მდგომარეობა
80 case TableStatus.Started:
81 {
82 // თუ ორივე მოთამაშემ არ გააკეთა ერთ სვლა მაინც, მაგიდის სტატუსი გადადის ახალზე
83 if (this.Players.filter(p => p.HasAnyMoveMade).length != 2) {
84
85 this.Status = TableStatus.New;
86 this.Players.remove(player);
87 this.playersChanged();
88 break;
89 }
90
91 this.Status = TableStatus.StartedWaiting;
92 this.playersChanged();
93 }
94 break;
95
96 case TableStatus.Finished:
97 {
98 this.Players.remove(player);
99 this.playersChanged();
100 }
101 break;
102
103 // სხვა ყველა შემთხვევაში, უბრალოდ გამოვდივართ
104 default:
105 return;
106 }
107 }
108
109
110 start() {
111
112 }
113
114 finish() {
115
116 }
117
118 playersChanged() {
119
120 }
121
122
123 send(command: string, ...params: any[]) {
124
125 params.unshift(command);
126
127 this.Players.forEach(p => p.send.apply(p, params));
128 }
129
130 getNextPlayer(player?: TGamePlayer): TGamePlayer {
131
132 if (this.Players.length <= 1) return;
133
134 if (!player)
135 player = this.ActivePlayer;
136
137 if (!player) return;
138
139
140 var index = this.Players.indexOf(player);
141
142 return this.Players[index < this.Players.length - 1 ? ++index : 0];
143 }
144}
145
146enum TableStatus {
147 New,
148 Started,
149 StartedWaiting,
150 Finished
151}
\No newline at end of file