Saturday, November 9, 2013

Cookie Module and Common JS

With the slow death of flash I have decided to build my game on a javascript MVC framework. The core is using the Common JS library Inject.

This mornings project was creating a cookie manager.  I stole and modified some basic cookie functions from W3Schools, and put it into the Common JS structure.  Happy Scripting.

Here is the code to call it.
////////////////////////////////////////////////////
var Cookie = require("modules.model.Cookie");

var App = function()
{
  this.Cookie = new Cookie();

  //set the cookie
  this.Cookie.setCookie("UserName", "Bob");

  //Get the cookie
  var userName = this.Cookie.getCookie("UserName");
  alert("Username = " + userName);

  //is the cookie
  var isUser = this.Cookie.isCookieValue("UserName", "Bob");
  alert("Is Bob the User? " + isUser);
};
///////////////////////////////////////////////////

Here is the module
/////////////////////////////////////////////////
var Cookie = function()
{
var self = this;
this.DEFAULT_EXP = 30; //days

this.setCookie = function(c_name,value,exdays)
{
var exdate=new Date();
if(!exdays)
{
exdays = self.DEFAULT_EXP;
}
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
};

this.getCookie = function(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
 {
  c_start = c_value.indexOf(c_name + "=");
 }
if (c_start == -1)
 {
  c_value = null;
 }
else
{
 c_start = c_value.indexOf("=", c_start) + 1;
 var c_end = c_value.indexOf(";", c_start);
 if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
};

this.isCookieValue = function(name, value)
{
debugger;
var cookie = self.getCookie(name);
 if(cookie===null || cookie==="")
 {
  return false;
 }
else
 {
  if(cookie === value)
   {
    return true;
   }
   else
   {
    return false;
   }
 }
};
};

module.exports = Cookie;
///////////////////////////////////////////////

Saturday, August 17, 2013

Singleton vrs Dependency Injection.

I have been starting to nail down the framework for my game. I have been reviewing my php knowledge base, on how to solve global variable, and object issues. I have friends that have been using singleton classes, and I defiantly see where they are going with it. It is handy for making sure you only have one instance of the thing running around, especially if you are dealing with a single object such as a web user. But I just don't feel it is the right solution for me. I think I am going to go more toward dependency injection at the bootstrap. My controller structure extends each other based on the page. So the sub page of a sub page will have been extended with each page all the way back to the main site controller. And any additional needed dependencies can be loaded for a new sub page controller. I can see going singleton if you have more then 1 controller object, which I have seen that approach before but it seemed cumbersome. I don't like cumbersome. I think it will be one controller that has been extended from the root, with dependency injection. Seems like it would be cleaner.

Saturday, July 20, 2013

Web Programmer, Tech Levels

Web Programmer, Tech Levels

What do you mean by Senior Web Programmer? After spending several years in the web development field, I think there needs to be a clarification on what a Senior Web Programmer is.  I have met people who's scripting ability is nothing less then beyond mortal.  And I have met some "Senior Programmers" who have code so dirty that it might be likened to a mud pit.  So I present to the general population of the world and, anyone else who might care a better scale to rate people on then Jr, and Sr, programmers.

Wanaabe

Yes I have built a website and it was legit.  I used a website builder and even went in to the code and added a <br>

Neophyte

I took some classes/read some books and learned html.  I use Dreamwaever, Druple, and Wordpress.

Web Guy

I build tons of web pages, I still might use Dreamweaver, Druple, or Wordpress. I have been working with libraries and frameworks.

Grunt

I have written some extensions for some frameworks and have started building up my own libraries

Minion (right hand man/women/igor)

I have extensive libraries, and am building my own framework.

Overlord

I have my own framework, and it has been used on several projects.

Neo

I don't believe in frameworks I just believe in me. Coo Coo Ka Choo.


And those looking to hire me I fall in to the Grunt catigory, and am on my way to Minion

Tuesday, June 18, 2013

Live!

I went Live with www.fracturednations.com today.  It is live with the release of my first solo Opencart extension Dynamic CSS.  Dynamic CSS allows CSS changes without scripting from the admin system.

You can also check out out the demo site to play with the CSS extension.
www.fracturednations.info 

Friday, April 19, 2013

I just wrote this the other day and thought it might be helpful for any one else that is working with google maps.  Its an earthquake map the queries the USGS for the latest earthquakes.  Check it out.
We show earthquakes that have happened around the world within the last 7 days. This map contains all earthquakes with magnitude greater than 2.5 located by the USGS in the last week.
<style type="text/css">#map-canvas { margin-top:10px; margin-bottom:10px; width: 720px; height: 480px; background-color:#ccc; } #map-text { width: 720px; } </style> <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization"></script> <script> window.onload=function() { var map_canvas = document.getElementById('map-canvas'); var map_options = { center: new google.maps.LatLng(40,-98), zoom: 2, mapTypeId: google.maps.MapTypeId.TERRAIN, } var map = new google.maps.Map(map_canvas, map_options); var script = document.createElement('script'); script.src = 'http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s); window.eqfeed_callback = function(results) { for (var i = 0; i < results.features.length; i++) { var coords = results.features[i].geometry.coordinates; var latLng = new google.maps.LatLng(coords[1], coords[0]); //build infowindow var d = new Date(parseInt(results.features[i].properties.time)); var month = Get_Month(d.getMonth()); //find id var id = results.features[i].properties.ids.substring(results.features[i].properties.ids.indexOf(',') + 1, results.features[i].properties.ids.indexOf(',', 2)); var contentString = '<div>M: ' + results.features[i].properties.mag.toString() + ', ' + results.features[i].properties.place + '</div><div>Time: ' + d.getDate() + ' ' + month + ' ' + d.getFullYear() + '<div><br><div><a href="http://earthquake.usgs.gov/earthquakes/eventpage/' + id + '" target="reqeventpage_' + id + '">Earthquake Details &#187;</a></div>'; var marker = new google.maps.Marker({ position: latLng, map: map, icon: getCircle(results.features[i].properties.mag), title: "M: " + results.features[i].properties.mag.toString() + ", " + results.features[i].properties.place, }); addInfoWindow(marker, contentString, map, latLng); } } } function Get_Month(month_num){ switch(month_num){ case 0: month = 'Jan' break; case 1: month = 'Feb' break; case 2: month = 'Mar' break; case 3: month = 'Apr' break; case 4: month = 'May' break; case 5: month = 'Jun' break; case 6: month = 'Jul' break; case 7: month = 'Aug' break; case 8: month = 'Sep' break; case 9: month = 'Oct' break; case 10: month = 'Nov' break; case 11: month = 'Dec' break; } return month; } function addInfoWindow(marker, message, map, latLng) { var infoWindow = new google.maps.InfoWindow({ content: message, position: latLng, }); google.maps.event.addListener(marker, 'click', function () { infoWindow.open(map, marker); }); } function getCircle(magnitude) { var fill = '#12ff00'; if(magnitude >= 2 && magnitude <= 4){ fill = '#aeff00'; } if(magnitude >= 4 && magnitude <= 6){ fill = '#fffc00'; } if(magnitude >= 6 && magnitude <= 8){ fill = '#ff3c00'; } if(magnitude >= 8 ){ fill = '#ff0000'; } var circle = { path: google.maps.SymbolPath.CIRCLE, scale: 1.75 * magnitude, strokeColor: fill, fillColor: fill, strokeOpacity: 0.5, fillOpacity: 0.5, strokeWeight: 1 }; return circle; } </script> <div id="map-text">We show earthquakes that have happened around the world within the last 7 days. This map contains all earthquakes with magnitude greater than 2.5 located by the USGS in the last week. </div> <div id="map-canvas"></div>
Joseph Tveter - Fractured Nations

Thursday, January 17, 2013

Embed Flash with PHP.

Its been a while. I figured I should put something up. Here is a class I wrote to put Flash on a page with PHP. Enjoy.


<?php
/**
 * Description of flash
 * @author Joseph Tveter
 *
 * This will build the object to embed a swf in.
 */
include_once 'view/html/HTML.php';
class flash {
    private $embedAttrs = array(
        "src"               => "flash_obj.swf",
        "quality"           => "high",
        "bgcolor"           => "#ffffff",
        "width"             => "1024",
        "height"            => "600",
        "name"              => "flash_obj",
        "align"             => "middle",
        "allowScriptAccess" => "sameDomain",
        "allowFullScreen"   => "false",
        "type"              => "application/x-shockwave-flash",
        "pluginspage"       => "http://www.adobe.com/go/getflashplayer",
        "vspace"            => "",
        "hspace"            => "",
        "class"             => "",
        "title"             => "",
        "accesskey"         => "",
        "tabindex"          => "",
        "flashvars"         => "",
    );
    private $params = array(
        "allowScriptAccess" => "sameDomain",
        "allowFullScreen"   => "false",
        "movie"             => "lobby.swf",
        "quality"           => "high",
        "bgcolor"           => "#ffffff",
    );
   
    private $objAttrs = array(
        "classid"            => "clsid:d27cdb6e-ae6d-11cf-96b8-444553540042",
        "codebase"           => "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0",
        "width"              => "1024",
        "height"             => "600",
        "id"                 => "flash_obj",
        "align"              => "middle",
        "onafterupdate"      => "",
        "onbeforeupdate"     => "",
        "onblur"             => "",
        "oncellchange"       => "",
        "onclick"            => "",
        "ondblclick"         => "",
        "ondrag"             => "",
        "ondragend"          => "",
        "ondragenter"        => "",
        "ondragleave"        => "",
        "ondragover"         => "",
        "ondrop"             => "",
        "onfinish"           => "",
        "onfocus"            => "",
        "onhelp"             => "",
        "onmousedown"        => "",
        "onmouseup"          => "",
        "onmouseover"        => "",
        "onmousemove"        => "",
        "onmouseout"         => "",
        "onkeypress"         => "",
        "onkeydown"          => "",
        "onkeyup"            => "",
        "onload"             => "",
        "onlosecapture"      => "",
        "onpropertychange"   => "",
        "onreadystatechange" => "",
        "onrowsdelete"       => "",
        "onrowenter"         => "",
        "onrowexit"          => "",
        "onrowsinserted"     => "",
        "onstart"            => "",
        "onscroll"           => "",
        "onbeforeeditfocus"  => "",
        "onactivate"         => "",
        "onbeforedeactivate" => "",
        "ondeactivate"       => "",
        "type"               => "",
        "vspace"             => "",
        "hspace"             => "",
        "class"              => "",
        "title"              => "",
        "accesskey"          => "",
        "name"               => "",
        "tabindex"           => "",
        "devicefont"         => "",
        "flashvars"          => "",
        );
   
    public function __construct($args) {
        foreach($args as $k => $v){
            foreach($this->embedAttrs as $key => $val){
               if($k == $key){
                   $this->embedAttrs[$k] = $v;
               }
            }
            foreach($this->params as $key => $val){
                if($k == $key){
                    $this->params[$k] = $v;
                }
            }
            foreach($this->objAttrs as $key => $val){
                if($k == $key){
                    $this->objAttrs[$k] = $v;
                }
            }
        }
    }
   
    public function draw(){
        $html = new HTML("object");
        foreach($this->objAttrs as $k => $v){
            if($v != ""){
                $html->addValue($k, $v);
            }
        }
       
        foreach($this->params as $k => $v){
            if($v != ""){
                $pram = new HTML("param");
                $pram->addValue("name", $k);
                $pram->addValue("value", $v);
                $html->addChild($pram);
            }
        }
        $embed = new html("embed");
        foreach($this->embedAttrs as $k => $v){
            if($v != ""){
                $embed->addValue($k, $v);
            }
        }
        $html->addChild($embed);
        return $html;
    }
}

?>