Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dima Krasner
Sharkey
Commits
1a01a851
Commit
1a01a851
authored
1 year ago
by
syuilo
Browse files
Options
Downloads
Patches
Plain Diff
perf(reversi): improve performance of reversi backend
parent
3ff229af
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/backend/src/core/ReversiService.ts
+92
-123
92 additions, 123 deletions
packages/backend/src/core/ReversiService.ts
with
92 additions
and
123 deletions
packages/backend/src/core/ReversiService.ts
+
92
−
123
View file @
1a01a851
...
@@ -12,18 +12,14 @@ import { IsNull } from 'typeorm';
...
@@ -12,18 +12,14 @@ import { IsNull } from 'typeorm';
import
type
{
import
type
{
MiReversiGame
,
MiReversiGame
,
ReversiGamesRepository
,
ReversiGamesRepository
,
UsersRepository
,
}
from
'
@/models/_.js
'
;
}
from
'
@/models/_.js
'
;
import
type
{
MiUser
}
from
'
@/models/User.js
'
;
import
type
{
MiUser
}
from
'
@/models/User.js
'
;
import
{
DI
}
from
'
@/di-symbols.js
'
;
import
{
DI
}
from
'
@/di-symbols.js
'
;
import
{
bindThis
}
from
'
@/decorators.js
'
;
import
{
bindThis
}
from
'
@/decorators.js
'
;
import
{
MetaService
}
from
'
@/core/MetaService.js
'
;
import
{
CacheService
}
from
'
@/core/CacheService.js
'
;
import
{
CacheService
}
from
'
@/core/CacheService.js
'
;
import
{
UserEntityService
}
from
'
@/core/entities/UserEntityService.js
'
;
import
{
UserEntityService
}
from
'
@/core/entities/UserEntityService.js
'
;
import
type
{
GlobalEvents
}
from
'
@/core/GlobalEventService.js
'
;
import
{
GlobalEventService
}
from
'
@/core/GlobalEventService.js
'
;
import
{
GlobalEventService
}
from
'
@/core/GlobalEventService.js
'
;
import
{
IdService
}
from
'
@/core/IdService.js
'
;
import
{
IdService
}
from
'
@/core/IdService.js
'
;
import
type
{
Packed
}
from
'
@/misc/json-schema.js
'
;
import
{
NotificationService
}
from
'
@/core/NotificationService.js
'
;
import
{
NotificationService
}
from
'
@/core/NotificationService.js
'
;
import
{
Serialized
}
from
'
@/types.js
'
;
import
{
Serialized
}
from
'
@/types.js
'
;
import
{
ReversiGameEntityService
}
from
'
./entities/ReversiGameEntityService.js
'
;
import
{
ReversiGameEntityService
}
from
'
./entities/ReversiGameEntityService.js
'
;
...
@@ -58,7 +54,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -58,7 +54,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
@
bindThis
@
bindThis
private
async
cacheGame
(
game
:
MiReversiGame
)
{
private
async
cacheGame
(
game
:
MiReversiGame
)
{
await
this
.
redisClient
.
setex
(
`reversi:game:cache:
${
game
.
id
}
`
,
60
*
3
,
JSON
.
stringify
(
game
));
await
this
.
redisClient
.
setex
(
`reversi:game:cache:
${
game
.
id
}
`
,
60
*
60
,
JSON
.
stringify
(
game
));
}
}
@
bindThis
@
bindThis
...
@@ -66,6 +62,33 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -66,6 +62,33 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
await
this
.
redisClient
.
del
(
`reversi:game:cache:
${
gameId
}
`
);
await
this
.
redisClient
.
del
(
`reversi:game:cache:
${
gameId
}
`
);
}
}
@
bindThis
private
getBakeProps
(
game
:
MiReversiGame
)
{
return
{
startedAt
:
game
.
startedAt
,
endedAt
:
game
.
endedAt
,
// ゲームの途中からユーザーが変わることは無いので
//user1Id: game.user1Id,
//user2Id: game.user2Id,
user1Ready
:
game
.
user1Ready
,
user2Ready
:
game
.
user2Ready
,
black
:
game
.
black
,
isStarted
:
game
.
isStarted
,
isEnded
:
game
.
isEnded
,
winnerId
:
game
.
winnerId
,
surrenderedUserId
:
game
.
surrenderedUserId
,
timeoutUserId
:
game
.
timeoutUserId
,
isLlotheo
:
game
.
isLlotheo
,
canPutEverywhere
:
game
.
canPutEverywhere
,
loopedBoard
:
game
.
loopedBoard
,
timeLimitForEachTurn
:
game
.
timeLimitForEachTurn
,
logs
:
game
.
logs
,
map
:
game
.
map
,
bw
:
game
.
bw
,
crc32
:
game
.
crc32
,
}
satisfies
Partial
<
MiReversiGame
>
;
}
@
bindThis
@
bindThis
public
async
matchSpecificUser
(
me
:
MiUser
,
targetUser
:
MiUser
):
Promise
<
MiReversiGame
|
null
>
{
public
async
matchSpecificUser
(
me
:
MiUser
,
targetUser
:
MiUser
):
Promise
<
MiReversiGame
|
null
>
{
if
(
targetUser
.
id
===
me
.
id
)
{
if
(
targetUser
.
id
===
me
.
id
)
{
...
@@ -204,14 +227,10 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -204,14 +227,10 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
let
isBothReady
=
false
;
let
isBothReady
=
false
;
if
(
game
.
user1Id
===
user
.
id
)
{
if
(
game
.
user1Id
===
user
.
id
)
{
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
const
updatedGame
=
{
.
set
({
...
game
,
user1Ready
:
ready
,
user1Ready
:
ready
,
})
};
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
returning
(
'
*
'
)
.
execute
()
.
then
((
response
)
=>
response
.
raw
[
0
]);
this
.
cacheGame
(
updatedGame
);
this
.
cacheGame
(
updatedGame
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
changeReadyStates
'
,
{
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
changeReadyStates
'
,
{
...
@@ -221,14 +240,10 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -221,14 +240,10 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
if
(
ready
&&
updatedGame
.
user2Ready
)
isBothReady
=
true
;
if
(
ready
&&
updatedGame
.
user2Ready
)
isBothReady
=
true
;
}
else
if
(
game
.
user2Id
===
user
.
id
)
{
}
else
if
(
game
.
user2Id
===
user
.
id
)
{
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
const
updatedGame
=
{
.
set
({
...
game
,
user2Ready
:
ready
,
user2Ready
:
ready
,
})
};
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
returning
(
'
*
'
)
.
execute
()
.
then
((
response
)
=>
response
.
raw
[
0
]);
this
.
cacheGame
(
updatedGame
);
this
.
cacheGame
(
updatedGame
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
changeReadyStates
'
,
{
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
changeReadyStates
'
,
{
...
@@ -262,22 +277,15 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -262,22 +277,15 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
bw
=
parseInt
(
game
.
bw
,
10
);
bw
=
parseInt
(
game
.
bw
,
10
);
}
}
function
getRandomMap
()
{
const
mapCount
=
Object
.
entries
(
Reversi
.
maps
).
length
;
const
rnd
=
Math
.
floor
(
Math
.
random
()
*
mapCount
);
return
Object
.
values
(
Reversi
.
maps
)[
rnd
].
data
;
}
const
map
=
game
.
map
!=
null
?
game
.
map
:
getRandomMap
();
const
crc32
=
CRC32
.
str
(
JSON
.
stringify
(
game
.
logs
)).
toString
();
const
crc32
=
CRC32
.
str
(
JSON
.
stringify
(
game
.
logs
)).
toString
();
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
.
set
({
.
set
({
...
this
.
getBakeProps
(
game
),
startedAt
:
new
Date
(),
startedAt
:
new
Date
(),
isStarted
:
true
,
isStarted
:
true
,
black
:
bw
,
black
:
bw
,
map
:
map
,
map
:
game
.
map
,
crc32
,
crc32
,
})
})
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
...
@@ -287,38 +295,23 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -287,38 +295,23 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
this
.
cacheGame
(
updatedGame
);
this
.
cacheGame
(
updatedGame
);
//#region 盤面に最初から石がないなどして始まった瞬間に勝敗が決定する場合があるのでその処理
//#region 盤面に最初から石がないなどして始まった瞬間に勝敗が決定する場合があるのでその処理
const
engine
=
new
Reversi
.
Game
(
map
,
{
const
engine
=
new
Reversi
.
Game
(
updatedGame
.
map
,
{
isLlotheo
:
g
ame
.
isLlotheo
,
isLlotheo
:
updatedG
ame
.
isLlotheo
,
canPutEverywhere
:
g
ame
.
canPutEverywhere
,
canPutEverywhere
:
updatedG
ame
.
canPutEverywhere
,
loopedBoard
:
g
ame
.
loopedBoard
,
loopedBoard
:
updatedG
ame
.
loopedBoard
,
});
});
if
(
engine
.
isEnded
)
{
if
(
engine
.
isEnded
)
{
let
winner
;
let
winner
Id
;
if
(
engine
.
winner
===
true
)
{
if
(
engine
.
winner
===
true
)
{
winner
=
bw
===
1
?
g
ame
.
user1Id
:
g
ame
.
user2Id
;
winner
Id
=
bw
===
1
?
updatedG
ame
.
user1Id
:
updatedG
ame
.
user2Id
;
}
else
if
(
engine
.
winner
===
false
)
{
}
else
if
(
engine
.
winner
===
false
)
{
winner
=
bw
===
1
?
g
ame
.
user2Id
:
g
ame
.
user1Id
;
winner
Id
=
bw
===
1
?
updatedG
ame
.
user2Id
:
updatedG
ame
.
user1Id
;
}
else
{
}
else
{
winner
=
null
;
winner
Id
=
null
;
}
}
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
await
this
.
endGame
(
updatedGame
,
winnerId
,
null
);
.
set
({
isEnded
:
true
,
endedAt
:
new
Date
(),
winnerId
:
winner
,
})
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
returning
(
'
*
'
)
.
execute
()
.
then
((
response
)
=>
response
.
raw
[
0
]);
this
.
cacheGame
(
updatedGame
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
ended
'
,
{
winnerId
:
winner
,
game
:
await
this
.
reversiGameEntityService
.
packDetail
(
game
.
id
),
});
return
;
return
;
}
}
...
@@ -327,7 +320,30 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -327,7 +320,30 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
this
.
redisClient
.
setex
(
`reversi:game:turnTimer:
${
game
.
id
}
:1`
,
updatedGame
.
timeLimitForEachTurn
,
''
);
this
.
redisClient
.
setex
(
`reversi:game:turnTimer:
${
game
.
id
}
:1`
,
updatedGame
.
timeLimitForEachTurn
,
''
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
started
'
,
{
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
started
'
,
{
game
:
await
this
.
reversiGameEntityService
.
packDetail
(
game
.
id
),
game
:
await
this
.
reversiGameEntityService
.
packDetail
(
updatedGame
),
});
}
@
bindThis
private
async
endGame
(
game
:
MiReversiGame
,
winnerId
:
MiUser
[
'
id
'
]
|
null
,
reason
:
'
surrender
'
|
'
timeout
'
|
null
)
{
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
.
set
({
...
this
.
getBakeProps
(
game
),
isEnded
:
true
,
endedAt
:
new
Date
(),
winnerId
:
winnerId
,
surrenderedUserId
:
reason
===
'
surrender
'
?
(
winnerId
===
game
.
user1Id
?
game
.
user2Id
:
game
.
user1Id
)
:
null
,
timeoutUserId
:
reason
===
'
timeout
'
?
(
winnerId
===
game
.
user1Id
?
game
.
user2Id
:
game
.
user1Id
)
:
null
,
})
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
returning
(
'
*
'
)
.
execute
()
.
then
((
response
)
=>
response
.
raw
[
0
]);
this
.
cacheGame
(
updatedGame
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
ended
'
,
{
winnerId
:
winnerId
,
game
:
await
this
.
reversiGameEntityService
.
packDetail
(
updatedGame
),
});
});
}
}
...
@@ -354,14 +370,10 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -354,14 +370,10 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
// TODO: より厳格なバリデーション
// TODO: より厳格なバリデーション
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
const
updatedGame
=
{
.
set
({
...
game
,
[
key
]:
value
,
[
key
]:
value
,
})
};
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
returning
(
'
*
'
)
.
execute
()
.
then
((
response
)
=>
response
.
raw
[
0
]);
this
.
cacheGame
(
updatedGame
);
this
.
cacheGame
(
updatedGame
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
updateSettings
'
,
{
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
updateSettings
'
,
{
...
@@ -397,17 +409,6 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -397,17 +409,6 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
engine
.
putStone
(
pos
);
engine
.
putStone
(
pos
);
let
winner
;
if
(
engine
.
isEnded
)
{
if
(
engine
.
winner
===
true
)
{
winner
=
game
.
black
===
1
?
game
.
user1Id
:
game
.
user2Id
;
}
else
if
(
engine
.
winner
===
false
)
{
winner
=
game
.
black
===
1
?
game
.
user2Id
:
game
.
user1Id
;
}
else
{
winner
=
null
;
}
}
const
logs
=
Reversi
.
Serializer
.
deserializeLogs
(
game
.
logs
);
const
logs
=
Reversi
.
Serializer
.
deserializeLogs
(
game
.
logs
);
const
log
=
{
const
log
=
{
...
@@ -423,17 +424,11 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -423,17 +424,11 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
const
crc32
=
CRC32
.
str
(
JSON
.
stringify
(
serializeLogs
)).
toString
();
const
crc32
=
CRC32
.
str
(
JSON
.
stringify
(
serializeLogs
)).
toString
();
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
const
updatedGame
=
{
.
set
({
...
game
,
crc32
,
crc32
,
isEnded
:
engine
.
isEnded
,
logs
:
serializeLogs
,
winnerId
:
winner
,
};
logs
:
serializeLogs
,
})
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
returning
(
'
*
'
)
.
execute
()
.
then
((
response
)
=>
response
.
raw
[
0
]);
this
.
cacheGame
(
updatedGame
);
this
.
cacheGame
(
updatedGame
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
log
'
,
{
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
log
'
,
{
...
@@ -442,10 +437,16 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -442,10 +437,16 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
});
});
if
(
engine
.
isEnded
)
{
if
(
engine
.
isEnded
)
{
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
ended
'
,
{
let
winnerId
;
winnerId
:
winner
??
null
,
if
(
engine
.
winner
===
true
)
{
game
:
await
this
.
reversiGameEntityService
.
packDetail
(
game
.
id
),
winnerId
=
game
.
black
===
1
?
game
.
user1Id
:
game
.
user2Id
;
});
}
else
if
(
engine
.
winner
===
false
)
{
winnerId
=
game
.
black
===
1
?
game
.
user2Id
:
game
.
user1Id
;
}
else
{
winnerId
=
null
;
}
await
this
.
endGame
(
updatedGame
,
winnerId
,
null
);
}
else
{
}
else
{
this
.
redisClient
.
setex
(
`reversi:game:turnTimer:
${
game
.
id
}
:
${
engine
.
turn
?
'
1
'
:
'
0
'
}
`
,
updatedGame
.
timeLimitForEachTurn
,
''
);
this
.
redisClient
.
setex
(
`reversi:game:turnTimer:
${
game
.
id
}
:
${
engine
.
turn
?
'
1
'
:
'
0
'
}
`
,
updatedGame
.
timeLimitForEachTurn
,
''
);
}
}
...
@@ -460,23 +461,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -460,23 +461,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
const
winnerId
=
game
.
user1Id
===
user
.
id
?
game
.
user2Id
:
game
.
user1Id
;
const
winnerId
=
game
.
user1Id
===
user
.
id
?
game
.
user2Id
:
game
.
user1Id
;
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
await
this
.
endGame
(
game
,
winnerId
,
'
surrender
'
);
.
set
({
isEnded
:
true
,
endedAt
:
new
Date
(),
winnerId
:
winnerId
,
surrenderedUserId
:
user
.
id
,
})
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
returning
(
'
*
'
)
.
execute
()
.
then
((
response
)
=>
response
.
raw
[
0
]);
this
.
cacheGame
(
updatedGame
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
ended
'
,
{
winnerId
:
winnerId
,
game
:
await
this
.
reversiGameEntityService
.
packDetail
(
game
.
id
),
});
}
}
@
bindThis
@
bindThis
...
@@ -500,23 +485,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
...
@@ -500,23 +485,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
if
(
timer
===
0
)
{
if
(
timer
===
0
)
{
const
winnerId
=
engine
.
turn
?
(
game
.
black
===
1
?
game
.
user2Id
:
game
.
user1Id
)
:
(
game
.
black
===
1
?
game
.
user1Id
:
game
.
user2Id
);
const
winnerId
=
engine
.
turn
?
(
game
.
black
===
1
?
game
.
user2Id
:
game
.
user1Id
)
:
(
game
.
black
===
1
?
game
.
user1Id
:
game
.
user2Id
);
const
updatedGame
=
await
this
.
reversiGamesRepository
.
createQueryBuilder
().
update
()
await
this
.
endGame
(
game
,
winnerId
,
'
timeout
'
);
.
set
({
isEnded
:
true
,
endedAt
:
new
Date
(),
winnerId
:
winnerId
,
timeoutUserId
:
engine
.
turn
?
(
game
.
black
===
1
?
game
.
user1Id
:
game
.
user2Id
)
:
(
game
.
black
===
1
?
game
.
user2Id
:
game
.
user1Id
),
})
.
where
(
'
id = :id
'
,
{
id
:
game
.
id
})
.
returning
(
'
*
'
)
.
execute
()
.
then
((
response
)
=>
response
.
raw
[
0
]);
this
.
cacheGame
(
updatedGame
);
this
.
globalEventService
.
publishReversiGameStream
(
game
.
id
,
'
ended
'
,
{
winnerId
:
winnerId
,
game
:
await
this
.
reversiGameEntityService
.
packDetail
(
game
.
id
),
});
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment