-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhalfgame.js
More file actions
129 lines (124 loc) · 3.48 KB
/
halfgame.js
File metadata and controls
129 lines (124 loc) · 3.48 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
const memit = require("./memitter").memit
class HalfGame{
constructor(set, roomID, teamScore){
this.rodi = roomID
this.players = [set.batsman, set.bowler];
this.ballCount = 0;
this.score = 0;
this.gameBalls = [];
this.numBalls = 0;
this.ballSel = [false,false];
this.balls =[null,null];
this.gameStop = false;
this.players.forEach(plr => {
if(!this.gameStop){
plr.soc.emit("game-starts")
}
})
this.players.forEach((player, idx) => {
player.soc.on("disconnect", () => {
if(!this.gameStop){
this.gameStop = true
if(this.players.includes(player)){
this.players.splice(this.players.indexOf(player), 1)
if(this.players.length > 0){
this.players[0].soc.emit("error-opp-left-team")
memit.emit("player-out-team", this.players[0], this.score, this.rodi, "left")
}
}
}
})
})
this.players.forEach((plr,idx) => {
plr.soc.on("comp-mode", () => {
if(idx == 0 && !this.gameStop){
this.players[1].soc.emit("opp-comp")
}
else if(idx == 1 && !this.gameStop){
this.players[0].soc.emit("opp-comp")
}
})
plr.soc.on("back-to-plr", () => {
if(idx == 0 && !this.gameStop){
this.players[1].soc.emit("opp-bk-plr")
}
else if(idx == 1 && !this.gameStop){
this.players[0].soc.emit("opp-bk-plr")
}
})
plr.soc.on("message-to-player-team", msg => {
if(idx == 0 && !this.gameStop){
this.players[1].soc.emit("message-from-player-team", msg)
}
else if(idx == 1 && !this.gameStop){
this.players[0].soc.emit("message-from-player-team", msg)
}
})
plr.soc.on("game-ball-team", ball => {
if(typeof ball == "number" && ball <= 6 && ball >= 1){
if(this.ballCount < 2 && !this.gameStop && !this.ballSel[idx]){
this.balls[idx] = ball
this.ballSel[idx] = true
this.ballCount++
this.gameBall(this.balls)
}
else if(this.ballCount == 2 && !this.gameStop && !this.ballSel[idx]){
this.balls = [null, null]
this.ballSel = [false, false]
this.ballCount = 0
this.balls[idx] = ball
this.ballSel[idx] = true
this.ballCount++
this.gameBall(this.balls)
}
}
else{
plr.soc.emit('invalid-input')
}
})
})
}
gameBall(x){
if(!x.includes(null) && !this.gameStop){
this.numBalls++
this.ballSel = [false,false]
var out = false
if(x[0] == x[1]){
memit.emit("player-out-team", this.players[0], this.score, this.rodi, "out")
this.players[0].soc.emit("you-bats-out", this.score)
this.players[1].soc.emit("opp-bats-out", this.score, this.players[0].username)
out = true
this.gameStop = true
}
else if(x[0] != x[1]){
this.score += x[0]
}
var ball = {
ball: this.numBalls,
batsman: 0,
bowler: 1,
p1_ball: x[0],
p2_ball: x[1],
out: out
}
this.players.forEach((player,idx) => {
if(idx == 0){
player.soc.emit("both-choose-ok-team", x[0], x[1], this.numBalls, this.score)
}
else if(idx == 1){
player.soc.emit("both-choose-ok-team", x[1], x[0], this.numBalls, this.score, "no")
}
})
this.gameBalls.push(ball)
}
else if(!this.gameStop && x.includes(null)){
var indx = x.indexOf(null)
this.players.forEach((player,idx)=> {
if(indx != idx){
player.soc.emit("wait-for-opp", this.balls[idx], this.numBalls+1)
}
})
}
}
}
module.exports = HalfGame;