Giter Club home page Giter Club logo

ezyfox-sfs2x's Introduction

Build Status Dependency Status Coverage Status Maven Central Javadoc

Synopsis

This project supports to create a server side game application more efficiently and more quickly (use smartfox server engine)

Code Example

1. Handle server ready event

  @ServerEventHandler(event = ServerEvent.SERVER_READY)
  public class ServerReadyHandler {
    
    public void handle(AppContext context) {
        context.command(CreateRoom.class)
            .agents(RoomProvider.lobby()).execute();
        MyRoom[] rooms = RoomProvider.gameRooms(); 
        context.command(CreateRoom.class).agents(rooms).execute();
        for(MyRoom room : rooms) {
            context.command(UpdateRoom.class)
                .toClient(true).room(room).execute();
        }
    }
}

2. Handle user join zone event

  @ServerEventHandler(event = ServerEvent.USER_JOIN_ZONE)
  public class UserJoinZoneHandler {
    public void handle(AppContext context, ApiZone zone, MyUser user) {
        context.command(Log.class).from(this).info("user " + user.getName() 
              + " join zone " + zone.getName());
        context.command(JoinRoom.class)
            .roomToJoin("Lobby").user(user).execute();
    }
  }

3. Handle user join room event

  @RoomName("v")
  @ServerEventHandler(event = ServerEvent.USER_JOIN_ROOM)
  public class UserJoinRoomHandler {
    public void handle(AppContext context, MyRoom room, MyUser user) {
        context.command(Log.class).from(this).info("user " + user.getName() 
              + " joined room " + room.getName());
        context.command(Response.class)
            .command("1").recipient(user).data(room).execute();
    }
    
}

4. Listen a request from client and auto response

Let's say you want to listen a "bet" request from client and you also you want to response to that client, you can do like this:

  @Data
  @ClientResponseHandler // use this annotation to auto response to client
  @ClientRequestListener(command = "2") // use this annotation to listen a request from client
  public class BetRequestListener {
    @RequestParam("1")
    @ResponseParam("1")
    private int money;
    
    @RequestParam("2")
    @ResponseParam("2")
    private int roomId;
    
    public void execute(AppContext context, MyUser user) {
        MyRoom room = 
                context.command(FindRoom.class).by(roomId);
        context.command(Log.class).from(this).info("user {} has just betted {} chips", 
                user.getName(), btype.getMoney());
        user.decreaseMoney(btype.getMoney());
        user.increaseGameMoney(btype.getMoney());
        context.command(UpdateUser.class)
            .toClient(true).user(user).execute();
        context.command(UpdateRoom.class)
            .room(room).toClient(true).execute();
    }
    
}

5. Intercept a request from client

Let's say you need validate a parameter in a request from client (i.e money in "bet" request) you can do:

  @Data
  @ClientRequestListener(command = "2", priority = -1)
  public class BetRequestInterceptor {
    
    @RequestParam("1")
    @ResponseParam("1")
    private int money;
    
    @RequestParam("2")
    @ResponseParam("2")
    private int roomId;
    
    public void execute(AppContext context, MyUser user) throws Exception {
        if(money <= 0) {
            context.command(Response.class)
                .command("2").recipient(user).execute();
            throw new BadRequestException();
        }
    }
    
}

Motivation

We have used smartfox server engine, we must use key-value object like SFSObject, SFSUser, SFSRoom e.t.c, they make our source code too complex, hard to read and hard to map, example:

  // public user var
  UserVariable avatarPic = new SFSUserVariable("pic", "GonzoTheGreat.jpg");
   
  // private user var
  UserVariable dbId = new SFSUserVariable("dbId", databaseId);
  dbId.setHidden(true);
     
  // Set variables via the server side API
  getApi().setUserVariables(user, Arrays.asList(avatarPic, dbId));

We must spend a lot of time to declare, init variables. Some time we also have two or three user agent for each game and mapping an user agent to key-value object is hard. We think using POJO is good idea, so we make this project.

Installation

	<dependency>
		<groupId>com.tvd12</groupId>
		<artifactId>ezyfox-sfs2x</artifactId>
		<version>1.3.1</version>
	</dependency>

#API Reference

http://www.javadoc.io/doc/com.tvd12/ezyfox-sfs2x

Tests

mvn test

Contributors

License

  • Apache License, Version 2.0

ezyfox-sfs2x's People

Contributors

tvd12 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.