package com.game;

import java.lang.reflect.Method;

import com.game.data.Player;
import com.game.data.Room;
import com.taurus.core.entity.TObject;
import com.taurus.core.routes.Action;
import com.taurus.core.routes.IController;
import com.taurus.core.routes.Interceptor;
import com.taurus.permanent.data.Session;

public class GameInterceptor implements Interceptor{
	public final static int NOT_PLAYER = 1;
	
	@Override
	public void intercept(Action action, IController controller,Object... args) throws Exception{
		int validate = action.getActionKeyObj().validate();
		Session sender = (Session)args[0];
		TObject params = (TObject)args[1];
		int gid = (int)args[2];
		Method method = action.getMethod();
		if((validate&NOT_PLAYER)!=0) {
			method.invoke(controller,sender,params,gid);
		}else {
			Player player = Global.sessionMgr.getPlayer(sender);
			if (player != null) {
				Room room = player.room;
				if (room == null)return;
				if (room.isDestroy)return;
				room.enqueueRunnable(new Runnable() {
					@Override
					public void run() {
						if(player.isDestroy)return;
						if(player.room.playerMapById.containsKey(player.playerid)) {
							try {
								method.invoke(controller, sender,params,gid,player);
							} catch (Exception e) {
								throw new RuntimeException(e);
							}
						}
					}
				});
			}
		}
	}

}
