changeset 161:2c9db9a29429

weather: More work, bugfixes.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Jun 2014 15:53:53 +0300
parents e3e156911ab4
children 4f8a163b2bc1
files weather.tcl
diffstat 1 files changed, 69 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/weather.tcl	Mon Jun 02 15:52:58 2014 +0300
+++ b/weather.tcl	Mon Jun 02 15:53:53 2014 +0300
@@ -12,8 +12,9 @@
 
 set weather_default_locations "raahe"
 
-set weather_datafile "/home/niinuska/bot/data.weather"
-set weather_check_period 5
+set weather_datafile "/home/niinuska/bot/weather.data"
+set weather_aliasfile "/home/niinuska/bot/weather.alias"
+set weather_check_period 2
 
 set weather_max_results 3
 
@@ -74,11 +75,48 @@
 }
 
 
+proc weather_valid_user {uhand} {
+  if {$uhand != "" && $uhand != "{}" && $uhand != "*"} {
+    return 1
+  }
+  return 0
+}
+
+
+proc weather_check_user {uhand} {
+  if {![weather_valid_user $uhand]} {
+    weather_msg $upublic $unick $uchan $weather_msg_user_not_known
+    return 0
+  }
+  return 1
+}
+
+
+#-------------------------------------------------------------------------
+proc weather_get_data {ukey udata} {
+  set str "\002[lindex $udata 0]\002, mitattu klo [weather_ctime [lindex $udata 1]]: \002[lindex $udata 2]°C\002"
+  if {[llength $udata] > 3} {
+    append str ", tien pinta [lindex $udata 3]°C"
+  }
+  if {[llength $udata] > 4} {
+    append str ", [lindex $udata 4]"
+  }
+  return "${str}."
+}
+
+
+proc weather_get_key {ukey} {
+  global weather_data
+  return [weather_get_data $ukey $weather_data($ukey)]
+}
+
+
 #-------------------------------------------------------------------------
 proc weather_update {} {
   global weather_datafile weather_data
 
   # Create dict
+  array unset weather_data
   array set weather_data {}
   set wtemp_min_val 500000
   set wtemp_max_val -500000
@@ -96,11 +134,15 @@
 
         set weather_data($ukey) $udata
 
-        if {$utemp < $wtemp_min_val} {
-          set wtemp_min_key $ukey
-        }
-        if {$utemp > $wtemp_max_val} {
-          set wtemp_max_key $ukey
+	if {[string is double -strict $utemp]} {
+          if {$utemp < $wtemp_min_val} {
+            set wtemp_min_key $ukey
+            set wtemp_min_val $utemp
+          }
+          if {$utemp > $wtemp_max_val} {
+            set wtemp_max_key $ukey
+            set wtemp_max_val $utemp
+          }
         }
       }
     }
@@ -147,18 +189,6 @@
 }
 
 #-------------------------------------------------------------------------
-proc weather_get_data {ukey udata} {
-  return "\002[lindex $udata 0]\002, mitattu klo [weather_ctime [lindex $udata 1]]: \002[lindex $udata 2]°C\002."
-}
-
-
-proc weather_get_key {ukey} {
-  global weather_data
-  return [weather_get_data $ukey $weather_data($ukey)]
-}
-
-
-#-------------------------------------------------------------------------
 proc weather_cmd {unick uhand uchan uargs upublic} {
   global weather_default_locations weather_data weather_max_results
   global weather_msg_usage_1 weather_msg_usage_2 weather_msg_user_not_known
@@ -176,8 +206,7 @@
   
   # Setting the location
   if {$rarg == "aseta" || $rarg == "set"} {
-    if {$uhand == "" || $uhand == "{}"} {
-      weather_msg $upublic $unick $uchan $weather_msg_user_not_known
+    if {![weather_check_user $uhand]} {
       return 0
     }
 
@@ -195,12 +224,25 @@
     return 0
   }
 
+  if {$rarg == "default" || $rarg == "vakio"} {
+    if {![weather_check_user $uhand]} {
+      return 0
+    }
+
+    set lista [getuser $uhand XTRA "weather_locations"]
+    if {$lista == "" || $lista == "{}"} {
+      set lista "Ei asetettu"
+    }
+    weather_msg $upublic $unick $uchan "Vakiopaikat ${uhand}: \002${lista}\002."
+    return 0
+  }
+
   # Get args or default location(s)
   set rargs [join $rarglist " "]
-  if {$rargs == "" && ($uhand == "" || $uhand == "{}")} {
+  if {$rargs == "" && $uhand != "" && $uhand != "{}" && $uhand != "*"} {
     set rargs [getuser $uhand XTRA "weather_locations"]
   }
-  if {$rargs == "" || $rargs == "{}"} {
+  if {$rargs == ""} {
     set rargs $weather_default_locations
   }
 
@@ -237,6 +279,10 @@
           }
           incr ufound
         }
+
+        if {$nresults >= $weather_max_results} {
+          return 0
+        }
       }
       
       if {$ufound == 0} {