游戏中的工作安排!框架 [英] Job-Scheduling in Play! Framework

查看:90
本文介绍了游戏中的工作安排!框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发具有游戏功能(1.2.4版)的应用程序,在该应用程序中,用户可以执行某些任务并因此而获得回报.这些任务需要一些能量,这些能量会随着时间的流逝而重新填充.基本设置如下:

I am currently into the development of a play-powered (v. 1.2.4) application where users can perform certain tasks and gain rewards for doing so. These tasks require some energy, which will be refilled over time. The basic setting is as follows:

public class User extends Model {
    public Long energy;
    public Long maxenergy;
    public Long cooldown = Long.valueOf(300);
}

public class Task extends Controller {
    public static void perform(Long id) {
        User user = User.findById(id).first();

        // do some complex task here...

        user.energy--;
        user.save();
        Task.list();
    }
}

现在,我想在冷却(5分钟)后重新补充用户的能量.假设用户有10/10能量点,并且我想在使用5分钟后补充一个能量点,那么我可以轻松地为此使用一项工作:

Now I want to refill the energy of the user after the cooldown (5 min). Assuming the user has 10/10 energy points and I want to refill a point 5 minutes after it has been used, I could easily use a job for this:

public class EnergyHealer extends Job {
    public Long id;

    public EnergyHealer(Long id) {
        this.id = id;
    }

    public void doJob() throws Exception {
        User user = User.findById(id);
        user.energy++;
        if (user.energy > user.maxenergy) {
            user.energy = user.maxenergy;
        }
        user.save()
    }
}

...,在完成任务后立即在我的控制器中调用它:

... and call it in my controller right after the task has been competed:

new EnergyHealer(user.id).in(user.cooldown);

我的问题是,在这种情况下,作业是同时进行的,因此如果用户在执行前一个任务后2秒钟执行任务,则第一个能量点将在5分钟后重新填充,而随后的能量点将仅填充2分钟几秒钟后.

My problem here is, that in this case jobs are scheduled concurrently, thus if the user performs a task 2 seconds after he performed a previous task the first energy point is refilled after 5 mins, while the subsequent point is refilled only 2 seconds later.

因此,我需要对作业进行序列化,例如,假设我有10个能量点中的8个,则需要恰好 10分钟才能重新填充所有能量点.

So, I need the jobs to be serialized, e.g., assuming I have 8 of 10 energy points, it should take exactly 10 minutes until all energy points are refilled.

相关说明:一旦达到特定阈值,用户将具有不同的级别并获得完成任务的经验.它们的级别会增加,并且所有能量点都会重新填充,无论在先前的级别中已使用了多少能量点,因此某些作业可能在执行时就已过时.

On a related note: users have different levels and gain experience for completing tasks, once a certain threshold is reached. Their level is increased and all energy points are refilled, no matter how many of them have been used in the previous level, hence some jobs may become obsolete by the time they are executed.

考虑到成千上万的用户,工作可能根本不是完美的选择,因此,如果有人对如何实现所描述的方案有想法,我很乐意提供帮助!

Considering some thousand users, jobs may not be the perfect choice at all, so if someone has an idea on how to achieve the described scenario, I'm glad for any help!

推荐答案

我认为您的Job安排有误.与其在每次用户执行某项操作时就开始工作,不如让他们简单地拥有一个外观或仅在用户尚不存在的情况下才能开始工作的东西.

I think you just have your Job scheduling wrong. Rather than kick off your job every time they perform an action, you should simply have a facade or something that will only kick off a job if one does not already exist for the user.

  1. 如果该作业尚不存在,请创建它,否则不执行任何操作

  1. If the job does not already exist, create it, otherwise do nothing

然后这应该增加1能量

检查能量是否已满,如果已满,则结束

check if energy is full, if it is, end

如果没有,请暂停5分钟

if not, pause for 5 minutes

返回2

这篇关于游戏中的工作安排!框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆