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.21 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.             // this synchronized
  22.             money
  23.         }
  24. }
  25.  
  26. class SuperEvent {
  27.         private val participants = new ArrayList[Player]
  28.         private val limit = 10
  29.        
  30.         def register(player:Player){
  31.                         if(player removeMoney 1000){ //wpisowe
  32.                                 if(participants.size == limit)
  33.                                         unregister(participants.get(0))
  34.                                 participants add player
  35.  
  36.                         }
  37.         }
  38.        
  39.         def getTotalMoney = {
  40.             var money = 0
  41.             participants foreach {
  42.                 pl => money += pl.getMoney
  43.             }
  44.             money
  45.         }
  46.        
  47.         def unregister(player: Player){
  48.                         if(participants contains player){
  49.                                 participants remove player
  50.                                 player addMoney 1000   // zwraca wpisowe
  51.                         }
  52.         }
  53. }
  54.  
  55. class World{
  56.         val players = new HashMap[Int, ObjRef[Player]]
  57.         val event = new ObjRef[SuperEvent](new SuperEvent)
  58.         def getPlayer(id:Int):ObjRef[Player] = {
  59.                         players get id
  60.         }
  61. }
  62. class RequestRegister{
  63.         val world:World // wstrzykniete
  64.        
  65.         val playerId = 0 // przecytane z sieci
  66.        
  67.         def runImp{
  68.                 val eventRef = world.event
  69.                 val playerRef = world.getPlayer(playerId)
  70.                 TaskExecutor.executeTask(eventRef, playerRef, toDo)
  71.         }
  72.         def toDo(event:SuperEvent, player:Player) {
  73.                 val result = event register player
  74.                 result match {
  75.                     case true, money:Int => player.sendMessage("Udalo ci sie zarajestrowac a wszyscy maja w sumie "+money+" kasy")
  76.                     case false => player.sendMessage("Nie udalo ci sie zarejestrowac")
  77.                 }
  78.         }
  79. }
  80.  
  81. class RequestUnRegister{
  82.         val world:World // wstrzykniete
  83.        
  84.         val playerId = 0 // przecytane z sieci
  85.        
  86.         def runImp{
  87.                 val eventRef = world.event
  88.                 val playerRef = world.getPlayer(playerId)
  89.                 TaskExecutor.executeTask(eventRef, playerRef, toDo)
  90.                
  91.         }
  92.         def toDo(event:SuperEvent, player:Player) {
  93.                event unregister player
  94.         }
  95. }
  96.  
  97. class RequestDisplayMoneyInEvent{
  98.         val world:World // wstrzykniete
  99.        
  100.         val playerId = 0 // przecytane z sieci
  101.        
  102.         def runImp{
  103.                 val eventRef = world.event
  104.                 val playerRef = world.getPlayer(playerId)
  105.                 //chce wyslac do player wynik event.getTotalMoney
  106.                
  107.         }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement