Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util._
- class Player(val name:String){
- private var money = 10000
- def removeMoney(cnt:Int) = {
- if(money < cnt)
- false
- else{
- money -= cnt
- true
- }
- }
- def addMoney(cnt:Int){
- money += cnt
- }
- def getMoney = {
- // this synchronized
- money
- }
- }
- class SuperEvent {
- private val participants = new ArrayList[Player]
- private val limit = 10
- def register(player:Player){
- if(player removeMoney 1000){ //wpisowe
- if(participants.size == limit)
- unregister(participants.get(0))
- participants add player
- }
- }
- def getTotalMoney = {
- var money = 0
- participants foreach {
- pl => money += pl.getMoney
- }
- money
- }
- def unregister(player: Player){
- if(participants contains player){
- participants remove player
- player addMoney 1000 // zwraca wpisowe
- }
- }
- }
- class World{
- val players = new HashMap[Int, ObjRef[Player]]
- val event = new ObjRef[SuperEvent](new SuperEvent)
- def getPlayer(id:Int):ObjRef[Player] = {
- players get id
- }
- }
- class RequestRegister{
- val world:World // wstrzykniete
- val playerId = 0 // przecytane z sieci
- def runImp{
- val eventRef = world.event
- val playerRef = world.getPlayer(playerId)
- TaskExecutor.executeTask(eventRef, playerRef, toDo)
- }
- def toDo(event:SuperEvent, player:Player) {
- val result = event register player
- result match {
- case true, money:Int => player.sendMessage("Udalo ci sie zarajestrowac a wszyscy maja w sumie "+money+" kasy")
- case false => player.sendMessage("Nie udalo ci sie zarejestrowac")
- }
- }
- }
- class RequestUnRegister{
- val world:World // wstrzykniete
- val playerId = 0 // przecytane z sieci
- def runImp{
- val eventRef = world.event
- val playerRef = world.getPlayer(playerId)
- TaskExecutor.executeTask(eventRef, playerRef, toDo)
- }
- def toDo(event:SuperEvent, player:Player) {
- event unregister player
- }
- }
- class RequestDisplayMoneyInEvent{
- val world:World // wstrzykniete
- val playerId = 0 // przecytane z sieci
- def runImp{
- val eventRef = world.event
- val playerRef = world.getPlayer(playerId)
- //chce wyslac do player wynik event.getTotalMoney
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement