Code snippet 6

Posted October 19th, 2011 in Snippets by Metod

My (R):

  1. // safety switch :)

Code snippet 5 – CoD1 modding

Posted October 1st, 2011 in Snippets by Metod

I was administering a Call of Duty 1 server a while back and wanted a rename command for use through the remote console in AWE 2.12 mod. So I dug into the code of the admin tools. And wrote the following function:

  1. rename()
  2. {
  3.         self endon("boot");
  4.        
  5.         setcvar("g_rename", "");
  6.        
  7.         while(1)
  8.         {
  9.                 if(getcvar("g_rename") != "")
  10.                 {
  11.                         player_id = getcvarint("g_rename");
  12.                        
  13.                         for(i = 0; i < level.awe_allplayers.size; i++)
  14.                         {
  15.                                 thisPlayer = level.awe_allplayers[i];
  16.                                
  17.                                 if((thisPlayer getEntityNumber()) == player_id && (substr("Renamed", thisPlayer.name) == -1 || thisPlayer.name.size > 11))
  18.                                 {
  19.                                         level.awe_allplayers[i] setClientCvar("name", "Renamed " + randomInt(1000));
  20.                                         thisPlayer iprintlnbold(level.aweallplayers[i].name + " ^7renamed by admin");
  21.                                         iprintln(level.awe_allplayers[i].name + " ^7renamed by admin");
  22.                                         break;
  23.                                 }
  24.                         }
  25.                        
  26.                         setcvar("g_rename", "");
  27.                 }
  28.                
  29.                 wait .05;
  30.         }
  31. }
  32.  

Nothing special really, but it is an interesting look into how scripting works for Call of Duty 1.

Change keyboard layout in CentOS in console

Posted September 3rd, 2011 in Snippets by Metod

Quick tip. How to change keyboard layout in CentOS in console?

  1. # nano /etc/sysconfig/keyboard

and change the KEYTABLE key. All available options can be found in subdirectories under /lib/kbd/keymaps/i386.

After that, just restart the machine.

  1. # shutdown -r now

Login event listener in Symfony2

Posted July 11th, 2011 in Tehnikalije by Metod

So you have a Symfony2 project under construction. Login works. Now you want to execute some code right after the user successfully logs in.

The solution: Make a custom event listener.

An event listener is actually a service with a proper tag associated with it. So first step is to register the listener. The configuration file shown corresponds to the listener in file Acme/UserBundle/Listener/LoginListener.php. You can read more about services here.

In services.yml:

Let’s go through the configuration.

class defines the listener class.

With arguments we inject services that this listener depends on. In this case the security.context service for retrieving the User object and doctrine service for EntityManager.

tags is important. Every listener must be tagged with kernel.event_listener and must specify it’s event. For successful login that event is security.interactive_login. You can also specify the optional method tag.

Ok, our listener is registered. Let’s create it.

The above code should be pretty self-explanatory. The only required method is onSecurityInteractiveLogin(Event $event). You do not need the other stuff, but you probably will if you want to modify the User object, or something else in the DB.

SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

Posted June 25th, 2011 in Zanimivosti by Metod

When you run into this error, a few solutions apply.

First one is to check where mysql.sock or mysqld.sock actually resides and change values in php.ini appropriately.

  1. mysqli.default_socket = /var/run/mysqld/mysqld.sock

You can also make a symbolic link to the correct location.

  1. # ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

For me, the above solution did not work. All I got was the change in error from (2) to (13). Turns out the solution is quite interesting. Instead of using ‘localhost’ as host for DB, use 127.0.0.1 – it magically begins to work again.

Setup ssh-agent in KDE in Ubuntu

Posted May 28th, 2011 in Razno by Metod

I began toying around with KDE desktop since I got tired of Gnome. I wanted to try something new and shiny. Everything went great, it is highly customizable etc. Only one thing did not work. When ssh-ing to a server it did not ask me for keychain password anymore. So how to fix that?

First, if you don’t already have it installed, install ssh-askpass:

  1. sudo apt-get install ssh-askpass

Ok, now that that is installed, we must ensure that it asks us for password everytime we log on. For KDE we have to create a script inside ~/.kde/Autostart/ directory.

  1. #!/bin/sh
  2.  
  3. export SSH_ASKPASS=/usr/bin/ssh-askpass
  4. ssh-add < /dev/null

askpass.sh

You can also specify the name of the key file:

  1. ssh-add ~/.ssh/some_other_key < /dev/null

The startup script has to be correctly chmod-ed:

  1. chmod 755 askpass.sh

Now log out and log in again and it should ask you for password. Happy ssh-ing. :)

Unsetting elements of array in a for loop in php

Posted May 27th, 2011 in Snippets by Metod

I did something like this today:

  1. <?php
  2. $arr = array("some", "elements", "in", "array");
  3.  
  4. for($i = 0; $i < sizeof($arr); $i++)
  5.     if(true) // for sake of clarity
  6.         unset($arr[$i]);

The loop never came to the final element of the array, therefore not checking all of them. What was the problem?

Since sizeof($arr) is calculated every time the loop comes around, it was returning less and less with every unset instead of returning the same value.

Solution:

  1. <?php
  2. $arr = array("some", "elements", "in", "array");
  3.  
  4. $size = sizeof($arr);
  5.  
  6. for($i = 0; $i < $size; $i++)
  7.     if(true) // for sake of clarity
  8.         unset($arr[$i]);

That way you ensure that size is always the correct integer. And it will run faster.

Missing } in XML expression

Posted May 26th, 2011 in Razno by Metod

Today I got frustrated because when trying to parse a json string with jQuery, apparently it was causing a Missing } in XML expression error. But doing a bit more research I found the true problem. Boy was I wrong.

What was the problem?

  1. <script type="text/javascript"><script type="text/javascript"> …

And since Firefox and Firebug showed me purified and corrected HTML, I had no way of knowing that. So when you get this error next time, check for this situation. :)

How to stop a running cron job

Posted May 15th, 2011 in Razno by Metod

That was the question I asked myself when a running cron job on a shared hosting went horribly wrong. Turns out the solution is ‘pretty’ easy. You just have to know when the cron job started. In my case it was a PHP script. So i grep-ed for php also.

  1. ps -ef | grep php

You get the list of processes matching your criteria. Then you have to look for the time the script should have started. In the second column there is it’s PID. You can use that to kill it.

  1. kill $PID

Invalid property value javascript error in IE6 and IE7

Posted April 16th, 2011 in Razno by Metod

Recently I checked to see if my new project worked in IE6 and IE7. Well, actually I checked how it behaved. Obviously it did not work as expected.

The IE’s kept on bugging me with JS errors. IE6 said “Invalid property value”, while IE7 said “Unknown runtime error”. The code snippet was the following:

  1. el.style.color = "inherit";

And it worked in all browsers except in IE6 and IE7. As I learned later on, IE < 8 does not support the inherit value. So when changing the inherit to an actual color value, the errors were gone. So the next time you are bugged by this kind of errors in IE < 8, check for inherit values in your CSS, JS.