Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
game-server-flip-jump
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Gia Vương
game-server-flip-jump
Commits
2cc5435c
Commit
2cc5435c
authored
Aug 16, 2025
by
Vũ Gia Vương
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update try catch, get user by userId
parent
0ab5c370
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
385 additions
and
305 deletions
+385
-305
networkCtrl.js
dist/Controller/networkCtrl.js
+38
-28
User.js
dist/Model/User.js
+3
-1
index.js
dist/index.js
+6
-0
socket.js
dist/socket.js
+142
-116
networkCtrl.ts
src/Controller/networkCtrl.ts
+32
-24
User.ts
src/Model/User.ts
+12
-13
index.ts
src/index.ts
+9
-1
socket.ts
src/socket.ts
+143
-122
No files found.
dist/Controller/networkCtrl.js
View file @
2cc5435c
...
...
@@ -25,36 +25,47 @@ let matchId = '';
let
eventId
=
null
;
function
startGameApi
(
data
)
{
return
__awaiter
(
this
,
void
0
,
void
0
,
function
*
()
{
timeStart
=
new
Date
().
getTime
();
userId
=
(
Math
.
random
()
*
100
>>
0
)
+
10
;
matchId
=
`
${
userId
}
-
${
GAME_CODE
}
-
${
timeStart
}
`
;
data
=
{
matchId
,
gameCode
:
GAME_CODE
,
};
const
result
=
yield
callApi
(
URL_START_GAME
,
'POST'
,
data
);
return
result
;
try
{
timeStart
=
new
Date
().
getTime
();
userId
=
(
Math
.
random
()
*
100
>>
0
)
+
10
;
matchId
=
`
${
userId
}
-
${
GAME_CODE
}
-
${
timeStart
}
`
;
data
=
{
matchId
,
gameCode
:
GAME_CODE
,
};
const
result
=
yield
callApi
(
URL_START_GAME
,
'POST'
,
data
);
return
result
;
}
catch
(
error
)
{
console
.
log
(
'error'
,
error
);
}
});
}
function
endGameApi
(
user
)
{
return
__awaiter
(
this
,
void
0
,
void
0
,
function
*
()
{
const
playedSeconds
=
(
new
Date
().
getTime
()
-
timeStart
)
/
1
e3
;
const
details
=
user
.
history
.
map
(
h
=>
({
level
:
h
.
tower
,
score
:
h
.
score
,
timeStart
:
h
.
timeStart
,
timePlayed
:
h
.
timePlayed
/
1
e3
,
}));
const
params
=
{
matchId
,
gameCode
:
GAME_CODE
,
eventId
,
playedSeconds
,
score
:
user
.
totalScore
,
gameLevel
:
user
.
towerNumber
-
1
,
details
};
const
res
=
yield
callApi
(
URL_END_GAME
,
'POST'
,
params
);
try
{
const
playedSeconds
=
(
new
Date
().
getTime
()
-
timeStart
)
/
1
e3
;
const
details
=
user
.
history
.
map
(
h
=>
({
level
:
h
.
tower
,
score
:
h
.
score
,
timeStart
:
h
.
timeStart
,
timePlayed
:
h
.
timePlayed
/
1
e3
,
}));
const
params
=
{
matchId
,
gameCode
:
GAME_CODE
,
eventId
,
playedSeconds
,
score
:
user
.
totalScore
,
gameLevel
:
user
.
towerNumber
-
1
,
details
};
const
res
=
yield
callApi
(
URL_END_GAME
,
'POST'
,
params
);
return
res
;
}
catch
(
error
)
{
console
.
log
(
'error'
,
error
);
}
});
}
function
callApi
(
url
,
method
,
data
)
{
...
...
@@ -69,11 +80,10 @@ function callApi(url, method, data) {
body
:
JSON
.
stringify
(
data
),
});
const
result
=
yield
res
.
json
();
console
.
log
(
url
,
result
.
code
,
result
.
message
);
return
(
result
===
null
||
result
===
void
0
?
void
0
:
result
.
code
)
==
'success'
;
}
catch
(
error
)
{
console
.
log
(
'error call
a
pi: '
,
error
);
console
.
log
(
'error call
A
pi: '
,
error
);
}
});
}
dist/Model/User.js
View file @
2cc5435c
...
...
@@ -5,7 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object
.
defineProperty
(
exports
,
"__esModule"
,
{
value
:
true
});
const
v2_1
=
__importDefault
(
require
(
"./v2"
));
class
User
{
constructor
(
token
)
{
constructor
(
token
,
id
)
{
this
.
id
=
''
;
this
.
token
=
''
;
this
.
towerNumber
=
0
;
this
.
direction
=
1
;
...
...
@@ -21,6 +22,7 @@ class User {
this
.
isEndGame
=
true
;
this
.
history
=
[];
this
.
token
=
token
;
this
.
id
=
id
;
this
.
reset
();
}
reset
()
{
...
...
dist/index.js
View file @
2cc5435c
...
...
@@ -21,3 +21,9 @@ const io = new socket_io_1.Server(server, {
server
.
listen
(
PORT
,
()
=>
{
console
.
log
(
`Server is running on port
${
PORT
}
!`
);
});
process
.
on
(
'uncaughtException'
,
(
err
)
=>
{
console
.
error
(
'Uncaught Exception:'
,
err
);
});
process
.
on
(
'unhandledRejection'
,
(
reason
,
promise
)
=>
{
console
.
error
(
'Unhandled Rejection at:'
,
promise
,
'reason:'
,
reason
);
});
dist/socket.js
View file @
2cc5435c
This diff is collapsed.
Click to expand it.
src/Controller/networkCtrl.ts
View file @
2cc5435c
...
...
@@ -15,38 +15,47 @@ let matchId = '';
let
eventId
:
string
|
null
=
null
;
export
async
function
startGameApi
(
data
:
any
)
{
timeStart
=
new
Date
().
getTime
();
userId
=
(
Math
.
random
()
*
100
>>
0
)
+
10
;
matchId
=
`
${
userId
}
-
${
GAME_CODE
}
-
${
timeStart
}
`
;
try
{
timeStart
=
new
Date
().
getTime
();
userId
=
(
Math
.
random
()
*
100
>>
0
)
+
10
;
matchId
=
`
${
userId
}
-
${
GAME_CODE
}
-
${
timeStart
}
`
;
data
=
{
matchId
,
gameCode
:
GAME_CODE
,
};
const
result
=
await
callApi
(
URL_START_GAME
,
'POST'
,
data
);
return
result
;
data
=
{
matchId
,
gameCode
:
GAME_CODE
,
};
const
result
=
await
callApi
(
URL_START_GAME
,
'POST'
,
data
);
return
result
;
}
catch
(
error
)
{
console
.
log
(
'error'
,
error
)
}
}
export
async
function
endGameApi
(
user
:
User
)
{
const
playedSeconds
=
(
new
Date
().
getTime
()
-
timeStart
)
/
1
e3
;
const
details
=
user
.
history
.
map
(
h
=>
({
try
{
const
playedSeconds
=
(
new
Date
().
getTime
()
-
timeStart
)
/
1
e3
;
const
details
=
user
.
history
.
map
(
h
=>
({
level
:
h
.
tower
,
score
:
h
.
score
,
timeStart
:
h
.
timeStart
,
timePlayed
:
h
.
timePlayed
/
1
e3
,
}));
}));
const
params
=
{
matchId
,
gameCode
:
GAME_CODE
,
eventId
,
playedSeconds
,
score
:
user
.
totalScore
,
gameLevel
:
user
.
towerNumber
-
1
,
details
}
const
params
=
{
matchId
,
gameCode
:
GAME_CODE
,
eventId
,
playedSeconds
,
score
:
user
.
totalScore
,
gameLevel
:
user
.
towerNumber
-
1
,
details
}
const
res
=
await
callApi
(
URL_END_GAME
,
'POST'
,
params
);
const
res
=
await
callApi
(
URL_END_GAME
,
'POST'
,
params
);
return
res
;
}
catch
(
error
)
{
console
.
log
(
'error'
,
error
)
}
}
async
function
callApi
(
url
:
string
,
method
:
string
,
data
:
any
)
{
...
...
@@ -60,10 +69,9 @@ async function callApi(url: string, method: string, data: any) {
body
:
JSON
.
stringify
(
data
),
});
const
result
=
await
res
.
json
();
console
.
log
(
url
,
result
.
code
,
result
.
message
);
return
result
?.
code
==
'success'
;
}
catch
(
error
)
{
console
.
log
(
'error call
a
pi: '
,
error
);
console
.
log
(
'error call
A
pi: '
,
error
);
}
}
src/Model/User.ts
View file @
2cc5435c
import
v2
from
"./v2"
;
class
User
{
public
id
:
string
=
''
;
public
token
:
string
=
''
;
public
towerNumber
:
number
=
0
;
public
direction
:
number
=
1
;
...
...
@@ -10,17 +11,18 @@ class User {
public
totalScore
:
number
=
0
;
public
combo
:
number
=
0
;
public
curBlock
:
v2
=
new
v2
(
0
,
0
);
public
nextBlock
:
v2
=
new
v2
(
0
,
0
);
public
screenPos
:
v2
=
new
v2
(
0
,
0
);
public
position
:
v2
=
new
v2
(
0
,
0
);
public
curBlock
:
v2
=
new
v2
(
0
,
0
);
public
nextBlock
:
v2
=
new
v2
(
0
,
0
);
public
screenPos
:
v2
=
new
v2
(
0
,
0
);
public
position
:
v2
=
new
v2
(
0
,
0
);
public
timeStart
:
number
=
0
;
public
isEndGame
:
boolean
=
true
;
public
history
:
History
[]
=
[];
constructor
(
token
:
string
)
{
constructor
(
token
:
string
,
id
:
string
)
{
this
.
token
=
token
;
this
.
id
=
id
;
this
.
reset
();
}
...
...
@@ -61,14 +63,6 @@ export interface IDataPassTower {
totalScore
:
number
,
}
// {
// nextBlock: user.nextBlock,
// screenPos: user.screenPos,
// direction: user.direction,
// distance2Tower: user.distance2Tower,
// towerNumber: user.towerNumber - 1,
// };
export
interface
IDataSpawnTower
{
nextBlock
:
v2
,
screenPos
:
v2
,
...
...
@@ -77,4 +71,9 @@ export interface IDataSpawnTower {
towerNumber
:
number
,
}
export
interface
IQuery
{
token
?:
string
,
userId
?:
string
}
export
default
User
;
\ No newline at end of file
src/index.ts
View file @
2cc5435c
...
...
@@ -20,4 +20,12 @@ setupSocket(io);
server
.
listen
(
PORT
,
()
=>
{
console
.
log
(
`Server is running on port
${
PORT
}
!`
);
})
});
process
.
on
(
'uncaughtException'
,
(
err
)
=>
{
console
.
error
(
'Uncaught Exception:'
,
err
);
});
process
.
on
(
'unhandledRejection'
,
(
reason
,
promise
)
=>
{
console
.
error
(
'Unhandled Rejection at:'
,
promise
,
'reason:'
,
reason
);
});
src/socket.ts
View file @
2cc5435c
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment