-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_handler.ts
209 lines (180 loc) · 6.89 KB
/
match_handler.ts
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
// Copyright 2020 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const moduleName = "match";
const tickRate = 5;
const maxEmptySec = 30;
const delaybetweenGamesSec = 5;
const turnTimeFastSec = 10;
const turnTimeNormalSec = 20;
const winningPositions: number[][] = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
]
interface MatchLabel {
open: number
fast: number
}
interface State {
// Match label
label: MatchLabel
// Ticks where no actions have occurred.
emptyTicks: number
// Currently connected users, or reserved spaces.
presences: {[userId: string]: nkruntime.Presence | null}
// Number of users currently in the process of connecting to the match.
joinsInProgress: number
// True if there's a game currently in progress.
playing: boolean
// Current state of the board.
board: Board
// Mark assignments to player user IDs.
marks: {[userId: string]: Mark | null}
// Whose turn it currently is.
mark: Mark
// Ticks until they must submit their move.
deadlineRemainingTicks: number
// The winner of the current game.
winner: Mark | null
// The winner positions.
winnerPositions: BoardPosition[] | null
// Ticks until the next game starts, if applicable.
nextGameRemainingTicks: number
}
const matchInit = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, params: {[key: string]: string}): {state: nkruntime.MatchState, tickRate: number, label: string} {
return {
state: { Debug: true },
tickRate: 10,
label: ""
};
};
let joinOrCreateMatch: nkruntime.RpcFunction = function (context: nkruntime.Context, logger: nkruntime.Logger, nakama: nkruntime.Nakama, payload: string): string
{
let matches: nkruntime.Match[];
const MatchesLimit = 1;
const MinimumPlayers = 1;
const MaxPlayers = 2;
var label: MatchLabel = { open: 1 , fast : 1}
matches = nakama.matchList(MatchesLimit, true, JSON.stringify(label), MinimumPlayers, MaxPlayers);
if (matches.length > 0) {
logger.debug(matches[0].matchId);
return matches[0].matchId;
}
return nakama.matchCreate(moduleName);
}
let matchJoinAttempt: nkruntime.MatchJoinAttemptFunction<State> = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: State, presence: nkruntime.Presence, metadata: {[key: string]: any}) {
// Check if it's a user attempting to rejoin after a disconnect.
if (presence.userId in state.presences) {
if (state.presences[presence.userId] === null) {
// User rejoining after a disconnect.
state.joinsInProgress++;
return {
state: state,
accept: false,
}
} else {
// User attempting to join from 2 different devices at the same time.
return {
state: state,
accept: false,
rejectMessage: 'already joined',
}
}
}
// Check if match is full.
if (connectedPlayers(state) + state.joinsInProgress >= 2) {
return {
state: state,
accept: false,
rejectMessage: 'match full',
};
}
// New player attempting to connect.
state.joinsInProgress++;
return {
state,
accept: true,
}
}
const matchJoin = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : { state: nkruntime.MatchState } | null {
presences.forEach(function (presence) {
state.presences[presence.userId] = presence;
logger.debug('%q joined Lobby match', presence.userId);
});
return {
state
};
}
let matchLeave: nkruntime.MatchLeaveFunction<State> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: State, presences: nkruntime.Presence[]) {
for (let presence of presences) {
logger.info("Player: %s left match: %s.", presence.userId, ctx.matchId);
state.presences[presence.userId] = null;
}
return {state};
}
const matchLoop = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, messages: nkruntime.MatchMessage[]) : { state: nkruntime.MatchState} | null {
logger.debug('Lobby match loop executed');
const opCode = 1234;
const message = JSON.stringify({ hello: 'world' });
const presences = null; // Send to all.
const sender = null; // Used if a message should come from a specific user.
dispatcher.broadcastMessage(opCode, message, presences, sender, true);
return {
state
};
}
let rpcHandleMatchEnd: nkruntime.RpcFunction = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string): string {
// let request = JSON.parse(payload);
logger.info( payload)
return payload;
}
let matchTerminate: nkruntime.MatchTerminateFunction<State> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: State, graceSeconds: number) {
logger.info('matchTerminate!!!!!!!!!!!!!!!!!!')
return { state };
}
let matchSignal: nkruntime.MatchSignalFunction<State> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: State) {
logger.info('matchSignal!!!!!!!!!!!!!!!!!!')
return { state };
}
function calculateDeadlineTicks(l: MatchLabel): number {
if (l.fast === 1) {
return turnTimeFastSec * tickRate;
} else {
return turnTimeNormalSec * tickRate;
}
}
function winCheck(board: Board, mark: Mark): [boolean, Mark[] | null] {
for(let wp of winningPositions) {
if (board[wp[0]] === mark &&
board[wp[1]] === mark &&
board[wp[2]] === mark) {
return [true, wp];
}
}
return [false, null];
}
function connectedPlayers(s: State): number {
let count = 0;
for(const p of Object.keys(s.presences)) {
if (p !== null) {
count++;
}
}
return count;
}