Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.16 KB | None | 0 0
  1. import java.util._
  2.  
  3. class Player(val name:String){
  4.        
  5.         private var money = 10000
  6.        
  7.         def removeMoney(cnt:Int) = {
  8.                         if(money < cnt)
  9.                                 false
  10.                         else{
  11.                                 money -= cnt
  12.                                 true
  13.                         }
  14.         }
  15.        
  16.         def addMoney(cnt:Int){
  17.                        money += cnt
  18.         }
  19.        
  20.         def getMoney = {
  21.                 money
  22.         }
  23. }
  24.  
  25. class SuperEvent {
  26.         private val participants = new ArrayList[ObjRef<Player>]
  27.         private val limit = 10
  28.        
  29.         def register(player:Player){
  30.                         if(player removeMoney 1000){ //wpisowe
  31.                                 if(participants.size == limit)
  32.                                         unregister(participants.get(0))
  33.                                 participants add player toRef
  34.                          }
  35.         }
  36.        
  37.         def getTotalMoney = {
  38.                 var money = TaskExecutor.executeLoop(participants, func)
  39.                 money
  40.         }
  41.         def func(Player p) = p.getMoney
  42.        
  43.         def unregister(player: Player){
  44.                         if(participants contains player){
  45.                                 participants remove player toRef
  46.                                 player addMoney 1000   // zwraca wpisowe
  47.                         }
  48.         }
  49. }
  50.  
  51. class World{
  52.         val players = new HashMap[Int, ObjRef[Player]]
  53.         val event = new ObjRef[SuperEvent](new SuperEvent)
  54.         def getPlayer(id:Int):ObjRef[Player] = {
  55.                         players get id
  56.         }
  57. }
  58. class RequestRegister{
  59.         val world:World // wstrzykniete
  60.        
  61.         val playerId = 0 // przecytane z sieci
  62.        
  63.         def runImp{
  64.                 val eventRef = world.event
  65.                 val playerRef = world.getPlayer(playerId)
  66.                 TaskExecutor.executeTask(eventRef, playerRef, toDo)
  67.         }
  68.         def toDo(event:SuperEvent, player:Player) {
  69.                 val result = event register player
  70.                 result match {
  71.                         case true, money:Int => player.sendMessage("Udalo ci sie zarajestrowac a wszyscy maja w sumie "+money+" kasy")
  72.                         case false => player.sendMessage("Nie udalo ci sie zarejestrowac")
  73.                 }
  74.         }
  75. }
  76.  
  77. class RequestUnRegister{
  78.         val world:World // wstrzykniete
  79.        
  80.         val playerId = 0 // przecytane z sieci
  81.        
  82.         def runImp{
  83.                 val eventRef = world.event
  84.                 val playerRef = world.getPlayer(playerId)
  85.                 TaskExecutor.executeTask(eventRef, playerRef, toDo)
  86.                
  87.         }
  88.         def toDo(event:SuperEvent, player:Player) {
  89.                event unregister player
  90.         }
  91. }
  92.  
  93. class RequestDisplayMoneyInEvent{
  94.         val world:World // wstrzykniete
  95.        
  96.         def runImp{
  97.                 val eventRef = world.event
  98.                 Task.executeTask(eventRef, func)
  99.         }
  100.         def func(SuperEvent e) /chce wyslac do player wynik event.getTotalMoney
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement