Formatted codes.

This commit is contained in:
yflu 2022-10-25 23:36:55 +08:00
parent 4e0f7b52d4
commit 8038b393e0
3 changed files with 68 additions and 65 deletions

View File

@ -188,7 +188,7 @@ type Room struct {
StageTileH int32 StageTileH int32
RawBattleStrToVec2DListMap StrToVec2DListMap RawBattleStrToVec2DListMap StrToVec2DListMap
RawBattleStrToPolygon2DListMap StrToPolygon2DListMap RawBattleStrToPolygon2DListMap StrToPolygon2DListMap
BackendDynamicsEnabled bool BackendDynamicsEnabled bool
} }
const ( const (
@ -461,11 +461,11 @@ func (pR *Room) StartBattle() {
// Force setting all-confirmed of buffered inputFrames periodically // Force setting all-confirmed of buffered inputFrames periodically
unconfirmedMask := uint64(0) unconfirmedMask := uint64(0)
if pR.BackendDynamicsEnabled { if pR.BackendDynamicsEnabled {
unconfirmedMask = pR.forceConfirmationIfApplicable() unconfirmedMask = pR.forceConfirmationIfApplicable()
} else { } else {
pR.markConfirmationIfApplicable() pR.markConfirmationIfApplicable()
} }
upperToSendInputFrameId := atomic.LoadInt32(&(pR.LastAllConfirmedInputFrameId)) upperToSendInputFrameId := atomic.LoadInt32(&(pR.LastAllConfirmedInputFrameId))
/* /*
@ -481,22 +481,22 @@ func (pR *Room) StartBattle() {
refRenderFrameId = pR.RenderFrameId refRenderFrameId = pR.RenderFrameId
} }
dynamicsDuration := int64(0) dynamicsDuration := int64(0)
if pR.BackendDynamicsEnabled { if pR.BackendDynamicsEnabled {
if 0 <= pR.LastAllConfirmedInputFrameId { if 0 <= pR.LastAllConfirmedInputFrameId {
dynamicsStartedAt := utils.UnixtimeNano() dynamicsStartedAt := utils.UnixtimeNano()
// Apply "all-confirmed inputFrames" to move forward "pR.CurDynamicsRenderFrameId" // Apply "all-confirmed inputFrames" to move forward "pR.CurDynamicsRenderFrameId"
nextDynamicsRenderFrameId := pR.ConvertToLastUsedRenderFrameId(pR.LastAllConfirmedInputFrameId, pR.InputDelayFrames) nextDynamicsRenderFrameId := pR.ConvertToLastUsedRenderFrameId(pR.LastAllConfirmedInputFrameId, pR.InputDelayFrames)
Logger.Debug(fmt.Sprintf("roomId=%v, room.RenderFrameId=%v, LastAllConfirmedInputFrameId=%v, InputDelayFrames=%v, nextDynamicsRenderFrameId=%v", pR.Id, pR.RenderFrameId, pR.LastAllConfirmedInputFrameId, pR.InputDelayFrames, nextDynamicsRenderFrameId)) Logger.Debug(fmt.Sprintf("roomId=%v, room.RenderFrameId=%v, LastAllConfirmedInputFrameId=%v, InputDelayFrames=%v, nextDynamicsRenderFrameId=%v", pR.Id, pR.RenderFrameId, pR.LastAllConfirmedInputFrameId, pR.InputDelayFrames, nextDynamicsRenderFrameId))
pR.applyInputFrameDownsyncDynamics(pR.CurDynamicsRenderFrameId, nextDynamicsRenderFrameId) pR.applyInputFrameDownsyncDynamics(pR.CurDynamicsRenderFrameId, nextDynamicsRenderFrameId)
dynamicsDuration = utils.UnixtimeNano() - dynamicsStartedAt dynamicsDuration = utils.UnixtimeNano() - dynamicsStartedAt
} }
// [WARNING] The following inequality are seldom true, but just to avoid that in good network condition the frontend resyncs itself to a "too advanced frontend.renderFrameId", and then starts upsyncing "too advanced inputFrameId". // [WARNING] The following inequality are seldom true, but just to avoid that in good network condition the frontend resyncs itself to a "too advanced frontend.renderFrameId", and then starts upsyncing "too advanced inputFrameId".
if refRenderFrameId > pR.CurDynamicsRenderFrameId { if refRenderFrameId > pR.CurDynamicsRenderFrameId {
refRenderFrameId = pR.CurDynamicsRenderFrameId refRenderFrameId = pR.CurDynamicsRenderFrameId
} }
} }
for playerId, player := range pR.Players { for playerId, player := range pR.Players {
if swapped := atomic.CompareAndSwapInt32(&player.BattleState, PlayerBattleStateIns.ACTIVE, PlayerBattleStateIns.ACTIVE); !swapped { if swapped := atomic.CompareAndSwapInt32(&player.BattleState, PlayerBattleStateIns.ACTIVE, PlayerBattleStateIns.ACTIVE); !swapped {
// [WARNING] DON'T send anything if the player is disconnected, because it could jam the channel and cause significant delay upon "battle recovery for reconnected player". // [WARNING] DON'T send anything if the player is disconnected, because it could jam the channel and cause significant delay upon "battle recovery for reconnected player".
@ -810,7 +810,7 @@ func (pR *Room) OnDismissed() {
pR.InputFrameUpsyncDelayTolerance = 2 pR.InputFrameUpsyncDelayTolerance = 2
pR.MaxChasingRenderFramesPerUpdate = 10 pR.MaxChasingRenderFramesPerUpdate = 10
pR.BackendDynamicsEnabled = true // [WARNING] When "false", recovery upon reconnection wouldn't work! pR.BackendDynamicsEnabled = true // [WARNING] When "false", recovery upon reconnection wouldn't work!
pR.ChooseStage() pR.ChooseStage()
pR.EffectivePlayerCount = 0 pR.EffectivePlayerCount = 0
@ -1080,40 +1080,40 @@ func (pR *Room) prefabInputFrameDownsync(inputFrameId int32) *pb.InputFrameDowns
} }
func (pR *Room) markConfirmationIfApplicable() { func (pR *Room) markConfirmationIfApplicable() {
inputFrameId1 := pR.LastAllConfirmedInputFrameId+1 inputFrameId1 := pR.LastAllConfirmedInputFrameId + 1
gap := int32(4) // This value is hardcoded and doesn't need be much bigger, because the backend side is supposed to never lag when "false == BackendDynamicsEnabled". gap := int32(4) // This value is hardcoded and doesn't need be much bigger, because the backend side is supposed to never lag when "false == BackendDynamicsEnabled".
inputFrameId2 := inputFrameId1+gap inputFrameId2 := inputFrameId1 + gap
if inputFrameId2 > pR.InputsBuffer.EdFrameId { if inputFrameId2 > pR.InputsBuffer.EdFrameId {
inputFrameId2 = pR.InputsBuffer.EdFrameId inputFrameId2 = pR.InputsBuffer.EdFrameId
} }
totPlayerCnt := uint32(pR.Capacity) totPlayerCnt := uint32(pR.Capacity)
allConfirmedMask := uint64((1 << totPlayerCnt) - 1) allConfirmedMask := uint64((1 << totPlayerCnt) - 1)
for inputFrameId := inputFrameId1; inputFrameId < inputFrameId2; inputFrameId++ { for inputFrameId := inputFrameId1; inputFrameId < inputFrameId2; inputFrameId++ {
tmp := pR.InputsBuffer.GetByFrameId(inputFrameId) tmp := pR.InputsBuffer.GetByFrameId(inputFrameId)
if nil == tmp { if nil == tmp {
panic(fmt.Sprintf("inputFrameId=%v doesn't exist for roomId=%v, this is abnormal because the server should prefab inputFrameDownsync in a most advanced pace, check the prefab logic! InputsBuffer=%v", inputFrameId, pR.Id, pR.InputsBufferString(false))) panic(fmt.Sprintf("inputFrameId=%v doesn't exist for roomId=%v, this is abnormal because the server should prefab inputFrameDownsync in a most advanced pace, check the prefab logic! InputsBuffer=%v", inputFrameId, pR.Id, pR.InputsBufferString(false)))
} }
inputFrameDownsync := tmp.(*pb.InputFrameDownsync) inputFrameDownsync := tmp.(*pb.InputFrameDownsync)
for _, player := range pR.Players { for _, player := range pR.Players {
bufIndex := pR.toDiscreteInputsBufferIndex(inputFrameId, player.JoinIndex) bufIndex := pR.toDiscreteInputsBufferIndex(inputFrameId, player.JoinIndex)
tmp, loaded := pR.DiscreteInputsBuffer.LoadAndDelete(bufIndex) // It's safe to "LoadAndDelete" here because the "inputFrameUpsync" of this player is already remembered by the corresponding "inputFrameDown". tmp, loaded := pR.DiscreteInputsBuffer.LoadAndDelete(bufIndex) // It's safe to "LoadAndDelete" here because the "inputFrameUpsync" of this player is already remembered by the corresponding "inputFrameDown".
if !loaded { if !loaded {
continue continue
} }
inputFrameUpsync := tmp.(*pb.InputFrameUpsync) inputFrameUpsync := tmp.(*pb.InputFrameUpsync)
indiceInJoinIndexBooleanArr := uint32(player.JoinIndex - 1) indiceInJoinIndexBooleanArr := uint32(player.JoinIndex - 1)
inputFrameDownsync.InputList[indiceInJoinIndexBooleanArr] = pR.EncodeUpsyncCmd(inputFrameUpsync) inputFrameDownsync.InputList[indiceInJoinIndexBooleanArr] = pR.EncodeUpsyncCmd(inputFrameUpsync)
inputFrameDownsync.ConfirmedList |= (1 << indiceInJoinIndexBooleanArr) inputFrameDownsync.ConfirmedList |= (1 << indiceInJoinIndexBooleanArr)
} }
// Force confirmation of "inputFrame2" // Force confirmation of "inputFrame2"
if allConfirmedMask == inputFrameDownsync.ConfirmedList { if allConfirmedMask == inputFrameDownsync.ConfirmedList {
pR.onInputFrameDownsyncAllConfirmed(inputFrameDownsync, -1) pR.onInputFrameDownsyncAllConfirmed(inputFrameDownsync, -1)
} else { } else {
break break
} }
} }
} }
func (pR *Room) forceConfirmationIfApplicable() uint64 { func (pR *Room) forceConfirmationIfApplicable() uint64 {

View File

@ -382,7 +382,8 @@ cc.Class({
window.clearBoundRoomIdInBothVolatileAndPersistentStorage(); window.clearBoundRoomIdInBothVolatileAndPersistentStorage();
window.initPersistentSessionClient(self.initAfterWSConnected, null /* Deliberately NOT passing in any `expectedRoomId`. -- YFLu */ ); window.initPersistentSessionClient(self.initAfterWSConnected, null /* Deliberately NOT passing in any `expectedRoomId`. -- YFLu */ );
}; };
resultPanelScriptIns.onCloseDelegate = () => {}; resultPanelScriptIns.onCloseDelegate = () => {
};
self.gameRuleNode = cc.instantiate(self.gameRulePrefab); self.gameRuleNode = cc.instantiate(self.gameRulePrefab);
self.gameRuleNode.width = self.canvasNode.width; self.gameRuleNode.width = self.canvasNode.width;
@ -740,10 +741,12 @@ cc.Class({
newPlayerNode.setPosition(cc.v2(x, y)); newPlayerNode.setPosition(cc.v2(x, y));
newPlayerNode.getComponent("SelfPlayer").mapNode = self.node; newPlayerNode.getComponent("SelfPlayer").mapNode = self.node;
const currentSelfColliderCircle = newPlayerNode.getComponent(cc.CircleCollider); const currentSelfColliderCircle = newPlayerNode.getComponent(cc.CircleCollider);
const r = currentSelfColliderCircle.radius, d = 2*r; const r = currentSelfColliderCircle.radius,
// The collision box of an individual player is a polygon instead of a circle, because the backend collision engine doesn't handle circle alignment well. d = 2 * r;
const x0 = x-r, y0 = y-r; // The collision box of an individual player is a polygon instead of a circle, because the backend collision engine doesn't handle circle alignment well.
let pts = [[0, 0], [d, 0], [d, d], [0, d]]; const x0 = x - r,
y0 = y - r;
let pts = [[0, 0], [d, 0], [d, d], [0, d]];
const newPlayerColliderLatest = self.latestCollisionSys.createPolygon(x0, y0, pts); const newPlayerColliderLatest = self.latestCollisionSys.createPolygon(x0, y0, pts);
const newPlayerColliderChaser = self.chaserCollisionSys.createPolygon(x0, y0, pts); const newPlayerColliderChaser = self.chaserCollisionSys.createPolygon(x0, y0, pts);
@ -957,7 +960,7 @@ cc.Class({
const collisionPlayerIndex = self.collisionPlayerIndexPrefix + joinIndex; const collisionPlayerIndex = self.collisionPlayerIndexPrefix + joinIndex;
const playerCollider = collisionSysMap.get(collisionPlayerIndex); const playerCollider = collisionSysMap.get(collisionPlayerIndex);
const currentSelfColliderCircle = playerRichInfo.node.getComponent(cc.CircleCollider); const currentSelfColliderCircle = playerRichInfo.node.getComponent(cc.CircleCollider);
const r = currentSelfColliderCircle.radius; const r = currentSelfColliderCircle.radius;
rdf.players[playerRichInfo.id] = { rdf.players[playerRichInfo.id] = {
id: playerRichInfo.id, id: playerRichInfo.id,
x: playerCollider.x + r, // [WARNING] the (x, y) of "playerCollider" is offset to the anchor (i.e. first point of all points) of the polygon shape x: playerCollider.x + r, // [WARNING] the (x, y) of "playerCollider" is offset to the anchor (i.e. first point of all points) of the polygon shape
@ -1033,7 +1036,7 @@ cc.Class({
const player = latestRdf.players[playerId]; const player = latestRdf.players[playerId];
const currentSelfColliderCircle = playerRichInfo.node.getComponent(cc.CircleCollider); const currentSelfColliderCircle = playerRichInfo.node.getComponent(cc.CircleCollider);
const r = currentSelfColliderCircle.radius; const r = currentSelfColliderCircle.radius;
playerCollider.x = player.x - r; playerCollider.x = player.x - r;
playerCollider.y = player.y - r; playerCollider.y = player.y - r;
}); });

View File

@ -111,9 +111,9 @@ window.initPersistentSessionClient = function(onopenCb, expectedRoomId) {
} }
return; return;
} }
const selfPlayerStr = cc.sys.localStorage.getItem("selfPlayer"); const selfPlayerStr = cc.sys.localStorage.getItem("selfPlayer");
const selfPlayer = null == selfPlayerStr ? null : JSON.parse(selfPlayerStr); const selfPlayer = null == selfPlayerStr ? null : JSON.parse(selfPlayerStr);
const intAuthToken = null == selfPlayer ? "" : selfPlayer.intAuthToken; const intAuthToken = null == selfPlayer ? "" : selfPlayer.intAuthToken;
let urlToConnect = backendAddress.PROTOCOL.replace('http', 'ws') + '://' + backendAddress.HOST + ":" + backendAddress.PORT + backendAddress.WS_PATH_PREFIX + "?intAuthToken=" + intAuthToken; let urlToConnect = backendAddress.PROTOCOL.replace('http', 'ws') + '://' + backendAddress.HOST + ":" + backendAddress.PORT + backendAddress.WS_PATH_PREFIX + "?intAuthToken=" + intAuthToken;
@ -224,10 +224,10 @@ window.initPersistentSessionClient = function(onopenCb, expectedRoomId) {
case constants.RET_CODE.MYSQL_ERROR: case constants.RET_CODE.MYSQL_ERROR:
case constants.RET_CODE.PLAYER_NOT_FOUND: case constants.RET_CODE.PLAYER_NOT_FOUND:
case constants.RET_CODE.PLAYER_CHEATING: case constants.RET_CODE.PLAYER_CHEATING:
case 1006: // Peer(i.e. the backend) gone unexpectedly case 1006: // Peer(i.e. the backend) gone unexpectedly
if (window.handleClientSessionError) { if (window.handleClientSessionError) {
window.handleClientSessionError(); window.handleClientSessionError();
} }
break; break;
default: default:
break; break;