patternMinor
Game boot to some operations every second
Viewed 0 times
bootoperationseverygamesomesecond
Problem
I am building game, like Ogame, or Ikariam where you can play with other users in real-time. You train some soldiers, and build some vehicles or buildings. Every building or vehicle has some level. If a building has a higher level you must wait longer to use it etc. I will be grateful for any comments, especially about Scala language.
import java.text.SimpleDateFormat
import java.util.Date
import com.redis._
object worker extends App {
override def main(args: Array[String]): Unit = {
println("Worker start...")
val r = new RedisClient("localhost", 6379)
var i: Int = 0
while (true) {
Thread.sleep(1000)
i = i + 1
val numbers: List[Int] = getUsers(r)
checkQueue(r, numbers)
}
}
private def checkQueue(r: RedisClient, numbers: List[Int]): Unit = {
val d1 = new Date()
val format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val now = new Date()
for (nr for (range = 1) {
r.hincrby("user_" + nr + ":soldier:amount", range, 1)
r.hincrby("user_" + nr + ":soldier:queue_amount", range, -1)
}
}
}
case None =>
}
}
val d2 = new Date()
println("loop time: " + (d2.getTime - d1.getTime) + " milliseconds")
}
private def getUsers(r: RedisClient): List[Int] = {
val list = r.keys("*user_*:soldier:interval*")
var numbers = List[Int]()
list match {
case Some(s) => for (k
}
numbers
}
}Solution
The first problem with this code is that there is too little explanation for anything. There is no documentation. There is a referenced class
The next major problem is a classic issue for newer programmers (and something I have been guilty of myself) - big monolithic procedures, specifically
I would recommend fixing these two issues and then post a new question asking for a new review of the modified code.
RedisClient for which there is no explanation of what it is and how it is being used. The variable names tend to be undescriptive. When asking other people to read code, then it is the author's responsibility to make it so that they actually can read and follow the code.The next major problem is a classic issue for newer programmers (and something I have been guilty of myself) - big monolithic procedures, specifically
checkQueue. When the size of a procedure exceeds a reasonable size (definition of reasonable vary from person to person, but certainly no more than 10 lines), then the developer should look very hard for ways to refactor it and extract out pieces of discrete functionality. In this code, everything inside the for (range <- ss) block should almost certainly be extracted into a function of its own (with a descriptive function name).I would recommend fixing these two issues and then post a new question asking for a new review of the modified code.
Context
StackExchange Code Review Q#94252, answer score: 4
Revisions (0)
No revisions yet.