Routing
Introduction
The route concept here corresponds to a browser URL and represents a unique access address. A route is an address exposed externally for accessing business methods. Classes and methods with routes are also called Action.
Route Annotations
The business framework provides two annotations:
- @ActionController main route (applied to business classes).
- @ActionMethod sub route (applied to business methods).
@ActionController(2)
public class DemoAction {
@ActionMethod(0)
private HelloMessage here(HelloMessage message) {
...
}
@ActionMethod(1)
private HelloMessage say(HelloMessage message) {
...
}
}
The code above defines two externally exposed actions:
hereis an action with main route2and sub route0.sayis an action with main route2and sub route1.
Methods (Actions) under DemoAction share main route 2,
while each method uses its own ActionMethod value as sub route.
Understanding Routing
- @ActionController can be understood like Spring's
@RestController(class-level). - @ActionMethod can be understood like Spring's
@GetMapping(method-level).
Example
URL: https://ip:port/user/hello
When inputting the URL above in browser, it means accessing method hello under module user.
user corresponds to framework main route @ActionController.
hello corresponds to framework sub route @ActionMethod.
Best Practices
During development, define routes as constants.
A game usually has multiple modules, and each module has multiple business methods. You can define all module main routes in one interface.
Use one file per module, and each module file manages corresponding business methods.
Name module files with xxxCmd, and uniformly name module main route as cmd.
public interface CmdModule {
int heroCmd = 1;
int equipCmd = 2;
}
/** hero module */
public interface HeroCmd {
int cmd = CmdModule.heroCmd;
int hello = 0;
int age = 1;
}
/** equipment module */
public interface EquipCmd {
int cmd = CmdModule.equipCmd;
/** subCmd : equipment strengthen */
int intensify = 0;
/** subCmd : upgrade */
int upgrade = 1;
}
@ActionController(HeroCmd.cmd)
public final class HeroAction {
@ActionMethod(HeroCmd.hello)
public String helo(String message) {
return message + ", Michael Jackson";
}
}
Route constants are recommended for clearer module boundaries. For example:
- Player module: methods related to player (exp increase, level up).
- Equipment module: methods related to equipment (strengthen, gem inlay, identification, etc.).
Recommendation: define one cmd per module, and one subCmd per method in that module.
Route Calculation Utility
Frontend developers can copy this code to calculate routes. If SDK is used, this can be ignored.
@UtilityClass
public class CmdKit {
public int getCmd(int cmdMerge) {
return cmdMerge >> 16;
}
public int getSubCmd(int cmdMerge) {
return cmdMerge & 0xFFFF;
}
public int merge(int cmd, int subCmd) {
return (cmd << 16) | subCmd;
}
}