Created
October 27, 2013 09:23
-
-
Save arturaz/7179562 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinylabproductions.quazibuild.server | |
import com.tinylabproductions.quazibuild.messaging.Messages.C2S | |
import com.google.protobuf.GeneratedMessage | |
import com.tinylabproductions.quazibuild.messaging.{LiveGame, Messages} | |
import org.joda.time.{DateTimeZone, DateTime} | |
import scala.concurrent.duration.Duration | |
/** | |
* Created with IntelliJ IDEA. | |
* User: arturas | |
* Date: 13.10.16 | |
* Time: 13.54 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
package object messaging { | |
implicit class C2SExts(val c2sMessage: C2S) extends AnyVal { | |
import C2SMessage._ | |
def asString: String = { | |
if (c2sMessage.hasLogin) "Login" | |
else if (c2sMessage.hasRegister) "Register" | |
else if (c2sMessage.hasLgMoveShip) "LiveGame.MoveRequest" | |
else if (c2sMessage.hasLgActivateBuilding) | |
"LiveGame.BuildingActivationRequest" | |
else throw new IllegalStateException( | |
s"Missing header message type for\n$c2sMessage" | |
) | |
} | |
def map[A](pf: PartialFunction[GeneratedMessage, A]): A = { | |
collect(pf) getOrElse { | |
throw new MatchError(s"Unhandled message case: ${c2sMessage.asString}") | |
} | |
} | |
def collect[A](pf: PartialFunction[GeneratedMessage, A]): Option[A] = { | |
val lifted = pf.lift | |
def check(hasMessage: => Boolean, getMessage: => GeneratedMessage) = { | |
if (hasMessage) lifted(getMessage) else None | |
} | |
check(c2sMessage.hasLogin, c2sMessage.getLogin) orElse | |
check(c2sMessage.hasRegister, c2sMessage.getRegister) orElse | |
check(c2sMessage.hasLgMoveShip, c2sMessage.getLgMoveShip) orElse | |
check(c2sMessage.hasLgActivateBuilding, c2sMessage.getLgActivateBuilding) | |
} | |
def asScala: C2SMessage.Message = map { | |
case msg: Messages.Login => | |
Login(msg.getUsername, msg.getPassword) | |
case msg: Messages.Register => | |
Register(msg.getUsername, msg.getPassword) | |
case msg: LiveGame.MoveRequest => | |
val pos = msg.getPosition | |
MoveShipRequest((pos.getRow, pos.getCol)) | |
case msg: LiveGame.BuildingActivationRequest => | |
BuildingActivationRequest | |
} | |
} | |
implicit class DateTimeExts(val dt: DateTime) extends AnyVal { | |
def netMsg: Long = dt.getMillis | |
} | |
implicit class DurationExts(val duration: Duration) extends AnyVal { | |
def netMsg: Long = duration.toMillis | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinylabproductions.quazibuild.server.messaging | |
import com.tinylabproductions.quazibuild.{messaging => m} | |
import com.tinylabproductions.quazibuild.messaging.Messages.S2C | |
import com.google.protobuf.GeneratedMessage | |
import com.tinylabproductions.quazibuild.messaging.LiveGame._ | |
import com.tinylabproductions.quazibuild.server.model.{StoredResource, Resource} | |
import com.tinylabproductions.quazibuild.messaging.Base.User | |
import com.tinylabproductions.quazibuild.server.actor.live_game.model.Ship | |
import implicits._ | |
trait S2CMessage { | |
protected def build(builder: S2C.Builder): S2C.Builder | |
def forSending: S2C = build(S2C.newBuilder()).build() | |
} | |
sealed trait Networkable[A <: GeneratedMessage] { | |
def netMsg: A | |
} | |
object Networkable { | |
case class ValueWithStorage(sr: StoredResource[_ <: Resource]) | |
extends Networkable[m.LiveGame.ValueWithStorage] { | |
def netMsg = m.LiveGame.ValueWithStorage.newBuilder(). | |
setValue(sr.pool.value).setStorage(sr.storage.value).build() | |
} | |
} | |
object S2CMessage { | |
case class LoginResponse(id: Option[Int]) | |
extends S2CMessage { | |
protected def build(builder: S2C.Builder) = builder.setLoginResponse( | |
m.Messages.Login.Response.newBuilder(). | |
tapMap { b => id.map(b.setId).getOrElse(b) } | |
) | |
} | |
object RegisterResponse { | |
} | |
case class RegisterResponse() | |
object LiveGame { | |
object YourShipUpdate { | |
def apply(ship: Ship): YourShipUpdate = apply(ship.netMsgDetails) | |
} | |
case class YourShipUpdate(yourShip: ShipDetails) | |
extends S2CMessage { | |
protected def build(builder: S2C.Builder) = | |
builder.setLgYourShipUpdate(yourShip) | |
} | |
object ShipStateUpdate { | |
def apply(ship: Ship): ShipStateUpdate = apply(ship.netMsg) | |
} | |
case class ShipStateUpdate(state: ShipWithState) | |
extends S2CMessage { | |
protected def build(builder: S2C.Builder) = | |
builder.setLgShipStateUpdate(state) | |
} | |
case class ShipJoined(user: User, state: ShipState) | |
extends S2CMessage { | |
protected def build(builder: S2C.Builder) = builder.setLgShipJoined( | |
m.LiveGame.ShipJoined.newBuilder. | |
setUser(user).setState(state).build | |
) | |
} | |
case class ShipLeft(userId: Int) | |
extends S2CMessage { | |
protected def build(builder: S2C.Builder) = builder.setLgShipLeft( | |
m.LiveGame.ShipLeft.newBuilder.setUserId(userId).build | |
) | |
} | |
case class TileUpdate(tile: Tile) | |
extends S2CMessage { | |
protected def build(builder: S2C.Builder) = | |
builder.setLgTileUpdate(tile) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment