Skip to content

Instantly share code, notes, and snippets.

@dat-vikash
dat-vikash / gist:ae2d3183e28588cdf82d
Created April 2, 2015 14:55
Play 2.2.x WSClientVisitor
// define our events
case class RegisterSocket()
case class Connected(out: Enumerator[JsValue], ref: ActorRef)
class WSClientVisitor(name: String) extends Actor with ActorLogging with WebSocketChannel
{
def receive = {
case RegisterSocket => {
// close over sender
val myClient = sender
@dat-vikash
dat-vikash / gist:2ae6abfc9e6cf70c7660
Created April 2, 2015 14:52
Play 2.2.x WebSocketChannel trait
/* This trait allows websocket classes to push messages to client via a concurrent channel */
trait WebSocketChannel
{
//instantiate an Enumerator and Channel
val (out,channel) = Concurrent.broadcast[JsValue]
// method to allow pushing of data up the channel
def push(data: JsValue) = channel.push(data)
def cleanSocketResources() = channel.end()
}
/* This trait allows websocket classes to push messages to client via a concurrent channel */
trait WebSocketChannel
{
//instantiate an Enumerator and Channel
val (out,channel) = Concurrent.broadcast[JsValue]
// method to allow pushing of data up the channel
def push(data: JsValue) = channel.push(data)
def cleanSocketResources() = channel.end()
}
@dat-vikash
dat-vikash / gist:6f21ef2721147b09be55
Created March 31, 2015 16:35
SIM Distribution: defineSenatorTrack
[1] def defineSenatorTrack() =
{
// for every committee
[2] committeeIssueSenatorMap.foreach( committee => {
committee._2.foreach(issueConf => {
issueConf._2.foreach( senator => {
[3] val max = committee._2.foldLeft(0)( (result, current) => result + current._2.filter(_.faction.name.equalsIgnoreCase(senator.faction.name)).size) / 2
// does the senator fit in subcommittee 1
[4] if(doesSenatorFitInSubCommittee(senator.faction.name, max, senator, committee._1.name,SUBCOMMITTEE_1_KEY)) {
// add
@dat-vikash
dat-vikash / gist:d887af0fef06df61ceef
Created March 31, 2015 16:25
SIM Distribution: fitSenatorsBasedOnRarity
[1] def fitSenatorsBasedOnRarity(committeesWithIssueConfs: Map[Committee, mutable.Queue[String]], senators: Set[Senator], party: String) =
{
[2] var takenSenators: mutable.Set[Senator] = senators.to[mutable.Set]
[3] committeesWithIssueConfs.foreach(committee =>
committee._2.foreach(issue => {
// get a senator from each faction
[4] val tmpSenatorDem = takenSenators.find(s => s.faction.name.equalsIgnoreCase(party) && doesMatchPositionRule(issue, s))
[5] tmpSenatorDem match {
case None => // do nothing
case Some(senator) => {
// Create the Global class in your /app folder root package:
import play.api.{GlobalSettings, Play}
import play.api.Play.current
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
/**
@dat-vikash
dat-vikash / play2.2.0_websockets
Created October 11, 2013 15:26
Play2.2 basic websockets example
//This shows an updated websocket example for play2.2.0 utilizing Concurrent.broadcast vs Enumerator.imperative, which
// is now deprecated.
object Application extends Controller {
def index = WebSocket.using[String] { request =>
//Concurernt.broadcast returns (Enumerator, Concurrent.Channel)
val (out,channel) = Concurrent.broadcast[String]