Skip to content

Commit

Permalink
refactor(room): Add OperationCode and enhance Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Jan 8, 2025
1 parent e86f435 commit 47b1546
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.common.kit;

import lombok.experimental.UtilityClass;

import java.util.concurrent.atomic.AtomicInteger;

@UtilityClass
class OperationCodeKit {
final AtomicInteger codeAtomic = new AtomicInteger(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.common.kit;

/**
* OperationCode
*
* @author 渔民小镇
* @date 2025-01-08
* @since 21.23
*/
public interface OperationCode {
int getOperationCode();

static int getAndIncrementCode() {
return OperationCodeKit.codeAtomic.getAndIncrement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class DefaultDomainEventExceptionHandler implements ExceptionHandler<Object> {
@Override
public void handleEventException(Throwable ex, long sequence, Object event) {
log.error("{} - {}", ex, event);
log.error(ex.getMessage(), ex);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ default <T extends Player> Stream<T> streamPlayer() {
return (Stream<T>) this.listPlayer().stream();
}


/**
* 真实玩家列表: 所有的真实玩家(不包含 Robot)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package com.iohao.game.widget.light.room.operation;

import com.iohao.game.common.kit.OperationCode;

import java.util.Optional;

/**
Expand Down Expand Up @@ -60,6 +62,28 @@ public interface OperationFactory {
*/
void mappingUser(int operation, OperationHandler operationHandler);

/**
* 玩家可操作的 OperationHandler。将操作码与 OperationHandler(玩法操作业务类)关联
*
* @param operationCode 操作码
* @param operationHandler 玩法操作业务类
* @since 21.23
*/
default void mappingUser(OperationCode operationCode, OperationHandler operationHandler) {
this.mappingUser(operationCode.getOperationCode(), operationHandler);
}

/**
* 将操作码与 OperationHandler(玩法操作业务类)关联
*
* @param operationCode 操作码
* @param operationHandler 玩法操作业务类
* @since 21.23
*/
default void mapping(OperationCode operationCode, OperationHandler operationHandler) {
this.mapping(operationCode.getOperationCode(), operationHandler);
}

/**
* 通过操作码得到 OperationHandler Optional
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package com.iohao.game.widget.light.room.operation;

import com.iohao.game.common.kit.OperationCode;

/**
* 玩法操作相关服务。获取 user 的玩法操作、所有玩法操作、玩法操作工厂。
*
Expand Down Expand Up @@ -50,4 +52,12 @@ default OperationHandler getOperationHandler(int operation) {
default OperationHandler getUserOperationHandler(int operation) {
return this.getOperationFactory().getUserOperationHandler(operation);
}

default OperationHandler getOperationHandler(OperationCode operationCode) {
return this.getOperationHandler(operationCode.getOperationCode());
}

default OperationHandler getUserOperationHandler(OperationCode operationCode) {
return this.getUserOperationHandler(operationCode.getOperationCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.iohao.game.widget.light.room.operation;

import com.iohao.game.common.kit.exception.ThrowKit;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import org.jctools.maps.NonBlockingHashMap;
Expand Down Expand Up @@ -60,11 +61,20 @@ public OperationHandler getOperationHandler(int operation) {
}

public void mapping(int operation, OperationHandler operationHandler) {
if (this.operationMap.containsKey(operation)) {
ThrowKit.ofRuntimeException("operation already exists : " + operation);
}

this.operationMap.put(operation, operationHandler);
}

public void mappingUser(int operation, OperationHandler operationHandler) {
this.mapping(operation, operationHandler);

if (this.userOperationMap.containsKey(operation)) {
ThrowKit.ofRuntimeException("operation already exists : " + operation);
}

this.userOperationMap.put(operation, operationHandler);
}

Expand Down

0 comments on commit 47b1546

Please sign in to comment.